blob: 714ca963daa9d4a68e669d594be43154ae3483b2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070029#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070034#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070037#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070038#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070039#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070059void SetJniGlobalsMax(size_t max) {
60 if (max != 0) {
61 gGlobalsMax = max;
62 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
63 }
64}
Ian Rogersdf20fe02011-07-20 20:34:16 -070065
Elliott Hughesc5f7c912011-08-18 14:00:42 -070066/*
67 * Add a local reference for an object to the current stack frame. When
68 * the native function returns, the reference will be discarded.
69 *
70 * We need to allow the same reference to be added multiple times.
71 *
72 * This will be called on otherwise unreferenced objects. We cannot do
73 * GC allocations here, and it's best if we don't grab a mutex.
74 *
75 * Returns the local reference (currently just the same pointer that was
76 * passed in), or NULL on failure.
77 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070078template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
80 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
81 Object* obj = const_cast<Object*>(const_obj);
82
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 if (obj == NULL) {
84 return NULL;
85 }
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089
Ian Rogers5a7a74a2011-09-26 16:32:29 -070090 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 IndirectRef ref = locals.Add(cookie, obj);
92 if (ref == NULL) {
93 // TODO: just change Add's DCHECK to CHECK and lose this?
94 locals.Dump();
95 LOG(FATAL) << "Failed adding to JNI local reference table "
96 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 }
98
99#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700100 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101 size_t entry_count = locals.Capacity();
102 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700104 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700105 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800106 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700107 }
108 }
109#endif
110
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800111 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 // Hand out direct pointers to support broken old apps.
113 return reinterpret_cast<T>(obj);
114 }
115
116 return reinterpret_cast<T>(ref);
117}
Brian Carlstrom51477332012-03-25 20:20:26 -0700118// Explicit instantiations
119template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
120template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
121template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
122template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
123template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700124
Elliott Hughesbf86d042011-08-31 17:53:14 -0700125// For external use.
126template<typename T>
127T Decode(JNIEnv* public_env, jobject obj) {
128 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
129 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
130}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800131// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
132Object* DecodeObj(JNIEnv* public_env, jobject obj) {
133 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
134 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
135}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700137template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138template Class* Decode<Class*>(JNIEnv*, jobject);
139template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
140template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700141template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700142template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700143template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700144template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400145template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700146template String* Decode<String*>(JNIEnv*, jobject);
147template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148
Ian Rogers45619fc2012-02-29 11:15:25 -0800149size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
150 size_t num_bytes = 0;
151 for (size_t i = 1; i < shorty_len; ++i) {
152 char ch = shorty[i];
153 if (ch == 'D' || ch == 'J') {
154 num_bytes += 8;
155 } else if (ch == 'L') {
156 // Argument is a reference or an array. The shorty descriptor
157 // does not distinguish between these types.
158 num_bytes += sizeof(Object*);
159 } else {
160 num_bytes += 4;
161 }
162 }
163 return num_bytes;
164}
165
166class ArgArray {
167 public:
168 explicit ArgArray(Method* method) {
169 MethodHelper mh(method);
170 shorty_ = mh.GetShorty();
171 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700172 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800173 arg_array_ = small_arg_array_;
174 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700175 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800176 arg_array_ = large_arg_array_.get();
177 }
178 }
179
Elliott Hughes77405792012-03-15 15:22:12 -0700180 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800181 return arg_array_;
182 }
183
184 void BuildArgArray(JNIEnv* public_env, va_list ap) {
185 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700186 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800187 switch (shorty_[i]) {
188 case 'Z':
189 case 'B':
190 case 'C':
191 case 'S':
192 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700193 arg_array_[offset].i = va_arg(ap, jint);
Ian Rogers45619fc2012-02-29 11:15:25 -0800194 break;
195 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700196 arg_array_[offset].f = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800197 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700198 case 'L':
199 arg_array_[offset].l = DecodeObj(env, va_arg(ap, jobject));
Ian Rogers45619fc2012-02-29 11:15:25 -0800200 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800201 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700202 arg_array_[offset].d = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800203 break;
204 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700205 arg_array_[offset].j = va_arg(ap, jlong);
Ian Rogers45619fc2012-02-29 11:15:25 -0800206 break;
207 }
208 }
209 }
210
211 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
212 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700213 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800214 switch (shorty_[i]) {
215 case 'Z':
216 case 'B':
217 case 'C':
218 case 'S':
219 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700220 arg_array_[offset].i = args[offset].i;
Ian Rogers45619fc2012-02-29 11:15:25 -0800221 break;
222 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700223 arg_array_[offset].f = args[offset].f;
Ian Rogers45619fc2012-02-29 11:15:25 -0800224 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700225 case 'L':
226 arg_array_[offset].l = DecodeObj(env, args[offset].l);
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800228 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700229 arg_array_[offset].d = args[offset].d;
Ian Rogers45619fc2012-02-29 11:15:25 -0800230 break;
231 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700232 arg_array_[offset].j = args[offset].j;
Ian Rogers45619fc2012-02-29 11:15:25 -0800233 break;
234 }
235 }
236 }
237
Ian Rogers45619fc2012-02-29 11:15:25 -0800238 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700239 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800240 const char* shorty_;
241 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700242 JValue* arg_array_;
243 JValue small_arg_array_[kSmallArgArraySize];
244 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800245};
246
Elliott Hughes0512f022012-03-15 22:10:52 -0700247static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 if (obj == NULL) {
249 return NULL;
250 }
Elliott Hughes75770752011-08-24 17:52:38 -0700251 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700252 IndirectReferenceTable& weak_globals = vm->weak_globals;
253 MutexLock mu(vm->weak_globals_lock);
254 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
255 return reinterpret_cast<jweak>(ref);
256}
257
Elliott Hughesbf86d042011-08-31 17:53:14 -0700258// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700259template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700260static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700261 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700262}
263
Elliott Hughes77405792012-03-15 15:22:12 -0700264static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700265 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700266 JValue result = { 0 };
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700268 return result;
269}
270
Ian Rogers0571d352011-11-03 19:51:38 -0700271static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700272 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800273 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700274 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800275 ArgArray arg_array(method);
276 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700277 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700278}
279
Ian Rogers0571d352011-11-03 19:51:38 -0700280static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700281 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700282}
283
Ian Rogers0571d352011-11-03 19:51:38 -0700284static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
285 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800287 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700288 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800289 ArgArray arg_array(method);
290 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700291 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700292}
293
Ian Rogers0571d352011-11-03 19:51:38 -0700294static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
295 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700296 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800297 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700298 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800299 ArgArray arg_array(method);
300 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302}
303
Elliott Hughes6b436852011-08-12 10:16:44 -0700304// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
305// separated with slashes but aren't wrapped with "L;" like regular descriptors
306// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
307// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
308// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700309static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700310 std::string result;
311 // Add the missing "L;" if necessary.
312 if (name[0] == '[') {
313 result = name;
314 } else {
315 result += 'L';
316 result += name;
317 result += ';';
318 }
319 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700320 if (result.find('.') != std::string::npos) {
321 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
322 << "\"" << name << "\"";
323 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700324 }
325 return result;
326}
327
Ian Rogers0571d352011-11-03 19:51:38 -0700328static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700329 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800330 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700331}
332
Ian Rogers0571d352011-11-03 19:51:38 -0700333static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700334 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700335 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700336 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700337 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700338
339 Method* method = NULL;
340 if (is_static) {
341 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700342 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700343 method = c->FindVirtualMethod(name, sig);
344 if (method == NULL) {
345 // No virtual method matching the signature. Search declared
346 // private methods and constructors.
347 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700348 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700349 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700350
Elliott Hughescdf53122011-08-19 15:46:09 -0700351 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700352 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 return NULL;
354 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700355
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700356 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700357}
358
Ian Rogers0571d352011-11-03 19:51:38 -0700359static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700360 Frame frame = self->GetTopOfStack();
361 Method* method = frame.GetMethod();
362 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
363 return self->GetClassLoaderOverride();
364 }
365 return method->GetDeclaringClass()->GetClassLoader();
366}
367
Ian Rogers0571d352011-11-03 19:51:38 -0700368static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700370 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700371 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700372 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700373
374 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375 Class* field_type;
376 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
377 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700378 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700380 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 if (field_type == NULL) {
384 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700385 DCHECK(ts.Self()->IsExceptionPending());
386 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700387 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700388 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800389 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 return NULL;
391 }
392 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700397 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700398 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700399 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800400 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700401 return NULL;
402 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700403 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404}
405
Ian Rogers0571d352011-11-03 19:51:38 -0700406static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700407 JavaVMExt* vm = ts.Vm();
408 MutexLock mu(vm->pins_lock);
409 vm->pin_table.Add(array);
410}
411
Ian Rogers0571d352011-11-03 19:51:38 -0700412static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700413 JavaVMExt* vm = ts.Vm();
414 MutexLock mu(vm->pins_lock);
415 vm->pin_table.Remove(array);
416}
417
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700418template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700419static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700420 CHECK_GE(length, 0); // TODO: ReportJniError
421 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700422 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700423}
424
Elliott Hughes75770752011-08-24 17:52:38 -0700425template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700426static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700427 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
428 PinPrimitiveArray(ts, array);
429 if (is_copy != NULL) {
430 *is_copy = JNI_FALSE;
431 }
432 return array->GetData();
433}
434
435template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700436static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700437 if (mode != JNI_COMMIT) {
438 Array* array = Decode<Array*>(ts, java_array);
439 UnpinPrimitiveArray(ts, array);
440 }
441}
442
Ian Rogers0571d352011-11-03 19:51:38 -0700443static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700444 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700445 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700446 "%s offset=%d length=%d %s.length=%d",
447 type.c_str(), start, length, identifier, array->GetLength());
448}
Ian Rogers0571d352011-11-03 19:51:38 -0700449
450static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700451 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700452 "offset=%d length=%d string.length()=%d", start, length, array_length);
453}
Elliott Hughes814e4032011-08-23 12:07:56 -0700454
455template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700456static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700457 ArrayT* array = Decode<ArrayT*>(ts, java_array);
458 if (start < 0 || length < 0 || start + length > array->GetLength()) {
459 ThrowAIOOBE(ts, array, start, length, "src");
460 } else {
461 JavaT* data = array->GetData();
462 memcpy(buf, data + start, length * sizeof(JavaT));
463 }
464}
465
466template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700467static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700468 ArrayT* array = Decode<ArrayT*>(ts, java_array);
469 if (start < 0 || length < 0 || start + length > array->GetLength()) {
470 ThrowAIOOBE(ts, array, start, length, "dst");
471 } else {
472 JavaT* data = array->GetData();
473 memcpy(data + start, buf, length * sizeof(JavaT));
474 }
475}
476
Ian Rogers0571d352011-11-03 19:51:38 -0700477static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700478 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
479 CHECK(buffer_class.get() != NULL);
480 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
481}
482
Ian Rogers0571d352011-11-03 19:51:38 -0700483static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700484 static jclass buffer_class = InitDirectByteBufferClass(env);
485 return buffer_class;
486}
487
Ian Rogers0571d352011-11-03 19:51:38 -0700488static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700489 if (vm == NULL || p_env == NULL) {
490 return JNI_ERR;
491 }
492
Elliott Hughescac6cc72011-11-03 20:31:21 -0700493 // Return immediately if we're already one with the VM.
494 Thread* self = Thread::Current();
495 if (self != NULL) {
496 *p_env = self->GetJniEnv();
497 return JNI_OK;
498 }
499
500 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
501
502 // No threads allowed in zygote mode.
503 if (runtime->IsZygote()) {
504 LOG(ERROR) << "Attempt to attach a thread in the zygote";
505 return JNI_ERR;
506 }
507
Elliott Hughes75770752011-08-24 17:52:38 -0700508 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
509 JavaVMAttachArgs args;
510 if (thr_args == NULL) {
511 // Allow the v1.1 calling convention.
512 args.version = JNI_VERSION_1_2;
513 args.name = NULL;
514 args.group = NULL; // TODO: get "main" thread group
515 } else {
516 args.version = in_args->version;
517 args.name = in_args->name;
518 if (in_args->group != NULL) {
519 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
520 args.group = NULL; // TODO: decode in_args->group
521 } else {
522 args.group = NULL; // TODO: get "main" thread group
523 }
524 }
525 CHECK_GE(args.version, JNI_VERSION_1_2);
526
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700527 runtime->AttachCurrentThread(args.name, as_daemon);
528 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700529 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700530}
531
Elliott Hughes79082e32011-08-25 12:07:32 -0700532class SharedLibrary {
533 public:
534 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
535 : path_(path),
536 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700537 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700538 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700539 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700540 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 }
543
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 Object* GetClassLoader() {
545 return class_loader_;
546 }
547
548 std::string GetPath() {
549 return path_;
550 }
551
552 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700553 * Check the result of an earlier call to JNI_OnLoad on this library.
554 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700556 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700558 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 // Check this so we don't end up waiting for ourselves. We need
560 // to return "true" so the caller can continue.
561 LOG(INFO) << *self << " recursive attempt to load library "
562 << "\"" << path_ << "\"";
563 return true;
564 }
565
566 MutexLock mu(jni_on_load_lock_);
567 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800568 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
569 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700570 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700571 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 }
573
574 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
576 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 return okay;
578 }
579
580 void SetResult(bool result) {
581 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700582 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700583
584 // Broadcast a wakeup to anybody sleeping on the condition variable.
585 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700586 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 }
588
589 void* FindSymbol(const std::string& symbol_name) {
590 return dlsym(handle_, symbol_name.c_str());
591 }
592
593 private:
594 enum JNI_OnLoadState {
595 kPending,
596 kFailed,
597 kOkay,
598 };
599
600 // Path to library "/system/lib/libjni.so".
601 std::string path_;
602
603 // The void* returned by dlopen(3).
604 void* handle_;
605
606 // The ClassLoader this library is associated with.
607 Object* class_loader_;
608
609 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700610 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700612 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700614 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700615 // Result of earlier JNI_OnLoad call.
616 JNI_OnLoadState jni_on_load_result_;
617};
618
Elliott Hughes79082e32011-08-25 12:07:32 -0700619// This exists mainly to keep implementation details out of the header file.
620class Libraries {
621 public:
622 Libraries() {
623 }
624
625 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700626 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 }
628
629 SharedLibrary* Get(const std::string& path) {
630 return libraries_[path];
631 }
632
633 void Put(const std::string& path, SharedLibrary* library) {
634 libraries_[path] = library;
635 }
636
637 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700638 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 std::string jni_short_name(JniShortName(m));
640 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700641 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
643 SharedLibrary* library = it->second;
644 if (library->GetClassLoader() != declaring_class_loader) {
645 // We only search libraries loaded by the appropriate ClassLoader.
646 continue;
647 }
648 // Try the short name then the long name...
649 void* fn = library->FindSymbol(jni_short_name);
650 if (fn == NULL) {
651 fn = library->FindSymbol(jni_long_name);
652 }
653 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800654 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
655 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 return fn;
657 }
658 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700659 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700660 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700661 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700662 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700663 return NULL;
664 }
665
666 private:
667 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
668
669 std::map<std::string, SharedLibrary*> libraries_;
670};
671
Elliott Hughes418d20f2011-09-22 14:00:39 -0700672JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
673 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
674 Object* receiver = Decode<Object*>(env, obj);
675 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800676 ArgArray arg_array(method);
677 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700678 return InvokeWithArgArray(env, receiver, method, arg_array.get());
679}
680
Elliott Hughesd07986f2011-12-06 18:27:45 -0800681JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700682 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800683}
684
Elliott Hughescdf53122011-08-19 15:46:09 -0700685class JNI {
686 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 static jint GetVersion(JNIEnv* env) {
689 ScopedJniThreadState ts(env);
690 return JNI_VERSION_1_6;
691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
694 ScopedJniThreadState ts(env);
695 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700696 return NULL;
697 }
698
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 static jclass FindClass(JNIEnv* env, const char* name) {
700 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700701 Runtime* runtime = Runtime::Current();
702 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 Class* c = NULL;
705 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700706 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800707 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700708 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800709 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700710 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700711 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
715 ScopedJniThreadState ts(env);
716 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700717 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
721 ScopedJniThreadState ts(env);
722 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700723 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700725
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
727 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700728 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700729 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
733 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700734 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700735 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700737
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
739 ScopedJniThreadState ts(env);
740 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700741 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
743
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700744 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700746 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700747 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700749
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 Class* c1 = Decode<Class*>(ts, java_class1);
753 Class* c2 = Decode<Class*>(ts, java_class2);
754 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughese84278b2012-03-22 10:06:53 -0700757 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700759 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700761 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 return JNI_TRUE;
763 } else {
764 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700765 Class* c = Decode<Class*>(ts, java_class);
766 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700769
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 static jint Throw(JNIEnv* env, jthrowable java_exception) {
771 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700772 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 if (exception == NULL) {
774 return JNI_ERR;
775 }
776 ts.Self()->SetException(exception);
777 return JNI_OK;
778 }
779
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700782 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700783 jmethodID mid = ((msg != NULL)
784 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
785 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700786 if (mid == NULL) {
787 return JNI_ERR;
788 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700790 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700791 return JNI_ERR;
792 }
793
794 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 args[0].l = s.get();
796 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
797 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700798 return JNI_ERR;
799 }
800
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700802
Elliott Hughes37f7a402011-08-22 18:56:01 -0700803 return JNI_OK;
804 }
805
806 static jboolean ExceptionCheck(JNIEnv* env) {
807 ScopedJniThreadState ts(env);
808 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
809 }
810
811 static void ExceptionClear(JNIEnv* env) {
812 ScopedJniThreadState ts(env);
813 ts.Self()->ClearException();
814 }
815
816 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700818
819 Thread* self = ts.Self();
820 Throwable* original_exception = self->GetException();
821 self->ClearException();
822
Elliott Hughesbf86d042011-08-31 17:53:14 -0700823 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
825 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
826 if (mid == NULL) {
827 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700828 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 } else {
830 env->CallVoidMethod(exception.get(), mid);
831 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700832 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 << " thrown while calling printStackTrace";
834 self->ClearException();
835 }
836 }
837
838 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700840
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 static jthrowable ExceptionOccurred(JNIEnv* env) {
842 ScopedJniThreadState ts(env);
843 Object* exception = ts.Self()->GetException();
844 if (exception == NULL) {
845 return NULL;
846 } else {
847 // TODO: if adding a local reference failing causes the VM to abort
848 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700849 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 if (localException == NULL) {
851 // We were unable to add a new local reference, and threw a new
852 // exception. We can't return "exception", because it's not a
853 // local reference. So we have to return NULL, indicating that
854 // there was no exception, even though it's pretty much raining
855 // exceptions in here.
856 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
857 }
858 return localException;
859 }
860 }
861
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 static void FatalError(JNIEnv* env, const char* msg) {
863 ScopedJniThreadState ts(env);
864 LOG(FATAL) << "JNI FatalError called: " << msg;
865 }
866
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700867 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700869 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
870 return JNI_ERR;
871 }
872 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 return JNI_OK;
874 }
875
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700876 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700878 Object* survivor = Decode<Object*>(ts, java_survivor);
879 ts.Env()->PopFrame();
880 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 }
882
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700883 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700885 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
886 }
887
888 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
889 // TODO: we should try to expand the table if necessary.
890 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
891 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
892 return JNI_ERR;
893 }
894 // TODO: this isn't quite right, since "capacity" includes holes.
895 size_t capacity = ts.Env()->locals.Capacity();
896 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
897 if (!okay) {
898 ts.Self()->ThrowOutOfMemoryError(caller);
899 }
900 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 }
902
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
904 ScopedJniThreadState ts(env);
905 if (obj == NULL) {
906 return NULL;
907 }
908
Elliott Hughes75770752011-08-24 17:52:38 -0700909 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 IndirectReferenceTable& globals = vm->globals;
911 MutexLock mu(vm->globals_lock);
912 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
913 return reinterpret_cast<jobject>(ref);
914 }
915
916 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
917 ScopedJniThreadState ts(env);
918 if (obj == NULL) {
919 return;
920 }
921
Elliott Hughes75770752011-08-24 17:52:38 -0700922 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 IndirectReferenceTable& globals = vm->globals;
924 MutexLock mu(vm->globals_lock);
925
926 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
927 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
928 << "failed to find entry";
929 }
930 }
931
932 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
933 ScopedJniThreadState ts(env);
934 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
935 }
936
937 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
938 ScopedJniThreadState ts(env);
939 if (obj == NULL) {
940 return;
941 }
942
Elliott Hughes75770752011-08-24 17:52:38 -0700943 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 IndirectReferenceTable& weak_globals = vm->weak_globals;
945 MutexLock mu(vm->weak_globals_lock);
946
947 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
948 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
949 << "failed to find entry";
950 }
951 }
952
953 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
954 ScopedJniThreadState ts(env);
955 if (obj == NULL) {
956 return NULL;
957 }
958
959 IndirectReferenceTable& locals = ts.Env()->locals;
960
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700961 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
963 return reinterpret_cast<jobject>(ref);
964 }
965
966 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
967 ScopedJniThreadState ts(env);
968 if (obj == NULL) {
969 return;
970 }
971
972 IndirectReferenceTable& locals = ts.Env()->locals;
973
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700974 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 if (!locals.Remove(cookie, obj)) {
976 // Attempting to delete a local reference that is not in the
977 // topmost local reference frame is a no-op. DeleteLocalRef returns
978 // void and doesn't throw any exceptions, but we should probably
979 // complain about it so the user will notice that things aren't
980 // going quite the way they expect.
981 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
982 << "failed to find entry";
983 }
984 }
985
986 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
987 ScopedJniThreadState ts(env);
988 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
989 ? JNI_TRUE : JNI_FALSE;
990 }
991
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700992 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700994 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700995 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700996 return NULL;
997 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700998 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughese84278b2012-03-22 10:06:53 -07001001 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
1003 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001005 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 va_end(args);
1007 return result;
1008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001012 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001013 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001014 return NULL;
1015 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001016 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001017 if (result == NULL) {
1018 return NULL;
1019 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001020 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001022 if (!ts.Self()->IsExceptionPending()) {
1023 return local_result;
1024 } else {
1025 return NULL;
1026 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001031 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001032 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001033 return NULL;
1034 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001035 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001036 if (result == NULL) {
1037 return NULL;
1038 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001039 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001041 if (!ts.Self()->IsExceptionPending()) {
1042 return local_result;
1043 } else {
1044 return NULL;
1045 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1049 ScopedJniThreadState ts(env);
1050 return FindMethodID(ts, c, name, sig, false);
1051 }
1052
1053 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1054 ScopedJniThreadState ts(env);
1055 return FindMethodID(ts, c, name, sig, true);
1056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001062 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001064 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001070 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001075 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001076 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_list ap;
1082 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001083 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_end(ap);
1085 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001090 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001095 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_list ap;
1101 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001102 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_end(ap);
1104 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001109 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001114 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_list ap;
1120 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001121 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_end(ap);
1123 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001128 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001133 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_list ap;
1139 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001140 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_end(ap);
1142 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001147 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001152 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_list ap;
1158 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001159 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_end(ap);
1161 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001171 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_list ap;
1177 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001178 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_end(ap);
1180 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001185 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001190 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_list ap;
1196 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001197 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_end(ap);
1199 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001204 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001209 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_list ap;
1215 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001216 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_end(ap);
1218 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001223 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001228 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_list ap;
1234 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001241 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001246 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 static jobject CallNonvirtualObjectMethod(JNIEnv* env, 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 Hughesbf86d042011-08-31 17:53:14 -07001254 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
1256 return local_result;
1257 }
1258
1259 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001260 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001263 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
1266 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001267 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001270 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
1273 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001274 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
1276 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001278 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
1280 return result.z;
1281 }
1282
1283 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001284 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001295 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
1297 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001299 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_end(ap);
1301 return result.b;
1302 }
1303
1304 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001305 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001307 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
1310 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001313 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
1318 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
1322 return result.c;
1323 }
1324
1325 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001328 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 return result.s;
1344 }
1345
1346 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001347 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 ScopedJniThreadState ts(env);
1360 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001362 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 va_end(ap);
1364 return result.i;
1365 }
1366
1367 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001368 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001374 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001376 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
1381 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001383 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 va_end(ap);
1385 return result.j;
1386 }
1387
1388 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001389 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
1394 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001397 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001400 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
1402 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001404 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 va_end(ap);
1406 return result.f;
1407 }
1408
1409 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001418 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001421 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 ScopedJniThreadState ts(env);
1423 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001424 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001425 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 va_end(ap);
1427 return result.d;
1428 }
1429
1430 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001431 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001433 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
1436 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001439 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 ScopedJniThreadState ts(env);
1444 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001445 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001446 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 va_end(ap);
1448 }
1449
1450 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001453 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
1456 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001457 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001459 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001462 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
1464 return FindFieldID(ts, c, name, sig, false);
1465 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001466
1467
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001468 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 ScopedJniThreadState ts(env);
1470 return FindFieldID(ts, c, name, sig, true);
1471 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001472
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001476 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001477 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001482 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001483 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 Object* o = Decode<Object*>(ts, java_object);
1489 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001490 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001497 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501#define GET_PRIMITIVE_FIELD(fn, instance) \
1502 ScopedJniThreadState ts(env); \
1503 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001504 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 return f->fn(o)
1506
1507#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1508 ScopedJniThreadState ts(env); \
1509 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 f->fn(o, value)
1512
1513 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1514 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1518 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1522 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1526 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1530 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1534 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1538 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001545 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001557 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 GET_PRIMITIVE_FIELD(GetLong, NULL);
1567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1575 }
1576
1577 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1578 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1579 }
1580
1581 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1582 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1583 }
1584
1585 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1586 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1587 }
1588
1589 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1590 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1591 }
1592
1593 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1594 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1595 }
1596
1597 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1598 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1599 }
1600
1601 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1602 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1603 }
1604
1605 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1606 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1607 }
1608
1609 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1610 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1611 }
1612
1613 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1614 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1615 }
1616
1617 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1618 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1619 }
1620
1621 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1622 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1623 }
1624
1625 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1626 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1627 }
1628
1629 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1630 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1631 }
1632
1633 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1634 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1635 }
1636
1637 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1638 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
1643 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001646 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 va_end(ap);
1648 return local_result;
1649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001653 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001654 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001660 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
1665 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 va_end(ap);
1669 return result.z;
1670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
1684 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 va_end(ap);
1688 return result.b;
1689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
1703 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 va_end(ap);
1707 return result.c;
1708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001715 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
1722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001724 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
1726 return result.s;
1727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
1741 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001743 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 va_end(ap);
1745 return result.i;
1746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
1760 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001762 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 va_end(ap);
1764 return result.j;
1765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
1779 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001780 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001781 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_end(ap);
1783 return result.f;
1784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001788 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001791 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
1798 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001799 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001800 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
1802 return result.d;
1803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001807 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001810 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001812 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
1817 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001818 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001819 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 va_end(ap);
1821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001825 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001835 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001836 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
1839 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1840 ScopedJniThreadState ts(env);
1841 if (utf == NULL) {
1842 return NULL;
1843 }
1844 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001845 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes814e4032011-08-23 12:07:56 -07001848 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1849 ScopedJniThreadState ts(env);
1850 return Decode<String*>(ts, java_string)->GetLength();
1851 }
1852
1853 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1854 ScopedJniThreadState ts(env);
1855 return Decode<String*>(ts, java_string)->GetUtfLength();
1856 }
1857
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001860 String* s = Decode<String*>(ts, java_string);
1861 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1862 ThrowSIOOBE(ts, start, length, s->GetLength());
1863 } else {
1864 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1865 memcpy(buf, chars + start, length * sizeof(jchar));
1866 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 }
1868
Elliott Hughesb465ab02011-08-24 11:21:21 -07001869 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001871 String* s = Decode<String*>(ts, java_string);
1872 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1873 ThrowSIOOBE(ts, start, length, s->GetLength());
1874 } else {
1875 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1876 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1877 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001882 String* s = Decode<String*>(ts, java_string);
1883 const CharArray* chars = s->GetCharArray();
1884 PinPrimitiveArray(ts, chars);
1885 if (is_copy != NULL) {
1886 *is_copy = JNI_FALSE;
1887 }
1888 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 }
1890
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001891 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001892 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001893 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes75770752011-08-24 17:52:38 -07001896 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001898 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 }
1900
Elliott Hughes75770752011-08-24 17:52:38 -07001901 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001903 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes75770752011-08-24 17:52:38 -07001906 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001907 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001908 if (java_string == NULL) {
1909 return NULL;
1910 }
1911 if (is_copy != NULL) {
1912 *is_copy = JNI_TRUE;
1913 }
1914 String* s = Decode<String*>(ts, java_string);
1915 size_t byte_count = s->GetUtfLength();
1916 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001917 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001918 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1919 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1920 bytes[byte_count] = '\0';
1921 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001926 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001927 }
1928
Elliott Hughesbd935992011-08-22 11:59:34 -07001929 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001931 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001932 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001933 Array* array = obj->AsArray();
1934 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001939 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001940 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
1943 static void SetObjectArrayElement(JNIEnv* env,
1944 jobjectArray java_array, jsize index, jobject java_value) {
1945 ScopedJniThreadState ts(env);
1946 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1947 Object* value = Decode<Object*>(ts, java_value);
1948 array->Set(index, value);
1949 }
1950
1951 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1952 ScopedJniThreadState ts(env);
1953 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1954 }
1955
1956 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1957 ScopedJniThreadState ts(env);
1958 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1959 }
1960
1961 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1962 ScopedJniThreadState ts(env);
1963 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1964 }
1965
1966 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1967 ScopedJniThreadState ts(env);
1968 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1969 }
1970
1971 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1972 ScopedJniThreadState ts(env);
1973 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1974 }
1975
1976 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1977 ScopedJniThreadState ts(env);
1978 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1979 }
1980
1981 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1982 ScopedJniThreadState ts(env);
1983 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1984 }
1985
1986 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1987 ScopedJniThreadState ts(env);
1988 CHECK_GE(length, 0); // TODO: ReportJniError
1989
1990 // Compute the array class corresponding to the given element class.
1991 Class* element_class = Decode<Class*>(ts, element_jclass);
1992 std::string descriptor;
1993 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001994 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001995
1996 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001997 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1998 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 return NULL;
2000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 // Allocate and initialize if necessary.
2003 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 if (initial_element != NULL) {
2006 Object* initial_object = Decode<Object*>(ts, initial_element);
2007 for (jsize i = 0; i < length; ++i) {
2008 result->Set(i, initial_object);
2009 }
2010 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002011 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
2014 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2015 ScopedJniThreadState ts(env);
2016 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2017 }
2018
Ian Rogersa15e67d2012-02-28 13:51:55 -08002019 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002020 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002021 Array* array = Decode<Array*>(ts, java_array);
2022 PinPrimitiveArray(ts, array);
2023 if (is_copy != NULL) {
2024 *is_copy = JNI_FALSE;
2025 }
2026 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002027 }
2028
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002029 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray 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<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray 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<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes75770752011-08-24 17:52:38 -07002054 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes75770752011-08-24 17:52:38 -07002059 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002061 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes75770752011-08-24 17:52:38 -07002064 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002066 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes75770752011-08-24 17:52:38 -07002069 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002071 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002074 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002086 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002089 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002091 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002094 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002096 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002099 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002101 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002104 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002106 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002109 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002111 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002181 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002186 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002191 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 Class* c = Decode<Class*>(ts, java_class);
2197
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 const char* name = methods[i].name;
2200 const char* sig = methods[i].signature;
2201
2202 if (*sig == '!') {
2203 // TODO: fast jni. it's too noisy to log all these.
2204 ++sig;
2205 }
2206
Elliott Hughes5174fe62011-08-23 15:12:35 -07002207 Method* m = c->FindDirectMethod(name, sig);
2208 if (m == NULL) {
2209 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002211 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002212 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002213 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002216 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002217 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 return JNI_ERR;
2219 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002220
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002221 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002222
Ian Rogers60db5ab2012-02-20 17:02:00 -08002223 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225 return JNI_OK;
2226 }
2227
Elliott Hughes5174fe62011-08-23 15:12:35 -07002228 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002230 Class* c = Decode<Class*>(ts, java_class);
2231
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002232 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002233
2234 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2235 Method* m = c->GetDirectMethod(i);
2236 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002237 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002238 }
2239 }
2240 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2241 Method* m = c->GetVirtualMethod(i);
2242 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002243 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002244 }
2245 }
2246
2247 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughes72025e52011-08-23 17:50:30 -07002250 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002252 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002253 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughes72025e52011-08-23 17:50:30 -07002256 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002258 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002259 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
2262 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2263 ScopedJniThreadState ts(env);
2264 Runtime* runtime = Runtime::Current();
2265 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002266 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 } else {
2268 *vm = NULL;
2269 }
2270 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2271 }
2272
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2274 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275
2276 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002277 CHECK(address != NULL); // TODO: ReportJniError
2278 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279
2280 jclass buffer_class = GetDirectByteBufferClass(env);
2281 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2282 if (mid == NULL) {
2283 return NULL;
2284 }
2285
2286 // At the moment, the Java side is limited to 32 bits.
2287 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2288 CHECK_LE(capacity, 0xffffffff);
2289 jint address_arg = reinterpret_cast<jint>(address);
2290 jint capacity_arg = static_cast<jint>(capacity);
2291
2292 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2293 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2299 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2305 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002309 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002310
Elliott Hughes75770752011-08-24 17:52:38 -07002311 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312
2313 // Do we definitely know what kind of reference this is?
2314 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2315 IndirectRefKind kind = GetIndirectRefKind(ref);
2316 switch (kind) {
2317 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002318 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2319 return JNILocalRefType;
2320 }
2321 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002322 case kGlobal:
2323 return JNIGlobalRefType;
2324 case kWeakGlobal:
2325 return JNIWeakGlobalRefType;
2326 case kSirtOrInvalid:
2327 // Is it in a stack IRT?
2328 if (ts.Self()->SirtContains(java_object)) {
2329 return JNILocalRefType;
2330 }
2331
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002332 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002333 return JNIInvalidRefType;
2334 }
2335
Elliott Hughesb465ab02011-08-24 11:21:21 -07002336 // If we're handing out direct pointers, check whether it's a direct pointer
2337 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002338 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002339 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002340 return JNILocalRefType;
2341 }
2342 }
2343
2344 return JNIInvalidRefType;
2345 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002346 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2347 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002348 }
2349};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002350
Elliott Hughes88c5c352012-03-15 18:49:48 -07002351const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002352 NULL, // reserved0.
2353 NULL, // reserved1.
2354 NULL, // reserved2.
2355 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 JNI::GetVersion,
2357 JNI::DefineClass,
2358 JNI::FindClass,
2359 JNI::FromReflectedMethod,
2360 JNI::FromReflectedField,
2361 JNI::ToReflectedMethod,
2362 JNI::GetSuperclass,
2363 JNI::IsAssignableFrom,
2364 JNI::ToReflectedField,
2365 JNI::Throw,
2366 JNI::ThrowNew,
2367 JNI::ExceptionOccurred,
2368 JNI::ExceptionDescribe,
2369 JNI::ExceptionClear,
2370 JNI::FatalError,
2371 JNI::PushLocalFrame,
2372 JNI::PopLocalFrame,
2373 JNI::NewGlobalRef,
2374 JNI::DeleteGlobalRef,
2375 JNI::DeleteLocalRef,
2376 JNI::IsSameObject,
2377 JNI::NewLocalRef,
2378 JNI::EnsureLocalCapacity,
2379 JNI::AllocObject,
2380 JNI::NewObject,
2381 JNI::NewObjectV,
2382 JNI::NewObjectA,
2383 JNI::GetObjectClass,
2384 JNI::IsInstanceOf,
2385 JNI::GetMethodID,
2386 JNI::CallObjectMethod,
2387 JNI::CallObjectMethodV,
2388 JNI::CallObjectMethodA,
2389 JNI::CallBooleanMethod,
2390 JNI::CallBooleanMethodV,
2391 JNI::CallBooleanMethodA,
2392 JNI::CallByteMethod,
2393 JNI::CallByteMethodV,
2394 JNI::CallByteMethodA,
2395 JNI::CallCharMethod,
2396 JNI::CallCharMethodV,
2397 JNI::CallCharMethodA,
2398 JNI::CallShortMethod,
2399 JNI::CallShortMethodV,
2400 JNI::CallShortMethodA,
2401 JNI::CallIntMethod,
2402 JNI::CallIntMethodV,
2403 JNI::CallIntMethodA,
2404 JNI::CallLongMethod,
2405 JNI::CallLongMethodV,
2406 JNI::CallLongMethodA,
2407 JNI::CallFloatMethod,
2408 JNI::CallFloatMethodV,
2409 JNI::CallFloatMethodA,
2410 JNI::CallDoubleMethod,
2411 JNI::CallDoubleMethodV,
2412 JNI::CallDoubleMethodA,
2413 JNI::CallVoidMethod,
2414 JNI::CallVoidMethodV,
2415 JNI::CallVoidMethodA,
2416 JNI::CallNonvirtualObjectMethod,
2417 JNI::CallNonvirtualObjectMethodV,
2418 JNI::CallNonvirtualObjectMethodA,
2419 JNI::CallNonvirtualBooleanMethod,
2420 JNI::CallNonvirtualBooleanMethodV,
2421 JNI::CallNonvirtualBooleanMethodA,
2422 JNI::CallNonvirtualByteMethod,
2423 JNI::CallNonvirtualByteMethodV,
2424 JNI::CallNonvirtualByteMethodA,
2425 JNI::CallNonvirtualCharMethod,
2426 JNI::CallNonvirtualCharMethodV,
2427 JNI::CallNonvirtualCharMethodA,
2428 JNI::CallNonvirtualShortMethod,
2429 JNI::CallNonvirtualShortMethodV,
2430 JNI::CallNonvirtualShortMethodA,
2431 JNI::CallNonvirtualIntMethod,
2432 JNI::CallNonvirtualIntMethodV,
2433 JNI::CallNonvirtualIntMethodA,
2434 JNI::CallNonvirtualLongMethod,
2435 JNI::CallNonvirtualLongMethodV,
2436 JNI::CallNonvirtualLongMethodA,
2437 JNI::CallNonvirtualFloatMethod,
2438 JNI::CallNonvirtualFloatMethodV,
2439 JNI::CallNonvirtualFloatMethodA,
2440 JNI::CallNonvirtualDoubleMethod,
2441 JNI::CallNonvirtualDoubleMethodV,
2442 JNI::CallNonvirtualDoubleMethodA,
2443 JNI::CallNonvirtualVoidMethod,
2444 JNI::CallNonvirtualVoidMethodV,
2445 JNI::CallNonvirtualVoidMethodA,
2446 JNI::GetFieldID,
2447 JNI::GetObjectField,
2448 JNI::GetBooleanField,
2449 JNI::GetByteField,
2450 JNI::GetCharField,
2451 JNI::GetShortField,
2452 JNI::GetIntField,
2453 JNI::GetLongField,
2454 JNI::GetFloatField,
2455 JNI::GetDoubleField,
2456 JNI::SetObjectField,
2457 JNI::SetBooleanField,
2458 JNI::SetByteField,
2459 JNI::SetCharField,
2460 JNI::SetShortField,
2461 JNI::SetIntField,
2462 JNI::SetLongField,
2463 JNI::SetFloatField,
2464 JNI::SetDoubleField,
2465 JNI::GetStaticMethodID,
2466 JNI::CallStaticObjectMethod,
2467 JNI::CallStaticObjectMethodV,
2468 JNI::CallStaticObjectMethodA,
2469 JNI::CallStaticBooleanMethod,
2470 JNI::CallStaticBooleanMethodV,
2471 JNI::CallStaticBooleanMethodA,
2472 JNI::CallStaticByteMethod,
2473 JNI::CallStaticByteMethodV,
2474 JNI::CallStaticByteMethodA,
2475 JNI::CallStaticCharMethod,
2476 JNI::CallStaticCharMethodV,
2477 JNI::CallStaticCharMethodA,
2478 JNI::CallStaticShortMethod,
2479 JNI::CallStaticShortMethodV,
2480 JNI::CallStaticShortMethodA,
2481 JNI::CallStaticIntMethod,
2482 JNI::CallStaticIntMethodV,
2483 JNI::CallStaticIntMethodA,
2484 JNI::CallStaticLongMethod,
2485 JNI::CallStaticLongMethodV,
2486 JNI::CallStaticLongMethodA,
2487 JNI::CallStaticFloatMethod,
2488 JNI::CallStaticFloatMethodV,
2489 JNI::CallStaticFloatMethodA,
2490 JNI::CallStaticDoubleMethod,
2491 JNI::CallStaticDoubleMethodV,
2492 JNI::CallStaticDoubleMethodA,
2493 JNI::CallStaticVoidMethod,
2494 JNI::CallStaticVoidMethodV,
2495 JNI::CallStaticVoidMethodA,
2496 JNI::GetStaticFieldID,
2497 JNI::GetStaticObjectField,
2498 JNI::GetStaticBooleanField,
2499 JNI::GetStaticByteField,
2500 JNI::GetStaticCharField,
2501 JNI::GetStaticShortField,
2502 JNI::GetStaticIntField,
2503 JNI::GetStaticLongField,
2504 JNI::GetStaticFloatField,
2505 JNI::GetStaticDoubleField,
2506 JNI::SetStaticObjectField,
2507 JNI::SetStaticBooleanField,
2508 JNI::SetStaticByteField,
2509 JNI::SetStaticCharField,
2510 JNI::SetStaticShortField,
2511 JNI::SetStaticIntField,
2512 JNI::SetStaticLongField,
2513 JNI::SetStaticFloatField,
2514 JNI::SetStaticDoubleField,
2515 JNI::NewString,
2516 JNI::GetStringLength,
2517 JNI::GetStringChars,
2518 JNI::ReleaseStringChars,
2519 JNI::NewStringUTF,
2520 JNI::GetStringUTFLength,
2521 JNI::GetStringUTFChars,
2522 JNI::ReleaseStringUTFChars,
2523 JNI::GetArrayLength,
2524 JNI::NewObjectArray,
2525 JNI::GetObjectArrayElement,
2526 JNI::SetObjectArrayElement,
2527 JNI::NewBooleanArray,
2528 JNI::NewByteArray,
2529 JNI::NewCharArray,
2530 JNI::NewShortArray,
2531 JNI::NewIntArray,
2532 JNI::NewLongArray,
2533 JNI::NewFloatArray,
2534 JNI::NewDoubleArray,
2535 JNI::GetBooleanArrayElements,
2536 JNI::GetByteArrayElements,
2537 JNI::GetCharArrayElements,
2538 JNI::GetShortArrayElements,
2539 JNI::GetIntArrayElements,
2540 JNI::GetLongArrayElements,
2541 JNI::GetFloatArrayElements,
2542 JNI::GetDoubleArrayElements,
2543 JNI::ReleaseBooleanArrayElements,
2544 JNI::ReleaseByteArrayElements,
2545 JNI::ReleaseCharArrayElements,
2546 JNI::ReleaseShortArrayElements,
2547 JNI::ReleaseIntArrayElements,
2548 JNI::ReleaseLongArrayElements,
2549 JNI::ReleaseFloatArrayElements,
2550 JNI::ReleaseDoubleArrayElements,
2551 JNI::GetBooleanArrayRegion,
2552 JNI::GetByteArrayRegion,
2553 JNI::GetCharArrayRegion,
2554 JNI::GetShortArrayRegion,
2555 JNI::GetIntArrayRegion,
2556 JNI::GetLongArrayRegion,
2557 JNI::GetFloatArrayRegion,
2558 JNI::GetDoubleArrayRegion,
2559 JNI::SetBooleanArrayRegion,
2560 JNI::SetByteArrayRegion,
2561 JNI::SetCharArrayRegion,
2562 JNI::SetShortArrayRegion,
2563 JNI::SetIntArrayRegion,
2564 JNI::SetLongArrayRegion,
2565 JNI::SetFloatArrayRegion,
2566 JNI::SetDoubleArrayRegion,
2567 JNI::RegisterNatives,
2568 JNI::UnregisterNatives,
2569 JNI::MonitorEnter,
2570 JNI::MonitorExit,
2571 JNI::GetJavaVM,
2572 JNI::GetStringRegion,
2573 JNI::GetStringUTFRegion,
2574 JNI::GetPrimitiveArrayCritical,
2575 JNI::ReleasePrimitiveArrayCritical,
2576 JNI::GetStringCritical,
2577 JNI::ReleaseStringCritical,
2578 JNI::NewWeakGlobalRef,
2579 JNI::DeleteWeakGlobalRef,
2580 JNI::ExceptionCheck,
2581 JNI::NewDirectByteBuffer,
2582 JNI::GetDirectBufferAddress,
2583 JNI::GetDirectBufferCapacity,
2584 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002585};
2586
Elliott Hughes75770752011-08-24 17:52:38 -07002587JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002588 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002589 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002590 local_ref_cookie(IRT_FIRST_SEGMENT),
2591 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002592 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002593 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002594 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002595 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002596 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002597 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002598 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002599 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2600 // errors will ensue.
2601 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2602 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002603}
2604
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002605JNIEnvExt::~JNIEnvExt() {
2606}
2607
Elliott Hughes88c5c352012-03-15 18:49:48 -07002608void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2609 check_jni = enabled;
2610 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002611}
2612
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002613void JNIEnvExt::DumpReferenceTables() {
2614 locals.Dump();
2615 monitors.Dump();
2616}
2617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002618void JNIEnvExt::PushFrame(int /*capacity*/) {
2619 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002620 stacked_local_ref_cookies.push_back(local_ref_cookie);
2621 local_ref_cookie = locals.GetSegmentState();
2622}
2623
2624void JNIEnvExt::PopFrame() {
2625 locals.SetSegmentState(local_ref_cookie);
2626 local_ref_cookie = stacked_local_ref_cookies.back();
2627 stacked_local_ref_cookies.pop_back();
2628}
2629
Carl Shapiroea4dca82011-08-01 13:45:38 -07002630// JNI Invocation interface.
2631
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2633 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2634 if (args->version < JNI_VERSION_1_2) {
2635 return JNI_EVERSION;
2636 }
2637 Runtime::Options options;
2638 for (int i = 0; i < args->nOptions; ++i) {
2639 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002640 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 }
2642 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002643 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002644 if (runtime == NULL) {
2645 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002647 runtime->Start();
2648 *p_env = Thread::Current()->GetJniEnv();
2649 *p_vm = runtime->GetJavaVM();
2650 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651}
2652
Elliott Hughesf2682d52011-08-15 16:37:04 -07002653extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654 Runtime* runtime = Runtime::Current();
2655 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002656 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002657 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002658 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002659 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 }
2661 return JNI_OK;
2662}
2663
2664// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002665extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666 return JNI_ERR;
2667}
2668
Elliott Hughescdf53122011-08-19 15:46:09 -07002669class JII {
2670 public:
2671 static jint DestroyJavaVM(JavaVM* vm) {
2672 if (vm == NULL) {
2673 return JNI_ERR;
2674 } else {
2675 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2676 delete raw_vm->runtime;
2677 return JNI_OK;
2678 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002679 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680
Elliott Hughescdf53122011-08-19 15:46:09 -07002681 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002682 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002683 }
2684
2685 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002686 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002687 }
2688
2689 static jint DetachCurrentThread(JavaVM* vm) {
2690 if (vm == NULL) {
2691 return JNI_ERR;
2692 } else {
2693 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2694 Runtime* runtime = raw_vm->runtime;
2695 runtime->DetachCurrentThread();
2696 return JNI_OK;
2697 }
2698 }
2699
2700 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2701 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2702 return JNI_EVERSION;
2703 }
2704 if (vm == NULL || env == NULL) {
2705 return JNI_ERR;
2706 }
2707 Thread* thread = Thread::Current();
2708 if (thread == NULL) {
2709 *env = NULL;
2710 return JNI_EDETACHED;
2711 }
2712 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002713 return JNI_OK;
2714 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002715};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002716
Elliott Hughes88c5c352012-03-15 18:49:48 -07002717const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002718 NULL, // reserved0
2719 NULL, // reserved1
2720 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002721 JII::DestroyJavaVM,
2722 JII::AttachCurrentThread,
2723 JII::DetachCurrentThread,
2724 JII::GetEnv,
2725 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002726};
2727
Elliott Hughesa0957642011-09-02 14:27:33 -07002728JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002729 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002730 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002731 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002732 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002733 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002734 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002735 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002736 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002737 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002738 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002739 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002740 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002741 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002742 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002743 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002744 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002745 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002746 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002747}
2748
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002749JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002750 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002751}
2752
Elliott Hughes88c5c352012-03-15 18:49:48 -07002753void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2754 check_jni = enabled;
2755 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002756}
2757
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002758void JavaVMExt::DumpReferenceTables() {
2759 {
2760 MutexLock mu(globals_lock);
2761 globals.Dump();
2762 }
2763 {
2764 MutexLock mu(weak_globals_lock);
2765 weak_globals.Dump();
2766 }
2767 {
2768 MutexLock mu(pins_lock);
2769 pin_table.Dump();
2770 }
2771}
2772
Elliott Hughes75770752011-08-24 17:52:38 -07002773bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2774 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002775
2776 // See if we've already loaded this library. If we have, and the class loader
2777 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002778 // TODO: for better results we should canonicalize the pathname (or even compare
2779 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002780 SharedLibrary* library;
2781 {
2782 // TODO: move the locking (and more of this logic) into Libraries.
2783 MutexLock mu(libraries_lock);
2784 library = libraries->Get(path);
2785 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002786 if (library != NULL) {
2787 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002788 // The library will be associated with class_loader. The JNI
2789 // spec says we can't load the same library into more than one
2790 // class loader.
2791 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2792 "ClassLoader %p; can't open in ClassLoader %p",
2793 path.c_str(), library->GetClassLoader(), class_loader);
2794 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002795 return false;
2796 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002797 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2798 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002799 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002800 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2801 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002802 return false;
2803 }
2804 return true;
2805 }
2806
2807 // Open the shared library. Because we're using a full path, the system
2808 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2809 // resolve this library's dependencies though.)
2810
2811 // Failures here are expected when java.library.path has several entries
2812 // and we have to hunt for the lib.
2813
2814 // The current version of the dynamic linker prints detailed information
2815 // about dlopen() failures. Some things to check if the message is
2816 // cryptic:
2817 // - make sure the library exists on the device
2818 // - verify that the right path is being opened (the debug log message
2819 // above can help with that)
2820 // - check to see if the library is valid (e.g. not zero bytes long)
2821 // - check config/prelink-linux-arm.map to ensure that the library
2822 // is listed and is not being overrun by the previous entry (if
2823 // loading suddenly stops working on a prelinked library, this is
2824 // a good one to check)
2825 // - write a trivial app that calls sleep() then dlopen(), attach
2826 // to it with "strace -p <pid>" while it sleeps, and watch for
2827 // attempts to open nonexistent dependent shared libs
2828
2829 // TODO: automate some of these checks!
2830
2831 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002832 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002833 // the GC to ignore us.
2834 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002835 void* handle = NULL;
2836 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002837 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002838 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002839 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002840
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002841 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002842
2843 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002844 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002845 return false;
2846 }
2847
2848 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002849 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002850 // TODO: move the locking (and more of this logic) into Libraries.
2851 MutexLock mu(libraries_lock);
2852 library = libraries->Get(path);
2853 if (library != NULL) {
2854 LOG(INFO) << "WOW: we lost a race to add shared library: "
2855 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002856 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002857 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002858 library = new SharedLibrary(path, handle, class_loader);
2859 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002860 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002861
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002862 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002863
2864 bool result = true;
2865 void* sym = dlsym(handle, "JNI_OnLoad");
2866 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002867 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002868 } else {
2869 // Call JNI_OnLoad. We have to override the current class
2870 // loader, which will always be "null" since the stuff at the
2871 // top of the stack is around Runtime.loadLibrary(). (See
2872 // the comments in the JNI FindClass function.)
2873 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2874 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002875 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002876 self->SetClassLoaderOverride(class_loader);
2877
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002878 int version = 0;
2879 {
2880 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002881 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002882 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002883 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002884
Brian Carlstromaded5f72011-10-07 17:15:04 -07002885 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002886
2887 if (version != JNI_VERSION_1_2 &&
2888 version != JNI_VERSION_1_4 &&
2889 version != JNI_VERSION_1_6) {
2890 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2891 << "bad version: " << version;
2892 // It's unwise to call dlclose() here, but we can mark it
2893 // as bad and ensure that future load attempts will fail.
2894 // We don't know how far JNI_OnLoad got, so there could
2895 // be some partially-initialized stuff accessible through
2896 // newly-registered native method calls. We could try to
2897 // unregister them, but that doesn't seem worthwhile.
2898 result = false;
2899 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002900 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2901 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002902 }
2903 }
2904
2905 library->SetResult(result);
2906 return result;
2907}
2908
2909void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2910 CHECK(m->IsNative());
2911
2912 Class* c = m->GetDeclaringClass();
2913
2914 // If this is a static method, it could be called before the class
2915 // has been initialized.
2916 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002917 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002918 return NULL;
2919 }
2920 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002921 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002922 }
2923
Brian Carlstrom16192862011-09-12 17:50:06 -07002924 std::string detail;
2925 void* native_method;
2926 {
2927 MutexLock mu(libraries_lock);
2928 native_method = libraries->FindNativeMethod(m, detail);
2929 }
2930 // throwing can cause libraries_lock to be reacquired
2931 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002932 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002933 }
2934 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002935}
2936
Elliott Hughes410c0c82011-09-01 17:58:25 -07002937void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2938 {
2939 MutexLock mu(globals_lock);
2940 globals.VisitRoots(visitor, arg);
2941 }
2942 {
2943 MutexLock mu(pins_lock);
2944 pin_table.VisitRoots(visitor, arg);
2945 }
2946 // The weak_globals table is visited by the GC itself (because it mutates the table).
2947}
2948
Elliott Hughes844f9a02012-01-24 20:19:58 -08002949jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2950 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2951 if (c.get() == NULL) {
2952 return NULL;
2953 }
2954 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2955}
2956
Ian Rogersdf20fe02011-07-20 20:34:16 -07002957} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002958
2959std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2960 switch (rhs) {
2961 case JNIInvalidRefType:
2962 os << "JNIInvalidRefType";
2963 return os;
2964 case JNILocalRefType:
2965 os << "JNILocalRefType";
2966 return os;
2967 case JNIGlobalRefType:
2968 os << "JNIGlobalRefType";
2969 return os;
2970 case JNIWeakGlobalRefType:
2971 os << "JNIWeakGlobalRefType";
2972 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002973 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002974 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002975 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002976 }
2977}