blob: 5230de9207b5bc3182c56f20c0ea707062b02086 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070029#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070034#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070037#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070038#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070039#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070059void SetJniGlobalsMax(size_t max) {
60 if (max != 0) {
61 gGlobalsMax = max;
62 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
63 }
64}
Ian Rogersdf20fe02011-07-20 20:34:16 -070065
Elliott Hughesc5f7c912011-08-18 14:00:42 -070066/*
67 * Add a local reference for an object to the current stack frame. When
68 * the native function returns, the reference will be discarded.
69 *
70 * We need to allow the same reference to be added multiple times.
71 *
72 * This will be called on otherwise unreferenced objects. We cannot do
73 * GC allocations here, and it's best if we don't grab a mutex.
74 *
75 * Returns the local reference (currently just the same pointer that was
76 * passed in), or NULL on failure.
77 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070078template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
80 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
81 Object* obj = const_cast<Object*>(const_obj);
82
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 if (obj == NULL) {
84 return NULL;
85 }
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089
Ian Rogers5a7a74a2011-09-26 16:32:29 -070090 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 IndirectRef ref = locals.Add(cookie, obj);
92 if (ref == NULL) {
93 // TODO: just change Add's DCHECK to CHECK and lose this?
94 locals.Dump();
95 LOG(FATAL) << "Failed adding to JNI local reference table "
96 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 }
98
99#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700100 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101 size_t entry_count = locals.Capacity();
102 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700104 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700105 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800106 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700107 }
108 }
109#endif
110
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800111 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 // Hand out direct pointers to support broken old apps.
113 return reinterpret_cast<T>(obj);
114 }
115
116 return reinterpret_cast<T>(ref);
117}
118
Elliott Hughesbf86d042011-08-31 17:53:14 -0700119// For external use.
120template<typename T>
121T Decode(JNIEnv* public_env, jobject obj) {
122 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
123 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
124}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800125// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
126Object* DecodeObj(JNIEnv* public_env, jobject obj) {
127 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
128 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
129}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700131template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132template Class* Decode<Class*>(JNIEnv*, jobject);
133template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
134template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700135template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700136template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700137template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700138template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400139template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700140template String* Decode<String*>(JNIEnv*, jobject);
141template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142
Ian Rogers45619fc2012-02-29 11:15:25 -0800143size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
144 size_t num_bytes = 0;
145 for (size_t i = 1; i < shorty_len; ++i) {
146 char ch = shorty[i];
147 if (ch == 'D' || ch == 'J') {
148 num_bytes += 8;
149 } else if (ch == 'L') {
150 // Argument is a reference or an array. The shorty descriptor
151 // does not distinguish between these types.
152 num_bytes += sizeof(Object*);
153 } else {
154 num_bytes += 4;
155 }
156 }
157 return num_bytes;
158}
159
160class ArgArray {
161 public:
162 explicit ArgArray(Method* method) {
163 MethodHelper mh(method);
164 shorty_ = mh.GetShorty();
165 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700166 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800167 arg_array_ = small_arg_array_;
168 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700169 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800170 arg_array_ = large_arg_array_.get();
171 }
172 }
173
Elliott Hughes77405792012-03-15 15:22:12 -0700174 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800175 return arg_array_;
176 }
177
178 void BuildArgArray(JNIEnv* public_env, va_list ap) {
179 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700180 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800181 switch (shorty_[i]) {
182 case 'Z':
183 case 'B':
184 case 'C':
185 case 'S':
186 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700187 arg_array_[offset].i = va_arg(ap, jint);
Ian Rogers45619fc2012-02-29 11:15:25 -0800188 break;
189 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700190 arg_array_[offset].f = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800191 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700192 case 'L':
193 arg_array_[offset].l = DecodeObj(env, va_arg(ap, jobject));
Ian Rogers45619fc2012-02-29 11:15:25 -0800194 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800195 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700196 arg_array_[offset].d = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800197 break;
198 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700199 arg_array_[offset].j = va_arg(ap, jlong);
Ian Rogers45619fc2012-02-29 11:15:25 -0800200 break;
201 }
202 }
203 }
204
205 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
206 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700207 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800208 switch (shorty_[i]) {
209 case 'Z':
210 case 'B':
211 case 'C':
212 case 'S':
213 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700214 arg_array_[offset].i = args[offset].i;
Ian Rogers45619fc2012-02-29 11:15:25 -0800215 break;
216 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700217 arg_array_[offset].f = args[offset].f;
Ian Rogers45619fc2012-02-29 11:15:25 -0800218 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700219 case 'L':
220 arg_array_[offset].l = DecodeObj(env, args[offset].l);
Ian Rogers45619fc2012-02-29 11:15:25 -0800221 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800222 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700223 arg_array_[offset].d = args[offset].d;
Ian Rogers45619fc2012-02-29 11:15:25 -0800224 break;
225 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700226 arg_array_[offset].j = args[offset].j;
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 break;
228 }
229 }
230 }
231
Ian Rogers45619fc2012-02-29 11:15:25 -0800232 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700233 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800234 const char* shorty_;
235 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700236 JValue* arg_array_;
237 JValue small_arg_array_[kSmallArgArraySize];
238 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800239};
240
Elliott Hughes0512f022012-03-15 22:10:52 -0700241static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700242 if (obj == NULL) {
243 return NULL;
244 }
Elliott Hughes75770752011-08-24 17:52:38 -0700245 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 IndirectReferenceTable& weak_globals = vm->weak_globals;
247 MutexLock mu(vm->weak_globals_lock);
248 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
249 return reinterpret_cast<jweak>(ref);
250}
251
Elliott Hughesbf86d042011-08-31 17:53:14 -0700252// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700253template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700254static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700255 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700256}
257
Elliott Hughes77405792012-03-15 15:22:12 -0700258static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700259 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700260 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700262 return result;
263}
264
Ian Rogers0571d352011-11-03 19:51:38 -0700265static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700266 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800267 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700268 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800269 ArgArray arg_array(method);
270 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700272}
273
Ian Rogers0571d352011-11-03 19:51:38 -0700274static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700275 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700276}
277
Ian Rogers0571d352011-11-03 19:51:38 -0700278static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
279 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700280 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800281 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700282 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800283 ArgArray arg_array(method);
284 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700285 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700286}
287
Ian Rogers0571d352011-11-03 19:51:38 -0700288static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
289 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700290 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800291 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700292 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800293 ArgArray arg_array(method);
294 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700296}
297
Elliott Hughes6b436852011-08-12 10:16:44 -0700298// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
299// separated with slashes but aren't wrapped with "L;" like regular descriptors
300// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
301// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
302// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700303static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700304 std::string result;
305 // Add the missing "L;" if necessary.
306 if (name[0] == '[') {
307 result = name;
308 } else {
309 result += 'L';
310 result += name;
311 result += ';';
312 }
313 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700314 if (result.find('.') != std::string::npos) {
315 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
316 << "\"" << name << "\"";
317 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700318 }
319 return result;
320}
321
Ian Rogers0571d352011-11-03 19:51:38 -0700322static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700323 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800324 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700325}
326
Ian Rogers0571d352011-11-03 19:51:38 -0700327static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700328 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700329 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700330 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700331 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700332
333 Method* method = NULL;
334 if (is_static) {
335 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700336 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 method = c->FindVirtualMethod(name, sig);
338 if (method == NULL) {
339 // No virtual method matching the signature. Search declared
340 // private methods and constructors.
341 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700342 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700343 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700344
Elliott Hughescdf53122011-08-19 15:46:09 -0700345 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700346 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 return NULL;
348 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700349
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700350 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700351}
352
Ian Rogers0571d352011-11-03 19:51:38 -0700353static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700354 Frame frame = self->GetTopOfStack();
355 Method* method = frame.GetMethod();
356 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
357 return self->GetClassLoaderOverride();
358 }
359 return method->GetDeclaringClass()->GetClassLoader();
360}
361
Ian Rogers0571d352011-11-03 19:51:38 -0700362static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700363 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700364 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700365 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700366 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700367
368 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 Class* field_type;
370 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
371 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700372 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700374 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700376 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 if (field_type == NULL) {
378 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700379 DCHECK(ts.Self()->IsExceptionPending());
380 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700381 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700382 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800383 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 return NULL;
385 }
386 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700391 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700392 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800394 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700395 return NULL;
396 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700397 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700398}
399
Ian Rogers0571d352011-11-03 19:51:38 -0700400static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700401 JavaVMExt* vm = ts.Vm();
402 MutexLock mu(vm->pins_lock);
403 vm->pin_table.Add(array);
404}
405
Ian Rogers0571d352011-11-03 19:51:38 -0700406static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700407 JavaVMExt* vm = ts.Vm();
408 MutexLock mu(vm->pins_lock);
409 vm->pin_table.Remove(array);
410}
411
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700412template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700413static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700414 CHECK_GE(length, 0); // TODO: ReportJniError
415 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700416 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700417}
418
Elliott Hughes75770752011-08-24 17:52:38 -0700419template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700420static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700421 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
422 PinPrimitiveArray(ts, array);
423 if (is_copy != NULL) {
424 *is_copy = JNI_FALSE;
425 }
426 return array->GetData();
427}
428
429template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700430static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700431 if (mode != JNI_COMMIT) {
432 Array* array = Decode<Array*>(ts, java_array);
433 UnpinPrimitiveArray(ts, array);
434 }
435}
436
Ian Rogers0571d352011-11-03 19:51:38 -0700437static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700438 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700439 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700440 "%s offset=%d length=%d %s.length=%d",
441 type.c_str(), start, length, identifier, array->GetLength());
442}
Ian Rogers0571d352011-11-03 19:51:38 -0700443
444static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700445 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700446 "offset=%d length=%d string.length()=%d", start, length, array_length);
447}
Elliott Hughes814e4032011-08-23 12:07:56 -0700448
449template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700450static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700451 ArrayT* array = Decode<ArrayT*>(ts, java_array);
452 if (start < 0 || length < 0 || start + length > array->GetLength()) {
453 ThrowAIOOBE(ts, array, start, length, "src");
454 } else {
455 JavaT* data = array->GetData();
456 memcpy(buf, data + start, length * sizeof(JavaT));
457 }
458}
459
460template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700461static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700462 ArrayT* array = Decode<ArrayT*>(ts, java_array);
463 if (start < 0 || length < 0 || start + length > array->GetLength()) {
464 ThrowAIOOBE(ts, array, start, length, "dst");
465 } else {
466 JavaT* data = array->GetData();
467 memcpy(data + start, buf, length * sizeof(JavaT));
468 }
469}
470
Ian Rogers0571d352011-11-03 19:51:38 -0700471static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700472 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
473 CHECK(buffer_class.get() != NULL);
474 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
475}
476
Ian Rogers0571d352011-11-03 19:51:38 -0700477static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700478 static jclass buffer_class = InitDirectByteBufferClass(env);
479 return buffer_class;
480}
481
Ian Rogers0571d352011-11-03 19:51:38 -0700482static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700483 if (vm == NULL || p_env == NULL) {
484 return JNI_ERR;
485 }
486
Elliott Hughescac6cc72011-11-03 20:31:21 -0700487 // Return immediately if we're already one with the VM.
488 Thread* self = Thread::Current();
489 if (self != NULL) {
490 *p_env = self->GetJniEnv();
491 return JNI_OK;
492 }
493
494 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
495
496 // No threads allowed in zygote mode.
497 if (runtime->IsZygote()) {
498 LOG(ERROR) << "Attempt to attach a thread in the zygote";
499 return JNI_ERR;
500 }
501
Elliott Hughes75770752011-08-24 17:52:38 -0700502 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
503 JavaVMAttachArgs args;
504 if (thr_args == NULL) {
505 // Allow the v1.1 calling convention.
506 args.version = JNI_VERSION_1_2;
507 args.name = NULL;
508 args.group = NULL; // TODO: get "main" thread group
509 } else {
510 args.version = in_args->version;
511 args.name = in_args->name;
512 if (in_args->group != NULL) {
513 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
514 args.group = NULL; // TODO: decode in_args->group
515 } else {
516 args.group = NULL; // TODO: get "main" thread group
517 }
518 }
519 CHECK_GE(args.version, JNI_VERSION_1_2);
520
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700521 runtime->AttachCurrentThread(args.name, as_daemon);
522 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700523 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700524}
525
Elliott Hughes79082e32011-08-25 12:07:32 -0700526class SharedLibrary {
527 public:
528 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
529 : path_(path),
530 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700531 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700532 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700533 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700534 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700536 }
537
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 Object* GetClassLoader() {
539 return class_loader_;
540 }
541
542 std::string GetPath() {
543 return path_;
544 }
545
546 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700547 * Check the result of an earlier call to JNI_OnLoad on this library.
548 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700550 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700552 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 // Check this so we don't end up waiting for ourselves. We need
554 // to return "true" so the caller can continue.
555 LOG(INFO) << *self << " recursive attempt to load library "
556 << "\"" << path_ << "\"";
557 return true;
558 }
559
560 MutexLock mu(jni_on_load_lock_);
561 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800562 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
563 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700564 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700565 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 }
567
568 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800569 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
570 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 return okay;
572 }
573
574 void SetResult(bool result) {
575 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700576 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700577
578 // Broadcast a wakeup to anybody sleeping on the condition variable.
579 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700580 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700581 }
582
583 void* FindSymbol(const std::string& symbol_name) {
584 return dlsym(handle_, symbol_name.c_str());
585 }
586
587 private:
588 enum JNI_OnLoadState {
589 kPending,
590 kFailed,
591 kOkay,
592 };
593
594 // Path to library "/system/lib/libjni.so".
595 std::string path_;
596
597 // The void* returned by dlopen(3).
598 void* handle_;
599
600 // The ClassLoader this library is associated with.
601 Object* class_loader_;
602
603 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700604 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700606 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700608 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 // Result of earlier JNI_OnLoad call.
610 JNI_OnLoadState jni_on_load_result_;
611};
612
Elliott Hughes79082e32011-08-25 12:07:32 -0700613// This exists mainly to keep implementation details out of the header file.
614class Libraries {
615 public:
616 Libraries() {
617 }
618
619 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700620 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700621 }
622
623 SharedLibrary* Get(const std::string& path) {
624 return libraries_[path];
625 }
626
627 void Put(const std::string& path, SharedLibrary* library) {
628 libraries_[path] = library;
629 }
630
631 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700632 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700633 std::string jni_short_name(JniShortName(m));
634 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700635 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700636 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
637 SharedLibrary* library = it->second;
638 if (library->GetClassLoader() != declaring_class_loader) {
639 // We only search libraries loaded by the appropriate ClassLoader.
640 continue;
641 }
642 // Try the short name then the long name...
643 void* fn = library->FindSymbol(jni_short_name);
644 if (fn == NULL) {
645 fn = library->FindSymbol(jni_long_name);
646 }
647 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800648 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
649 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700650 return fn;
651 }
652 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700653 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700654 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700655 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700657 return NULL;
658 }
659
660 private:
661 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
662
663 std::map<std::string, SharedLibrary*> libraries_;
664};
665
Elliott Hughes418d20f2011-09-22 14:00:39 -0700666JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
667 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
668 Object* receiver = Decode<Object*>(env, obj);
669 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800670 ArgArray arg_array(method);
671 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700672 return InvokeWithArgArray(env, receiver, method, arg_array.get());
673}
674
Elliott Hughesd07986f2011-12-06 18:27:45 -0800675JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700676 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800677}
678
Elliott Hughescdf53122011-08-19 15:46:09 -0700679class JNI {
680 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jint GetVersion(JNIEnv* env) {
683 ScopedJniThreadState ts(env);
684 return JNI_VERSION_1_6;
685 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700686
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
688 ScopedJniThreadState ts(env);
689 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700690 return NULL;
691 }
692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jclass FindClass(JNIEnv* env, const char* name) {
694 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700695 Runtime* runtime = Runtime::Current();
696 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700698 Class* c = NULL;
699 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700700 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800701 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700702 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800703 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700705 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
709 ScopedJniThreadState ts(env);
710 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700711 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
715 ScopedJniThreadState ts(env);
716 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700717 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
721 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700722 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700723 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700725
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
727 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700728 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700729 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
733 ScopedJniThreadState ts(env);
734 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700735 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 }
737
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700738 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700740 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700741 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700743
Elliott Hughes37f7a402011-08-22 18:56:01 -0700744 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 Class* c1 = Decode<Class*>(ts, java_class1);
747 Class* c2 = Decode<Class*>(ts, java_class2);
748 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700750
Elliott Hughes37f7a402011-08-22 18:56:01 -0700751 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700753 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700755 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 return JNI_TRUE;
757 } else {
758 Object* obj = Decode<Object*>(ts, jobj);
759 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700760 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700763
Elliott Hughes37f7a402011-08-22 18:56:01 -0700764 static jint Throw(JNIEnv* env, jthrowable java_exception) {
765 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700766 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 if (exception == NULL) {
768 return JNI_ERR;
769 }
770 ts.Self()->SetException(exception);
771 return JNI_OK;
772 }
773
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700774 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700775 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700777 jmethodID mid = ((msg != NULL)
778 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
779 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 if (mid == NULL) {
781 return JNI_ERR;
782 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700784 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700785 return JNI_ERR;
786 }
787
788 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 args[0].l = s.get();
790 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
791 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700792 return JNI_ERR;
793 }
794
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700796
Elliott Hughes37f7a402011-08-22 18:56:01 -0700797 return JNI_OK;
798 }
799
800 static jboolean ExceptionCheck(JNIEnv* env) {
801 ScopedJniThreadState ts(env);
802 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
803 }
804
805 static void ExceptionClear(JNIEnv* env) {
806 ScopedJniThreadState ts(env);
807 ts.Self()->ClearException();
808 }
809
810 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700812
813 Thread* self = ts.Self();
814 Throwable* original_exception = self->GetException();
815 self->ClearException();
816
Elliott Hughesbf86d042011-08-31 17:53:14 -0700817 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
819 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
820 if (mid == NULL) {
821 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700822 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 } else {
824 env->CallVoidMethod(exception.get(), mid);
825 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700826 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 << " thrown while calling printStackTrace";
828 self->ClearException();
829 }
830 }
831
832 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700834
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 static jthrowable ExceptionOccurred(JNIEnv* env) {
836 ScopedJniThreadState ts(env);
837 Object* exception = ts.Self()->GetException();
838 if (exception == NULL) {
839 return NULL;
840 } else {
841 // TODO: if adding a local reference failing causes the VM to abort
842 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700843 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 if (localException == NULL) {
845 // We were unable to add a new local reference, and threw a new
846 // exception. We can't return "exception", because it's not a
847 // local reference. So we have to return NULL, indicating that
848 // there was no exception, even though it's pretty much raining
849 // exceptions in here.
850 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
851 }
852 return localException;
853 }
854 }
855
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 static void FatalError(JNIEnv* env, const char* msg) {
857 ScopedJniThreadState ts(env);
858 LOG(FATAL) << "JNI FatalError called: " << msg;
859 }
860
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700861 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700863 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
864 return JNI_ERR;
865 }
866 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 return JNI_OK;
868 }
869
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700870 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700872 Object* survivor = Decode<Object*>(ts, java_survivor);
873 ts.Env()->PopFrame();
874 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700877 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700879 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
880 }
881
882 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
883 // TODO: we should try to expand the table if necessary.
884 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
885 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
886 return JNI_ERR;
887 }
888 // TODO: this isn't quite right, since "capacity" includes holes.
889 size_t capacity = ts.Env()->locals.Capacity();
890 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
891 if (!okay) {
892 ts.Self()->ThrowOutOfMemoryError(caller);
893 }
894 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 }
896
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
898 ScopedJniThreadState ts(env);
899 if (obj == NULL) {
900 return NULL;
901 }
902
Elliott Hughes75770752011-08-24 17:52:38 -0700903 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 IndirectReferenceTable& globals = vm->globals;
905 MutexLock mu(vm->globals_lock);
906 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
907 return reinterpret_cast<jobject>(ref);
908 }
909
910 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
911 ScopedJniThreadState ts(env);
912 if (obj == NULL) {
913 return;
914 }
915
Elliott Hughes75770752011-08-24 17:52:38 -0700916 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 IndirectReferenceTable& globals = vm->globals;
918 MutexLock mu(vm->globals_lock);
919
920 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
921 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
922 << "failed to find entry";
923 }
924 }
925
926 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
927 ScopedJniThreadState ts(env);
928 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
929 }
930
931 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
932 ScopedJniThreadState ts(env);
933 if (obj == NULL) {
934 return;
935 }
936
Elliott Hughes75770752011-08-24 17:52:38 -0700937 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 IndirectReferenceTable& weak_globals = vm->weak_globals;
939 MutexLock mu(vm->weak_globals_lock);
940
941 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
942 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
943 << "failed to find entry";
944 }
945 }
946
947 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
948 ScopedJniThreadState ts(env);
949 if (obj == NULL) {
950 return NULL;
951 }
952
953 IndirectReferenceTable& locals = ts.Env()->locals;
954
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700955 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
957 return reinterpret_cast<jobject>(ref);
958 }
959
960 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
961 ScopedJniThreadState ts(env);
962 if (obj == NULL) {
963 return;
964 }
965
966 IndirectReferenceTable& locals = ts.Env()->locals;
967
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700968 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 if (!locals.Remove(cookie, obj)) {
970 // Attempting to delete a local reference that is not in the
971 // topmost local reference frame is a no-op. DeleteLocalRef returns
972 // void and doesn't throw any exceptions, but we should probably
973 // complain about it so the user will notice that things aren't
974 // going quite the way they expect.
975 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
976 << "failed to find entry";
977 }
978 }
979
980 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
981 ScopedJniThreadState ts(env);
982 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
983 ? JNI_TRUE : JNI_FALSE;
984 }
985
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700986 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700988 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700989 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700990 return NULL;
991 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700992 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 ScopedJniThreadState ts(env);
997 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_start(args, mid);
999 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 va_end(args);
1001 return result;
1002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001006 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001007 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001008 return NULL;
1009 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001010 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001011 if (result == NULL) {
1012 return NULL;
1013 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001014 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001016 if (!ts.Self()->IsExceptionPending()) {
1017 return local_result;
1018 } else {
1019 return NULL;
1020 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001025 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001026 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001027 return NULL;
1028 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001029 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001030 if (result == NULL) {
1031 return NULL;
1032 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001033 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001035 if (!ts.Self()->IsExceptionPending()) {
1036 return local_result;
1037 } else {
1038 return NULL;
1039 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1043 ScopedJniThreadState ts(env);
1044 return FindMethodID(ts, c, name, sig, false);
1045 }
1046
1047 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1048 ScopedJniThreadState ts(env);
1049 return FindMethodID(ts, c, name, sig, true);
1050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001056 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001058 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001063 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001064 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jobject CallObjectMethodA(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 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001070 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_list ap;
1076 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_end(ap);
1079 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001084 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_list ap;
1095 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
1098 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001103 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_end(ap);
1117 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001122 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001127 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_list ap;
1133 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001134 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_end(ap);
1136 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_list ap;
1152 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001153 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_end(ap);
1155 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001160 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001165 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_list ap;
1171 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001172 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_end(ap);
1174 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001184 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_list ap;
1190 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001191 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_end(ap);
1193 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001198 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001203 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_list ap;
1209 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_end(ap);
1212 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001217 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001222 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_list ap;
1228 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001229 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001240 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001243 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
1245 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001247 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001248 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_end(ap);
1250 return local_result;
1251 }
1252
1253 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001254 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001256 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001257 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001261 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001263 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001264 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
1270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001272 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
1274 return result.z;
1275 }
1276
1277 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001280 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001284 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001289 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
1291 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001293 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_end(ap);
1295 return result.b;
1296 }
1297
1298 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001301 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
1304 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001305 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001307 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
1312 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001314 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 va_end(ap);
1316 return result.c;
1317 }
1318
1319 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001328 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
1333 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001335 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_end(ap);
1337 return result.s;
1338 }
1339
1340 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001343 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001347 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
1354 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_end(ap);
1358 return result.i;
1359 }
1360
1361 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001362 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001368 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001373 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
1375 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 va_end(ap);
1379 return result.j;
1380 }
1381
1382 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001385 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
1388 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001389 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
1396 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 va_end(ap);
1400 return result.f;
1401 }
1402
1403 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001406 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
1409 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
1417 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001418 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001419 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 va_end(ap);
1421 return result.d;
1422 }
1423
1424 static jdouble CallNonvirtualDoubleMethodV(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 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
1430 static jdouble CallNonvirtualDoubleMethodA(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 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
1438 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001439 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001440 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 va_end(ap);
1442 }
1443
1444 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001445 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001447 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
1450 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001453 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001456 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 ScopedJniThreadState ts(env);
1458 return FindFieldID(ts, c, name, sig, false);
1459 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001460
1461
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001462 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
1464 return FindFieldID(ts, c, name, sig, true);
1465 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001470 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001471 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001476 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001477 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 Object* o = Decode<Object*>(ts, java_object);
1483 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001484 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001491 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495#define GET_PRIMITIVE_FIELD(fn, instance) \
1496 ScopedJniThreadState ts(env); \
1497 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001498 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 return f->fn(o)
1500
1501#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1502 ScopedJniThreadState ts(env); \
1503 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001504 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 f->fn(o, value)
1506
1507 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1524 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1528 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1532 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1536 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001551 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001555 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 GET_PRIMITIVE_FIELD(GetLong, NULL);
1561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1565 }
1566
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001567 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1569 }
1570
1571 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1572 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1573 }
1574
1575 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1576 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1577 }
1578
1579 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1580 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1581 }
1582
1583 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1584 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1585 }
1586
1587 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1588 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1589 }
1590
1591 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1592 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1593 }
1594
1595 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1596 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1597 }
1598
1599 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1600 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1601 }
1602
1603 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1604 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1605 }
1606
1607 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1608 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1609 }
1610
1611 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1612 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1613 }
1614
1615 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1616 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1617 }
1618
1619 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1620 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1621 }
1622
1623 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1624 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1625 }
1626
1627 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1628 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1629 }
1630
1631 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1632 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
1637 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001639 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001640 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
1642 return local_result;
1643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001648 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jobject CallStaticObjectMethodA(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 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001654 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
1659 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_end(ap);
1663 return result.z;
1664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001671 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
1678 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_end(ap);
1682 return result.b;
1683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001690 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
1697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 va_end(ap);
1701 return result.c;
1702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001706 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
1716 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_end(ap);
1720 return result.s;
1721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001725 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
1735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_end(ap);
1739 return result.i;
1740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001744 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001747 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
1754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
1758 return result.j;
1759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001763 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
1773 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001775 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 va_end(ap);
1777 return result.f;
1778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001782 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
1792 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001793 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001794 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_end(ap);
1796 return result.d;
1797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001801 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001804 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001806 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
1811 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001812 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001813 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 va_end(ap);
1815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001819 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001824 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes814e4032011-08-23 12:07:56 -07001827 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001829 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001830 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
1833 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1834 ScopedJniThreadState ts(env);
1835 if (utf == NULL) {
1836 return NULL;
1837 }
1838 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001839 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes814e4032011-08-23 12:07:56 -07001842 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1843 ScopedJniThreadState ts(env);
1844 return Decode<String*>(ts, java_string)->GetLength();
1845 }
1846
1847 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1848 ScopedJniThreadState ts(env);
1849 return Decode<String*>(ts, java_string)->GetUtfLength();
1850 }
1851
Elliott Hughesb465ab02011-08-24 11:21:21 -07001852 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 String* s = Decode<String*>(ts, java_string);
1855 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1856 ThrowSIOOBE(ts, start, length, s->GetLength());
1857 } else {
1858 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1859 memcpy(buf, chars + start, length * sizeof(jchar));
1860 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 }
1862
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001865 String* s = Decode<String*>(ts, java_string);
1866 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1867 ThrowSIOOBE(ts, start, length, s->GetLength());
1868 } else {
1869 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1870 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1871 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 String* s = Decode<String*>(ts, java_string);
1877 const CharArray* chars = s->GetCharArray();
1878 PinPrimitiveArray(ts, chars);
1879 if (is_copy != NULL) {
1880 *is_copy = JNI_FALSE;
1881 }
1882 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001883 }
1884
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001885 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001886 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001887 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001892 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001897 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001901 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001902 if (java_string == NULL) {
1903 return NULL;
1904 }
1905 if (is_copy != NULL) {
1906 *is_copy = JNI_TRUE;
1907 }
1908 String* s = Decode<String*>(ts, java_string);
1909 size_t byte_count = s->GetUtfLength();
1910 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001911 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001912 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1913 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1914 bytes[byte_count] = '\0';
1915 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 }
1917
Elliott Hughes75770752011-08-24 17:52:38 -07001918 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001919 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001920 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001921 }
1922
Elliott Hughesbd935992011-08-22 11:59:34 -07001923 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001925 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001926 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001927 Array* array = obj->AsArray();
1928 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes814e4032011-08-23 12:07:56 -07001931 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001933 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001934 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
1937 static void SetObjectArrayElement(JNIEnv* env,
1938 jobjectArray java_array, jsize index, jobject java_value) {
1939 ScopedJniThreadState ts(env);
1940 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1941 Object* value = Decode<Object*>(ts, java_value);
1942 array->Set(index, value);
1943 }
1944
1945 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1948 }
1949
1950 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1951 ScopedJniThreadState ts(env);
1952 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1953 }
1954
1955 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1958 }
1959
1960 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1961 ScopedJniThreadState ts(env);
1962 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1963 }
1964
1965 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1966 ScopedJniThreadState ts(env);
1967 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1968 }
1969
1970 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1971 ScopedJniThreadState ts(env);
1972 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1973 }
1974
1975 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1976 ScopedJniThreadState ts(env);
1977 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1978 }
1979
1980 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1981 ScopedJniThreadState ts(env);
1982 CHECK_GE(length, 0); // TODO: ReportJniError
1983
1984 // Compute the array class corresponding to the given element class.
1985 Class* element_class = Decode<Class*>(ts, element_jclass);
1986 std::string descriptor;
1987 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001988 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001989
1990 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001991 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1992 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 return NULL;
1994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 // Allocate and initialize if necessary.
1997 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001999 if (initial_element != NULL) {
2000 Object* initial_object = Decode<Object*>(ts, initial_element);
2001 for (jsize i = 0; i < length; ++i) {
2002 result->Set(i, initial_object);
2003 }
2004 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002005 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
2008 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2009 ScopedJniThreadState ts(env);
2010 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2011 }
2012
Ian Rogersa15e67d2012-02-28 13:51:55 -08002013 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002014 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002015 Array* array = Decode<Array*>(ts, java_array);
2016 PinPrimitiveArray(ts, array);
2017 if (is_copy != NULL) {
2018 *is_copy = JNI_FALSE;
2019 }
2020 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002021 }
2022
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002023 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray 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<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray 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<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray 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<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002068 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, 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 Hughes1bac54f2012-03-16 12:48:31 -07002088 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002093 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002098 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002100 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002103 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002105 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes5174fe62011-08-23 15:12:35 -07002188 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002190 Class* c = Decode<Class*>(ts, java_class);
2191
Elliott Hughes5174fe62011-08-23 15:12:35 -07002192 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 const char* name = methods[i].name;
2194 const char* sig = methods[i].signature;
2195
2196 if (*sig == '!') {
2197 // TODO: fast jni. it's too noisy to log all these.
2198 ++sig;
2199 }
2200
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201 Method* m = c->FindDirectMethod(name, sig);
2202 if (m == NULL) {
2203 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002206 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002207 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002209 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002210 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002211 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 return JNI_ERR;
2213 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002215 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002216
Ian Rogers60db5ab2012-02-20 17:02:00 -08002217 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219 return JNI_OK;
2220 }
2221
Elliott Hughes5174fe62011-08-23 15:12:35 -07002222 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 Class* c = Decode<Class*>(ts, java_class);
2225
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002226 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002227
2228 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2229 Method* m = c->GetDirectMethod(i);
2230 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002231 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002232 }
2233 }
2234 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2235 Method* m = c->GetVirtualMethod(i);
2236 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002237 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002238 }
2239 }
2240
2241 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes72025e52011-08-23 17:50:30 -07002244 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002246 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002247 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughes72025e52011-08-23 17:50:30 -07002250 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002252 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002253 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
2256 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2257 ScopedJniThreadState ts(env);
2258 Runtime* runtime = Runtime::Current();
2259 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002260 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 } else {
2262 *vm = NULL;
2263 }
2264 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2265 }
2266
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2268 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002269
2270 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002271 CHECK(address != NULL); // TODO: ReportJniError
2272 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273
2274 jclass buffer_class = GetDirectByteBufferClass(env);
2275 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2276 if (mid == NULL) {
2277 return NULL;
2278 }
2279
2280 // At the moment, the Java side is limited to 32 bits.
2281 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2282 CHECK_LE(capacity, 0xffffffff);
2283 jint address_arg = reinterpret_cast<jint>(address);
2284 jint capacity_arg = static_cast<jint>(capacity);
2285
2286 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2287 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2293 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2299 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304
Elliott Hughes75770752011-08-24 17:52:38 -07002305 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306
2307 // Do we definitely know what kind of reference this is?
2308 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2309 IndirectRefKind kind = GetIndirectRefKind(ref);
2310 switch (kind) {
2311 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002312 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2313 return JNILocalRefType;
2314 }
2315 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316 case kGlobal:
2317 return JNIGlobalRefType;
2318 case kWeakGlobal:
2319 return JNIWeakGlobalRefType;
2320 case kSirtOrInvalid:
2321 // Is it in a stack IRT?
2322 if (ts.Self()->SirtContains(java_object)) {
2323 return JNILocalRefType;
2324 }
2325
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002326 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002327 return JNIInvalidRefType;
2328 }
2329
Elliott Hughesb465ab02011-08-24 11:21:21 -07002330 // If we're handing out direct pointers, check whether it's a direct pointer
2331 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002332 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002333 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002334 return JNILocalRefType;
2335 }
2336 }
2337
2338 return JNIInvalidRefType;
2339 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002340 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2341 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002342 }
2343};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002344
Elliott Hughes88c5c352012-03-15 18:49:48 -07002345const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002346 NULL, // reserved0.
2347 NULL, // reserved1.
2348 NULL, // reserved2.
2349 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 JNI::GetVersion,
2351 JNI::DefineClass,
2352 JNI::FindClass,
2353 JNI::FromReflectedMethod,
2354 JNI::FromReflectedField,
2355 JNI::ToReflectedMethod,
2356 JNI::GetSuperclass,
2357 JNI::IsAssignableFrom,
2358 JNI::ToReflectedField,
2359 JNI::Throw,
2360 JNI::ThrowNew,
2361 JNI::ExceptionOccurred,
2362 JNI::ExceptionDescribe,
2363 JNI::ExceptionClear,
2364 JNI::FatalError,
2365 JNI::PushLocalFrame,
2366 JNI::PopLocalFrame,
2367 JNI::NewGlobalRef,
2368 JNI::DeleteGlobalRef,
2369 JNI::DeleteLocalRef,
2370 JNI::IsSameObject,
2371 JNI::NewLocalRef,
2372 JNI::EnsureLocalCapacity,
2373 JNI::AllocObject,
2374 JNI::NewObject,
2375 JNI::NewObjectV,
2376 JNI::NewObjectA,
2377 JNI::GetObjectClass,
2378 JNI::IsInstanceOf,
2379 JNI::GetMethodID,
2380 JNI::CallObjectMethod,
2381 JNI::CallObjectMethodV,
2382 JNI::CallObjectMethodA,
2383 JNI::CallBooleanMethod,
2384 JNI::CallBooleanMethodV,
2385 JNI::CallBooleanMethodA,
2386 JNI::CallByteMethod,
2387 JNI::CallByteMethodV,
2388 JNI::CallByteMethodA,
2389 JNI::CallCharMethod,
2390 JNI::CallCharMethodV,
2391 JNI::CallCharMethodA,
2392 JNI::CallShortMethod,
2393 JNI::CallShortMethodV,
2394 JNI::CallShortMethodA,
2395 JNI::CallIntMethod,
2396 JNI::CallIntMethodV,
2397 JNI::CallIntMethodA,
2398 JNI::CallLongMethod,
2399 JNI::CallLongMethodV,
2400 JNI::CallLongMethodA,
2401 JNI::CallFloatMethod,
2402 JNI::CallFloatMethodV,
2403 JNI::CallFloatMethodA,
2404 JNI::CallDoubleMethod,
2405 JNI::CallDoubleMethodV,
2406 JNI::CallDoubleMethodA,
2407 JNI::CallVoidMethod,
2408 JNI::CallVoidMethodV,
2409 JNI::CallVoidMethodA,
2410 JNI::CallNonvirtualObjectMethod,
2411 JNI::CallNonvirtualObjectMethodV,
2412 JNI::CallNonvirtualObjectMethodA,
2413 JNI::CallNonvirtualBooleanMethod,
2414 JNI::CallNonvirtualBooleanMethodV,
2415 JNI::CallNonvirtualBooleanMethodA,
2416 JNI::CallNonvirtualByteMethod,
2417 JNI::CallNonvirtualByteMethodV,
2418 JNI::CallNonvirtualByteMethodA,
2419 JNI::CallNonvirtualCharMethod,
2420 JNI::CallNonvirtualCharMethodV,
2421 JNI::CallNonvirtualCharMethodA,
2422 JNI::CallNonvirtualShortMethod,
2423 JNI::CallNonvirtualShortMethodV,
2424 JNI::CallNonvirtualShortMethodA,
2425 JNI::CallNonvirtualIntMethod,
2426 JNI::CallNonvirtualIntMethodV,
2427 JNI::CallNonvirtualIntMethodA,
2428 JNI::CallNonvirtualLongMethod,
2429 JNI::CallNonvirtualLongMethodV,
2430 JNI::CallNonvirtualLongMethodA,
2431 JNI::CallNonvirtualFloatMethod,
2432 JNI::CallNonvirtualFloatMethodV,
2433 JNI::CallNonvirtualFloatMethodA,
2434 JNI::CallNonvirtualDoubleMethod,
2435 JNI::CallNonvirtualDoubleMethodV,
2436 JNI::CallNonvirtualDoubleMethodA,
2437 JNI::CallNonvirtualVoidMethod,
2438 JNI::CallNonvirtualVoidMethodV,
2439 JNI::CallNonvirtualVoidMethodA,
2440 JNI::GetFieldID,
2441 JNI::GetObjectField,
2442 JNI::GetBooleanField,
2443 JNI::GetByteField,
2444 JNI::GetCharField,
2445 JNI::GetShortField,
2446 JNI::GetIntField,
2447 JNI::GetLongField,
2448 JNI::GetFloatField,
2449 JNI::GetDoubleField,
2450 JNI::SetObjectField,
2451 JNI::SetBooleanField,
2452 JNI::SetByteField,
2453 JNI::SetCharField,
2454 JNI::SetShortField,
2455 JNI::SetIntField,
2456 JNI::SetLongField,
2457 JNI::SetFloatField,
2458 JNI::SetDoubleField,
2459 JNI::GetStaticMethodID,
2460 JNI::CallStaticObjectMethod,
2461 JNI::CallStaticObjectMethodV,
2462 JNI::CallStaticObjectMethodA,
2463 JNI::CallStaticBooleanMethod,
2464 JNI::CallStaticBooleanMethodV,
2465 JNI::CallStaticBooleanMethodA,
2466 JNI::CallStaticByteMethod,
2467 JNI::CallStaticByteMethodV,
2468 JNI::CallStaticByteMethodA,
2469 JNI::CallStaticCharMethod,
2470 JNI::CallStaticCharMethodV,
2471 JNI::CallStaticCharMethodA,
2472 JNI::CallStaticShortMethod,
2473 JNI::CallStaticShortMethodV,
2474 JNI::CallStaticShortMethodA,
2475 JNI::CallStaticIntMethod,
2476 JNI::CallStaticIntMethodV,
2477 JNI::CallStaticIntMethodA,
2478 JNI::CallStaticLongMethod,
2479 JNI::CallStaticLongMethodV,
2480 JNI::CallStaticLongMethodA,
2481 JNI::CallStaticFloatMethod,
2482 JNI::CallStaticFloatMethodV,
2483 JNI::CallStaticFloatMethodA,
2484 JNI::CallStaticDoubleMethod,
2485 JNI::CallStaticDoubleMethodV,
2486 JNI::CallStaticDoubleMethodA,
2487 JNI::CallStaticVoidMethod,
2488 JNI::CallStaticVoidMethodV,
2489 JNI::CallStaticVoidMethodA,
2490 JNI::GetStaticFieldID,
2491 JNI::GetStaticObjectField,
2492 JNI::GetStaticBooleanField,
2493 JNI::GetStaticByteField,
2494 JNI::GetStaticCharField,
2495 JNI::GetStaticShortField,
2496 JNI::GetStaticIntField,
2497 JNI::GetStaticLongField,
2498 JNI::GetStaticFloatField,
2499 JNI::GetStaticDoubleField,
2500 JNI::SetStaticObjectField,
2501 JNI::SetStaticBooleanField,
2502 JNI::SetStaticByteField,
2503 JNI::SetStaticCharField,
2504 JNI::SetStaticShortField,
2505 JNI::SetStaticIntField,
2506 JNI::SetStaticLongField,
2507 JNI::SetStaticFloatField,
2508 JNI::SetStaticDoubleField,
2509 JNI::NewString,
2510 JNI::GetStringLength,
2511 JNI::GetStringChars,
2512 JNI::ReleaseStringChars,
2513 JNI::NewStringUTF,
2514 JNI::GetStringUTFLength,
2515 JNI::GetStringUTFChars,
2516 JNI::ReleaseStringUTFChars,
2517 JNI::GetArrayLength,
2518 JNI::NewObjectArray,
2519 JNI::GetObjectArrayElement,
2520 JNI::SetObjectArrayElement,
2521 JNI::NewBooleanArray,
2522 JNI::NewByteArray,
2523 JNI::NewCharArray,
2524 JNI::NewShortArray,
2525 JNI::NewIntArray,
2526 JNI::NewLongArray,
2527 JNI::NewFloatArray,
2528 JNI::NewDoubleArray,
2529 JNI::GetBooleanArrayElements,
2530 JNI::GetByteArrayElements,
2531 JNI::GetCharArrayElements,
2532 JNI::GetShortArrayElements,
2533 JNI::GetIntArrayElements,
2534 JNI::GetLongArrayElements,
2535 JNI::GetFloatArrayElements,
2536 JNI::GetDoubleArrayElements,
2537 JNI::ReleaseBooleanArrayElements,
2538 JNI::ReleaseByteArrayElements,
2539 JNI::ReleaseCharArrayElements,
2540 JNI::ReleaseShortArrayElements,
2541 JNI::ReleaseIntArrayElements,
2542 JNI::ReleaseLongArrayElements,
2543 JNI::ReleaseFloatArrayElements,
2544 JNI::ReleaseDoubleArrayElements,
2545 JNI::GetBooleanArrayRegion,
2546 JNI::GetByteArrayRegion,
2547 JNI::GetCharArrayRegion,
2548 JNI::GetShortArrayRegion,
2549 JNI::GetIntArrayRegion,
2550 JNI::GetLongArrayRegion,
2551 JNI::GetFloatArrayRegion,
2552 JNI::GetDoubleArrayRegion,
2553 JNI::SetBooleanArrayRegion,
2554 JNI::SetByteArrayRegion,
2555 JNI::SetCharArrayRegion,
2556 JNI::SetShortArrayRegion,
2557 JNI::SetIntArrayRegion,
2558 JNI::SetLongArrayRegion,
2559 JNI::SetFloatArrayRegion,
2560 JNI::SetDoubleArrayRegion,
2561 JNI::RegisterNatives,
2562 JNI::UnregisterNatives,
2563 JNI::MonitorEnter,
2564 JNI::MonitorExit,
2565 JNI::GetJavaVM,
2566 JNI::GetStringRegion,
2567 JNI::GetStringUTFRegion,
2568 JNI::GetPrimitiveArrayCritical,
2569 JNI::ReleasePrimitiveArrayCritical,
2570 JNI::GetStringCritical,
2571 JNI::ReleaseStringCritical,
2572 JNI::NewWeakGlobalRef,
2573 JNI::DeleteWeakGlobalRef,
2574 JNI::ExceptionCheck,
2575 JNI::NewDirectByteBuffer,
2576 JNI::GetDirectBufferAddress,
2577 JNI::GetDirectBufferCapacity,
2578 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002579};
2580
Elliott Hughes75770752011-08-24 17:52:38 -07002581JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002582 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002583 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002584 local_ref_cookie(IRT_FIRST_SEGMENT),
2585 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002586 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002587 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002588 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002589 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002590 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002591 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002592 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002593 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2594 // errors will ensue.
2595 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2596 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002597}
2598
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002599JNIEnvExt::~JNIEnvExt() {
2600}
2601
Elliott Hughes88c5c352012-03-15 18:49:48 -07002602void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2603 check_jni = enabled;
2604 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002605}
2606
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002607void JNIEnvExt::DumpReferenceTables() {
2608 locals.Dump();
2609 monitors.Dump();
2610}
2611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002612void JNIEnvExt::PushFrame(int /*capacity*/) {
2613 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002614 stacked_local_ref_cookies.push_back(local_ref_cookie);
2615 local_ref_cookie = locals.GetSegmentState();
2616}
2617
2618void JNIEnvExt::PopFrame() {
2619 locals.SetSegmentState(local_ref_cookie);
2620 local_ref_cookie = stacked_local_ref_cookies.back();
2621 stacked_local_ref_cookies.pop_back();
2622}
2623
Carl Shapiroea4dca82011-08-01 13:45:38 -07002624// JNI Invocation interface.
2625
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2627 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2628 if (args->version < JNI_VERSION_1_2) {
2629 return JNI_EVERSION;
2630 }
2631 Runtime::Options options;
2632 for (int i = 0; i < args->nOptions; ++i) {
2633 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002634 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002635 }
2636 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002637 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 if (runtime == NULL) {
2639 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002641 runtime->Start();
2642 *p_env = Thread::Current()->GetJniEnv();
2643 *p_vm = runtime->GetJavaVM();
2644 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645}
2646
Elliott Hughesf2682d52011-08-15 16:37:04 -07002647extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002648 Runtime* runtime = Runtime::Current();
2649 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002650 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002652 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002653 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654 }
2655 return JNI_OK;
2656}
2657
2658// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002659extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 return JNI_ERR;
2661}
2662
Elliott Hughescdf53122011-08-19 15:46:09 -07002663class JII {
2664 public:
2665 static jint DestroyJavaVM(JavaVM* vm) {
2666 if (vm == NULL) {
2667 return JNI_ERR;
2668 } else {
2669 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2670 delete raw_vm->runtime;
2671 return JNI_OK;
2672 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002674
Elliott Hughescdf53122011-08-19 15:46:09 -07002675 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002676 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002677 }
2678
2679 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002680 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002681 }
2682
2683 static jint DetachCurrentThread(JavaVM* vm) {
2684 if (vm == NULL) {
2685 return JNI_ERR;
2686 } else {
2687 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2688 Runtime* runtime = raw_vm->runtime;
2689 runtime->DetachCurrentThread();
2690 return JNI_OK;
2691 }
2692 }
2693
2694 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2695 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2696 return JNI_EVERSION;
2697 }
2698 if (vm == NULL || env == NULL) {
2699 return JNI_ERR;
2700 }
2701 Thread* thread = Thread::Current();
2702 if (thread == NULL) {
2703 *env = NULL;
2704 return JNI_EDETACHED;
2705 }
2706 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002707 return JNI_OK;
2708 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002709};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710
Elliott Hughes88c5c352012-03-15 18:49:48 -07002711const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002712 NULL, // reserved0
2713 NULL, // reserved1
2714 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002715 JII::DestroyJavaVM,
2716 JII::AttachCurrentThread,
2717 JII::DetachCurrentThread,
2718 JII::GetEnv,
2719 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002720};
2721
Elliott Hughesa0957642011-09-02 14:27:33 -07002722JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002723 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002724 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002725 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002726 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002727 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002728 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002729 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002730 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002731 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002732 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002733 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002734 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002735 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002736 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002737 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002738 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002739 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002740 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002741}
2742
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002743JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002744 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002745}
2746
Elliott Hughes88c5c352012-03-15 18:49:48 -07002747void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2748 check_jni = enabled;
2749 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002750}
2751
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002752void JavaVMExt::DumpReferenceTables() {
2753 {
2754 MutexLock mu(globals_lock);
2755 globals.Dump();
2756 }
2757 {
2758 MutexLock mu(weak_globals_lock);
2759 weak_globals.Dump();
2760 }
2761 {
2762 MutexLock mu(pins_lock);
2763 pin_table.Dump();
2764 }
2765}
2766
Elliott Hughes75770752011-08-24 17:52:38 -07002767bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2768 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002769
2770 // See if we've already loaded this library. If we have, and the class loader
2771 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002772 // TODO: for better results we should canonicalize the pathname (or even compare
2773 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 SharedLibrary* library;
2775 {
2776 // TODO: move the locking (and more of this logic) into Libraries.
2777 MutexLock mu(libraries_lock);
2778 library = libraries->Get(path);
2779 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002780 if (library != NULL) {
2781 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002782 // The library will be associated with class_loader. The JNI
2783 // spec says we can't load the same library into more than one
2784 // class loader.
2785 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2786 "ClassLoader %p; can't open in ClassLoader %p",
2787 path.c_str(), library->GetClassLoader(), class_loader);
2788 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002789 return false;
2790 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002791 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2792 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002793 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002794 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2795 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 return false;
2797 }
2798 return true;
2799 }
2800
2801 // Open the shared library. Because we're using a full path, the system
2802 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2803 // resolve this library's dependencies though.)
2804
2805 // Failures here are expected when java.library.path has several entries
2806 // and we have to hunt for the lib.
2807
2808 // The current version of the dynamic linker prints detailed information
2809 // about dlopen() failures. Some things to check if the message is
2810 // cryptic:
2811 // - make sure the library exists on the device
2812 // - verify that the right path is being opened (the debug log message
2813 // above can help with that)
2814 // - check to see if the library is valid (e.g. not zero bytes long)
2815 // - check config/prelink-linux-arm.map to ensure that the library
2816 // is listed and is not being overrun by the previous entry (if
2817 // loading suddenly stops working on a prelinked library, this is
2818 // a good one to check)
2819 // - write a trivial app that calls sleep() then dlopen(), attach
2820 // to it with "strace -p <pid>" while it sleeps, and watch for
2821 // attempts to open nonexistent dependent shared libs
2822
2823 // TODO: automate some of these checks!
2824
2825 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002826 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002827 // the GC to ignore us.
2828 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002829 void* handle = NULL;
2830 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002831 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002832 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002833 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002834
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002835 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002836
2837 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002838 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002839 return false;
2840 }
2841
2842 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002843 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002844 // TODO: move the locking (and more of this logic) into Libraries.
2845 MutexLock mu(libraries_lock);
2846 library = libraries->Get(path);
2847 if (library != NULL) {
2848 LOG(INFO) << "WOW: we lost a race to add shared library: "
2849 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002850 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002851 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002852 library = new SharedLibrary(path, handle, class_loader);
2853 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002854 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002855
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002856 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002857
2858 bool result = true;
2859 void* sym = dlsym(handle, "JNI_OnLoad");
2860 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002861 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002862 } else {
2863 // Call JNI_OnLoad. We have to override the current class
2864 // loader, which will always be "null" since the stuff at the
2865 // top of the stack is around Runtime.loadLibrary(). (See
2866 // the comments in the JNI FindClass function.)
2867 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2868 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002869 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002870 self->SetClassLoaderOverride(class_loader);
2871
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002872 int version = 0;
2873 {
2874 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002875 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002876 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002878
Brian Carlstromaded5f72011-10-07 17:15:04 -07002879 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002880
2881 if (version != JNI_VERSION_1_2 &&
2882 version != JNI_VERSION_1_4 &&
2883 version != JNI_VERSION_1_6) {
2884 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2885 << "bad version: " << version;
2886 // It's unwise to call dlclose() here, but we can mark it
2887 // as bad and ensure that future load attempts will fail.
2888 // We don't know how far JNI_OnLoad got, so there could
2889 // be some partially-initialized stuff accessible through
2890 // newly-registered native method calls. We could try to
2891 // unregister them, but that doesn't seem worthwhile.
2892 result = false;
2893 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002894 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2895 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002896 }
2897 }
2898
2899 library->SetResult(result);
2900 return result;
2901}
2902
2903void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2904 CHECK(m->IsNative());
2905
2906 Class* c = m->GetDeclaringClass();
2907
2908 // If this is a static method, it could be called before the class
2909 // has been initialized.
2910 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002911 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002912 return NULL;
2913 }
2914 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002915 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002916 }
2917
Brian Carlstrom16192862011-09-12 17:50:06 -07002918 std::string detail;
2919 void* native_method;
2920 {
2921 MutexLock mu(libraries_lock);
2922 native_method = libraries->FindNativeMethod(m, detail);
2923 }
2924 // throwing can cause libraries_lock to be reacquired
2925 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002926 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002927 }
2928 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002929}
2930
Elliott Hughes410c0c82011-09-01 17:58:25 -07002931void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2932 {
2933 MutexLock mu(globals_lock);
2934 globals.VisitRoots(visitor, arg);
2935 }
2936 {
2937 MutexLock mu(pins_lock);
2938 pin_table.VisitRoots(visitor, arg);
2939 }
2940 // The weak_globals table is visited by the GC itself (because it mutates the table).
2941}
2942
Elliott Hughes844f9a02012-01-24 20:19:58 -08002943jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2944 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2945 if (c.get() == NULL) {
2946 return NULL;
2947 }
2948 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2949}
2950
Ian Rogersdf20fe02011-07-20 20:34:16 -07002951} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002952
2953std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2954 switch (rhs) {
2955 case JNIInvalidRefType:
2956 os << "JNIInvalidRefType";
2957 return os;
2958 case JNILocalRefType:
2959 os << "JNILocalRefType";
2960 return os;
2961 case JNIGlobalRefType:
2962 os << "JNIGlobalRefType";
2963 return os;
2964 case JNIWeakGlobalRefType:
2965 os << "JNIWeakGlobalRefType";
2966 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002967 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002968 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002969 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002970 }
2971}