blob: 64d8203517c1128ee982d222cf7096747f0f0a6f [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 Hughesbf86d042011-08-31 17:53:14 -0700241namespace {
242
Elliott Hughescdf53122011-08-19 15:46:09 -0700243jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
244 if (obj == NULL) {
245 return NULL;
246 }
Elliott Hughes75770752011-08-24 17:52:38 -0700247 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 IndirectReferenceTable& weak_globals = vm->weak_globals;
249 MutexLock mu(vm->weak_globals_lock);
250 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
251 return reinterpret_cast<jweak>(ref);
252}
253
Elliott Hughesbf86d042011-08-31 17:53:14 -0700254// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700255template<typename T>
256T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700257 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700258}
259
Elliott Hughes77405792012-03-15 15:22:12 -0700260static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700262 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700263 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700264 return result;
265}
266
Ian Rogers0571d352011-11-03 19:51:38 -0700267static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700268 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800269 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700270 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800271 ArgArray arg_array(method);
272 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700273 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700274}
275
Ian Rogers0571d352011-11-03 19:51:38 -0700276static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700277 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700278}
279
Ian Rogers0571d352011-11-03 19:51:38 -0700280static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
281 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700282 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800283 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700284 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800285 ArgArray arg_array(method);
286 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700287 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700288}
289
Ian Rogers0571d352011-11-03 19:51:38 -0700290static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
291 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700292 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800293 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700294 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800295 ArgArray arg_array(method);
296 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700297 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700298}
299
Elliott Hughes6b436852011-08-12 10:16:44 -0700300// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
301// separated with slashes but aren't wrapped with "L;" like regular descriptors
302// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
303// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
304// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700305static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700306 std::string result;
307 // Add the missing "L;" if necessary.
308 if (name[0] == '[') {
309 result = name;
310 } else {
311 result += 'L';
312 result += name;
313 result += ';';
314 }
315 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700316 if (result.find('.') != std::string::npos) {
317 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
318 << "\"" << name << "\"";
319 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700320 }
321 return result;
322}
323
Ian Rogers0571d352011-11-03 19:51:38 -0700324static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700325 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800326 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700327}
328
Ian Rogers0571d352011-11-03 19:51:38 -0700329static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700330 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700331 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700332 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700333 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700334
335 Method* method = NULL;
336 if (is_static) {
337 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700338 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 method = c->FindVirtualMethod(name, sig);
340 if (method == NULL) {
341 // No virtual method matching the signature. Search declared
342 // private methods and constructors.
343 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700344 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700345 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700348 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700349 return NULL;
350 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700351
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700352 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700353}
354
Ian Rogers0571d352011-11-03 19:51:38 -0700355static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700356 Frame frame = self->GetTopOfStack();
357 Method* method = frame.GetMethod();
358 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
359 return self->GetClassLoaderOverride();
360 }
361 return method->GetDeclaringClass()->GetClassLoader();
362}
363
Ian Rogers0571d352011-11-03 19:51:38 -0700364static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700365 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700366 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700367 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700368 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700369
370 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 Class* field_type;
372 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
373 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700374 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700376 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700378 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 if (field_type == NULL) {
380 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700381 DCHECK(ts.Self()->IsExceptionPending());
382 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700383 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700384 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800385 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 return NULL;
387 }
388 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700394 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800396 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700397 return NULL;
398 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700399 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700400}
401
Ian Rogers0571d352011-11-03 19:51:38 -0700402static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700403 JavaVMExt* vm = ts.Vm();
404 MutexLock mu(vm->pins_lock);
405 vm->pin_table.Add(array);
406}
407
Ian Rogers0571d352011-11-03 19:51:38 -0700408static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700409 JavaVMExt* vm = ts.Vm();
410 MutexLock mu(vm->pins_lock);
411 vm->pin_table.Remove(array);
412}
413
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700414template<typename JniT, typename ArtT>
415JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
416 CHECK_GE(length, 0); // TODO: ReportJniError
417 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700418 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700419}
420
Elliott Hughes75770752011-08-24 17:52:38 -0700421template <typename ArrayT, typename CArrayT, typename ArtArrayT>
422CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
423 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
424 PinPrimitiveArray(ts, array);
425 if (is_copy != NULL) {
426 *is_copy = JNI_FALSE;
427 }
428 return array->GetData();
429}
430
431template <typename ArrayT>
432void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
433 if (mode != JNI_COMMIT) {
434 Array* array = Decode<Array*>(ts, java_array);
435 UnpinPrimitiveArray(ts, array);
436 }
437}
438
Ian Rogers0571d352011-11-03 19:51:38 -0700439static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700440 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700441 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700442 "%s offset=%d length=%d %s.length=%d",
443 type.c_str(), start, length, identifier, array->GetLength());
444}
Ian Rogers0571d352011-11-03 19:51:38 -0700445
446static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700447 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700448 "offset=%d length=%d string.length()=%d", start, length, array_length);
449}
Elliott Hughes814e4032011-08-23 12:07:56 -0700450
451template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700452void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700453 ArrayT* array = Decode<ArrayT*>(ts, java_array);
454 if (start < 0 || length < 0 || start + length > array->GetLength()) {
455 ThrowAIOOBE(ts, array, start, length, "src");
456 } else {
457 JavaT* data = array->GetData();
458 memcpy(buf, data + start, length * sizeof(JavaT));
459 }
460}
461
462template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700463void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700464 ArrayT* array = Decode<ArrayT*>(ts, java_array);
465 if (start < 0 || length < 0 || start + length > array->GetLength()) {
466 ThrowAIOOBE(ts, array, start, length, "dst");
467 } else {
468 JavaT* data = array->GetData();
469 memcpy(data + start, buf, length * sizeof(JavaT));
470 }
471}
472
Ian Rogers0571d352011-11-03 19:51:38 -0700473static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700474 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
475 CHECK(buffer_class.get() != NULL);
476 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
477}
478
Ian Rogers0571d352011-11-03 19:51:38 -0700479static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700480 static jclass buffer_class = InitDirectByteBufferClass(env);
481 return buffer_class;
482}
483
Ian Rogers0571d352011-11-03 19:51:38 -0700484static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700485 if (vm == NULL || p_env == NULL) {
486 return JNI_ERR;
487 }
488
Elliott Hughescac6cc72011-11-03 20:31:21 -0700489 // Return immediately if we're already one with the VM.
490 Thread* self = Thread::Current();
491 if (self != NULL) {
492 *p_env = self->GetJniEnv();
493 return JNI_OK;
494 }
495
496 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
497
498 // No threads allowed in zygote mode.
499 if (runtime->IsZygote()) {
500 LOG(ERROR) << "Attempt to attach a thread in the zygote";
501 return JNI_ERR;
502 }
503
Elliott Hughes75770752011-08-24 17:52:38 -0700504 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
505 JavaVMAttachArgs args;
506 if (thr_args == NULL) {
507 // Allow the v1.1 calling convention.
508 args.version = JNI_VERSION_1_2;
509 args.name = NULL;
510 args.group = NULL; // TODO: get "main" thread group
511 } else {
512 args.version = in_args->version;
513 args.name = in_args->name;
514 if (in_args->group != NULL) {
515 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
516 args.group = NULL; // TODO: decode in_args->group
517 } else {
518 args.group = NULL; // TODO: get "main" thread group
519 }
520 }
521 CHECK_GE(args.version, JNI_VERSION_1_2);
522
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700523 runtime->AttachCurrentThread(args.name, as_daemon);
524 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700525 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700526}
527
Elliott Hughes79082e32011-08-25 12:07:32 -0700528class SharedLibrary {
529 public:
530 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
531 : path_(path),
532 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700533 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700534 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700535 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700536 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 }
539
Elliott Hughes79082e32011-08-25 12:07:32 -0700540 Object* GetClassLoader() {
541 return class_loader_;
542 }
543
544 std::string GetPath() {
545 return path_;
546 }
547
548 /*
549 * Check the result of an earlier call to JNI_OnLoad on this library. If
550 * the call has not yet finished in another thread, wait for it.
551 */
552 bool CheckOnLoadResult(JavaVMExt* vm) {
553 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700554 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 // Check this so we don't end up waiting for ourselves. We need
556 // to return "true" so the caller can continue.
557 LOG(INFO) << *self << " recursive attempt to load library "
558 << "\"" << path_ << "\"";
559 return true;
560 }
561
562 MutexLock mu(jni_on_load_lock_);
563 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800564 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
565 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700566 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700567 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 }
569
570 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800571 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
572 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700573 return okay;
574 }
575
576 void SetResult(bool result) {
577 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700578 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700579
580 // Broadcast a wakeup to anybody sleeping on the condition variable.
581 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700582 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 }
584
585 void* FindSymbol(const std::string& symbol_name) {
586 return dlsym(handle_, symbol_name.c_str());
587 }
588
589 private:
590 enum JNI_OnLoadState {
591 kPending,
592 kFailed,
593 kOkay,
594 };
595
596 // Path to library "/system/lib/libjni.so".
597 std::string path_;
598
599 // The void* returned by dlopen(3).
600 void* handle_;
601
602 // The ClassLoader this library is associated with.
603 Object* class_loader_;
604
605 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700606 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700608 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700610 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 // Result of earlier JNI_OnLoad call.
612 JNI_OnLoadState jni_on_load_result_;
613};
614
Elliott Hughescdf53122011-08-19 15:46:09 -0700615} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughes79082e32011-08-25 12:07:32 -0700617// This exists mainly to keep implementation details out of the header file.
618class Libraries {
619 public:
620 Libraries() {
621 }
622
623 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700624 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700625 }
626
627 SharedLibrary* Get(const std::string& path) {
628 return libraries_[path];
629 }
630
631 void Put(const std::string& path, SharedLibrary* library) {
632 libraries_[path] = library;
633 }
634
635 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700636 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700637 std::string jni_short_name(JniShortName(m));
638 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700639 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
641 SharedLibrary* library = it->second;
642 if (library->GetClassLoader() != declaring_class_loader) {
643 // We only search libraries loaded by the appropriate ClassLoader.
644 continue;
645 }
646 // Try the short name then the long name...
647 void* fn = library->FindSymbol(jni_short_name);
648 if (fn == NULL) {
649 fn = library->FindSymbol(jni_long_name);
650 }
651 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800652 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
653 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700654 return fn;
655 }
656 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700657 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700658 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700659 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700660 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700661 return NULL;
662 }
663
664 private:
665 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
666
667 std::map<std::string, SharedLibrary*> libraries_;
668};
669
Elliott Hughes418d20f2011-09-22 14:00:39 -0700670JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
671 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
672 Object* receiver = Decode<Object*>(env, obj);
673 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800674 ArgArray arg_array(method);
675 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700676 return InvokeWithArgArray(env, receiver, method, arg_array.get());
677}
678
Elliott Hughesd07986f2011-12-06 18:27:45 -0800679JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700680 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800681}
682
Elliott Hughescdf53122011-08-19 15:46:09 -0700683class JNI {
684 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700685
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 static jint GetVersion(JNIEnv* env) {
687 ScopedJniThreadState ts(env);
688 return JNI_VERSION_1_6;
689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
692 ScopedJniThreadState ts(env);
693 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700694 return NULL;
695 }
696
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 static jclass FindClass(JNIEnv* env, const char* name) {
698 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700699 Runtime* runtime = Runtime::Current();
700 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700702 Class* c = NULL;
703 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700704 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800705 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700706 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800707 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700708 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700709 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
713 ScopedJniThreadState ts(env);
714 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700715 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700716 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700717
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
719 ScopedJniThreadState ts(env);
720 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700721 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
725 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700726 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700727 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700729
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
731 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700732 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700733 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
737 ScopedJniThreadState ts(env);
738 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700739 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 }
741
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700742 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700744 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700745 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700747
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 Class* c1 = Decode<Class*>(ts, java_class1);
751 Class* c2 = Decode<Class*>(ts, java_class2);
752 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700754
Elliott Hughes37f7a402011-08-22 18:56:01 -0700755 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700757 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700759 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 return JNI_TRUE;
761 } else {
762 Object* obj = Decode<Object*>(ts, jobj);
763 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700764 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700767
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 static jint Throw(JNIEnv* env, jthrowable java_exception) {
769 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700770 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 if (exception == NULL) {
772 return JNI_ERR;
773 }
774 ts.Self()->SetException(exception);
775 return JNI_OK;
776 }
777
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700778 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700779 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700781 jmethodID mid = ((msg != NULL)
782 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
783 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700784 if (mid == NULL) {
785 return JNI_ERR;
786 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700788 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700789 return JNI_ERR;
790 }
791
792 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 args[0].l = s.get();
794 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
795 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700796 return JNI_ERR;
797 }
798
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700800
Elliott Hughes37f7a402011-08-22 18:56:01 -0700801 return JNI_OK;
802 }
803
804 static jboolean ExceptionCheck(JNIEnv* env) {
805 ScopedJniThreadState ts(env);
806 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
807 }
808
809 static void ExceptionClear(JNIEnv* env) {
810 ScopedJniThreadState ts(env);
811 ts.Self()->ClearException();
812 }
813
814 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700816
817 Thread* self = ts.Self();
818 Throwable* original_exception = self->GetException();
819 self->ClearException();
820
Elliott Hughesbf86d042011-08-31 17:53:14 -0700821 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
823 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
824 if (mid == NULL) {
825 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700826 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 } else {
828 env->CallVoidMethod(exception.get(), mid);
829 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700830 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 << " thrown while calling printStackTrace";
832 self->ClearException();
833 }
834 }
835
836 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700838
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 static jthrowable ExceptionOccurred(JNIEnv* env) {
840 ScopedJniThreadState ts(env);
841 Object* exception = ts.Self()->GetException();
842 if (exception == NULL) {
843 return NULL;
844 } else {
845 // TODO: if adding a local reference failing causes the VM to abort
846 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700847 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 if (localException == NULL) {
849 // We were unable to add a new local reference, and threw a new
850 // exception. We can't return "exception", because it's not a
851 // local reference. So we have to return NULL, indicating that
852 // there was no exception, even though it's pretty much raining
853 // exceptions in here.
854 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
855 }
856 return localException;
857 }
858 }
859
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 static void FatalError(JNIEnv* env, const char* msg) {
861 ScopedJniThreadState ts(env);
862 LOG(FATAL) << "JNI FatalError called: " << msg;
863 }
864
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700865 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700867 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
868 return JNI_ERR;
869 }
870 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 return JNI_OK;
872 }
873
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700874 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700876 Object* survivor = Decode<Object*>(ts, java_survivor);
877 ts.Env()->PopFrame();
878 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700881 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700883 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
884 }
885
886 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
887 // TODO: we should try to expand the table if necessary.
888 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
889 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
890 return JNI_ERR;
891 }
892 // TODO: this isn't quite right, since "capacity" includes holes.
893 size_t capacity = ts.Env()->locals.Capacity();
894 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
895 if (!okay) {
896 ts.Self()->ThrowOutOfMemoryError(caller);
897 }
898 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 }
900
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
902 ScopedJniThreadState ts(env);
903 if (obj == NULL) {
904 return NULL;
905 }
906
Elliott Hughes75770752011-08-24 17:52:38 -0700907 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 IndirectReferenceTable& globals = vm->globals;
909 MutexLock mu(vm->globals_lock);
910 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
911 return reinterpret_cast<jobject>(ref);
912 }
913
914 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
915 ScopedJniThreadState ts(env);
916 if (obj == NULL) {
917 return;
918 }
919
Elliott Hughes75770752011-08-24 17:52:38 -0700920 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 IndirectReferenceTable& globals = vm->globals;
922 MutexLock mu(vm->globals_lock);
923
924 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
925 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
926 << "failed to find entry";
927 }
928 }
929
930 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
931 ScopedJniThreadState ts(env);
932 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
933 }
934
935 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
936 ScopedJniThreadState ts(env);
937 if (obj == NULL) {
938 return;
939 }
940
Elliott Hughes75770752011-08-24 17:52:38 -0700941 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 IndirectReferenceTable& weak_globals = vm->weak_globals;
943 MutexLock mu(vm->weak_globals_lock);
944
945 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
946 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
947 << "failed to find entry";
948 }
949 }
950
951 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
952 ScopedJniThreadState ts(env);
953 if (obj == NULL) {
954 return NULL;
955 }
956
957 IndirectReferenceTable& locals = ts.Env()->locals;
958
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700959 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
961 return reinterpret_cast<jobject>(ref);
962 }
963
964 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
965 ScopedJniThreadState ts(env);
966 if (obj == NULL) {
967 return;
968 }
969
970 IndirectReferenceTable& locals = ts.Env()->locals;
971
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700972 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 if (!locals.Remove(cookie, obj)) {
974 // Attempting to delete a local reference that is not in the
975 // topmost local reference frame is a no-op. DeleteLocalRef returns
976 // void and doesn't throw any exceptions, but we should probably
977 // complain about it so the user will notice that things aren't
978 // going quite the way they expect.
979 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
980 << "failed to find entry";
981 }
982 }
983
984 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
985 ScopedJniThreadState ts(env);
986 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
987 ? JNI_TRUE : JNI_FALSE;
988 }
989
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700990 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700992 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700993 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700994 return NULL;
995 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700996 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 ScopedJniThreadState ts(env);
1001 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_start(args, mid);
1003 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 va_end(args);
1005 return result;
1006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001010 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001011 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001012 return NULL;
1013 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001014 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001015 if (result == NULL) {
1016 return NULL;
1017 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001018 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001020 if (!ts.Self()->IsExceptionPending()) {
1021 return local_result;
1022 } else {
1023 return NULL;
1024 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001029 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001030 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001031 return NULL;
1032 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001033 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001034 if (result == NULL) {
1035 return NULL;
1036 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001037 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001039 if (!ts.Self()->IsExceptionPending()) {
1040 return local_result;
1041 } else {
1042 return NULL;
1043 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1047 ScopedJniThreadState ts(env);
1048 return FindMethodID(ts, c, name, sig, false);
1049 }
1050
1051 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1052 ScopedJniThreadState ts(env);
1053 return FindMethodID(ts, c, name, sig, true);
1054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001060 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001062 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001067 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001068 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001073 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001074 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001081 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
1083 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001093 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_list ap;
1099 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001100 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
1102 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001119 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_end(ap);
1121 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001126 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_list ap;
1137 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001138 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
1140 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001145 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_list ap;
1156 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_end(ap);
1159 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001164 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_list ap;
1175 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001176 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 va_end(ap);
1178 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001183 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_list ap;
1194 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001195 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_end(ap);
1197 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001202 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001207 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_list ap;
1213 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001214 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_end(ap);
1216 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_list ap;
1232 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001233 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001244 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
1247 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
1250 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001252 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001253 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return local_result;
1256 }
1257
1258 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001262 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001268 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001269 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
1275 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001277 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_end(ap);
1279 return result.z;
1280 }
1281
1282 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001285 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
1288 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001291 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
1297 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001299 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_end(ap);
1301 return result.b;
1302 }
1303
1304 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001307 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
1310 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001313 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
1319 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001321 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 va_end(ap);
1323 return result.c;
1324 }
1325
1326 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001329 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
1332 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001335 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
1338 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
1341 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001343 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_end(ap);
1345 return result.s;
1346 }
1347
1348 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001351 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
1354 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001357 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Elliott Hughes418d20f2011-09-22 14:00:39 -07001360 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
1362 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_end(ap);
1366 return result.i;
1367 }
1368
1369 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001372 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
1375 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
1381 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
1384 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001385 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001386 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 va_end(ap);
1388 return result.j;
1389 }
1390
1391 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001392 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001394 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
1397 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001398 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001400 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
1403 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
1406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001408 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_end(ap);
1410 return result.f;
1411 }
1412
1413 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001414 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001416 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
1419 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001420 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001422 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
1425 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001426 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
1428 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001429 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001430 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 va_end(ap);
1432 return result.d;
1433 }
1434
1435 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001436 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001438 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
1441 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001442 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001444 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
1447 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001448 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
1450 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001451 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001452 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 va_end(ap);
1454 }
1455
1456 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001457 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001459 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
1462 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001463 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001465 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001468 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 ScopedJniThreadState ts(env);
1470 return FindFieldID(ts, c, name, sig, false);
1471 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001472
1473
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001474 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 ScopedJniThreadState ts(env);
1476 return FindFieldID(ts, c, name, sig, true);
1477 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001482 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001483 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001488 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001489 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 Object* o = Decode<Object*>(ts, java_object);
1495 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001496 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001503 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507#define GET_PRIMITIVE_FIELD(fn, instance) \
1508 ScopedJniThreadState ts(env); \
1509 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 return f->fn(o)
1512
1513#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1514 ScopedJniThreadState ts(env); \
1515 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001516 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 f->fn(o, value)
1518
1519 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1524 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1528 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1532 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1536 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1540 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1544 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1548 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1552 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1556 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1560 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1564 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetLong, NULL);
1573 }
1574
1575 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1577 }
1578
1579 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1580 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1581 }
1582
1583 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1584 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1585 }
1586
1587 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1588 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1589 }
1590
1591 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1592 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1593 }
1594
1595 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1596 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1597 }
1598
1599 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1600 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1601 }
1602
1603 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1604 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1605 }
1606
1607 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1608 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1609 }
1610
1611 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1612 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1613 }
1614
1615 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1616 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1617 }
1618
1619 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1620 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1621 }
1622
1623 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1624 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1625 }
1626
1627 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1628 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1629 }
1630
1631 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1632 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1633 }
1634
1635 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1636 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1637 }
1638
1639 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1640 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1641 }
1642
1643 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1644 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001651 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001652 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 va_end(ap);
1654 return local_result;
1655 }
1656
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001660 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001665 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001666 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
1671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_end(ap);
1675 return result.z;
1676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
1690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
1694 return result.b;
1695 }
1696
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
1709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_end(ap);
1713 return result.c;
1714 }
1715
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
1728 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_end(ap);
1732 return result.s;
1733 }
1734
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
1747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_end(ap);
1751 return result.i;
1752 }
1753
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
1766 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_end(ap);
1770 return result.j;
1771 }
1772
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001775 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001780 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
1785 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001786 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 va_end(ap);
1789 return result.f;
1790 }
1791
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001794 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes418d20f2011-09-22 14:00:39 -07001797 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001799 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes72025e52011-08-23 17:50:30 -07001802 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 ScopedJniThreadState ts(env);
1804 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001806 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 va_end(ap);
1808 return result.d;
1809 }
1810
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001813 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes418d20f2011-09-22 14:00:39 -07001816 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001818 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes72025e52011-08-23 17:50:30 -07001821 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 ScopedJniThreadState ts(env);
1823 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001824 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001825 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 va_end(ap);
1827 }
1828
Elliott Hughes418d20f2011-09-22 14:00:39 -07001829 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001831 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes418d20f2011-09-22 14:00:39 -07001834 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001836 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes814e4032011-08-23 12:07:56 -07001839 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001842 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
1845 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1846 ScopedJniThreadState ts(env);
1847 if (utf == NULL) {
1848 return NULL;
1849 }
1850 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001851 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1855 ScopedJniThreadState ts(env);
1856 return Decode<String*>(ts, java_string)->GetLength();
1857 }
1858
1859 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1860 ScopedJniThreadState ts(env);
1861 return Decode<String*>(ts, java_string)->GetUtfLength();
1862 }
1863
Elliott Hughesb465ab02011-08-24 11:21:21 -07001864 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001866 String* s = Decode<String*>(ts, java_string);
1867 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1868 ThrowSIOOBE(ts, start, length, s->GetLength());
1869 } else {
1870 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1871 memcpy(buf, chars + start, length * sizeof(jchar));
1872 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 }
1874
Elliott Hughesb465ab02011-08-24 11:21:21 -07001875 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001876 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001877 String* s = Decode<String*>(ts, java_string);
1878 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1879 ThrowSIOOBE(ts, start, length, s->GetLength());
1880 } else {
1881 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1882 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1883 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001884 }
1885
Elliott Hughes75770752011-08-24 17:52:38 -07001886 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001888 String* s = Decode<String*>(ts, java_string);
1889 const CharArray* chars = s->GetCharArray();
1890 PinPrimitiveArray(ts, chars);
1891 if (is_copy != NULL) {
1892 *is_copy = JNI_FALSE;
1893 }
1894 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001895 }
1896
Elliott Hughes75770752011-08-24 17:52:38 -07001897 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001898 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001899 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
Elliott Hughes75770752011-08-24 17:52:38 -07001902 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001904 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
Elliott Hughes75770752011-08-24 17:52:38 -07001907 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001909 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
Elliott Hughes75770752011-08-24 17:52:38 -07001912 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001913 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001914 if (java_string == NULL) {
1915 return NULL;
1916 }
1917 if (is_copy != NULL) {
1918 *is_copy = JNI_TRUE;
1919 }
1920 String* s = Decode<String*>(ts, java_string);
1921 size_t byte_count = s->GetUtfLength();
1922 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001923 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001924 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1925 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1926 bytes[byte_count] = '\0';
1927 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001931 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001932 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001933 }
1934
Elliott Hughesbd935992011-08-22 11:59:34 -07001935 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001937 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001938 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001939 Array* array = obj->AsArray();
1940 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes814e4032011-08-23 12:07:56 -07001943 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001945 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001946 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
1949 static void SetObjectArrayElement(JNIEnv* env,
1950 jobjectArray java_array, jsize index, jobject java_value) {
1951 ScopedJniThreadState ts(env);
1952 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1953 Object* value = Decode<Object*>(ts, java_value);
1954 array->Set(index, value);
1955 }
1956
1957 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1958 ScopedJniThreadState ts(env);
1959 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1960 }
1961
1962 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1963 ScopedJniThreadState ts(env);
1964 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1965 }
1966
1967 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1968 ScopedJniThreadState ts(env);
1969 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1970 }
1971
1972 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1973 ScopedJniThreadState ts(env);
1974 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1975 }
1976
1977 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1978 ScopedJniThreadState ts(env);
1979 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1980 }
1981
1982 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1983 ScopedJniThreadState ts(env);
1984 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1985 }
1986
1987 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1988 ScopedJniThreadState ts(env);
1989 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1990 }
1991
1992 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1993 ScopedJniThreadState ts(env);
1994 CHECK_GE(length, 0); // TODO: ReportJniError
1995
1996 // Compute the array class corresponding to the given element class.
1997 Class* element_class = Decode<Class*>(ts, element_jclass);
1998 std::string descriptor;
1999 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002000 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002001
2002 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002003 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2004 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 return NULL;
2006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 // Allocate and initialize if necessary.
2009 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 if (initial_element != NULL) {
2012 Object* initial_object = Decode<Object*>(ts, initial_element);
2013 for (jsize i = 0; i < length; ++i) {
2014 result->Set(i, initial_object);
2015 }
2016 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002017 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
2020 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2021 ScopedJniThreadState ts(env);
2022 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2023 }
2024
Ian Rogersa15e67d2012-02-28 13:51:55 -08002025 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002026 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002027 Array* array = Decode<Array*>(ts, java_array);
2028 PinPrimitiveArray(ts, array);
2029 if (is_copy != NULL) {
2030 *is_copy = JNI_FALSE;
2031 }
2032 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002052 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002057 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes75770752011-08-24 17:52:38 -07002060 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002062 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes75770752011-08-24 17:52:38 -07002065 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002067 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes75770752011-08-24 17:52:38 -07002070 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002072 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes75770752011-08-24 17:52:38 -07002075 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002077 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes75770752011-08-24 17:52:38 -07002080 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002082 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes75770752011-08-24 17:52:38 -07002085 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002087 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes75770752011-08-24 17:52:38 -07002090 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002092 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes75770752011-08-24 17:52:38 -07002095 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002097 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes75770752011-08-24 17:52:38 -07002100 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002102 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes75770752011-08-24 17:52:38 -07002105 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002107 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes75770752011-08-24 17:52:38 -07002110 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002112 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes75770752011-08-24 17:52:38 -07002115 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002117 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 }
2179
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 Class* c = Decode<Class*>(ts, java_class);
2203
Elliott Hughes5174fe62011-08-23 15:12:35 -07002204 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 const char* name = methods[i].name;
2206 const char* sig = methods[i].signature;
2207
2208 if (*sig == '!') {
2209 // TODO: fast jni. it's too noisy to log all these.
2210 ++sig;
2211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 Method* m = c->FindDirectMethod(name, sig);
2214 if (m == NULL) {
2215 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002218 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002219 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002222 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002223 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 return JNI_ERR;
2225 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002227 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002228
Ian Rogers60db5ab2012-02-20 17:02:00 -08002229 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231 return JNI_OK;
2232 }
2233
Elliott Hughes5174fe62011-08-23 15:12:35 -07002234 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002236 Class* c = Decode<Class*>(ts, java_class);
2237
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002238 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002239
2240 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2241 Method* m = c->GetDirectMethod(i);
2242 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002243 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002244 }
2245 }
2246 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2247 Method* m = c->GetVirtualMethod(i);
2248 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002249 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002250 }
2251 }
2252
2253 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughes72025e52011-08-23 17:50:30 -07002256 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002258 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002259 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Elliott Hughes72025e52011-08-23 17:50:30 -07002262 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002264 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002265 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
2268 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2269 ScopedJniThreadState ts(env);
2270 Runtime* runtime = Runtime::Current();
2271 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002272 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 } else {
2274 *vm = NULL;
2275 }
2276 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2277 }
2278
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2280 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281
2282 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002283 CHECK(address != NULL); // TODO: ReportJniError
2284 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002285
2286 jclass buffer_class = GetDirectByteBufferClass(env);
2287 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2288 if (mid == NULL) {
2289 return NULL;
2290 }
2291
2292 // At the moment, the Java side is limited to 32 bits.
2293 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2294 CHECK_LE(capacity, 0xffffffff);
2295 jint address_arg = reinterpret_cast<jint>(address);
2296 jint capacity_arg = static_cast<jint>(capacity);
2297
2298 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2299 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2305 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002309 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002310 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2311 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Elliott Hughesb465ab02011-08-24 11:21:21 -07002314 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002315 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316
Elliott Hughes75770752011-08-24 17:52:38 -07002317 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002318
2319 // Do we definitely know what kind of reference this is?
2320 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2321 IndirectRefKind kind = GetIndirectRefKind(ref);
2322 switch (kind) {
2323 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002324 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2325 return JNILocalRefType;
2326 }
2327 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002328 case kGlobal:
2329 return JNIGlobalRefType;
2330 case kWeakGlobal:
2331 return JNIWeakGlobalRefType;
2332 case kSirtOrInvalid:
2333 // Is it in a stack IRT?
2334 if (ts.Self()->SirtContains(java_object)) {
2335 return JNILocalRefType;
2336 }
2337
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002338 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002339 return JNIInvalidRefType;
2340 }
2341
Elliott Hughesb465ab02011-08-24 11:21:21 -07002342 // If we're handing out direct pointers, check whether it's a direct pointer
2343 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002344 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002345 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002346 return JNILocalRefType;
2347 }
2348 }
2349
2350 return JNIInvalidRefType;
2351 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002352 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2353 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002354 }
2355};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002356
Elliott Hughes88c5c352012-03-15 18:49:48 -07002357const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002358 NULL, // reserved0.
2359 NULL, // reserved1.
2360 NULL, // reserved2.
2361 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 JNI::GetVersion,
2363 JNI::DefineClass,
2364 JNI::FindClass,
2365 JNI::FromReflectedMethod,
2366 JNI::FromReflectedField,
2367 JNI::ToReflectedMethod,
2368 JNI::GetSuperclass,
2369 JNI::IsAssignableFrom,
2370 JNI::ToReflectedField,
2371 JNI::Throw,
2372 JNI::ThrowNew,
2373 JNI::ExceptionOccurred,
2374 JNI::ExceptionDescribe,
2375 JNI::ExceptionClear,
2376 JNI::FatalError,
2377 JNI::PushLocalFrame,
2378 JNI::PopLocalFrame,
2379 JNI::NewGlobalRef,
2380 JNI::DeleteGlobalRef,
2381 JNI::DeleteLocalRef,
2382 JNI::IsSameObject,
2383 JNI::NewLocalRef,
2384 JNI::EnsureLocalCapacity,
2385 JNI::AllocObject,
2386 JNI::NewObject,
2387 JNI::NewObjectV,
2388 JNI::NewObjectA,
2389 JNI::GetObjectClass,
2390 JNI::IsInstanceOf,
2391 JNI::GetMethodID,
2392 JNI::CallObjectMethod,
2393 JNI::CallObjectMethodV,
2394 JNI::CallObjectMethodA,
2395 JNI::CallBooleanMethod,
2396 JNI::CallBooleanMethodV,
2397 JNI::CallBooleanMethodA,
2398 JNI::CallByteMethod,
2399 JNI::CallByteMethodV,
2400 JNI::CallByteMethodA,
2401 JNI::CallCharMethod,
2402 JNI::CallCharMethodV,
2403 JNI::CallCharMethodA,
2404 JNI::CallShortMethod,
2405 JNI::CallShortMethodV,
2406 JNI::CallShortMethodA,
2407 JNI::CallIntMethod,
2408 JNI::CallIntMethodV,
2409 JNI::CallIntMethodA,
2410 JNI::CallLongMethod,
2411 JNI::CallLongMethodV,
2412 JNI::CallLongMethodA,
2413 JNI::CallFloatMethod,
2414 JNI::CallFloatMethodV,
2415 JNI::CallFloatMethodA,
2416 JNI::CallDoubleMethod,
2417 JNI::CallDoubleMethodV,
2418 JNI::CallDoubleMethodA,
2419 JNI::CallVoidMethod,
2420 JNI::CallVoidMethodV,
2421 JNI::CallVoidMethodA,
2422 JNI::CallNonvirtualObjectMethod,
2423 JNI::CallNonvirtualObjectMethodV,
2424 JNI::CallNonvirtualObjectMethodA,
2425 JNI::CallNonvirtualBooleanMethod,
2426 JNI::CallNonvirtualBooleanMethodV,
2427 JNI::CallNonvirtualBooleanMethodA,
2428 JNI::CallNonvirtualByteMethod,
2429 JNI::CallNonvirtualByteMethodV,
2430 JNI::CallNonvirtualByteMethodA,
2431 JNI::CallNonvirtualCharMethod,
2432 JNI::CallNonvirtualCharMethodV,
2433 JNI::CallNonvirtualCharMethodA,
2434 JNI::CallNonvirtualShortMethod,
2435 JNI::CallNonvirtualShortMethodV,
2436 JNI::CallNonvirtualShortMethodA,
2437 JNI::CallNonvirtualIntMethod,
2438 JNI::CallNonvirtualIntMethodV,
2439 JNI::CallNonvirtualIntMethodA,
2440 JNI::CallNonvirtualLongMethod,
2441 JNI::CallNonvirtualLongMethodV,
2442 JNI::CallNonvirtualLongMethodA,
2443 JNI::CallNonvirtualFloatMethod,
2444 JNI::CallNonvirtualFloatMethodV,
2445 JNI::CallNonvirtualFloatMethodA,
2446 JNI::CallNonvirtualDoubleMethod,
2447 JNI::CallNonvirtualDoubleMethodV,
2448 JNI::CallNonvirtualDoubleMethodA,
2449 JNI::CallNonvirtualVoidMethod,
2450 JNI::CallNonvirtualVoidMethodV,
2451 JNI::CallNonvirtualVoidMethodA,
2452 JNI::GetFieldID,
2453 JNI::GetObjectField,
2454 JNI::GetBooleanField,
2455 JNI::GetByteField,
2456 JNI::GetCharField,
2457 JNI::GetShortField,
2458 JNI::GetIntField,
2459 JNI::GetLongField,
2460 JNI::GetFloatField,
2461 JNI::GetDoubleField,
2462 JNI::SetObjectField,
2463 JNI::SetBooleanField,
2464 JNI::SetByteField,
2465 JNI::SetCharField,
2466 JNI::SetShortField,
2467 JNI::SetIntField,
2468 JNI::SetLongField,
2469 JNI::SetFloatField,
2470 JNI::SetDoubleField,
2471 JNI::GetStaticMethodID,
2472 JNI::CallStaticObjectMethod,
2473 JNI::CallStaticObjectMethodV,
2474 JNI::CallStaticObjectMethodA,
2475 JNI::CallStaticBooleanMethod,
2476 JNI::CallStaticBooleanMethodV,
2477 JNI::CallStaticBooleanMethodA,
2478 JNI::CallStaticByteMethod,
2479 JNI::CallStaticByteMethodV,
2480 JNI::CallStaticByteMethodA,
2481 JNI::CallStaticCharMethod,
2482 JNI::CallStaticCharMethodV,
2483 JNI::CallStaticCharMethodA,
2484 JNI::CallStaticShortMethod,
2485 JNI::CallStaticShortMethodV,
2486 JNI::CallStaticShortMethodA,
2487 JNI::CallStaticIntMethod,
2488 JNI::CallStaticIntMethodV,
2489 JNI::CallStaticIntMethodA,
2490 JNI::CallStaticLongMethod,
2491 JNI::CallStaticLongMethodV,
2492 JNI::CallStaticLongMethodA,
2493 JNI::CallStaticFloatMethod,
2494 JNI::CallStaticFloatMethodV,
2495 JNI::CallStaticFloatMethodA,
2496 JNI::CallStaticDoubleMethod,
2497 JNI::CallStaticDoubleMethodV,
2498 JNI::CallStaticDoubleMethodA,
2499 JNI::CallStaticVoidMethod,
2500 JNI::CallStaticVoidMethodV,
2501 JNI::CallStaticVoidMethodA,
2502 JNI::GetStaticFieldID,
2503 JNI::GetStaticObjectField,
2504 JNI::GetStaticBooleanField,
2505 JNI::GetStaticByteField,
2506 JNI::GetStaticCharField,
2507 JNI::GetStaticShortField,
2508 JNI::GetStaticIntField,
2509 JNI::GetStaticLongField,
2510 JNI::GetStaticFloatField,
2511 JNI::GetStaticDoubleField,
2512 JNI::SetStaticObjectField,
2513 JNI::SetStaticBooleanField,
2514 JNI::SetStaticByteField,
2515 JNI::SetStaticCharField,
2516 JNI::SetStaticShortField,
2517 JNI::SetStaticIntField,
2518 JNI::SetStaticLongField,
2519 JNI::SetStaticFloatField,
2520 JNI::SetStaticDoubleField,
2521 JNI::NewString,
2522 JNI::GetStringLength,
2523 JNI::GetStringChars,
2524 JNI::ReleaseStringChars,
2525 JNI::NewStringUTF,
2526 JNI::GetStringUTFLength,
2527 JNI::GetStringUTFChars,
2528 JNI::ReleaseStringUTFChars,
2529 JNI::GetArrayLength,
2530 JNI::NewObjectArray,
2531 JNI::GetObjectArrayElement,
2532 JNI::SetObjectArrayElement,
2533 JNI::NewBooleanArray,
2534 JNI::NewByteArray,
2535 JNI::NewCharArray,
2536 JNI::NewShortArray,
2537 JNI::NewIntArray,
2538 JNI::NewLongArray,
2539 JNI::NewFloatArray,
2540 JNI::NewDoubleArray,
2541 JNI::GetBooleanArrayElements,
2542 JNI::GetByteArrayElements,
2543 JNI::GetCharArrayElements,
2544 JNI::GetShortArrayElements,
2545 JNI::GetIntArrayElements,
2546 JNI::GetLongArrayElements,
2547 JNI::GetFloatArrayElements,
2548 JNI::GetDoubleArrayElements,
2549 JNI::ReleaseBooleanArrayElements,
2550 JNI::ReleaseByteArrayElements,
2551 JNI::ReleaseCharArrayElements,
2552 JNI::ReleaseShortArrayElements,
2553 JNI::ReleaseIntArrayElements,
2554 JNI::ReleaseLongArrayElements,
2555 JNI::ReleaseFloatArrayElements,
2556 JNI::ReleaseDoubleArrayElements,
2557 JNI::GetBooleanArrayRegion,
2558 JNI::GetByteArrayRegion,
2559 JNI::GetCharArrayRegion,
2560 JNI::GetShortArrayRegion,
2561 JNI::GetIntArrayRegion,
2562 JNI::GetLongArrayRegion,
2563 JNI::GetFloatArrayRegion,
2564 JNI::GetDoubleArrayRegion,
2565 JNI::SetBooleanArrayRegion,
2566 JNI::SetByteArrayRegion,
2567 JNI::SetCharArrayRegion,
2568 JNI::SetShortArrayRegion,
2569 JNI::SetIntArrayRegion,
2570 JNI::SetLongArrayRegion,
2571 JNI::SetFloatArrayRegion,
2572 JNI::SetDoubleArrayRegion,
2573 JNI::RegisterNatives,
2574 JNI::UnregisterNatives,
2575 JNI::MonitorEnter,
2576 JNI::MonitorExit,
2577 JNI::GetJavaVM,
2578 JNI::GetStringRegion,
2579 JNI::GetStringUTFRegion,
2580 JNI::GetPrimitiveArrayCritical,
2581 JNI::ReleasePrimitiveArrayCritical,
2582 JNI::GetStringCritical,
2583 JNI::ReleaseStringCritical,
2584 JNI::NewWeakGlobalRef,
2585 JNI::DeleteWeakGlobalRef,
2586 JNI::ExceptionCheck,
2587 JNI::NewDirectByteBuffer,
2588 JNI::GetDirectBufferAddress,
2589 JNI::GetDirectBufferCapacity,
2590 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002591};
2592
Elliott Hughes75770752011-08-24 17:52:38 -07002593JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002594 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002595 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002596 local_ref_cookie(IRT_FIRST_SEGMENT),
2597 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002598 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002599 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002600 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002601 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002602 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002603 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002604 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002605 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2606 // errors will ensue.
2607 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2608 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002609}
2610
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002611JNIEnvExt::~JNIEnvExt() {
2612}
2613
Elliott Hughes88c5c352012-03-15 18:49:48 -07002614void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2615 check_jni = enabled;
2616 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002617}
2618
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002619void JNIEnvExt::DumpReferenceTables() {
2620 locals.Dump();
2621 monitors.Dump();
2622}
2623
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002624void JNIEnvExt::PushFrame(int capacity) {
2625 stacked_local_ref_cookies.push_back(local_ref_cookie);
2626 local_ref_cookie = locals.GetSegmentState();
2627}
2628
2629void JNIEnvExt::PopFrame() {
2630 locals.SetSegmentState(local_ref_cookie);
2631 local_ref_cookie = stacked_local_ref_cookies.back();
2632 stacked_local_ref_cookies.pop_back();
2633}
2634
Carl Shapiroea4dca82011-08-01 13:45:38 -07002635// JNI Invocation interface.
2636
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2638 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2639 if (args->version < JNI_VERSION_1_2) {
2640 return JNI_EVERSION;
2641 }
2642 Runtime::Options options;
2643 for (int i = 0; i < args->nOptions; ++i) {
2644 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002645 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646 }
2647 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002648 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002649 if (runtime == NULL) {
2650 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002652 runtime->Start();
2653 *p_env = Thread::Current()->GetJniEnv();
2654 *p_vm = runtime->GetJavaVM();
2655 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002656}
2657
Elliott Hughesf2682d52011-08-15 16:37:04 -07002658extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659 Runtime* runtime = Runtime::Current();
2660 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002661 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002662 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002663 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002664 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002665 }
2666 return JNI_OK;
2667}
2668
2669// Historically unsupported.
2670extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2671 return JNI_ERR;
2672}
2673
Elliott Hughescdf53122011-08-19 15:46:09 -07002674class JII {
2675 public:
2676 static jint DestroyJavaVM(JavaVM* vm) {
2677 if (vm == NULL) {
2678 return JNI_ERR;
2679 } else {
2680 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2681 delete raw_vm->runtime;
2682 return JNI_OK;
2683 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002684 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002685
Elliott Hughescdf53122011-08-19 15:46:09 -07002686 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002687 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 }
2689
2690 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002691 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 }
2693
2694 static jint DetachCurrentThread(JavaVM* vm) {
2695 if (vm == NULL) {
2696 return JNI_ERR;
2697 } else {
2698 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2699 Runtime* runtime = raw_vm->runtime;
2700 runtime->DetachCurrentThread();
2701 return JNI_OK;
2702 }
2703 }
2704
2705 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2706 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2707 return JNI_EVERSION;
2708 }
2709 if (vm == NULL || env == NULL) {
2710 return JNI_ERR;
2711 }
2712 Thread* thread = Thread::Current();
2713 if (thread == NULL) {
2714 *env = NULL;
2715 return JNI_EDETACHED;
2716 }
2717 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002718 return JNI_OK;
2719 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002720};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002721
Elliott Hughes88c5c352012-03-15 18:49:48 -07002722const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002723 NULL, // reserved0
2724 NULL, // reserved1
2725 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 JII::DestroyJavaVM,
2727 JII::AttachCurrentThread,
2728 JII::DetachCurrentThread,
2729 JII::GetEnv,
2730 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002731};
2732
Elliott Hughesa0957642011-09-02 14:27:33 -07002733JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002734 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002735 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002736 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002737 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002738 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002739 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002740 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002741 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002742 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002743 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002744 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002745 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002746 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002747 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002748 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002749 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002750 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002751 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002752}
2753
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002754JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002755 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002756}
2757
Elliott Hughes88c5c352012-03-15 18:49:48 -07002758void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2759 check_jni = enabled;
2760 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002761}
2762
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002763void JavaVMExt::DumpReferenceTables() {
2764 {
2765 MutexLock mu(globals_lock);
2766 globals.Dump();
2767 }
2768 {
2769 MutexLock mu(weak_globals_lock);
2770 weak_globals.Dump();
2771 }
2772 {
2773 MutexLock mu(pins_lock);
2774 pin_table.Dump();
2775 }
2776}
2777
Elliott Hughes75770752011-08-24 17:52:38 -07002778bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2779 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002780
2781 // See if we've already loaded this library. If we have, and the class loader
2782 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002783 // TODO: for better results we should canonicalize the pathname (or even compare
2784 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002785 SharedLibrary* library;
2786 {
2787 // TODO: move the locking (and more of this logic) into Libraries.
2788 MutexLock mu(libraries_lock);
2789 library = libraries->Get(path);
2790 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002791 if (library != NULL) {
2792 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002793 // The library will be associated with class_loader. The JNI
2794 // spec says we can't load the same library into more than one
2795 // class loader.
2796 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2797 "ClassLoader %p; can't open in ClassLoader %p",
2798 path.c_str(), library->GetClassLoader(), class_loader);
2799 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002800 return false;
2801 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002802 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2803 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002805 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2806 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 return false;
2808 }
2809 return true;
2810 }
2811
2812 // Open the shared library. Because we're using a full path, the system
2813 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2814 // resolve this library's dependencies though.)
2815
2816 // Failures here are expected when java.library.path has several entries
2817 // and we have to hunt for the lib.
2818
2819 // The current version of the dynamic linker prints detailed information
2820 // about dlopen() failures. Some things to check if the message is
2821 // cryptic:
2822 // - make sure the library exists on the device
2823 // - verify that the right path is being opened (the debug log message
2824 // above can help with that)
2825 // - check to see if the library is valid (e.g. not zero bytes long)
2826 // - check config/prelink-linux-arm.map to ensure that the library
2827 // is listed and is not being overrun by the previous entry (if
2828 // loading suddenly stops working on a prelinked library, this is
2829 // a good one to check)
2830 // - write a trivial app that calls sleep() then dlopen(), attach
2831 // to it with "strace -p <pid>" while it sleeps, and watch for
2832 // attempts to open nonexistent dependent shared libs
2833
2834 // TODO: automate some of these checks!
2835
2836 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002837 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002838 // the GC to ignore us.
2839 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002840 void* handle = NULL;
2841 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002842 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002843 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002844 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002845
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002846 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002847
2848 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002849 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 return false;
2851 }
2852
2853 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002854 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002855 // TODO: move the locking (and more of this logic) into Libraries.
2856 MutexLock mu(libraries_lock);
2857 library = libraries->Get(path);
2858 if (library != NULL) {
2859 LOG(INFO) << "WOW: we lost a race to add shared library: "
2860 << "\"" << path << "\" ClassLoader=" << class_loader;
2861 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002862 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 library = new SharedLibrary(path, handle, class_loader);
2864 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002865 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002866
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002867 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002868
2869 bool result = true;
2870 void* sym = dlsym(handle, "JNI_OnLoad");
2871 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002872 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002873 } else {
2874 // Call JNI_OnLoad. We have to override the current class
2875 // loader, which will always be "null" since the stuff at the
2876 // top of the stack is around Runtime.loadLibrary(). (See
2877 // the comments in the JNI FindClass function.)
2878 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2879 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002880 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002881 self->SetClassLoaderOverride(class_loader);
2882
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002883 int version = 0;
2884 {
2885 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002886 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002887 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002888 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002889
Brian Carlstromaded5f72011-10-07 17:15:04 -07002890 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002891
2892 if (version != JNI_VERSION_1_2 &&
2893 version != JNI_VERSION_1_4 &&
2894 version != JNI_VERSION_1_6) {
2895 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2896 << "bad version: " << version;
2897 // It's unwise to call dlclose() here, but we can mark it
2898 // as bad and ensure that future load attempts will fail.
2899 // We don't know how far JNI_OnLoad got, so there could
2900 // be some partially-initialized stuff accessible through
2901 // newly-registered native method calls. We could try to
2902 // unregister them, but that doesn't seem worthwhile.
2903 result = false;
2904 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002905 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2906 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002907 }
2908 }
2909
2910 library->SetResult(result);
2911 return result;
2912}
2913
2914void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2915 CHECK(m->IsNative());
2916
2917 Class* c = m->GetDeclaringClass();
2918
2919 // If this is a static method, it could be called before the class
2920 // has been initialized.
2921 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002922 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002923 return NULL;
2924 }
2925 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002926 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002927 }
2928
Brian Carlstrom16192862011-09-12 17:50:06 -07002929 std::string detail;
2930 void* native_method;
2931 {
2932 MutexLock mu(libraries_lock);
2933 native_method = libraries->FindNativeMethod(m, detail);
2934 }
2935 // throwing can cause libraries_lock to be reacquired
2936 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002937 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002938 }
2939 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002940}
2941
Elliott Hughes410c0c82011-09-01 17:58:25 -07002942void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2943 {
2944 MutexLock mu(globals_lock);
2945 globals.VisitRoots(visitor, arg);
2946 }
2947 {
2948 MutexLock mu(pins_lock);
2949 pin_table.VisitRoots(visitor, arg);
2950 }
2951 // The weak_globals table is visited by the GC itself (because it mutates the table).
2952}
2953
Elliott Hughes844f9a02012-01-24 20:19:58 -08002954jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2955 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2956 if (c.get() == NULL) {
2957 return NULL;
2958 }
2959 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2960}
2961
Ian Rogersdf20fe02011-07-20 20:34:16 -07002962} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002963
2964std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2965 switch (rhs) {
2966 case JNIInvalidRefType:
2967 os << "JNIInvalidRefType";
2968 return os;
2969 case JNILocalRefType:
2970 os << "JNILocalRefType";
2971 return os;
2972 case JNIGlobalRefType:
2973 os << "JNIGlobalRefType";
2974 return os;
2975 case JNIWeakGlobalRefType:
2976 os << "JNIWeakGlobalRefType";
2977 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002978 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002979 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002980 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002981 }
2982}