blob: c5ffec10791a2216317c2d51084780e3c0703ad0 [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) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700359 Frame frame = self->GetTopOfStack();
360 Method* method = frame.GetMethod();
361 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
362 return self->GetClassLoaderOverride();
363 }
364 return method->GetDeclaringClass()->GetClassLoader();
365}
366
Ian Rogers0571d352011-11-03 19:51:38 -0700367static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700368 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700369 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700370 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700371 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700372
373 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 Class* field_type;
375 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
376 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700377 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700379 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700381 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 if (field_type == NULL) {
383 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700384 DCHECK(ts.Self()->IsExceptionPending());
385 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700386 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700387 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800388 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 return NULL;
390 }
391 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800392 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800394 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700397 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700398 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800399 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700400 return NULL;
401 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700402 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403}
404
Ian Rogers0571d352011-11-03 19:51:38 -0700405static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700406 JavaVMExt* vm = ts.Vm();
407 MutexLock mu(vm->pins_lock);
408 vm->pin_table.Add(array);
409}
410
Ian Rogers0571d352011-11-03 19:51:38 -0700411static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700412 JavaVMExt* vm = ts.Vm();
413 MutexLock mu(vm->pins_lock);
414 vm->pin_table.Remove(array);
415}
416
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700417template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700418static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700419 CHECK_GE(length, 0); // TODO: ReportJniError
420 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700421 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700425static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700426 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
427 PinPrimitiveArray(ts, array);
428 if (is_copy != NULL) {
429 *is_copy = JNI_FALSE;
430 }
431 return array->GetData();
432}
433
434template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700435static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700436 if (mode != JNI_COMMIT) {
437 Array* array = Decode<Array*>(ts, java_array);
438 UnpinPrimitiveArray(ts, array);
439 }
440}
441
Ian Rogers0571d352011-11-03 19:51:38 -0700442static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700443 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700444 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700445 "%s offset=%d length=%d %s.length=%d",
446 type.c_str(), start, length, identifier, array->GetLength());
447}
Ian Rogers0571d352011-11-03 19:51:38 -0700448
449static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700450 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700451 "offset=%d length=%d string.length()=%d", start, length, array_length);
452}
Elliott Hughes814e4032011-08-23 12:07:56 -0700453
454template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700455static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700456 ArrayT* array = Decode<ArrayT*>(ts, java_array);
457 if (start < 0 || length < 0 || start + length > array->GetLength()) {
458 ThrowAIOOBE(ts, array, start, length, "src");
459 } else {
460 JavaT* data = array->GetData();
461 memcpy(buf, data + start, length * sizeof(JavaT));
462 }
463}
464
465template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700466static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700467 ArrayT* array = Decode<ArrayT*>(ts, java_array);
468 if (start < 0 || length < 0 || start + length > array->GetLength()) {
469 ThrowAIOOBE(ts, array, start, length, "dst");
470 } else {
471 JavaT* data = array->GetData();
472 memcpy(data + start, buf, length * sizeof(JavaT));
473 }
474}
475
Ian Rogers0571d352011-11-03 19:51:38 -0700476static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700477 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
478 CHECK(buffer_class.get() != NULL);
479 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
480}
481
Ian Rogers0571d352011-11-03 19:51:38 -0700482static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700483 static jclass buffer_class = InitDirectByteBufferClass(env);
484 return buffer_class;
485}
486
Elliott Hughes462c9442012-03-23 18:47:50 -0700487static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700488 if (vm == NULL || p_env == NULL) {
489 return JNI_ERR;
490 }
491
Elliott Hughes462c9442012-03-23 18:47:50 -0700492 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700493 Thread* self = Thread::Current();
494 if (self != NULL) {
495 *p_env = self->GetJniEnv();
496 return JNI_OK;
497 }
498
499 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
500
501 // No threads allowed in zygote mode.
502 if (runtime->IsZygote()) {
503 LOG(ERROR) << "Attempt to attach a thread in the zygote";
504 return JNI_ERR;
505 }
506
Elliott Hughes462c9442012-03-23 18:47:50 -0700507 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
508 const char* thread_name = NULL;
509 Object* thread_group = NULL;
510 if (args != NULL) {
511 CHECK_GE(args->version, JNI_VERSION_1_2);
512 thread_name = args->name;
513 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700514 }
Elliott Hughes75770752011-08-24 17:52:38 -0700515
Elliott Hughes462c9442012-03-23 18:47:50 -0700516 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700517 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700518 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700519}
520
Elliott Hughes79082e32011-08-25 12:07:32 -0700521class SharedLibrary {
522 public:
523 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
524 : path_(path),
525 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700526 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700527 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700528 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 }
532
Elliott Hughes79082e32011-08-25 12:07:32 -0700533 Object* GetClassLoader() {
534 return class_loader_;
535 }
536
537 std::string GetPath() {
538 return path_;
539 }
540
541 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700542 * Check the result of an earlier call to JNI_OnLoad on this library.
543 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700545 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700547 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 // Check this so we don't end up waiting for ourselves. We need
549 // to return "true" so the caller can continue.
550 LOG(INFO) << *self << " recursive attempt to load library "
551 << "\"" << path_ << "\"";
552 return true;
553 }
554
555 MutexLock mu(jni_on_load_lock_);
556 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800557 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
558 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700559 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700560 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800564 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
565 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 return okay;
567 }
568
569 void SetResult(bool result) {
570 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700571 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700572
573 // Broadcast a wakeup to anybody sleeping on the condition variable.
574 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700575 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 }
577
578 void* FindSymbol(const std::string& symbol_name) {
579 return dlsym(handle_, symbol_name.c_str());
580 }
581
582 private:
583 enum JNI_OnLoadState {
584 kPending,
585 kFailed,
586 kOkay,
587 };
588
589 // Path to library "/system/lib/libjni.so".
590 std::string path_;
591
592 // The void* returned by dlopen(3).
593 void* handle_;
594
595 // The ClassLoader this library is associated with.
596 Object* class_loader_;
597
598 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700599 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700601 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700603 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 // Result of earlier JNI_OnLoad call.
605 JNI_OnLoadState jni_on_load_result_;
606};
607
Elliott Hughes79082e32011-08-25 12:07:32 -0700608// This exists mainly to keep implementation details out of the header file.
609class Libraries {
610 public:
611 Libraries() {
612 }
613
614 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700615 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700616 }
617
618 SharedLibrary* Get(const std::string& path) {
619 return libraries_[path];
620 }
621
622 void Put(const std::string& path, SharedLibrary* library) {
623 libraries_[path] = library;
624 }
625
626 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700627 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700628 std::string jni_short_name(JniShortName(m));
629 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700630 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700631 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
632 SharedLibrary* library = it->second;
633 if (library->GetClassLoader() != declaring_class_loader) {
634 // We only search libraries loaded by the appropriate ClassLoader.
635 continue;
636 }
637 // Try the short name then the long name...
638 void* fn = library->FindSymbol(jni_short_name);
639 if (fn == NULL) {
640 fn = library->FindSymbol(jni_long_name);
641 }
642 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800643 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
644 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700645 return fn;
646 }
647 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700648 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700649 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700650 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700651 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700652 return NULL;
653 }
654
655 private:
656 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
657
658 std::map<std::string, SharedLibrary*> libraries_;
659};
660
Elliott Hughes418d20f2011-09-22 14:00:39 -0700661JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
662 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
663 Object* receiver = Decode<Object*>(env, obj);
664 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800665 ArgArray arg_array(method);
666 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700667 return InvokeWithArgArray(env, receiver, method, arg_array.get());
668}
669
Elliott Hughesd07986f2011-12-06 18:27:45 -0800670JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700671 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800672}
673
Elliott Hughescdf53122011-08-19 15:46:09 -0700674class JNI {
675 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 static jint GetVersion(JNIEnv* env) {
678 ScopedJniThreadState ts(env);
679 return JNI_VERSION_1_6;
680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
683 ScopedJniThreadState ts(env);
684 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700685 return NULL;
686 }
687
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 static jclass FindClass(JNIEnv* env, const char* name) {
689 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700690 Runtime* runtime = Runtime::Current();
691 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700693 Class* c = NULL;
694 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700695 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800696 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700697 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800698 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700699 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700700 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
704 ScopedJniThreadState ts(env);
705 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700706 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
710 ScopedJniThreadState ts(env);
711 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700712 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
716 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700717 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700718 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
722 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700723 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700724 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700726
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
728 ScopedJniThreadState ts(env);
729 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700730 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 }
732
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700733 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700735 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700736 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700738
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 Class* c1 = Decode<Class*>(ts, java_class1);
742 Class* c2 = Decode<Class*>(ts, java_class2);
743 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughese84278b2012-03-22 10:06:53 -0700746 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700748 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700750 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700751 return JNI_TRUE;
752 } else {
753 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700754 Class* c = Decode<Class*>(ts, java_class);
755 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700758
Elliott Hughes37f7a402011-08-22 18:56:01 -0700759 static jint Throw(JNIEnv* env, jthrowable java_exception) {
760 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700761 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 if (exception == NULL) {
763 return JNI_ERR;
764 }
765 ts.Self()->SetException(exception);
766 return JNI_OK;
767 }
768
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700769 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700771 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700772 jmethodID mid = ((msg != NULL)
773 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
774 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700775 if (mid == NULL) {
776 return JNI_ERR;
777 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700778 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700779 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 return JNI_ERR;
781 }
782
783 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700784 args[0].l = s.get();
785 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
786 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700787 return JNI_ERR;
788 }
789
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700791
Elliott Hughes37f7a402011-08-22 18:56:01 -0700792 return JNI_OK;
793 }
794
795 static jboolean ExceptionCheck(JNIEnv* env) {
796 ScopedJniThreadState ts(env);
797 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
798 }
799
800 static void ExceptionClear(JNIEnv* env) {
801 ScopedJniThreadState ts(env);
802 ts.Self()->ClearException();
803 }
804
805 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700807
808 Thread* self = ts.Self();
809 Throwable* original_exception = self->GetException();
810 self->ClearException();
811
Elliott Hughesbf86d042011-08-31 17:53:14 -0700812 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
814 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
815 if (mid == NULL) {
816 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700817 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 } else {
819 env->CallVoidMethod(exception.get(), mid);
820 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700821 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 << " thrown while calling printStackTrace";
823 self->ClearException();
824 }
825 }
826
827 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700829
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 static jthrowable ExceptionOccurred(JNIEnv* env) {
831 ScopedJniThreadState ts(env);
832 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700833 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 }
835
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 static void FatalError(JNIEnv* env, const char* msg) {
837 ScopedJniThreadState ts(env);
838 LOG(FATAL) << "JNI FatalError called: " << msg;
839 }
840
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700841 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700843 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
844 return JNI_ERR;
845 }
846 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 return JNI_OK;
848 }
849
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700850 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700852 Object* survivor = Decode<Object*>(ts, java_survivor);
853 ts.Env()->PopFrame();
854 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700857 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700859 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
860 }
861
862 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
863 // TODO: we should try to expand the table if necessary.
864 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
865 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
866 return JNI_ERR;
867 }
868 // TODO: this isn't quite right, since "capacity" includes holes.
869 size_t capacity = ts.Env()->locals.Capacity();
870 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
871 if (!okay) {
872 ts.Self()->ThrowOutOfMemoryError(caller);
873 }
874 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 }
876
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
878 ScopedJniThreadState ts(env);
879 if (obj == NULL) {
880 return NULL;
881 }
882
Elliott Hughes75770752011-08-24 17:52:38 -0700883 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 IndirectReferenceTable& globals = vm->globals;
885 MutexLock mu(vm->globals_lock);
886 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
887 return reinterpret_cast<jobject>(ref);
888 }
889
890 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
891 ScopedJniThreadState ts(env);
892 if (obj == NULL) {
893 return;
894 }
895
Elliott Hughes75770752011-08-24 17:52:38 -0700896 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 IndirectReferenceTable& globals = vm->globals;
898 MutexLock mu(vm->globals_lock);
899
900 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
901 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
902 << "failed to find entry";
903 }
904 }
905
906 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
907 ScopedJniThreadState ts(env);
908 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
909 }
910
911 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
912 ScopedJniThreadState ts(env);
913 if (obj == NULL) {
914 return;
915 }
916
Elliott Hughes75770752011-08-24 17:52:38 -0700917 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 IndirectReferenceTable& weak_globals = vm->weak_globals;
919 MutexLock mu(vm->weak_globals_lock);
920
921 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
922 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
923 << "failed to find entry";
924 }
925 }
926
927 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
928 ScopedJniThreadState ts(env);
929 if (obj == NULL) {
930 return NULL;
931 }
932
933 IndirectReferenceTable& locals = ts.Env()->locals;
934
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700935 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
937 return reinterpret_cast<jobject>(ref);
938 }
939
940 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
941 ScopedJniThreadState ts(env);
942 if (obj == NULL) {
943 return;
944 }
945
946 IndirectReferenceTable& locals = ts.Env()->locals;
947
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700948 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 if (!locals.Remove(cookie, obj)) {
950 // Attempting to delete a local reference that is not in the
951 // topmost local reference frame is a no-op. DeleteLocalRef returns
952 // void and doesn't throw any exceptions, but we should probably
953 // complain about it so the user will notice that things aren't
954 // going quite the way they expect.
955 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
956 << "failed to find entry";
957 }
958 }
959
960 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
961 ScopedJniThreadState ts(env);
962 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
963 ? JNI_TRUE : JNI_FALSE;
964 }
965
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700966 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700968 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700969 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700970 return NULL;
971 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700972 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughese84278b2012-03-22 10:06:53 -0700975 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
977 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700979 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 va_end(args);
981 return result;
982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700986 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700987 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700988 return NULL;
989 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700990 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700991 if (result == NULL) {
992 return NULL;
993 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700994 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700996 if (!ts.Self()->IsExceptionPending()) {
997 return local_result;
998 } else {
999 return NULL;
1000 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001005 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001006 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001007 return NULL;
1008 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001009 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001010 if (result == NULL) {
1011 return NULL;
1012 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001013 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001015 if (!ts.Self()->IsExceptionPending()) {
1016 return local_result;
1017 } else {
1018 return NULL;
1019 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1023 ScopedJniThreadState ts(env);
1024 return FindMethodID(ts, c, name, sig, false);
1025 }
1026
1027 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1028 ScopedJniThreadState ts(env);
1029 return FindMethodID(ts, c, name, sig, true);
1030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001036 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001038 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001043 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001044 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001049 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001050 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_list ap;
1056 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001057 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_end(ap);
1059 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001064 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_list ap;
1075 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001076 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
1078 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001083 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001095 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
1097 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001102 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_list ap;
1113 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001114 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_end(ap);
1116 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001121 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001126 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_list ap;
1132 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001133 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_end(ap);
1135 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001140 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001145 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 va_list ap;
1151 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001152 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_end(ap);
1154 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001159 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001164 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_list ap;
1170 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001171 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_end(ap);
1173 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001178 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001183 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 va_list ap;
1189 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001190 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_end(ap);
1192 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001197 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001202 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 va_list ap;
1208 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001209 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001215 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001220 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
1225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001227 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001228 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
1230 return local_result;
1231 }
1232
1233 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001236 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001237 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001244 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
1247 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001248 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
1250 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001252 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_end(ap);
1254 return result.z;
1255 }
1256
1257 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001258 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
1263 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001264 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001266 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001273 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return result.b;
1276 }
1277
1278 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001287 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
1292 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001294 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_end(ap);
1296 return result.c;
1297 }
1298
1299 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001300 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001302 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
1313 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001315 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_end(ap);
1317 return result.s;
1318 }
1319
1320 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001323 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
1326 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001327 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001329 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
1334 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001336 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_end(ap);
1338 return result.i;
1339 }
1340
1341 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001344 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001348 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001350 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
1355 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001357 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
1359 return result.j;
1360 }
1361
1362 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001365 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
1368 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001369 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001371 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001374 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
1376 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_end(ap);
1380 return result.f;
1381 }
1382
1383 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001384 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001386 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
1389 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001390 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001392 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
1397 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001398 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001399 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 va_end(ap);
1401 return result.d;
1402 }
1403
1404 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001407 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
1410 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001413 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
1418 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001420 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 va_end(ap);
1422 }
1423
1424 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001425 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001427 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
1430 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001431 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001433 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001436 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
1438 return FindFieldID(ts, c, name, sig, false);
1439 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001440
1441
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001442 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 ScopedJniThreadState ts(env);
1444 return FindFieldID(ts, c, name, sig, true);
1445 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001450 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001451 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001456 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001457 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 Object* o = Decode<Object*>(ts, java_object);
1463 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001464 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001471 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475#define GET_PRIMITIVE_FIELD(fn, instance) \
1476 ScopedJniThreadState ts(env); \
1477 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001478 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 return f->fn(o)
1480
1481#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1482 ScopedJniThreadState ts(env); \
1483 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001484 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485 f->fn(o, value)
1486
1487 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001519 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001523 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001527 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001531 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 GET_PRIMITIVE_FIELD(GetLong, NULL);
1541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1549 }
1550
1551 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1552 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1553 }
1554
1555 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1556 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1557 }
1558
1559 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1560 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1561 }
1562
1563 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1564 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1565 }
1566
1567 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1568 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1569 }
1570
1571 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1572 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1573 }
1574
1575 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1576 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1577 }
1578
1579 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1580 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1581 }
1582
1583 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1584 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1585 }
1586
1587 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1588 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1589 }
1590
1591 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1592 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1593 }
1594
1595 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1596 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1597 }
1598
1599 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1600 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1601 }
1602
1603 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1604 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1605 }
1606
1607 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1608 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1609 }
1610
1611 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1612 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
1617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001620 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_end(ap);
1622 return local_result;
1623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001627 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001628 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001633 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001634 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
1639 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001641 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_end(ap);
1643 return result.z;
1644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001653 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
1658 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001660 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_end(ap);
1662 return result.b;
1663 }
1664
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001665 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001672 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
1677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_end(ap);
1681 return result.c;
1682 }
1683
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001684 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001691 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001694 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
1696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 return result.s;
1701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 ScopedJniThreadState ts(env);
1715 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_end(ap);
1719 return result.i;
1720 }
1721
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001722 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001724 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001729 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001732 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 ScopedJniThreadState ts(env);
1734 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 va_end(ap);
1738 return result.j;
1739 }
1740
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001741 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001743 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001748 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 ScopedJniThreadState ts(env);
1753 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 va_end(ap);
1757 return result.f;
1758 }
1759
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001760 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001762 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001765 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001767 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001770 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 ScopedJniThreadState ts(env);
1772 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 va_end(ap);
1776 return result.d;
1777 }
1778
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001779 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001781 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001786 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 ScopedJniThreadState ts(env);
1791 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001792 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 va_end(ap);
1795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001799 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001802 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes814e4032011-08-23 12:07:56 -07001807 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001810 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
1813 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1814 ScopedJniThreadState ts(env);
1815 if (utf == NULL) {
1816 return NULL;
1817 }
1818 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001819 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes814e4032011-08-23 12:07:56 -07001822 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1823 ScopedJniThreadState ts(env);
1824 return Decode<String*>(ts, java_string)->GetLength();
1825 }
1826
1827 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1828 ScopedJniThreadState ts(env);
1829 return Decode<String*>(ts, java_string)->GetUtfLength();
1830 }
1831
Elliott Hughesb465ab02011-08-24 11:21:21 -07001832 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001834 String* s = Decode<String*>(ts, java_string);
1835 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1836 ThrowSIOOBE(ts, start, length, s->GetLength());
1837 } else {
1838 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1839 memcpy(buf, chars + start, length * sizeof(jchar));
1840 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 }
1842
Elliott Hughesb465ab02011-08-24 11:21:21 -07001843 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001844 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001845 String* s = Decode<String*>(ts, java_string);
1846 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1847 ThrowSIOOBE(ts, start, length, s->GetLength());
1848 } else {
1849 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1850 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1851 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 }
1853
Elliott Hughes75770752011-08-24 17:52:38 -07001854 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001855 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001856 String* s = Decode<String*>(ts, java_string);
1857 const CharArray* chars = s->GetCharArray();
1858 PinPrimitiveArray(ts, chars);
1859 if (is_copy != NULL) {
1860 *is_copy = JNI_FALSE;
1861 }
1862 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001872 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001877 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001881 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001882 if (java_string == NULL) {
1883 return NULL;
1884 }
1885 if (is_copy != NULL) {
1886 *is_copy = JNI_TRUE;
1887 }
1888 String* s = Decode<String*>(ts, java_string);
1889 size_t byte_count = s->GetUtfLength();
1890 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001891 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001892 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1893 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1894 bytes[byte_count] = '\0';
1895 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 }
1897
Elliott Hughes75770752011-08-24 17:52:38 -07001898 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001899 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001900 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001901 }
1902
Elliott Hughesbd935992011-08-22 11:59:34 -07001903 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001905 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001906 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001907 Array* array = obj->AsArray();
1908 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 }
1910
Elliott Hughes814e4032011-08-23 12:07:56 -07001911 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001913 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001914 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
1917 static void SetObjectArrayElement(JNIEnv* env,
1918 jobjectArray java_array, jsize index, jobject java_value) {
1919 ScopedJniThreadState ts(env);
1920 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1921 Object* value = Decode<Object*>(ts, java_value);
1922 array->Set(index, value);
1923 }
1924
1925 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1926 ScopedJniThreadState ts(env);
1927 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1928 }
1929
1930 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1931 ScopedJniThreadState ts(env);
1932 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1933 }
1934
1935 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1936 ScopedJniThreadState ts(env);
1937 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1938 }
1939
1940 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1941 ScopedJniThreadState ts(env);
1942 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1943 }
1944
1945 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1948 }
1949
1950 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1951 ScopedJniThreadState ts(env);
1952 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1953 }
1954
1955 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1958 }
1959
1960 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1961 ScopedJniThreadState ts(env);
1962 CHECK_GE(length, 0); // TODO: ReportJniError
1963
1964 // Compute the array class corresponding to the given element class.
1965 Class* element_class = Decode<Class*>(ts, element_jclass);
1966 std::string descriptor;
1967 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001968 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001969
1970 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001971 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1972 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 return NULL;
1974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 // Allocate and initialize if necessary.
1977 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001979 if (initial_element != NULL) {
1980 Object* initial_object = Decode<Object*>(ts, initial_element);
1981 for (jsize i = 0; i < length; ++i) {
1982 result->Set(i, initial_object);
1983 }
1984 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001985 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
1988 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1989 ScopedJniThreadState ts(env);
1990 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1991 }
1992
Ian Rogersa15e67d2012-02-28 13:51:55 -08001993 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001994 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001995 Array* array = Decode<Array*>(ts, java_array);
1996 PinPrimitiveArray(ts, array);
1997 if (is_copy != NULL) {
1998 *is_copy = JNI_FALSE;
1999 }
2000 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002001 }
2002
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002003 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002048 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002053 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002058 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002063 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002068 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002073 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002078 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002080 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002083 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 Class* c = Decode<Class*>(ts, java_class);
2171
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 const char* name = methods[i].name;
2174 const char* sig = methods[i].signature;
2175
2176 if (*sig == '!') {
2177 // TODO: fast jni. it's too noisy to log all these.
2178 ++sig;
2179 }
2180
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 Method* m = c->FindDirectMethod(name, sig);
2182 if (m == NULL) {
2183 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002185 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002186 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002187 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002189 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002190 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002191 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 return JNI_ERR;
2193 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002195 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196
Ian Rogers60db5ab2012-02-20 17:02:00 -08002197 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199 return JNI_OK;
2200 }
2201
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002204 Class* c = Decode<Class*>(ts, java_class);
2205
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002206 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002207
2208 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2209 Method* m = c->GetDirectMethod(i);
2210 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002211 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002212 }
2213 }
2214 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2215 Method* m = c->GetVirtualMethod(i);
2216 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002217 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002218 }
2219 }
2220
2221 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughes72025e52011-08-23 17:50:30 -07002224 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002226 Object* o = Decode<Object*>(ts, java_object);
2227 o->MonitorEnter(ts.Self());
2228 if (ts.Self()->IsExceptionPending()) {
2229 return JNI_ERR;
2230 }
2231 ts.Env()->monitors.Add(o);
2232 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes72025e52011-08-23 17:50:30 -07002235 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002237 Object* o = Decode<Object*>(ts, java_object);
2238 o->MonitorExit(ts.Self());
2239 if (ts.Self()->IsExceptionPending()) {
2240 return JNI_ERR;
2241 }
2242 ts.Env()->monitors.Remove(o);
2243 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
2246 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2247 ScopedJniThreadState ts(env);
2248 Runtime* runtime = Runtime::Current();
2249 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002250 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 } else {
2252 *vm = NULL;
2253 }
2254 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2255 }
2256
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2258 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002259
2260 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002261 CHECK(address != NULL); // TODO: ReportJniError
2262 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263
2264 jclass buffer_class = GetDirectByteBufferClass(env);
2265 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2266 if (mid == NULL) {
2267 return NULL;
2268 }
2269
2270 // At the moment, the Java side is limited to 32 bits.
2271 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2272 CHECK_LE(capacity, 0xffffffff);
2273 jint address_arg = reinterpret_cast<jint>(address);
2274 jint capacity_arg = static_cast<jint>(capacity);
2275
2276 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2277 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002282 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2283 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002288 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2289 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002294
Elliott Hughes75770752011-08-24 17:52:38 -07002295 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296
2297 // Do we definitely know what kind of reference this is?
2298 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2299 IndirectRefKind kind = GetIndirectRefKind(ref);
2300 switch (kind) {
2301 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002302 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2303 return JNILocalRefType;
2304 }
2305 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306 case kGlobal:
2307 return JNIGlobalRefType;
2308 case kWeakGlobal:
2309 return JNIWeakGlobalRefType;
2310 case kSirtOrInvalid:
2311 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002312 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002313 return JNILocalRefType;
2314 }
2315
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002316 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002317 return JNIInvalidRefType;
2318 }
2319
Elliott Hughesb465ab02011-08-24 11:21:21 -07002320 // If we're handing out direct pointers, check whether it's a direct pointer
2321 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002322 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002323 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002324 return JNILocalRefType;
2325 }
2326 }
2327
2328 return JNIInvalidRefType;
2329 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002330 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2331 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 }
2333};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002334
Elliott Hughes88c5c352012-03-15 18:49:48 -07002335const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002336 NULL, // reserved0.
2337 NULL, // reserved1.
2338 NULL, // reserved2.
2339 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 JNI::GetVersion,
2341 JNI::DefineClass,
2342 JNI::FindClass,
2343 JNI::FromReflectedMethod,
2344 JNI::FromReflectedField,
2345 JNI::ToReflectedMethod,
2346 JNI::GetSuperclass,
2347 JNI::IsAssignableFrom,
2348 JNI::ToReflectedField,
2349 JNI::Throw,
2350 JNI::ThrowNew,
2351 JNI::ExceptionOccurred,
2352 JNI::ExceptionDescribe,
2353 JNI::ExceptionClear,
2354 JNI::FatalError,
2355 JNI::PushLocalFrame,
2356 JNI::PopLocalFrame,
2357 JNI::NewGlobalRef,
2358 JNI::DeleteGlobalRef,
2359 JNI::DeleteLocalRef,
2360 JNI::IsSameObject,
2361 JNI::NewLocalRef,
2362 JNI::EnsureLocalCapacity,
2363 JNI::AllocObject,
2364 JNI::NewObject,
2365 JNI::NewObjectV,
2366 JNI::NewObjectA,
2367 JNI::GetObjectClass,
2368 JNI::IsInstanceOf,
2369 JNI::GetMethodID,
2370 JNI::CallObjectMethod,
2371 JNI::CallObjectMethodV,
2372 JNI::CallObjectMethodA,
2373 JNI::CallBooleanMethod,
2374 JNI::CallBooleanMethodV,
2375 JNI::CallBooleanMethodA,
2376 JNI::CallByteMethod,
2377 JNI::CallByteMethodV,
2378 JNI::CallByteMethodA,
2379 JNI::CallCharMethod,
2380 JNI::CallCharMethodV,
2381 JNI::CallCharMethodA,
2382 JNI::CallShortMethod,
2383 JNI::CallShortMethodV,
2384 JNI::CallShortMethodA,
2385 JNI::CallIntMethod,
2386 JNI::CallIntMethodV,
2387 JNI::CallIntMethodA,
2388 JNI::CallLongMethod,
2389 JNI::CallLongMethodV,
2390 JNI::CallLongMethodA,
2391 JNI::CallFloatMethod,
2392 JNI::CallFloatMethodV,
2393 JNI::CallFloatMethodA,
2394 JNI::CallDoubleMethod,
2395 JNI::CallDoubleMethodV,
2396 JNI::CallDoubleMethodA,
2397 JNI::CallVoidMethod,
2398 JNI::CallVoidMethodV,
2399 JNI::CallVoidMethodA,
2400 JNI::CallNonvirtualObjectMethod,
2401 JNI::CallNonvirtualObjectMethodV,
2402 JNI::CallNonvirtualObjectMethodA,
2403 JNI::CallNonvirtualBooleanMethod,
2404 JNI::CallNonvirtualBooleanMethodV,
2405 JNI::CallNonvirtualBooleanMethodA,
2406 JNI::CallNonvirtualByteMethod,
2407 JNI::CallNonvirtualByteMethodV,
2408 JNI::CallNonvirtualByteMethodA,
2409 JNI::CallNonvirtualCharMethod,
2410 JNI::CallNonvirtualCharMethodV,
2411 JNI::CallNonvirtualCharMethodA,
2412 JNI::CallNonvirtualShortMethod,
2413 JNI::CallNonvirtualShortMethodV,
2414 JNI::CallNonvirtualShortMethodA,
2415 JNI::CallNonvirtualIntMethod,
2416 JNI::CallNonvirtualIntMethodV,
2417 JNI::CallNonvirtualIntMethodA,
2418 JNI::CallNonvirtualLongMethod,
2419 JNI::CallNonvirtualLongMethodV,
2420 JNI::CallNonvirtualLongMethodA,
2421 JNI::CallNonvirtualFloatMethod,
2422 JNI::CallNonvirtualFloatMethodV,
2423 JNI::CallNonvirtualFloatMethodA,
2424 JNI::CallNonvirtualDoubleMethod,
2425 JNI::CallNonvirtualDoubleMethodV,
2426 JNI::CallNonvirtualDoubleMethodA,
2427 JNI::CallNonvirtualVoidMethod,
2428 JNI::CallNonvirtualVoidMethodV,
2429 JNI::CallNonvirtualVoidMethodA,
2430 JNI::GetFieldID,
2431 JNI::GetObjectField,
2432 JNI::GetBooleanField,
2433 JNI::GetByteField,
2434 JNI::GetCharField,
2435 JNI::GetShortField,
2436 JNI::GetIntField,
2437 JNI::GetLongField,
2438 JNI::GetFloatField,
2439 JNI::GetDoubleField,
2440 JNI::SetObjectField,
2441 JNI::SetBooleanField,
2442 JNI::SetByteField,
2443 JNI::SetCharField,
2444 JNI::SetShortField,
2445 JNI::SetIntField,
2446 JNI::SetLongField,
2447 JNI::SetFloatField,
2448 JNI::SetDoubleField,
2449 JNI::GetStaticMethodID,
2450 JNI::CallStaticObjectMethod,
2451 JNI::CallStaticObjectMethodV,
2452 JNI::CallStaticObjectMethodA,
2453 JNI::CallStaticBooleanMethod,
2454 JNI::CallStaticBooleanMethodV,
2455 JNI::CallStaticBooleanMethodA,
2456 JNI::CallStaticByteMethod,
2457 JNI::CallStaticByteMethodV,
2458 JNI::CallStaticByteMethodA,
2459 JNI::CallStaticCharMethod,
2460 JNI::CallStaticCharMethodV,
2461 JNI::CallStaticCharMethodA,
2462 JNI::CallStaticShortMethod,
2463 JNI::CallStaticShortMethodV,
2464 JNI::CallStaticShortMethodA,
2465 JNI::CallStaticIntMethod,
2466 JNI::CallStaticIntMethodV,
2467 JNI::CallStaticIntMethodA,
2468 JNI::CallStaticLongMethod,
2469 JNI::CallStaticLongMethodV,
2470 JNI::CallStaticLongMethodA,
2471 JNI::CallStaticFloatMethod,
2472 JNI::CallStaticFloatMethodV,
2473 JNI::CallStaticFloatMethodA,
2474 JNI::CallStaticDoubleMethod,
2475 JNI::CallStaticDoubleMethodV,
2476 JNI::CallStaticDoubleMethodA,
2477 JNI::CallStaticVoidMethod,
2478 JNI::CallStaticVoidMethodV,
2479 JNI::CallStaticVoidMethodA,
2480 JNI::GetStaticFieldID,
2481 JNI::GetStaticObjectField,
2482 JNI::GetStaticBooleanField,
2483 JNI::GetStaticByteField,
2484 JNI::GetStaticCharField,
2485 JNI::GetStaticShortField,
2486 JNI::GetStaticIntField,
2487 JNI::GetStaticLongField,
2488 JNI::GetStaticFloatField,
2489 JNI::GetStaticDoubleField,
2490 JNI::SetStaticObjectField,
2491 JNI::SetStaticBooleanField,
2492 JNI::SetStaticByteField,
2493 JNI::SetStaticCharField,
2494 JNI::SetStaticShortField,
2495 JNI::SetStaticIntField,
2496 JNI::SetStaticLongField,
2497 JNI::SetStaticFloatField,
2498 JNI::SetStaticDoubleField,
2499 JNI::NewString,
2500 JNI::GetStringLength,
2501 JNI::GetStringChars,
2502 JNI::ReleaseStringChars,
2503 JNI::NewStringUTF,
2504 JNI::GetStringUTFLength,
2505 JNI::GetStringUTFChars,
2506 JNI::ReleaseStringUTFChars,
2507 JNI::GetArrayLength,
2508 JNI::NewObjectArray,
2509 JNI::GetObjectArrayElement,
2510 JNI::SetObjectArrayElement,
2511 JNI::NewBooleanArray,
2512 JNI::NewByteArray,
2513 JNI::NewCharArray,
2514 JNI::NewShortArray,
2515 JNI::NewIntArray,
2516 JNI::NewLongArray,
2517 JNI::NewFloatArray,
2518 JNI::NewDoubleArray,
2519 JNI::GetBooleanArrayElements,
2520 JNI::GetByteArrayElements,
2521 JNI::GetCharArrayElements,
2522 JNI::GetShortArrayElements,
2523 JNI::GetIntArrayElements,
2524 JNI::GetLongArrayElements,
2525 JNI::GetFloatArrayElements,
2526 JNI::GetDoubleArrayElements,
2527 JNI::ReleaseBooleanArrayElements,
2528 JNI::ReleaseByteArrayElements,
2529 JNI::ReleaseCharArrayElements,
2530 JNI::ReleaseShortArrayElements,
2531 JNI::ReleaseIntArrayElements,
2532 JNI::ReleaseLongArrayElements,
2533 JNI::ReleaseFloatArrayElements,
2534 JNI::ReleaseDoubleArrayElements,
2535 JNI::GetBooleanArrayRegion,
2536 JNI::GetByteArrayRegion,
2537 JNI::GetCharArrayRegion,
2538 JNI::GetShortArrayRegion,
2539 JNI::GetIntArrayRegion,
2540 JNI::GetLongArrayRegion,
2541 JNI::GetFloatArrayRegion,
2542 JNI::GetDoubleArrayRegion,
2543 JNI::SetBooleanArrayRegion,
2544 JNI::SetByteArrayRegion,
2545 JNI::SetCharArrayRegion,
2546 JNI::SetShortArrayRegion,
2547 JNI::SetIntArrayRegion,
2548 JNI::SetLongArrayRegion,
2549 JNI::SetFloatArrayRegion,
2550 JNI::SetDoubleArrayRegion,
2551 JNI::RegisterNatives,
2552 JNI::UnregisterNatives,
2553 JNI::MonitorEnter,
2554 JNI::MonitorExit,
2555 JNI::GetJavaVM,
2556 JNI::GetStringRegion,
2557 JNI::GetStringUTFRegion,
2558 JNI::GetPrimitiveArrayCritical,
2559 JNI::ReleasePrimitiveArrayCritical,
2560 JNI::GetStringCritical,
2561 JNI::ReleaseStringCritical,
2562 JNI::NewWeakGlobalRef,
2563 JNI::DeleteWeakGlobalRef,
2564 JNI::ExceptionCheck,
2565 JNI::NewDirectByteBuffer,
2566 JNI::GetDirectBufferAddress,
2567 JNI::GetDirectBufferCapacity,
2568 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002569};
2570
Elliott Hughes75770752011-08-24 17:52:38 -07002571JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002572 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002573 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002574 local_ref_cookie(IRT_FIRST_SEGMENT),
2575 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002576 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002577 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002578 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002579 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002580 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002581 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002582 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002583 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2584 // errors will ensue.
2585 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2586 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002587}
2588
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002589JNIEnvExt::~JNIEnvExt() {
2590}
2591
Elliott Hughes88c5c352012-03-15 18:49:48 -07002592void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2593 check_jni = enabled;
2594 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002595}
2596
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002597void JNIEnvExt::DumpReferenceTables() {
2598 locals.Dump();
2599 monitors.Dump();
2600}
2601
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002602void JNIEnvExt::PushFrame(int /*capacity*/) {
2603 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002604 stacked_local_ref_cookies.push_back(local_ref_cookie);
2605 local_ref_cookie = locals.GetSegmentState();
2606}
2607
2608void JNIEnvExt::PopFrame() {
2609 locals.SetSegmentState(local_ref_cookie);
2610 local_ref_cookie = stacked_local_ref_cookies.back();
2611 stacked_local_ref_cookies.pop_back();
2612}
2613
Carl Shapiroea4dca82011-08-01 13:45:38 -07002614// JNI Invocation interface.
2615
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2617 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2618 if (args->version < JNI_VERSION_1_2) {
2619 return JNI_EVERSION;
2620 }
2621 Runtime::Options options;
2622 for (int i = 0; i < args->nOptions; ++i) {
2623 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002624 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002625 }
2626 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002627 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002628 if (runtime == NULL) {
2629 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002630 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002631 runtime->Start();
2632 *p_env = Thread::Current()->GetJniEnv();
2633 *p_vm = runtime->GetJavaVM();
2634 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002635}
2636
Elliott Hughesf2682d52011-08-15 16:37:04 -07002637extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 Runtime* runtime = Runtime::Current();
2639 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002640 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002642 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002643 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002644 }
2645 return JNI_OK;
2646}
2647
2648// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002649extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002650 return JNI_ERR;
2651}
2652
Elliott Hughescdf53122011-08-19 15:46:09 -07002653class JII {
2654 public:
2655 static jint DestroyJavaVM(JavaVM* vm) {
2656 if (vm == NULL) {
2657 return JNI_ERR;
2658 } else {
2659 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2660 delete raw_vm->runtime;
2661 return JNI_OK;
2662 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002663 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664
Elliott Hughescdf53122011-08-19 15:46:09 -07002665 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002666 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002667 }
2668
2669 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002670 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002671 }
2672
2673 static jint DetachCurrentThread(JavaVM* vm) {
2674 if (vm == NULL) {
2675 return JNI_ERR;
2676 } else {
2677 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2678 Runtime* runtime = raw_vm->runtime;
2679 runtime->DetachCurrentThread();
2680 return JNI_OK;
2681 }
2682 }
2683
2684 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2685 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2686 return JNI_EVERSION;
2687 }
2688 if (vm == NULL || env == NULL) {
2689 return JNI_ERR;
2690 }
2691 Thread* thread = Thread::Current();
2692 if (thread == NULL) {
2693 *env = NULL;
2694 return JNI_EDETACHED;
2695 }
2696 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697 return JNI_OK;
2698 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002699};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002700
Elliott Hughes88c5c352012-03-15 18:49:48 -07002701const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002702 NULL, // reserved0
2703 NULL, // reserved1
2704 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 JII::DestroyJavaVM,
2706 JII::AttachCurrentThread,
2707 JII::DetachCurrentThread,
2708 JII::GetEnv,
2709 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002710};
2711
Elliott Hughesa0957642011-09-02 14:27:33 -07002712JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002713 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002714 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002715 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002716 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002717 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002718 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002719 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002720 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002721 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002722 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002723 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002724 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002725 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002726 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002727 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002728 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002729 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002730 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002731}
2732
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002733JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002734 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002735}
2736
Elliott Hughes88c5c352012-03-15 18:49:48 -07002737void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2738 check_jni = enabled;
2739 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002740}
2741
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002742void JavaVMExt::DumpReferenceTables() {
2743 {
2744 MutexLock mu(globals_lock);
2745 globals.Dump();
2746 }
2747 {
2748 MutexLock mu(weak_globals_lock);
2749 weak_globals.Dump();
2750 }
2751 {
2752 MutexLock mu(pins_lock);
2753 pin_table.Dump();
2754 }
2755}
2756
Elliott Hughes75770752011-08-24 17:52:38 -07002757bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2758 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002759
2760 // See if we've already loaded this library. If we have, and the class loader
2761 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002762 // TODO: for better results we should canonicalize the pathname (or even compare
2763 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002764 SharedLibrary* library;
2765 {
2766 // TODO: move the locking (and more of this logic) into Libraries.
2767 MutexLock mu(libraries_lock);
2768 library = libraries->Get(path);
2769 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 if (library != NULL) {
2771 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002772 // The library will be associated with class_loader. The JNI
2773 // spec says we can't load the same library into more than one
2774 // class loader.
2775 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2776 "ClassLoader %p; can't open in ClassLoader %p",
2777 path.c_str(), library->GetClassLoader(), class_loader);
2778 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002779 return false;
2780 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002781 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2782 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002783 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002784 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2785 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002786 return false;
2787 }
2788 return true;
2789 }
2790
2791 // Open the shared library. Because we're using a full path, the system
2792 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2793 // resolve this library's dependencies though.)
2794
2795 // Failures here are expected when java.library.path has several entries
2796 // and we have to hunt for the lib.
2797
2798 // The current version of the dynamic linker prints detailed information
2799 // about dlopen() failures. Some things to check if the message is
2800 // cryptic:
2801 // - make sure the library exists on the device
2802 // - verify that the right path is being opened (the debug log message
2803 // above can help with that)
2804 // - check to see if the library is valid (e.g. not zero bytes long)
2805 // - check config/prelink-linux-arm.map to ensure that the library
2806 // is listed and is not being overrun by the previous entry (if
2807 // loading suddenly stops working on a prelinked library, this is
2808 // a good one to check)
2809 // - write a trivial app that calls sleep() then dlopen(), attach
2810 // to it with "strace -p <pid>" while it sleeps, and watch for
2811 // attempts to open nonexistent dependent shared libs
2812
2813 // TODO: automate some of these checks!
2814
2815 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002816 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002817 // the GC to ignore us.
2818 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002819 void* handle = NULL;
2820 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002821 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002822 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002823 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002824
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002825 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002826
2827 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002828 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002829 return false;
2830 }
2831
2832 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002833 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002834 // TODO: move the locking (and more of this logic) into Libraries.
2835 MutexLock mu(libraries_lock);
2836 library = libraries->Get(path);
2837 if (library != NULL) {
2838 LOG(INFO) << "WOW: we lost a race to add shared library: "
2839 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002840 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002841 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002842 library = new SharedLibrary(path, handle, class_loader);
2843 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002844 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002845
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002846 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002847
2848 bool result = true;
2849 void* sym = dlsym(handle, "JNI_OnLoad");
2850 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002851 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002852 } else {
2853 // Call JNI_OnLoad. We have to override the current class
2854 // loader, which will always be "null" since the stuff at the
2855 // top of the stack is around Runtime.loadLibrary(). (See
2856 // the comments in the JNI FindClass function.)
2857 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2858 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002859 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002860 self->SetClassLoaderOverride(class_loader);
2861
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002862 int version = 0;
2863 {
2864 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002865 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002866 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002868
Brian Carlstromaded5f72011-10-07 17:15:04 -07002869 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002870
2871 if (version != JNI_VERSION_1_2 &&
2872 version != JNI_VERSION_1_4 &&
2873 version != JNI_VERSION_1_6) {
2874 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2875 << "bad version: " << version;
2876 // It's unwise to call dlclose() here, but we can mark it
2877 // as bad and ensure that future load attempts will fail.
2878 // We don't know how far JNI_OnLoad got, so there could
2879 // be some partially-initialized stuff accessible through
2880 // newly-registered native method calls. We could try to
2881 // unregister them, but that doesn't seem worthwhile.
2882 result = false;
2883 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002884 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2885 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 }
2887 }
2888
2889 library->SetResult(result);
2890 return result;
2891}
2892
2893void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2894 CHECK(m->IsNative());
2895
2896 Class* c = m->GetDeclaringClass();
2897
2898 // If this is a static method, it could be called before the class
2899 // has been initialized.
2900 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002901 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002902 return NULL;
2903 }
2904 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002905 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002906 }
2907
Brian Carlstrom16192862011-09-12 17:50:06 -07002908 std::string detail;
2909 void* native_method;
2910 {
2911 MutexLock mu(libraries_lock);
2912 native_method = libraries->FindNativeMethod(m, detail);
2913 }
2914 // throwing can cause libraries_lock to be reacquired
2915 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002916 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002917 }
2918 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002919}
2920
Elliott Hughes410c0c82011-09-01 17:58:25 -07002921void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2922 {
2923 MutexLock mu(globals_lock);
2924 globals.VisitRoots(visitor, arg);
2925 }
2926 {
2927 MutexLock mu(pins_lock);
2928 pin_table.VisitRoots(visitor, arg);
2929 }
2930 // The weak_globals table is visited by the GC itself (because it mutates the table).
2931}
2932
Elliott Hughes844f9a02012-01-24 20:19:58 -08002933jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2934 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2935 if (c.get() == NULL) {
2936 return NULL;
2937 }
2938 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2939}
2940
Ian Rogersdf20fe02011-07-20 20:34:16 -07002941} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002942
2943std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2944 switch (rhs) {
2945 case JNIInvalidRefType:
2946 os << "JNIInvalidRefType";
2947 return os;
2948 case JNILocalRefType:
2949 os << "JNILocalRefType";
2950 return os;
2951 case JNIGlobalRefType:
2952 os << "JNIGlobalRefType";
2953 return os;
2954 case JNIWeakGlobalRefType:
2955 os << "JNIWeakGlobalRefType";
2956 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002957 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002958 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002959 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002960 }
2961}