blob: 7d273ac23a21b4d4ab3780b7a76b856a2f7734b3 [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 /*
547 * Check the result of an earlier call to JNI_OnLoad on this library. If
548 * the call has not yet finished in another thread, wait for it.
549 */
550 bool CheckOnLoadResult(JavaVMExt* vm) {
551 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
1243 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
1246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001248 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001249 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
1251 return local_result;
1252 }
1253
1254 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001257 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001258 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
1261 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001264 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001265 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001273 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return result.z;
1276 }
1277
1278 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001287 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001295 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.b;
1298 }
1299
1300 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001309 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
1315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001317 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 return result.c;
1320 }
1321
1322 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001325 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001331 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
1334 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
1337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001339 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
1341 return result.s;
1342 }
1343
1344 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001353 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
1358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001360 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_end(ap);
1362 return result.i;
1363 }
1364
1365 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001368 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001374 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001378 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
1380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001382 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 va_end(ap);
1384 return result.j;
1385 }
1386
1387 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001390 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
1393 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001394 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001396 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
1399 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001400 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
1402 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001404 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 va_end(ap);
1406 return result.f;
1407 }
1408
1409 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001418 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
1421 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001422 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 ScopedJniThreadState ts(env);
1424 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001425 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001426 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 va_end(ap);
1428 return result.d;
1429 }
1430
1431 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001432 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001434 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
1437 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001438 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001440 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
1443 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001444 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 ScopedJniThreadState ts(env);
1446 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001447 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001448 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 va_end(ap);
1450 }
1451
1452 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001453 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001455 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
1458 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001459 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001461 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001464 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 ScopedJniThreadState ts(env);
1466 return FindFieldID(ts, c, name, sig, false);
1467 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001468
1469
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001470 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 ScopedJniThreadState ts(env);
1472 return FindFieldID(ts, c, name, sig, true);
1473 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001478 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001479 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001484 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001485 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static void SetObjectField(JNIEnv* env, jobject java_object, 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* o = Decode<Object*>(ts, java_object);
1491 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001492 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001499 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503#define GET_PRIMITIVE_FIELD(fn, instance) \
1504 ScopedJniThreadState ts(env); \
1505 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001506 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 return f->fn(o)
1508
1509#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1510 ScopedJniThreadState ts(env); \
1511 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001512 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001513 f->fn(o, value)
1514
1515 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1524 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1528 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1532 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1536 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1540 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1544 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1548 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1552 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1556 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1560 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1564 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetLong, NULL);
1569 }
1570
1571 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1573 }
1574
1575 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1577 }
1578
1579 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1580 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1581 }
1582
1583 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1584 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1585 }
1586
1587 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1588 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1589 }
1590
1591 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1592 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1593 }
1594
1595 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1596 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1597 }
1598
1599 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1600 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1601 }
1602
1603 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1604 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1605 }
1606
1607 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1608 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1609 }
1610
1611 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1612 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1613 }
1614
1615 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1616 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1617 }
1618
1619 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1620 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1621 }
1622
1623 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1624 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1625 }
1626
1627 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1628 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1629 }
1630
1631 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1632 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1633 }
1634
1635 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1636 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1637 }
1638
1639 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1640 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
1645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001648 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_end(ap);
1650 return local_result;
1651 }
1652
Elliott Hughes418d20f2011-09-22 14:00:39 -07001653 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001655 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001656 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001662 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes418d20f2011-09-22 14:00:39 -07001665 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
1667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
1671 return result.z;
1672 }
1673
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
1686 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 va_end(ap);
1690 return result.b;
1691 }
1692
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
1705 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_end(ap);
1709 return result.c;
1710 }
1711
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
1724 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_end(ap);
1728 return result.s;
1729 }
1730
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
1743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001745 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
1747 return result.i;
1748 }
1749
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 ScopedJniThreadState ts(env);
1762 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001764 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 va_end(ap);
1766 return result.j;
1767 }
1768
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001776 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 ScopedJniThreadState ts(env);
1781 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001783 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_end(ap);
1785 return result.f;
1786 }
1787
Elliott Hughes418d20f2011-09-22 14:00:39 -07001788 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001795 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
Elliott Hughes72025e52011-08-23 17:50:30 -07001798 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 ScopedJniThreadState ts(env);
1800 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001802 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 va_end(ap);
1804 return result.d;
1805 }
1806
Elliott Hughes418d20f2011-09-22 14:00:39 -07001807 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001809 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes418d20f2011-09-22 14:00:39 -07001812 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001814 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes72025e52011-08-23 17:50:30 -07001817 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 ScopedJniThreadState ts(env);
1819 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001820 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001821 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 va_end(ap);
1823 }
1824
Elliott Hughes418d20f2011-09-22 14:00:39 -07001825 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001827 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001832 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes814e4032011-08-23 12:07:56 -07001835 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001838 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
1841 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1842 ScopedJniThreadState ts(env);
1843 if (utf == NULL) {
1844 return NULL;
1845 }
1846 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001847 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes814e4032011-08-23 12:07:56 -07001850 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1851 ScopedJniThreadState ts(env);
1852 return Decode<String*>(ts, java_string)->GetLength();
1853 }
1854
1855 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1856 ScopedJniThreadState ts(env);
1857 return Decode<String*>(ts, java_string)->GetUtfLength();
1858 }
1859
Elliott Hughesb465ab02011-08-24 11:21:21 -07001860 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001862 String* s = Decode<String*>(ts, java_string);
1863 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1864 ThrowSIOOBE(ts, start, length, s->GetLength());
1865 } else {
1866 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1867 memcpy(buf, chars + start, length * sizeof(jchar));
1868 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001869 }
1870
Elliott Hughesb465ab02011-08-24 11:21:21 -07001871 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001873 String* s = Decode<String*>(ts, java_string);
1874 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1875 ThrowSIOOBE(ts, start, length, s->GetLength());
1876 } else {
1877 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1878 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1879 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001880 }
1881
Elliott Hughes75770752011-08-24 17:52:38 -07001882 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001883 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001884 String* s = Decode<String*>(ts, java_string);
1885 const CharArray* chars = s->GetCharArray();
1886 PinPrimitiveArray(ts, chars);
1887 if (is_copy != NULL) {
1888 *is_copy = JNI_FALSE;
1889 }
1890 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001891 }
1892
Elliott Hughes75770752011-08-24 17:52:38 -07001893 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001894 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001895 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Elliott Hughes75770752011-08-24 17:52:38 -07001898 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001900 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes75770752011-08-24 17:52:38 -07001903 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001905 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes75770752011-08-24 17:52:38 -07001908 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001909 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001910 if (java_string == NULL) {
1911 return NULL;
1912 }
1913 if (is_copy != NULL) {
1914 *is_copy = JNI_TRUE;
1915 }
1916 String* s = Decode<String*>(ts, java_string);
1917 size_t byte_count = s->GetUtfLength();
1918 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001919 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001920 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1921 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1922 bytes[byte_count] = '\0';
1923 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001924 }
1925
Elliott Hughes75770752011-08-24 17:52:38 -07001926 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001927 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001928 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001929 }
1930
Elliott Hughesbd935992011-08-22 11:59:34 -07001931 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001933 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001934 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001935 Array* array = obj->AsArray();
1936 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes814e4032011-08-23 12:07:56 -07001939 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001942 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
1945 static void SetObjectArrayElement(JNIEnv* env,
1946 jobjectArray java_array, jsize index, jobject java_value) {
1947 ScopedJniThreadState ts(env);
1948 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1949 Object* value = Decode<Object*>(ts, java_value);
1950 array->Set(index, value);
1951 }
1952
1953 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1954 ScopedJniThreadState ts(env);
1955 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1956 }
1957
1958 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1959 ScopedJniThreadState ts(env);
1960 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1961 }
1962
1963 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1964 ScopedJniThreadState ts(env);
1965 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1966 }
1967
1968 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1969 ScopedJniThreadState ts(env);
1970 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1971 }
1972
1973 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1974 ScopedJniThreadState ts(env);
1975 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1976 }
1977
1978 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1979 ScopedJniThreadState ts(env);
1980 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1981 }
1982
1983 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1984 ScopedJniThreadState ts(env);
1985 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1986 }
1987
1988 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1989 ScopedJniThreadState ts(env);
1990 CHECK_GE(length, 0); // TODO: ReportJniError
1991
1992 // Compute the array class corresponding to the given element class.
1993 Class* element_class = Decode<Class*>(ts, element_jclass);
1994 std::string descriptor;
1995 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001996 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001997
1998 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001999 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2000 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 return NULL;
2002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 // Allocate and initialize if necessary.
2005 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 if (initial_element != NULL) {
2008 Object* initial_object = Decode<Object*>(ts, initial_element);
2009 for (jsize i = 0; i < length; ++i) {
2010 result->Set(i, initial_object);
2011 }
2012 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002013 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
2016 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2017 ScopedJniThreadState ts(env);
2018 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2019 }
2020
Ian Rogersa15e67d2012-02-28 13:51:55 -08002021 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002022 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002023 Array* array = Decode<Array*>(ts, java_array);
2024 PinPrimitiveArray(ts, array);
2025 if (is_copy != NULL) {
2026 *is_copy = JNI_FALSE;
2027 }
2028 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes75770752011-08-24 17:52:38 -07002051 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes75770752011-08-24 17:52:38 -07002056 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002058 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002063 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes75770752011-08-24 17:52:38 -07002066 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002068 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes75770752011-08-24 17:52:38 -07002071 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002073 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes75770752011-08-24 17:52:38 -07002076 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002078 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes75770752011-08-24 17:52:38 -07002081 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002083 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes75770752011-08-24 17:52:38 -07002086 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002088 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes75770752011-08-24 17:52:38 -07002091 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002093 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes75770752011-08-24 17:52:38 -07002096 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002098 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes75770752011-08-24 17:52:38 -07002101 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002103 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes75770752011-08-24 17:52:38 -07002106 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002108 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes75770752011-08-24 17:52:38 -07002111 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002113 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes814e4032011-08-23 12:07:56 -07002181 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Elliott Hughes814e4032011-08-23 12:07:56 -07002186 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes814e4032011-08-23 12:07:56 -07002191 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198 Class* c = Decode<Class*>(ts, java_class);
2199
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 const char* name = methods[i].name;
2202 const char* sig = methods[i].signature;
2203
2204 if (*sig == '!') {
2205 // TODO: fast jni. it's too noisy to log all these.
2206 ++sig;
2207 }
2208
Elliott Hughes5174fe62011-08-23 15:12:35 -07002209 Method* m = c->FindDirectMethod(name, sig);
2210 if (m == NULL) {
2211 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002214 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002215 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002218 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002219 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 return JNI_ERR;
2221 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002222
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002223 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224
Ian Rogers60db5ab2012-02-20 17:02:00 -08002225 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227 return JNI_OK;
2228 }
2229
Elliott Hughes5174fe62011-08-23 15:12:35 -07002230 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002232 Class* c = Decode<Class*>(ts, java_class);
2233
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002234 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002235
2236 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2237 Method* m = c->GetDirectMethod(i);
2238 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002239 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002240 }
2241 }
2242 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2243 Method* m = c->GetVirtualMethod(i);
2244 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002245 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002246 }
2247 }
2248
2249 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughes72025e52011-08-23 17:50:30 -07002252 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002254 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002255 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Elliott Hughes72025e52011-08-23 17:50:30 -07002258 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002260 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002261 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
2264 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2265 ScopedJniThreadState ts(env);
2266 Runtime* runtime = Runtime::Current();
2267 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002268 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 } else {
2270 *vm = NULL;
2271 }
2272 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2273 }
2274
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2276 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277
2278 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002279 CHECK(address != NULL); // TODO: ReportJniError
2280 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281
2282 jclass buffer_class = GetDirectByteBufferClass(env);
2283 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2284 if (mid == NULL) {
2285 return NULL;
2286 }
2287
2288 // At the moment, the Java side is limited to 32 bits.
2289 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2290 CHECK_LE(capacity, 0xffffffff);
2291 jint address_arg = reinterpret_cast<jint>(address);
2292 jint capacity_arg = static_cast<jint>(capacity);
2293
2294 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2295 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002300 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2301 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2307 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Elliott Hughesb465ab02011-08-24 11:21:21 -07002310 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312
Elliott Hughes75770752011-08-24 17:52:38 -07002313 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002314
2315 // Do we definitely know what kind of reference this is?
2316 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2317 IndirectRefKind kind = GetIndirectRefKind(ref);
2318 switch (kind) {
2319 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002320 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2321 return JNILocalRefType;
2322 }
2323 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002324 case kGlobal:
2325 return JNIGlobalRefType;
2326 case kWeakGlobal:
2327 return JNIWeakGlobalRefType;
2328 case kSirtOrInvalid:
2329 // Is it in a stack IRT?
2330 if (ts.Self()->SirtContains(java_object)) {
2331 return JNILocalRefType;
2332 }
2333
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002334 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002335 return JNIInvalidRefType;
2336 }
2337
Elliott Hughesb465ab02011-08-24 11:21:21 -07002338 // If we're handing out direct pointers, check whether it's a direct pointer
2339 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002340 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002341 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002342 return JNILocalRefType;
2343 }
2344 }
2345
2346 return JNIInvalidRefType;
2347 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002348 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2349 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002352
Elliott Hughes88c5c352012-03-15 18:49:48 -07002353const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002354 NULL, // reserved0.
2355 NULL, // reserved1.
2356 NULL, // reserved2.
2357 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 JNI::GetVersion,
2359 JNI::DefineClass,
2360 JNI::FindClass,
2361 JNI::FromReflectedMethod,
2362 JNI::FromReflectedField,
2363 JNI::ToReflectedMethod,
2364 JNI::GetSuperclass,
2365 JNI::IsAssignableFrom,
2366 JNI::ToReflectedField,
2367 JNI::Throw,
2368 JNI::ThrowNew,
2369 JNI::ExceptionOccurred,
2370 JNI::ExceptionDescribe,
2371 JNI::ExceptionClear,
2372 JNI::FatalError,
2373 JNI::PushLocalFrame,
2374 JNI::PopLocalFrame,
2375 JNI::NewGlobalRef,
2376 JNI::DeleteGlobalRef,
2377 JNI::DeleteLocalRef,
2378 JNI::IsSameObject,
2379 JNI::NewLocalRef,
2380 JNI::EnsureLocalCapacity,
2381 JNI::AllocObject,
2382 JNI::NewObject,
2383 JNI::NewObjectV,
2384 JNI::NewObjectA,
2385 JNI::GetObjectClass,
2386 JNI::IsInstanceOf,
2387 JNI::GetMethodID,
2388 JNI::CallObjectMethod,
2389 JNI::CallObjectMethodV,
2390 JNI::CallObjectMethodA,
2391 JNI::CallBooleanMethod,
2392 JNI::CallBooleanMethodV,
2393 JNI::CallBooleanMethodA,
2394 JNI::CallByteMethod,
2395 JNI::CallByteMethodV,
2396 JNI::CallByteMethodA,
2397 JNI::CallCharMethod,
2398 JNI::CallCharMethodV,
2399 JNI::CallCharMethodA,
2400 JNI::CallShortMethod,
2401 JNI::CallShortMethodV,
2402 JNI::CallShortMethodA,
2403 JNI::CallIntMethod,
2404 JNI::CallIntMethodV,
2405 JNI::CallIntMethodA,
2406 JNI::CallLongMethod,
2407 JNI::CallLongMethodV,
2408 JNI::CallLongMethodA,
2409 JNI::CallFloatMethod,
2410 JNI::CallFloatMethodV,
2411 JNI::CallFloatMethodA,
2412 JNI::CallDoubleMethod,
2413 JNI::CallDoubleMethodV,
2414 JNI::CallDoubleMethodA,
2415 JNI::CallVoidMethod,
2416 JNI::CallVoidMethodV,
2417 JNI::CallVoidMethodA,
2418 JNI::CallNonvirtualObjectMethod,
2419 JNI::CallNonvirtualObjectMethodV,
2420 JNI::CallNonvirtualObjectMethodA,
2421 JNI::CallNonvirtualBooleanMethod,
2422 JNI::CallNonvirtualBooleanMethodV,
2423 JNI::CallNonvirtualBooleanMethodA,
2424 JNI::CallNonvirtualByteMethod,
2425 JNI::CallNonvirtualByteMethodV,
2426 JNI::CallNonvirtualByteMethodA,
2427 JNI::CallNonvirtualCharMethod,
2428 JNI::CallNonvirtualCharMethodV,
2429 JNI::CallNonvirtualCharMethodA,
2430 JNI::CallNonvirtualShortMethod,
2431 JNI::CallNonvirtualShortMethodV,
2432 JNI::CallNonvirtualShortMethodA,
2433 JNI::CallNonvirtualIntMethod,
2434 JNI::CallNonvirtualIntMethodV,
2435 JNI::CallNonvirtualIntMethodA,
2436 JNI::CallNonvirtualLongMethod,
2437 JNI::CallNonvirtualLongMethodV,
2438 JNI::CallNonvirtualLongMethodA,
2439 JNI::CallNonvirtualFloatMethod,
2440 JNI::CallNonvirtualFloatMethodV,
2441 JNI::CallNonvirtualFloatMethodA,
2442 JNI::CallNonvirtualDoubleMethod,
2443 JNI::CallNonvirtualDoubleMethodV,
2444 JNI::CallNonvirtualDoubleMethodA,
2445 JNI::CallNonvirtualVoidMethod,
2446 JNI::CallNonvirtualVoidMethodV,
2447 JNI::CallNonvirtualVoidMethodA,
2448 JNI::GetFieldID,
2449 JNI::GetObjectField,
2450 JNI::GetBooleanField,
2451 JNI::GetByteField,
2452 JNI::GetCharField,
2453 JNI::GetShortField,
2454 JNI::GetIntField,
2455 JNI::GetLongField,
2456 JNI::GetFloatField,
2457 JNI::GetDoubleField,
2458 JNI::SetObjectField,
2459 JNI::SetBooleanField,
2460 JNI::SetByteField,
2461 JNI::SetCharField,
2462 JNI::SetShortField,
2463 JNI::SetIntField,
2464 JNI::SetLongField,
2465 JNI::SetFloatField,
2466 JNI::SetDoubleField,
2467 JNI::GetStaticMethodID,
2468 JNI::CallStaticObjectMethod,
2469 JNI::CallStaticObjectMethodV,
2470 JNI::CallStaticObjectMethodA,
2471 JNI::CallStaticBooleanMethod,
2472 JNI::CallStaticBooleanMethodV,
2473 JNI::CallStaticBooleanMethodA,
2474 JNI::CallStaticByteMethod,
2475 JNI::CallStaticByteMethodV,
2476 JNI::CallStaticByteMethodA,
2477 JNI::CallStaticCharMethod,
2478 JNI::CallStaticCharMethodV,
2479 JNI::CallStaticCharMethodA,
2480 JNI::CallStaticShortMethod,
2481 JNI::CallStaticShortMethodV,
2482 JNI::CallStaticShortMethodA,
2483 JNI::CallStaticIntMethod,
2484 JNI::CallStaticIntMethodV,
2485 JNI::CallStaticIntMethodA,
2486 JNI::CallStaticLongMethod,
2487 JNI::CallStaticLongMethodV,
2488 JNI::CallStaticLongMethodA,
2489 JNI::CallStaticFloatMethod,
2490 JNI::CallStaticFloatMethodV,
2491 JNI::CallStaticFloatMethodA,
2492 JNI::CallStaticDoubleMethod,
2493 JNI::CallStaticDoubleMethodV,
2494 JNI::CallStaticDoubleMethodA,
2495 JNI::CallStaticVoidMethod,
2496 JNI::CallStaticVoidMethodV,
2497 JNI::CallStaticVoidMethodA,
2498 JNI::GetStaticFieldID,
2499 JNI::GetStaticObjectField,
2500 JNI::GetStaticBooleanField,
2501 JNI::GetStaticByteField,
2502 JNI::GetStaticCharField,
2503 JNI::GetStaticShortField,
2504 JNI::GetStaticIntField,
2505 JNI::GetStaticLongField,
2506 JNI::GetStaticFloatField,
2507 JNI::GetStaticDoubleField,
2508 JNI::SetStaticObjectField,
2509 JNI::SetStaticBooleanField,
2510 JNI::SetStaticByteField,
2511 JNI::SetStaticCharField,
2512 JNI::SetStaticShortField,
2513 JNI::SetStaticIntField,
2514 JNI::SetStaticLongField,
2515 JNI::SetStaticFloatField,
2516 JNI::SetStaticDoubleField,
2517 JNI::NewString,
2518 JNI::GetStringLength,
2519 JNI::GetStringChars,
2520 JNI::ReleaseStringChars,
2521 JNI::NewStringUTF,
2522 JNI::GetStringUTFLength,
2523 JNI::GetStringUTFChars,
2524 JNI::ReleaseStringUTFChars,
2525 JNI::GetArrayLength,
2526 JNI::NewObjectArray,
2527 JNI::GetObjectArrayElement,
2528 JNI::SetObjectArrayElement,
2529 JNI::NewBooleanArray,
2530 JNI::NewByteArray,
2531 JNI::NewCharArray,
2532 JNI::NewShortArray,
2533 JNI::NewIntArray,
2534 JNI::NewLongArray,
2535 JNI::NewFloatArray,
2536 JNI::NewDoubleArray,
2537 JNI::GetBooleanArrayElements,
2538 JNI::GetByteArrayElements,
2539 JNI::GetCharArrayElements,
2540 JNI::GetShortArrayElements,
2541 JNI::GetIntArrayElements,
2542 JNI::GetLongArrayElements,
2543 JNI::GetFloatArrayElements,
2544 JNI::GetDoubleArrayElements,
2545 JNI::ReleaseBooleanArrayElements,
2546 JNI::ReleaseByteArrayElements,
2547 JNI::ReleaseCharArrayElements,
2548 JNI::ReleaseShortArrayElements,
2549 JNI::ReleaseIntArrayElements,
2550 JNI::ReleaseLongArrayElements,
2551 JNI::ReleaseFloatArrayElements,
2552 JNI::ReleaseDoubleArrayElements,
2553 JNI::GetBooleanArrayRegion,
2554 JNI::GetByteArrayRegion,
2555 JNI::GetCharArrayRegion,
2556 JNI::GetShortArrayRegion,
2557 JNI::GetIntArrayRegion,
2558 JNI::GetLongArrayRegion,
2559 JNI::GetFloatArrayRegion,
2560 JNI::GetDoubleArrayRegion,
2561 JNI::SetBooleanArrayRegion,
2562 JNI::SetByteArrayRegion,
2563 JNI::SetCharArrayRegion,
2564 JNI::SetShortArrayRegion,
2565 JNI::SetIntArrayRegion,
2566 JNI::SetLongArrayRegion,
2567 JNI::SetFloatArrayRegion,
2568 JNI::SetDoubleArrayRegion,
2569 JNI::RegisterNatives,
2570 JNI::UnregisterNatives,
2571 JNI::MonitorEnter,
2572 JNI::MonitorExit,
2573 JNI::GetJavaVM,
2574 JNI::GetStringRegion,
2575 JNI::GetStringUTFRegion,
2576 JNI::GetPrimitiveArrayCritical,
2577 JNI::ReleasePrimitiveArrayCritical,
2578 JNI::GetStringCritical,
2579 JNI::ReleaseStringCritical,
2580 JNI::NewWeakGlobalRef,
2581 JNI::DeleteWeakGlobalRef,
2582 JNI::ExceptionCheck,
2583 JNI::NewDirectByteBuffer,
2584 JNI::GetDirectBufferAddress,
2585 JNI::GetDirectBufferCapacity,
2586 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002587};
2588
Elliott Hughes75770752011-08-24 17:52:38 -07002589JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002590 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002591 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002592 local_ref_cookie(IRT_FIRST_SEGMENT),
2593 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002594 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002595 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002596 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002597 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002598 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002599 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002600 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002601 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2602 // errors will ensue.
2603 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2604 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002605}
2606
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002607JNIEnvExt::~JNIEnvExt() {
2608}
2609
Elliott Hughes88c5c352012-03-15 18:49:48 -07002610void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2611 check_jni = enabled;
2612 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002613}
2614
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002615void JNIEnvExt::DumpReferenceTables() {
2616 locals.Dump();
2617 monitors.Dump();
2618}
2619
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002620void JNIEnvExt::PushFrame(int capacity) {
2621 stacked_local_ref_cookies.push_back(local_ref_cookie);
2622 local_ref_cookie = locals.GetSegmentState();
2623}
2624
2625void JNIEnvExt::PopFrame() {
2626 locals.SetSegmentState(local_ref_cookie);
2627 local_ref_cookie = stacked_local_ref_cookies.back();
2628 stacked_local_ref_cookies.pop_back();
2629}
2630
Carl Shapiroea4dca82011-08-01 13:45:38 -07002631// JNI Invocation interface.
2632
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2634 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2635 if (args->version < JNI_VERSION_1_2) {
2636 return JNI_EVERSION;
2637 }
2638 Runtime::Options options;
2639 for (int i = 0; i < args->nOptions; ++i) {
2640 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002641 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642 }
2643 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002644 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 if (runtime == NULL) {
2646 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002647 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002648 runtime->Start();
2649 *p_env = Thread::Current()->GetJniEnv();
2650 *p_vm = runtime->GetJavaVM();
2651 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002652}
2653
Elliott Hughesf2682d52011-08-15 16:37:04 -07002654extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 Runtime* runtime = Runtime::Current();
2656 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002657 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002659 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002660 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661 }
2662 return JNI_OK;
2663}
2664
2665// Historically unsupported.
2666extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2667 return JNI_ERR;
2668}
2669
Elliott Hughescdf53122011-08-19 15:46:09 -07002670class JII {
2671 public:
2672 static jint DestroyJavaVM(JavaVM* vm) {
2673 if (vm == NULL) {
2674 return JNI_ERR;
2675 } else {
2676 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2677 delete raw_vm->runtime;
2678 return JNI_OK;
2679 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002681
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002683 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002684 }
2685
2686 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002687 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 }
2689
2690 static jint DetachCurrentThread(JavaVM* vm) {
2691 if (vm == NULL) {
2692 return JNI_ERR;
2693 } else {
2694 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2695 Runtime* runtime = raw_vm->runtime;
2696 runtime->DetachCurrentThread();
2697 return JNI_OK;
2698 }
2699 }
2700
2701 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2702 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2703 return JNI_EVERSION;
2704 }
2705 if (vm == NULL || env == NULL) {
2706 return JNI_ERR;
2707 }
2708 Thread* thread = Thread::Current();
2709 if (thread == NULL) {
2710 *env = NULL;
2711 return JNI_EDETACHED;
2712 }
2713 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002714 return JNI_OK;
2715 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002716};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002717
Elliott Hughes88c5c352012-03-15 18:49:48 -07002718const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002719 NULL, // reserved0
2720 NULL, // reserved1
2721 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002722 JII::DestroyJavaVM,
2723 JII::AttachCurrentThread,
2724 JII::DetachCurrentThread,
2725 JII::GetEnv,
2726 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002727};
2728
Elliott Hughesa0957642011-09-02 14:27:33 -07002729JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002730 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002731 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002732 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002733 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002734 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002735 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002736 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002737 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002738 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002739 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002740 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002741 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002742 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002743 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002744 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002745 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002746 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002747 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002748}
2749
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002750JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002751 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002752}
2753
Elliott Hughes88c5c352012-03-15 18:49:48 -07002754void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2755 check_jni = enabled;
2756 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002757}
2758
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002759void JavaVMExt::DumpReferenceTables() {
2760 {
2761 MutexLock mu(globals_lock);
2762 globals.Dump();
2763 }
2764 {
2765 MutexLock mu(weak_globals_lock);
2766 weak_globals.Dump();
2767 }
2768 {
2769 MutexLock mu(pins_lock);
2770 pin_table.Dump();
2771 }
2772}
2773
Elliott Hughes75770752011-08-24 17:52:38 -07002774bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2775 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002776
2777 // See if we've already loaded this library. If we have, and the class loader
2778 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002779 // TODO: for better results we should canonicalize the pathname (or even compare
2780 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002781 SharedLibrary* library;
2782 {
2783 // TODO: move the locking (and more of this logic) into Libraries.
2784 MutexLock mu(libraries_lock);
2785 library = libraries->Get(path);
2786 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002787 if (library != NULL) {
2788 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002789 // The library will be associated with class_loader. The JNI
2790 // spec says we can't load the same library into more than one
2791 // class loader.
2792 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2793 "ClassLoader %p; can't open in ClassLoader %p",
2794 path.c_str(), library->GetClassLoader(), class_loader);
2795 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 return false;
2797 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002798 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2799 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002800 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002801 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2802 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002803 return false;
2804 }
2805 return true;
2806 }
2807
2808 // Open the shared library. Because we're using a full path, the system
2809 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2810 // resolve this library's dependencies though.)
2811
2812 // Failures here are expected when java.library.path has several entries
2813 // and we have to hunt for the lib.
2814
2815 // The current version of the dynamic linker prints detailed information
2816 // about dlopen() failures. Some things to check if the message is
2817 // cryptic:
2818 // - make sure the library exists on the device
2819 // - verify that the right path is being opened (the debug log message
2820 // above can help with that)
2821 // - check to see if the library is valid (e.g. not zero bytes long)
2822 // - check config/prelink-linux-arm.map to ensure that the library
2823 // is listed and is not being overrun by the previous entry (if
2824 // loading suddenly stops working on a prelinked library, this is
2825 // a good one to check)
2826 // - write a trivial app that calls sleep() then dlopen(), attach
2827 // to it with "strace -p <pid>" while it sleeps, and watch for
2828 // attempts to open nonexistent dependent shared libs
2829
2830 // TODO: automate some of these checks!
2831
2832 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002833 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002834 // the GC to ignore us.
2835 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002836 void* handle = NULL;
2837 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002838 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002839 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002840 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002841
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002842 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002843
2844 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002845 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002846 return false;
2847 }
2848
2849 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002851 // TODO: move the locking (and more of this logic) into Libraries.
2852 MutexLock mu(libraries_lock);
2853 library = libraries->Get(path);
2854 if (library != NULL) {
2855 LOG(INFO) << "WOW: we lost a race to add shared library: "
2856 << "\"" << path << "\" ClassLoader=" << class_loader;
2857 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002858 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002859 library = new SharedLibrary(path, handle, class_loader);
2860 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002861 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002862
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002863 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002864
2865 bool result = true;
2866 void* sym = dlsym(handle, "JNI_OnLoad");
2867 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002868 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002869 } else {
2870 // Call JNI_OnLoad. We have to override the current class
2871 // loader, which will always be "null" since the stuff at the
2872 // top of the stack is around Runtime.loadLibrary(). (See
2873 // the comments in the JNI FindClass function.)
2874 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2875 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002876 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 self->SetClassLoaderOverride(class_loader);
2878
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002879 int version = 0;
2880 {
2881 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002882 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002883 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002884 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002885
Brian Carlstromaded5f72011-10-07 17:15:04 -07002886 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002887
2888 if (version != JNI_VERSION_1_2 &&
2889 version != JNI_VERSION_1_4 &&
2890 version != JNI_VERSION_1_6) {
2891 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2892 << "bad version: " << version;
2893 // It's unwise to call dlclose() here, but we can mark it
2894 // as bad and ensure that future load attempts will fail.
2895 // We don't know how far JNI_OnLoad got, so there could
2896 // be some partially-initialized stuff accessible through
2897 // newly-registered native method calls. We could try to
2898 // unregister them, but that doesn't seem worthwhile.
2899 result = false;
2900 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002901 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2902 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 }
2904 }
2905
2906 library->SetResult(result);
2907 return result;
2908}
2909
2910void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2911 CHECK(m->IsNative());
2912
2913 Class* c = m->GetDeclaringClass();
2914
2915 // If this is a static method, it could be called before the class
2916 // has been initialized.
2917 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002918 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002919 return NULL;
2920 }
2921 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002922 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002923 }
2924
Brian Carlstrom16192862011-09-12 17:50:06 -07002925 std::string detail;
2926 void* native_method;
2927 {
2928 MutexLock mu(libraries_lock);
2929 native_method = libraries->FindNativeMethod(m, detail);
2930 }
2931 // throwing can cause libraries_lock to be reacquired
2932 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002933 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002934 }
2935 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002936}
2937
Elliott Hughes410c0c82011-09-01 17:58:25 -07002938void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2939 {
2940 MutexLock mu(globals_lock);
2941 globals.VisitRoots(visitor, arg);
2942 }
2943 {
2944 MutexLock mu(pins_lock);
2945 pin_table.VisitRoots(visitor, arg);
2946 }
2947 // The weak_globals table is visited by the GC itself (because it mutates the table).
2948}
2949
Elliott Hughes844f9a02012-01-24 20:19:58 -08002950jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2951 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2952 if (c.get() == NULL) {
2953 return NULL;
2954 }
2955 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2956}
2957
Ian Rogersdf20fe02011-07-20 20:34:16 -07002958} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002959
2960std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2961 switch (rhs) {
2962 case JNIInvalidRefType:
2963 os << "JNIInvalidRefType";
2964 return os;
2965 case JNILocalRefType:
2966 os << "JNILocalRefType";
2967 return os;
2968 case JNIGlobalRefType:
2969 os << "JNIGlobalRefType";
2970 return os;
2971 case JNIWeakGlobalRefType:
2972 os << "JNIWeakGlobalRefType";
2973 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002974 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002975 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002976 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002977 }
2978}