blob: 3bbd3cd1c647517b206ff3f7a5642a2f4af271fd [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 Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070031#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070033#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080034#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070035#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070036#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070037#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070038#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070039#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041namespace art {
42
Elliott Hughes2ced6a52011-10-16 18:44:48 -070043static const size_t kMonitorsInitial = 32; // Arbitrary.
44static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
45
46static const size_t kLocalsInitial = 64; // Arbitrary.
47static const size_t kLocalsMax = 512; // Arbitrary sanity check.
48
49static const size_t kPinTableInitial = 16; // Arbitrary.
50static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052static size_t gGlobalsInitial = 512; // Arbitrary.
53static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054
55static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
56static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
57
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070058void SetJniGlobalsMax(size_t max) {
59 if (max != 0) {
60 gGlobalsMax = max;
61 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
62 }
63}
Ian Rogersdf20fe02011-07-20 20:34:16 -070064
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065/*
66 * Add a local reference for an object to the current stack frame. When
67 * the native function returns, the reference will be discarded.
68 *
69 * We need to allow the same reference to be added multiple times.
70 *
71 * This will be called on otherwise unreferenced objects. We cannot do
72 * GC allocations here, and it's best if we don't grab a mutex.
73 *
74 * Returns the local reference (currently just the same pointer that was
75 * passed in), or NULL on failure.
76 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070077template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070078T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
79 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
80 Object* obj = const_cast<Object*>(const_obj);
81
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 if (obj == NULL) {
83 return NULL;
84 }
85
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
87 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088
Ian Rogers5a7a74a2011-09-26 16:32:29 -070089 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090 IndirectRef ref = locals.Add(cookie, obj);
91 if (ref == NULL) {
92 // TODO: just change Add's DCHECK to CHECK and lose this?
93 locals.Dump();
94 LOG(FATAL) << "Failed adding to JNI local reference table "
95 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070096 }
97
98#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070099 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700100 size_t entry_count = locals.Capacity();
101 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700102 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700103 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700104 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800105 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700106 }
107 }
108#endif
109
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800110 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700111 // Hand out direct pointers to support broken old apps.
112 return reinterpret_cast<T>(obj);
113 }
114
115 return reinterpret_cast<T>(ref);
116}
Brian Carlstrom51477332012-03-25 20:20:26 -0700117// Explicit instantiations
118template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
119template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
120template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
121template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
122template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700123
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124// For external use.
125template<typename T>
126T Decode(JNIEnv* public_env, jobject obj) {
127 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
128 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
129}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800130// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
131Object* DecodeObj(JNIEnv* public_env, jobject obj) {
132 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
133 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
134}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700135// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700136template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137template Class* Decode<Class*>(JNIEnv*, jobject);
138template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
139template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700140template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700141template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700143template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400144template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700145template String* Decode<String*>(JNIEnv*, jobject);
146template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700147
Ian Rogers45619fc2012-02-29 11:15:25 -0800148size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
149 size_t num_bytes = 0;
150 for (size_t i = 1; i < shorty_len; ++i) {
151 char ch = shorty[i];
152 if (ch == 'D' || ch == 'J') {
153 num_bytes += 8;
154 } else if (ch == 'L') {
155 // Argument is a reference or an array. The shorty descriptor
156 // does not distinguish between these types.
157 num_bytes += sizeof(Object*);
158 } else {
159 num_bytes += 4;
160 }
161 }
162 return num_bytes;
163}
164
165class ArgArray {
166 public:
167 explicit ArgArray(Method* method) {
168 MethodHelper mh(method);
169 shorty_ = mh.GetShorty();
170 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700171 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800172 arg_array_ = small_arg_array_;
173 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700174 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800175 arg_array_ = large_arg_array_.get();
176 }
177 }
178
Elliott Hughes77405792012-03-15 15:22:12 -0700179 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800180 return arg_array_;
181 }
182
183 void BuildArgArray(JNIEnv* public_env, va_list ap) {
184 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700185 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800186 switch (shorty_[i]) {
187 case 'Z':
188 case 'B':
189 case 'C':
190 case 'S':
191 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700192 arg_array_[offset].i = va_arg(ap, jint);
Ian Rogers45619fc2012-02-29 11:15:25 -0800193 break;
194 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700195 arg_array_[offset].f = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800196 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700197 case 'L':
198 arg_array_[offset].l = DecodeObj(env, va_arg(ap, jobject));
Ian Rogers45619fc2012-02-29 11:15:25 -0800199 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800200 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700201 arg_array_[offset].d = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800202 break;
203 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700204 arg_array_[offset].j = va_arg(ap, jlong);
Ian Rogers45619fc2012-02-29 11:15:25 -0800205 break;
206 }
207 }
208 }
209
210 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
211 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700212 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800213 switch (shorty_[i]) {
214 case 'Z':
215 case 'B':
216 case 'C':
217 case 'S':
218 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700219 arg_array_[offset].i = args[offset].i;
Ian Rogers45619fc2012-02-29 11:15:25 -0800220 break;
221 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700222 arg_array_[offset].f = args[offset].f;
Ian Rogers45619fc2012-02-29 11:15:25 -0800223 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700224 case 'L':
225 arg_array_[offset].l = DecodeObj(env, args[offset].l);
Ian Rogers45619fc2012-02-29 11:15:25 -0800226 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700228 arg_array_[offset].d = args[offset].d;
Ian Rogers45619fc2012-02-29 11:15:25 -0800229 break;
230 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700231 arg_array_[offset].j = args[offset].j;
Ian Rogers45619fc2012-02-29 11:15:25 -0800232 break;
233 }
234 }
235 }
236
Ian Rogers45619fc2012-02-29 11:15:25 -0800237 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700238 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800239 const char* shorty_;
240 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700241 JValue* arg_array_;
242 JValue small_arg_array_[kSmallArgArraySize];
243 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800244};
245
Elliott Hughes0512f022012-03-15 22:10:52 -0700246static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700247 if (obj == NULL) {
248 return NULL;
249 }
Elliott Hughes75770752011-08-24 17:52:38 -0700250 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700251 IndirectReferenceTable& weak_globals = vm->weak_globals;
252 MutexLock mu(vm->weak_globals_lock);
253 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
254 return reinterpret_cast<jweak>(ref);
255}
256
Elliott Hughesbf86d042011-08-31 17:53:14 -0700257// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700258template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700259static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700260 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700261}
262
Elliott Hughes77405792012-03-15 15:22:12 -0700263static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700264 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700265 JValue result = { 0 };
Elliott Hughes418d20f2011-09-22 14:00:39 -0700266 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700267 return result;
268}
269
Ian Rogers0571d352011-11-03 19:51:38 -0700270static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800272 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700273 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800274 ArgArray arg_array(method);
275 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700277}
278
Ian Rogers0571d352011-11-03 19:51:38 -0700279static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700280 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700281}
282
Ian Rogers0571d352011-11-03 19:51:38 -0700283static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
284 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700285 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800286 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700287 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800288 ArgArray arg_array(method);
289 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700290 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700291}
292
Ian Rogers0571d352011-11-03 19:51:38 -0700293static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
294 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800296 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700297 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800298 ArgArray arg_array(method);
299 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700300 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700301}
302
Elliott Hughes6b436852011-08-12 10:16:44 -0700303// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
304// separated with slashes but aren't wrapped with "L;" like regular descriptors
305// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
306// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
307// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700308static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700309 std::string result;
310 // Add the missing "L;" if necessary.
311 if (name[0] == '[') {
312 result = name;
313 } else {
314 result += 'L';
315 result += name;
316 result += ';';
317 }
318 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700319 if (result.find('.') != std::string::npos) {
320 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
321 << "\"" << name << "\"";
322 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700323 }
324 return result;
325}
326
Ian Rogers0571d352011-11-03 19:51:38 -0700327static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700328 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800329 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700330}
331
Ian Rogers0571d352011-11-03 19:51:38 -0700332static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700333 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700334 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700335 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700336 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700337
338 Method* method = NULL;
339 if (is_static) {
340 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700341 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700342 method = c->FindVirtualMethod(name, sig);
343 if (method == NULL) {
344 // No virtual method matching the signature. Search declared
345 // private methods and constructors.
346 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700347 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700348 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700349
Elliott Hughescdf53122011-08-19 15:46:09 -0700350 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700351 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700352 return NULL;
353 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700354
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700355 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700356}
357
Ian Rogers0571d352011-11-03 19:51:38 -0700358static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700359 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700360 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
361 return self->GetClassLoaderOverride();
362 }
363 return method->GetDeclaringClass()->GetClassLoader();
364}
365
Ian Rogers0571d352011-11-03 19:51:38 -0700366static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700367 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700368 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700369 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700370 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700371
372 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 Class* field_type;
374 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
375 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700376 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700378 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700380 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 if (field_type == NULL) {
382 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700383 DCHECK(ts.Self()->IsExceptionPending());
384 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700385 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700386 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800387 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 return NULL;
389 }
390 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700396 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700397 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800398 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700399 return NULL;
400 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700401 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700402}
403
Ian Rogers0571d352011-11-03 19:51:38 -0700404static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700405 JavaVMExt* vm = ts.Vm();
406 MutexLock mu(vm->pins_lock);
407 vm->pin_table.Add(array);
408}
409
Ian Rogers0571d352011-11-03 19:51:38 -0700410static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700411 JavaVMExt* vm = ts.Vm();
412 MutexLock mu(vm->pins_lock);
413 vm->pin_table.Remove(array);
414}
415
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700416template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700417static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700418 CHECK_GE(length, 0); // TODO: ReportJniError
419 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700420 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700421}
422
Elliott Hughes75770752011-08-24 17:52:38 -0700423template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700424static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700425 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
426 PinPrimitiveArray(ts, array);
427 if (is_copy != NULL) {
428 *is_copy = JNI_FALSE;
429 }
430 return array->GetData();
431}
432
433template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700434static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700435 if (mode != JNI_COMMIT) {
436 Array* array = Decode<Array*>(ts, java_array);
437 UnpinPrimitiveArray(ts, array);
438 }
439}
440
Ian Rogers0571d352011-11-03 19:51:38 -0700441static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700442 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700443 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700444 "%s offset=%d length=%d %s.length=%d",
445 type.c_str(), start, length, identifier, array->GetLength());
446}
Ian Rogers0571d352011-11-03 19:51:38 -0700447
448static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700449 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700450 "offset=%d length=%d string.length()=%d", start, length, array_length);
451}
Elliott Hughes814e4032011-08-23 12:07:56 -0700452
453template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700454static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700455 ArrayT* array = Decode<ArrayT*>(ts, java_array);
456 if (start < 0 || length < 0 || start + length > array->GetLength()) {
457 ThrowAIOOBE(ts, array, start, length, "src");
458 } else {
459 JavaT* data = array->GetData();
460 memcpy(buf, data + start, length * sizeof(JavaT));
461 }
462}
463
464template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700465static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700466 ArrayT* array = Decode<ArrayT*>(ts, java_array);
467 if (start < 0 || length < 0 || start + length > array->GetLength()) {
468 ThrowAIOOBE(ts, array, start, length, "dst");
469 } else {
470 JavaT* data = array->GetData();
471 memcpy(data + start, buf, length * sizeof(JavaT));
472 }
473}
474
Ian Rogers0571d352011-11-03 19:51:38 -0700475static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700476 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
477 CHECK(buffer_class.get() != NULL);
478 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
479}
480
Ian Rogers0571d352011-11-03 19:51:38 -0700481static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700482 static jclass buffer_class = InitDirectByteBufferClass(env);
483 return buffer_class;
484}
485
Elliott Hughes462c9442012-03-23 18:47:50 -0700486static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700487 if (vm == NULL || p_env == NULL) {
488 return JNI_ERR;
489 }
490
Elliott Hughes462c9442012-03-23 18:47:50 -0700491 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700492 Thread* self = Thread::Current();
493 if (self != NULL) {
494 *p_env = self->GetJniEnv();
495 return JNI_OK;
496 }
497
498 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
499
500 // No threads allowed in zygote mode.
501 if (runtime->IsZygote()) {
502 LOG(ERROR) << "Attempt to attach a thread in the zygote";
503 return JNI_ERR;
504 }
505
Elliott Hughes462c9442012-03-23 18:47:50 -0700506 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
507 const char* thread_name = NULL;
508 Object* thread_group = NULL;
509 if (args != NULL) {
510 CHECK_GE(args->version, JNI_VERSION_1_2);
511 thread_name = args->name;
512 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700513 }
Elliott Hughes75770752011-08-24 17:52:38 -0700514
Elliott Hughes462c9442012-03-23 18:47:50 -0700515 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700516 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700517 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700518}
519
Elliott Hughes79082e32011-08-25 12:07:32 -0700520class SharedLibrary {
521 public:
522 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
523 : path_(path),
524 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700525 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700526 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700527 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700528 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 }
531
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 Object* GetClassLoader() {
533 return class_loader_;
534 }
535
536 std::string GetPath() {
537 return path_;
538 }
539
540 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700541 * Check the result of an earlier call to JNI_OnLoad on this library.
542 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700544 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700546 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 // Check this so we don't end up waiting for ourselves. We need
548 // to return "true" so the caller can continue.
549 LOG(INFO) << *self << " recursive attempt to load library "
550 << "\"" << path_ << "\"";
551 return true;
552 }
553
554 MutexLock mu(jni_on_load_lock_);
555 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800556 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
557 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700558 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700559 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 }
561
562 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800563 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
564 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700565 return okay;
566 }
567
568 void SetResult(bool result) {
569 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700570 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700571
572 // Broadcast a wakeup to anybody sleeping on the condition variable.
573 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700574 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 }
576
577 void* FindSymbol(const std::string& symbol_name) {
578 return dlsym(handle_, symbol_name.c_str());
579 }
580
581 private:
582 enum JNI_OnLoadState {
583 kPending,
584 kFailed,
585 kOkay,
586 };
587
588 // Path to library "/system/lib/libjni.so".
589 std::string path_;
590
591 // The void* returned by dlopen(3).
592 void* handle_;
593
594 // The ClassLoader this library is associated with.
595 Object* class_loader_;
596
597 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700598 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700600 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700602 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700603 // Result of earlier JNI_OnLoad call.
604 JNI_OnLoadState jni_on_load_result_;
605};
606
Elliott Hughes79082e32011-08-25 12:07:32 -0700607// This exists mainly to keep implementation details out of the header file.
608class Libraries {
609 public:
610 Libraries() {
611 }
612
613 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700614 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700615 }
616
617 SharedLibrary* Get(const std::string& path) {
618 return libraries_[path];
619 }
620
621 void Put(const std::string& path, SharedLibrary* library) {
622 libraries_[path] = library;
623 }
624
625 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700626 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 std::string jni_short_name(JniShortName(m));
628 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700629 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700630 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
631 SharedLibrary* library = it->second;
632 if (library->GetClassLoader() != declaring_class_loader) {
633 // We only search libraries loaded by the appropriate ClassLoader.
634 continue;
635 }
636 // Try the short name then the long name...
637 void* fn = library->FindSymbol(jni_short_name);
638 if (fn == NULL) {
639 fn = library->FindSymbol(jni_long_name);
640 }
641 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800642 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
643 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700644 return fn;
645 }
646 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700647 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700648 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700649 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700650 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700651 return NULL;
652 }
653
654 private:
655 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
656
657 std::map<std::string, SharedLibrary*> libraries_;
658};
659
Elliott Hughes418d20f2011-09-22 14:00:39 -0700660JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
661 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
662 Object* receiver = Decode<Object*>(env, obj);
663 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800664 ArgArray arg_array(method);
665 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700666 return InvokeWithArgArray(env, receiver, method, arg_array.get());
667}
668
Elliott Hughesd07986f2011-12-06 18:27:45 -0800669JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700670 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800671}
672
Elliott Hughescdf53122011-08-19 15:46:09 -0700673class JNI {
674 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 static jint GetVersion(JNIEnv* env) {
677 ScopedJniThreadState ts(env);
678 return JNI_VERSION_1_6;
679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
682 ScopedJniThreadState ts(env);
683 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700684 return NULL;
685 }
686
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 static jclass FindClass(JNIEnv* env, const char* name) {
688 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700689 Runtime* runtime = Runtime::Current();
690 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700692 Class* c = NULL;
693 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700694 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800695 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700696 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800697 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700698 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700699 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700700 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700701
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
703 ScopedJniThreadState ts(env);
704 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700705 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
709 ScopedJniThreadState ts(env);
710 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700711 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
715 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700716 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700717 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
721 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700722 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700723 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700725
Elliott Hughes37f7a402011-08-22 18:56:01 -0700726 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
727 ScopedJniThreadState ts(env);
728 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700729 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 }
731
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700732 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700734 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700735 return AddLocalReference<jclass>(env, c->GetSuperClass());
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 jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 Class* c1 = Decode<Class*>(ts, java_class1);
741 Class* c2 = Decode<Class*>(ts, java_class2);
742 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughese84278b2012-03-22 10:06:53 -0700745 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700747 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700749 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 return JNI_TRUE;
751 } else {
752 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700753 Class* c = Decode<Class*>(ts, java_class);
754 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700755 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700757
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 static jint Throw(JNIEnv* env, jthrowable java_exception) {
759 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700760 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 if (exception == NULL) {
762 return JNI_ERR;
763 }
764 ts.Self()->SetException(exception);
765 return JNI_OK;
766 }
767
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700768 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700770 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700771 jmethodID mid = ((msg != NULL)
772 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
773 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700774 if (mid == NULL) {
775 return JNI_ERR;
776 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700777 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700778 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700779 return JNI_ERR;
780 }
781
782 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 args[0].l = s.get();
784 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
785 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700786 return JNI_ERR;
787 }
788
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700790
Elliott Hughes37f7a402011-08-22 18:56:01 -0700791 return JNI_OK;
792 }
793
794 static jboolean ExceptionCheck(JNIEnv* env) {
795 ScopedJniThreadState ts(env);
796 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
797 }
798
799 static void ExceptionClear(JNIEnv* env) {
800 ScopedJniThreadState ts(env);
801 ts.Self()->ClearException();
802 }
803
804 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700806
807 Thread* self = ts.Self();
808 Throwable* original_exception = self->GetException();
809 self->ClearException();
810
Elliott Hughesbf86d042011-08-31 17:53:14 -0700811 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700812 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
813 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
814 if (mid == NULL) {
815 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700816 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 } else {
818 env->CallVoidMethod(exception.get(), mid);
819 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700820 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 << " thrown while calling printStackTrace";
822 self->ClearException();
823 }
824 }
825
826 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700828
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 static jthrowable ExceptionOccurred(JNIEnv* env) {
830 ScopedJniThreadState ts(env);
831 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700832 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 }
834
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 static void FatalError(JNIEnv* env, const char* msg) {
836 ScopedJniThreadState ts(env);
837 LOG(FATAL) << "JNI FatalError called: " << msg;
838 }
839
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700840 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700842 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
843 return JNI_ERR;
844 }
845 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 return JNI_OK;
847 }
848
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700849 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700851 Object* survivor = Decode<Object*>(ts, java_survivor);
852 ts.Env()->PopFrame();
853 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700856 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700858 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
859 }
860
861 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
862 // TODO: we should try to expand the table if necessary.
863 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
864 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
865 return JNI_ERR;
866 }
867 // TODO: this isn't quite right, since "capacity" includes holes.
868 size_t capacity = ts.Env()->locals.Capacity();
869 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
870 if (!okay) {
871 ts.Self()->ThrowOutOfMemoryError(caller);
872 }
873 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 }
875
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
877 ScopedJniThreadState ts(env);
878 if (obj == NULL) {
879 return NULL;
880 }
881
Elliott Hughes75770752011-08-24 17:52:38 -0700882 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 IndirectReferenceTable& globals = vm->globals;
884 MutexLock mu(vm->globals_lock);
885 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
886 return reinterpret_cast<jobject>(ref);
887 }
888
889 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
890 ScopedJniThreadState ts(env);
891 if (obj == NULL) {
892 return;
893 }
894
Elliott Hughes75770752011-08-24 17:52:38 -0700895 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 IndirectReferenceTable& globals = vm->globals;
897 MutexLock mu(vm->globals_lock);
898
899 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
900 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
901 << "failed to find entry";
902 }
903 }
904
905 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
906 ScopedJniThreadState ts(env);
907 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
908 }
909
910 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
911 ScopedJniThreadState ts(env);
912 if (obj == NULL) {
913 return;
914 }
915
Elliott Hughes75770752011-08-24 17:52:38 -0700916 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 IndirectReferenceTable& weak_globals = vm->weak_globals;
918 MutexLock mu(vm->weak_globals_lock);
919
920 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
921 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
922 << "failed to find entry";
923 }
924 }
925
926 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
927 ScopedJniThreadState ts(env);
928 if (obj == NULL) {
929 return NULL;
930 }
931
932 IndirectReferenceTable& locals = ts.Env()->locals;
933
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700934 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
936 return reinterpret_cast<jobject>(ref);
937 }
938
939 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
940 ScopedJniThreadState ts(env);
941 if (obj == NULL) {
942 return;
943 }
944
945 IndirectReferenceTable& locals = ts.Env()->locals;
946
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700947 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 if (!locals.Remove(cookie, obj)) {
949 // Attempting to delete a local reference that is not in the
950 // topmost local reference frame is a no-op. DeleteLocalRef returns
951 // void and doesn't throw any exceptions, but we should probably
952 // complain about it so the user will notice that things aren't
953 // going quite the way they expect.
954 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
955 << "failed to find entry";
956 }
957 }
958
959 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
960 ScopedJniThreadState ts(env);
961 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
962 ? JNI_TRUE : JNI_FALSE;
963 }
964
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700965 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700967 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700968 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700969 return NULL;
970 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700971 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughese84278b2012-03-22 10:06:53 -0700974 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
976 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700978 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 va_end(args);
980 return result;
981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700985 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700986 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700987 return NULL;
988 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700989 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700990 if (result == NULL) {
991 return NULL;
992 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700993 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700995 if (!ts.Self()->IsExceptionPending()) {
996 return local_result;
997 } else {
998 return NULL;
999 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001004 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001005 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001006 return NULL;
1007 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001008 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001009 if (result == NULL) {
1010 return NULL;
1011 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001012 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001014 if (!ts.Self()->IsExceptionPending()) {
1015 return local_result;
1016 } else {
1017 return NULL;
1018 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1022 ScopedJniThreadState ts(env);
1023 return FindMethodID(ts, c, name, sig, false);
1024 }
1025
1026 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1027 ScopedJniThreadState ts(env);
1028 return FindMethodID(ts, c, name, sig, true);
1029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_list ap;
1034 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001035 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001037 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001042 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001043 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001048 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001049 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001056 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
1058 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001063 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001068 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_list ap;
1074 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001075 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_end(ap);
1077 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001082 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001087 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_list ap;
1093 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001094 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
1096 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001101 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_list ap;
1112 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001113 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_end(ap);
1115 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001120 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_list ap;
1131 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001132 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_end(ap);
1134 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001139 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_list ap;
1150 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001151 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 va_end(ap);
1153 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001158 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_list ap;
1169 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001170 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_end(ap);
1172 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001177 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 va_list ap;
1188 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001189 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_end(ap);
1191 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001196 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_list ap;
1207 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001208 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001214 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001219 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001222 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
1224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001227 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 va_end(ap);
1229 return local_result;
1230 }
1231
1232 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001233 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001236 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001242 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001243 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001247 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
1249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_end(ap);
1253 return result.z;
1254 }
1255
1256 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001257 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001259 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001263 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001265 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
1270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001272 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
1274 return result.b;
1275 }
1276
1277 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001280 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001284 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001289 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
1291 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001293 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_end(ap);
1295 return result.c;
1296 }
1297
1298 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001301 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
1304 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001305 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001307 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
1312 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001314 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 va_end(ap);
1316 return result.s;
1317 }
1318
1319 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001328 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
1333 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001335 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_end(ap);
1337 return result.i;
1338 }
1339
1340 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001343 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001347 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
1354 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_end(ap);
1358 return result.j;
1359 }
1360
1361 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001362 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001368 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001373 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
1375 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 va_end(ap);
1379 return result.f;
1380 }
1381
1382 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001385 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
1388 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001389 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
1396 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 va_end(ap);
1400 return result.d;
1401 }
1402
1403 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001406 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
1409 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
1417 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001418 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001419 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 va_end(ap);
1421 }
1422
1423 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001426 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
1429 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001432 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001435 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
1437 return FindFieldID(ts, c, name, sig, false);
1438 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001439
1440
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001441 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
1443 return FindFieldID(ts, c, name, sig, true);
1444 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001449 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001450 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001452
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001455 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001456 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 Object* o = Decode<Object*>(ts, java_object);
1462 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001463 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001470 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474#define GET_PRIMITIVE_FIELD(fn, instance) \
1475 ScopedJniThreadState ts(env); \
1476 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001477 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 return f->fn(o)
1479
1480#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1481 ScopedJniThreadState ts(env); \
1482 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001483 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 f->fn(o, value)
1485
1486 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1491 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1495 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1499 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1503 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1507 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1511 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1515 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001522 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001526 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 GET_PRIMITIVE_FIELD(GetLong, NULL);
1540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1548 }
1549
1550 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1551 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1552 }
1553
1554 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1555 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1556 }
1557
1558 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1559 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1560 }
1561
1562 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1563 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1564 }
1565
1566 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1567 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1568 }
1569
1570 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1571 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1572 }
1573
1574 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1575 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1576 }
1577
1578 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1579 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1580 }
1581
1582 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1583 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1584 }
1585
1586 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1587 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1588 }
1589
1590 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1591 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1592 }
1593
1594 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1595 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1596 }
1597
1598 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1599 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1600 }
1601
1602 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1603 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1604 }
1605
1606 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1607 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1608 }
1609
1610 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1611 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001614 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 ScopedJniThreadState ts(env);
1616 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001619 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
1621 return local_result;
1622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001626 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001627 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001632 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001633 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
1638 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
1642 return result.z;
1643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001652 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
1657 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 va_end(ap);
1661 return result.b;
1662 }
1663
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001664 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001666 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
1676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
1680 return result.c;
1681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
1695 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_end(ap);
1699 return result.s;
1700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
1714 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
1718 return result.i;
1719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
1733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
1737 return result.j;
1738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
1752 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 va_end(ap);
1756 return result.f;
1757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001769 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
1771 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_end(ap);
1775 return result.d;
1776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001780 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001788 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
1790 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 va_end(ap);
1794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001798 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001803 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes814e4032011-08-23 12:07:56 -07001806 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001809 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
1812 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1813 ScopedJniThreadState ts(env);
1814 if (utf == NULL) {
1815 return NULL;
1816 }
1817 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001818 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes814e4032011-08-23 12:07:56 -07001821 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1822 ScopedJniThreadState ts(env);
1823 return Decode<String*>(ts, java_string)->GetLength();
1824 }
1825
1826 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1827 ScopedJniThreadState ts(env);
1828 return Decode<String*>(ts, java_string)->GetUtfLength();
1829 }
1830
Elliott Hughesb465ab02011-08-24 11:21:21 -07001831 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001833 String* s = Decode<String*>(ts, java_string);
1834 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1835 ThrowSIOOBE(ts, start, length, s->GetLength());
1836 } else {
1837 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1838 memcpy(buf, chars + start, length * sizeof(jchar));
1839 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 }
1841
Elliott Hughesb465ab02011-08-24 11:21:21 -07001842 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001843 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 String* s = Decode<String*>(ts, java_string);
1845 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1846 ThrowSIOOBE(ts, start, length, s->GetLength());
1847 } else {
1848 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1849 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1850 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 }
1852
Elliott Hughes75770752011-08-24 17:52:38 -07001853 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001855 String* s = Decode<String*>(ts, java_string);
1856 const CharArray* chars = s->GetCharArray();
1857 PinPrimitiveArray(ts, chars);
1858 if (is_copy != NULL) {
1859 *is_copy = JNI_FALSE;
1860 }
1861 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001862 }
1863
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001864 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001865 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001866 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001871 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes75770752011-08-24 17:52:38 -07001879 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001881 if (java_string == NULL) {
1882 return NULL;
1883 }
1884 if (is_copy != NULL) {
1885 *is_copy = JNI_TRUE;
1886 }
1887 String* s = Decode<String*>(ts, java_string);
1888 size_t byte_count = s->GetUtfLength();
1889 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001890 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001891 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1892 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1893 bytes[byte_count] = '\0';
1894 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001895 }
1896
Elliott Hughes75770752011-08-24 17:52:38 -07001897 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001899 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 }
1901
Elliott Hughesbd935992011-08-22 11:59:34 -07001902 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001904 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001905 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001906 Array* array = obj->AsArray();
1907 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes814e4032011-08-23 12:07:56 -07001910 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001912 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001913 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
1916 static void SetObjectArrayElement(JNIEnv* env,
1917 jobjectArray java_array, jsize index, jobject java_value) {
1918 ScopedJniThreadState ts(env);
1919 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1920 Object* value = Decode<Object*>(ts, java_value);
1921 array->Set(index, value);
1922 }
1923
1924 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1925 ScopedJniThreadState ts(env);
1926 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1927 }
1928
1929 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1930 ScopedJniThreadState ts(env);
1931 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1932 }
1933
1934 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1935 ScopedJniThreadState ts(env);
1936 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1937 }
1938
1939 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1940 ScopedJniThreadState ts(env);
1941 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1942 }
1943
1944 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1945 ScopedJniThreadState ts(env);
1946 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1947 }
1948
1949 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1950 ScopedJniThreadState ts(env);
1951 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1952 }
1953
1954 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1955 ScopedJniThreadState ts(env);
1956 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1957 }
1958
1959 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1960 ScopedJniThreadState ts(env);
1961 CHECK_GE(length, 0); // TODO: ReportJniError
1962
1963 // Compute the array class corresponding to the given element class.
1964 Class* element_class = Decode<Class*>(ts, element_jclass);
1965 std::string descriptor;
1966 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001967 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001968
1969 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001970 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1971 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 return NULL;
1973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 // Allocate and initialize if necessary.
1976 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 if (initial_element != NULL) {
1979 Object* initial_object = Decode<Object*>(ts, initial_element);
1980 for (jsize i = 0; i < length; ++i) {
1981 result->Set(i, initial_object);
1982 }
1983 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001984 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
1987 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1988 ScopedJniThreadState ts(env);
1989 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1990 }
1991
Ian Rogersa15e67d2012-02-28 13:51:55 -08001992 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001993 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001994 Array* array = Decode<Array*>(ts, java_array);
1995 PinPrimitiveArray(ts, array);
1996 if (is_copy != NULL) {
1997 *is_copy = JNI_FALSE;
1998 }
1999 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002000 }
2001
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002002 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002005 }
2006
Elliott Hughes75770752011-08-24 17:52:38 -07002007 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002009 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002014 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002019 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002024 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes75770752011-08-24 17:52:38 -07002027 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002044 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002047 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002052 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002054 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002057 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002062 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002064 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002067 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002069 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002072 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002077 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002082 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 Class* c = Decode<Class*>(ts, java_class);
2170
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 const char* name = methods[i].name;
2173 const char* sig = methods[i].signature;
2174
2175 if (*sig == '!') {
2176 // TODO: fast jni. it's too noisy to log all these.
2177 ++sig;
2178 }
2179
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 Method* m = c->FindDirectMethod(name, sig);
2181 if (m == NULL) {
2182 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002184 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002185 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002186 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002188 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002189 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002190 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 return JNI_ERR;
2192 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002193
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002194 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002195
Ian Rogers60db5ab2012-02-20 17:02:00 -08002196 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198 return JNI_OK;
2199 }
2200
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002203 Class* c = Decode<Class*>(ts, java_class);
2204
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002205 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002206
2207 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2208 Method* m = c->GetDirectMethod(i);
2209 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002210 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002211 }
2212 }
2213 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2214 Method* m = c->GetVirtualMethod(i);
2215 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002216 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 }
2218 }
2219
2220 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Elliott Hughes72025e52011-08-23 17:50:30 -07002223 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002225 Object* o = Decode<Object*>(ts, java_object);
2226 o->MonitorEnter(ts.Self());
2227 if (ts.Self()->IsExceptionPending()) {
2228 return JNI_ERR;
2229 }
2230 ts.Env()->monitors.Add(o);
2231 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughes72025e52011-08-23 17:50:30 -07002234 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002236 Object* o = Decode<Object*>(ts, java_object);
2237 o->MonitorExit(ts.Self());
2238 if (ts.Self()->IsExceptionPending()) {
2239 return JNI_ERR;
2240 }
2241 ts.Env()->monitors.Remove(o);
2242 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
2245 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2246 ScopedJniThreadState ts(env);
2247 Runtime* runtime = Runtime::Current();
2248 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002249 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 } else {
2251 *vm = NULL;
2252 }
2253 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2254 }
2255
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2257 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258
2259 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002260 CHECK(address != NULL); // TODO: ReportJniError
2261 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002262
2263 jclass buffer_class = GetDirectByteBufferClass(env);
2264 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2265 if (mid == NULL) {
2266 return NULL;
2267 }
2268
2269 // At the moment, the Java side is limited to 32 bits.
2270 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2271 CHECK_LE(capacity, 0xffffffff);
2272 jint address_arg = reinterpret_cast<jint>(address);
2273 jint capacity_arg = static_cast<jint>(capacity);
2274
2275 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2276 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2282 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Elliott Hughesb465ab02011-08-24 11:21:21 -07002285 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2288 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Elliott Hughesb465ab02011-08-24 11:21:21 -07002291 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002293
Elliott Hughes75770752011-08-24 17:52:38 -07002294 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002295
2296 // Do we definitely know what kind of reference this is?
2297 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2298 IndirectRefKind kind = GetIndirectRefKind(ref);
2299 switch (kind) {
2300 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002301 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2302 return JNILocalRefType;
2303 }
2304 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002305 case kGlobal:
2306 return JNIGlobalRefType;
2307 case kWeakGlobal:
2308 return JNIWeakGlobalRefType;
2309 case kSirtOrInvalid:
2310 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002311 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312 return JNILocalRefType;
2313 }
2314
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002315 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002316 return JNIInvalidRefType;
2317 }
2318
Elliott Hughesb465ab02011-08-24 11:21:21 -07002319 // If we're handing out direct pointers, check whether it's a direct pointer
2320 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002321 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002322 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002323 return JNILocalRefType;
2324 }
2325 }
2326
2327 return JNIInvalidRefType;
2328 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002329 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2330 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002333
Elliott Hughes88c5c352012-03-15 18:49:48 -07002334const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002335 NULL, // reserved0.
2336 NULL, // reserved1.
2337 NULL, // reserved2.
2338 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 JNI::GetVersion,
2340 JNI::DefineClass,
2341 JNI::FindClass,
2342 JNI::FromReflectedMethod,
2343 JNI::FromReflectedField,
2344 JNI::ToReflectedMethod,
2345 JNI::GetSuperclass,
2346 JNI::IsAssignableFrom,
2347 JNI::ToReflectedField,
2348 JNI::Throw,
2349 JNI::ThrowNew,
2350 JNI::ExceptionOccurred,
2351 JNI::ExceptionDescribe,
2352 JNI::ExceptionClear,
2353 JNI::FatalError,
2354 JNI::PushLocalFrame,
2355 JNI::PopLocalFrame,
2356 JNI::NewGlobalRef,
2357 JNI::DeleteGlobalRef,
2358 JNI::DeleteLocalRef,
2359 JNI::IsSameObject,
2360 JNI::NewLocalRef,
2361 JNI::EnsureLocalCapacity,
2362 JNI::AllocObject,
2363 JNI::NewObject,
2364 JNI::NewObjectV,
2365 JNI::NewObjectA,
2366 JNI::GetObjectClass,
2367 JNI::IsInstanceOf,
2368 JNI::GetMethodID,
2369 JNI::CallObjectMethod,
2370 JNI::CallObjectMethodV,
2371 JNI::CallObjectMethodA,
2372 JNI::CallBooleanMethod,
2373 JNI::CallBooleanMethodV,
2374 JNI::CallBooleanMethodA,
2375 JNI::CallByteMethod,
2376 JNI::CallByteMethodV,
2377 JNI::CallByteMethodA,
2378 JNI::CallCharMethod,
2379 JNI::CallCharMethodV,
2380 JNI::CallCharMethodA,
2381 JNI::CallShortMethod,
2382 JNI::CallShortMethodV,
2383 JNI::CallShortMethodA,
2384 JNI::CallIntMethod,
2385 JNI::CallIntMethodV,
2386 JNI::CallIntMethodA,
2387 JNI::CallLongMethod,
2388 JNI::CallLongMethodV,
2389 JNI::CallLongMethodA,
2390 JNI::CallFloatMethod,
2391 JNI::CallFloatMethodV,
2392 JNI::CallFloatMethodA,
2393 JNI::CallDoubleMethod,
2394 JNI::CallDoubleMethodV,
2395 JNI::CallDoubleMethodA,
2396 JNI::CallVoidMethod,
2397 JNI::CallVoidMethodV,
2398 JNI::CallVoidMethodA,
2399 JNI::CallNonvirtualObjectMethod,
2400 JNI::CallNonvirtualObjectMethodV,
2401 JNI::CallNonvirtualObjectMethodA,
2402 JNI::CallNonvirtualBooleanMethod,
2403 JNI::CallNonvirtualBooleanMethodV,
2404 JNI::CallNonvirtualBooleanMethodA,
2405 JNI::CallNonvirtualByteMethod,
2406 JNI::CallNonvirtualByteMethodV,
2407 JNI::CallNonvirtualByteMethodA,
2408 JNI::CallNonvirtualCharMethod,
2409 JNI::CallNonvirtualCharMethodV,
2410 JNI::CallNonvirtualCharMethodA,
2411 JNI::CallNonvirtualShortMethod,
2412 JNI::CallNonvirtualShortMethodV,
2413 JNI::CallNonvirtualShortMethodA,
2414 JNI::CallNonvirtualIntMethod,
2415 JNI::CallNonvirtualIntMethodV,
2416 JNI::CallNonvirtualIntMethodA,
2417 JNI::CallNonvirtualLongMethod,
2418 JNI::CallNonvirtualLongMethodV,
2419 JNI::CallNonvirtualLongMethodA,
2420 JNI::CallNonvirtualFloatMethod,
2421 JNI::CallNonvirtualFloatMethodV,
2422 JNI::CallNonvirtualFloatMethodA,
2423 JNI::CallNonvirtualDoubleMethod,
2424 JNI::CallNonvirtualDoubleMethodV,
2425 JNI::CallNonvirtualDoubleMethodA,
2426 JNI::CallNonvirtualVoidMethod,
2427 JNI::CallNonvirtualVoidMethodV,
2428 JNI::CallNonvirtualVoidMethodA,
2429 JNI::GetFieldID,
2430 JNI::GetObjectField,
2431 JNI::GetBooleanField,
2432 JNI::GetByteField,
2433 JNI::GetCharField,
2434 JNI::GetShortField,
2435 JNI::GetIntField,
2436 JNI::GetLongField,
2437 JNI::GetFloatField,
2438 JNI::GetDoubleField,
2439 JNI::SetObjectField,
2440 JNI::SetBooleanField,
2441 JNI::SetByteField,
2442 JNI::SetCharField,
2443 JNI::SetShortField,
2444 JNI::SetIntField,
2445 JNI::SetLongField,
2446 JNI::SetFloatField,
2447 JNI::SetDoubleField,
2448 JNI::GetStaticMethodID,
2449 JNI::CallStaticObjectMethod,
2450 JNI::CallStaticObjectMethodV,
2451 JNI::CallStaticObjectMethodA,
2452 JNI::CallStaticBooleanMethod,
2453 JNI::CallStaticBooleanMethodV,
2454 JNI::CallStaticBooleanMethodA,
2455 JNI::CallStaticByteMethod,
2456 JNI::CallStaticByteMethodV,
2457 JNI::CallStaticByteMethodA,
2458 JNI::CallStaticCharMethod,
2459 JNI::CallStaticCharMethodV,
2460 JNI::CallStaticCharMethodA,
2461 JNI::CallStaticShortMethod,
2462 JNI::CallStaticShortMethodV,
2463 JNI::CallStaticShortMethodA,
2464 JNI::CallStaticIntMethod,
2465 JNI::CallStaticIntMethodV,
2466 JNI::CallStaticIntMethodA,
2467 JNI::CallStaticLongMethod,
2468 JNI::CallStaticLongMethodV,
2469 JNI::CallStaticLongMethodA,
2470 JNI::CallStaticFloatMethod,
2471 JNI::CallStaticFloatMethodV,
2472 JNI::CallStaticFloatMethodA,
2473 JNI::CallStaticDoubleMethod,
2474 JNI::CallStaticDoubleMethodV,
2475 JNI::CallStaticDoubleMethodA,
2476 JNI::CallStaticVoidMethod,
2477 JNI::CallStaticVoidMethodV,
2478 JNI::CallStaticVoidMethodA,
2479 JNI::GetStaticFieldID,
2480 JNI::GetStaticObjectField,
2481 JNI::GetStaticBooleanField,
2482 JNI::GetStaticByteField,
2483 JNI::GetStaticCharField,
2484 JNI::GetStaticShortField,
2485 JNI::GetStaticIntField,
2486 JNI::GetStaticLongField,
2487 JNI::GetStaticFloatField,
2488 JNI::GetStaticDoubleField,
2489 JNI::SetStaticObjectField,
2490 JNI::SetStaticBooleanField,
2491 JNI::SetStaticByteField,
2492 JNI::SetStaticCharField,
2493 JNI::SetStaticShortField,
2494 JNI::SetStaticIntField,
2495 JNI::SetStaticLongField,
2496 JNI::SetStaticFloatField,
2497 JNI::SetStaticDoubleField,
2498 JNI::NewString,
2499 JNI::GetStringLength,
2500 JNI::GetStringChars,
2501 JNI::ReleaseStringChars,
2502 JNI::NewStringUTF,
2503 JNI::GetStringUTFLength,
2504 JNI::GetStringUTFChars,
2505 JNI::ReleaseStringUTFChars,
2506 JNI::GetArrayLength,
2507 JNI::NewObjectArray,
2508 JNI::GetObjectArrayElement,
2509 JNI::SetObjectArrayElement,
2510 JNI::NewBooleanArray,
2511 JNI::NewByteArray,
2512 JNI::NewCharArray,
2513 JNI::NewShortArray,
2514 JNI::NewIntArray,
2515 JNI::NewLongArray,
2516 JNI::NewFloatArray,
2517 JNI::NewDoubleArray,
2518 JNI::GetBooleanArrayElements,
2519 JNI::GetByteArrayElements,
2520 JNI::GetCharArrayElements,
2521 JNI::GetShortArrayElements,
2522 JNI::GetIntArrayElements,
2523 JNI::GetLongArrayElements,
2524 JNI::GetFloatArrayElements,
2525 JNI::GetDoubleArrayElements,
2526 JNI::ReleaseBooleanArrayElements,
2527 JNI::ReleaseByteArrayElements,
2528 JNI::ReleaseCharArrayElements,
2529 JNI::ReleaseShortArrayElements,
2530 JNI::ReleaseIntArrayElements,
2531 JNI::ReleaseLongArrayElements,
2532 JNI::ReleaseFloatArrayElements,
2533 JNI::ReleaseDoubleArrayElements,
2534 JNI::GetBooleanArrayRegion,
2535 JNI::GetByteArrayRegion,
2536 JNI::GetCharArrayRegion,
2537 JNI::GetShortArrayRegion,
2538 JNI::GetIntArrayRegion,
2539 JNI::GetLongArrayRegion,
2540 JNI::GetFloatArrayRegion,
2541 JNI::GetDoubleArrayRegion,
2542 JNI::SetBooleanArrayRegion,
2543 JNI::SetByteArrayRegion,
2544 JNI::SetCharArrayRegion,
2545 JNI::SetShortArrayRegion,
2546 JNI::SetIntArrayRegion,
2547 JNI::SetLongArrayRegion,
2548 JNI::SetFloatArrayRegion,
2549 JNI::SetDoubleArrayRegion,
2550 JNI::RegisterNatives,
2551 JNI::UnregisterNatives,
2552 JNI::MonitorEnter,
2553 JNI::MonitorExit,
2554 JNI::GetJavaVM,
2555 JNI::GetStringRegion,
2556 JNI::GetStringUTFRegion,
2557 JNI::GetPrimitiveArrayCritical,
2558 JNI::ReleasePrimitiveArrayCritical,
2559 JNI::GetStringCritical,
2560 JNI::ReleaseStringCritical,
2561 JNI::NewWeakGlobalRef,
2562 JNI::DeleteWeakGlobalRef,
2563 JNI::ExceptionCheck,
2564 JNI::NewDirectByteBuffer,
2565 JNI::GetDirectBufferAddress,
2566 JNI::GetDirectBufferCapacity,
2567 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002568};
2569
Elliott Hughes75770752011-08-24 17:52:38 -07002570JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002571 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002572 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002573 local_ref_cookie(IRT_FIRST_SEGMENT),
2574 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002575 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002576 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002577 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002578 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002579 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002580 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002581 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002582 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2583 // errors will ensue.
2584 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2585 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002586}
2587
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002588JNIEnvExt::~JNIEnvExt() {
2589}
2590
Elliott Hughes88c5c352012-03-15 18:49:48 -07002591void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2592 check_jni = enabled;
2593 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002594}
2595
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002596void JNIEnvExt::DumpReferenceTables() {
2597 locals.Dump();
2598 monitors.Dump();
2599}
2600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002601void JNIEnvExt::PushFrame(int /*capacity*/) {
2602 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002603 stacked_local_ref_cookies.push_back(local_ref_cookie);
2604 local_ref_cookie = locals.GetSegmentState();
2605}
2606
2607void JNIEnvExt::PopFrame() {
2608 locals.SetSegmentState(local_ref_cookie);
2609 local_ref_cookie = stacked_local_ref_cookies.back();
2610 stacked_local_ref_cookies.pop_back();
2611}
2612
Carl Shapiroea4dca82011-08-01 13:45:38 -07002613// JNI Invocation interface.
2614
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002615extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2616 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2617 if (args->version < JNI_VERSION_1_2) {
2618 return JNI_EVERSION;
2619 }
2620 Runtime::Options options;
2621 for (int i = 0; i < args->nOptions; ++i) {
2622 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002623 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 }
2625 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002626 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 if (runtime == NULL) {
2628 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002630 runtime->Start();
2631 *p_env = Thread::Current()->GetJniEnv();
2632 *p_vm = runtime->GetJavaVM();
2633 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002634}
2635
Elliott Hughesf2682d52011-08-15 16:37:04 -07002636extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637 Runtime* runtime = Runtime::Current();
2638 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002639 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002641 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002642 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002643 }
2644 return JNI_OK;
2645}
2646
2647// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002648extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002649 return JNI_ERR;
2650}
2651
Elliott Hughescdf53122011-08-19 15:46:09 -07002652class JII {
2653 public:
2654 static jint DestroyJavaVM(JavaVM* vm) {
2655 if (vm == NULL) {
2656 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002657 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002658 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2659 delete raw_vm->runtime;
2660 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002662
Elliott Hughescdf53122011-08-19 15:46:09 -07002663 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002664 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002665 }
2666
2667 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002668 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002669 }
2670
2671 static jint DetachCurrentThread(JavaVM* vm) {
2672 if (vm == NULL) {
2673 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002674 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002675 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2676 Runtime* runtime = raw_vm->runtime;
2677 runtime->DetachCurrentThread();
2678 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002679 }
2680
2681 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2682 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2683 return JNI_EVERSION;
2684 }
2685 if (vm == NULL || env == NULL) {
2686 return JNI_ERR;
2687 }
2688 Thread* thread = Thread::Current();
2689 if (thread == NULL) {
2690 *env = NULL;
2691 return JNI_EDETACHED;
2692 }
2693 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002694 return JNI_OK;
2695 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002696};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697
Elliott Hughes88c5c352012-03-15 18:49:48 -07002698const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002699 NULL, // reserved0
2700 NULL, // reserved1
2701 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 JII::DestroyJavaVM,
2703 JII::AttachCurrentThread,
2704 JII::DetachCurrentThread,
2705 JII::GetEnv,
2706 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002707};
2708
Elliott Hughesa0957642011-09-02 14:27:33 -07002709JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002710 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002711 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002712 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002713 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002714 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002715 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002716 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002717 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002718 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002719 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002720 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002721 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002722 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002723 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002724 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002725 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002726 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002727 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002728}
2729
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002730JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002731 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002732}
2733
Elliott Hughes88c5c352012-03-15 18:49:48 -07002734void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2735 check_jni = enabled;
2736 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002737}
2738
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002739void JavaVMExt::DumpReferenceTables() {
2740 {
2741 MutexLock mu(globals_lock);
2742 globals.Dump();
2743 }
2744 {
2745 MutexLock mu(weak_globals_lock);
2746 weak_globals.Dump();
2747 }
2748 {
2749 MutexLock mu(pins_lock);
2750 pin_table.Dump();
2751 }
2752}
2753
Elliott Hughes75770752011-08-24 17:52:38 -07002754bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2755 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002756
2757 // See if we've already loaded this library. If we have, and the class loader
2758 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002759 // TODO: for better results we should canonicalize the pathname (or even compare
2760 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002761 SharedLibrary* library;
2762 {
2763 // TODO: move the locking (and more of this logic) into Libraries.
2764 MutexLock mu(libraries_lock);
2765 library = libraries->Get(path);
2766 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002767 if (library != NULL) {
2768 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002769 // The library will be associated with class_loader. The JNI
2770 // spec says we can't load the same library into more than one
2771 // class loader.
2772 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2773 "ClassLoader %p; can't open in ClassLoader %p",
2774 path.c_str(), library->GetClassLoader(), class_loader);
2775 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002776 return false;
2777 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002778 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2779 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002780 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002781 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2782 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002783 return false;
2784 }
2785 return true;
2786 }
2787
2788 // Open the shared library. Because we're using a full path, the system
2789 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2790 // resolve this library's dependencies though.)
2791
2792 // Failures here are expected when java.library.path has several entries
2793 // and we have to hunt for the lib.
2794
2795 // The current version of the dynamic linker prints detailed information
2796 // about dlopen() failures. Some things to check if the message is
2797 // cryptic:
2798 // - make sure the library exists on the device
2799 // - verify that the right path is being opened (the debug log message
2800 // above can help with that)
2801 // - check to see if the library is valid (e.g. not zero bytes long)
2802 // - check config/prelink-linux-arm.map to ensure that the library
2803 // is listed and is not being overrun by the previous entry (if
2804 // loading suddenly stops working on a prelinked library, this is
2805 // a good one to check)
2806 // - write a trivial app that calls sleep() then dlopen(), attach
2807 // to it with "strace -p <pid>" while it sleeps, and watch for
2808 // attempts to open nonexistent dependent shared libs
2809
2810 // TODO: automate some of these checks!
2811
2812 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002813 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002814 // the GC to ignore us.
2815 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002816 void* handle = NULL;
2817 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002818 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002819 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002820 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002821
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002822 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002823
2824 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002825 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002826 return false;
2827 }
2828
2829 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002830 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002831 // TODO: move the locking (and more of this logic) into Libraries.
2832 MutexLock mu(libraries_lock);
2833 library = libraries->Get(path);
2834 if (library != NULL) {
2835 LOG(INFO) << "WOW: we lost a race to add shared library: "
2836 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002837 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002838 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 library = new SharedLibrary(path, handle, class_loader);
2840 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002841 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002842
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002843 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002844
2845 bool result = true;
2846 void* sym = dlsym(handle, "JNI_OnLoad");
2847 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002848 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002849 } else {
2850 // Call JNI_OnLoad. We have to override the current class
2851 // loader, which will always be "null" since the stuff at the
2852 // top of the stack is around Runtime.loadLibrary(). (See
2853 // the comments in the JNI FindClass function.)
2854 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2855 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002856 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002857 self->SetClassLoaderOverride(class_loader);
2858
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002859 int version = 0;
2860 {
2861 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002862 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002863 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002864 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002865
Brian Carlstromaded5f72011-10-07 17:15:04 -07002866 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002867
2868 if (version != JNI_VERSION_1_2 &&
2869 version != JNI_VERSION_1_4 &&
2870 version != JNI_VERSION_1_6) {
2871 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2872 << "bad version: " << version;
2873 // It's unwise to call dlclose() here, but we can mark it
2874 // as bad and ensure that future load attempts will fail.
2875 // We don't know how far JNI_OnLoad got, so there could
2876 // be some partially-initialized stuff accessible through
2877 // newly-registered native method calls. We could try to
2878 // unregister them, but that doesn't seem worthwhile.
2879 result = false;
2880 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002881 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2882 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002883 }
2884 }
2885
2886 library->SetResult(result);
2887 return result;
2888}
2889
2890void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2891 CHECK(m->IsNative());
2892
2893 Class* c = m->GetDeclaringClass();
2894
2895 // If this is a static method, it could be called before the class
2896 // has been initialized.
2897 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002898 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002899 return NULL;
2900 }
2901 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002902 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 }
2904
Brian Carlstrom16192862011-09-12 17:50:06 -07002905 std::string detail;
2906 void* native_method;
2907 {
2908 MutexLock mu(libraries_lock);
2909 native_method = libraries->FindNativeMethod(m, detail);
2910 }
2911 // throwing can cause libraries_lock to be reacquired
2912 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002913 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002914 }
2915 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002916}
2917
Elliott Hughes410c0c82011-09-01 17:58:25 -07002918void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2919 {
2920 MutexLock mu(globals_lock);
2921 globals.VisitRoots(visitor, arg);
2922 }
2923 {
2924 MutexLock mu(pins_lock);
2925 pin_table.VisitRoots(visitor, arg);
2926 }
2927 // The weak_globals table is visited by the GC itself (because it mutates the table).
2928}
2929
Elliott Hughes844f9a02012-01-24 20:19:58 -08002930jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2931 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2932 if (c.get() == NULL) {
2933 return NULL;
2934 }
2935 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2936}
2937
Ian Rogersdf20fe02011-07-20 20:34:16 -07002938} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002939
2940std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2941 switch (rhs) {
2942 case JNIInvalidRefType:
2943 os << "JNIInvalidRefType";
2944 return os;
2945 case JNILocalRefType:
2946 os << "JNILocalRefType";
2947 return os;
2948 case JNIGlobalRefType:
2949 os << "JNIGlobalRefType";
2950 return os;
2951 case JNIWeakGlobalRefType:
2952 os << "JNIWeakGlobalRefType";
2953 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002954 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002955 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002956 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002957 }
2958}