blob: db72a9938ba9979e4a76ab6fde80fd1095299079 [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>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070027#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "class_loader.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070029#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070030#include "jni.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070031#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070037#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070038#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070039#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070040#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070041#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070042
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070043namespace art {
44
Elliott Hughes2ced6a52011-10-16 18:44:48 -070045static const size_t kMonitorsInitial = 32; // Arbitrary.
46static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
47
48static const size_t kLocalsInitial = 64; // Arbitrary.
49static const size_t kLocalsMax = 512; // Arbitrary sanity check.
50
51static const size_t kPinTableInitial = 16; // Arbitrary.
52static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
53
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070054static size_t gGlobalsInitial = 512; // Arbitrary.
55static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070056
57static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
58static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
59
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
61 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070062 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
63 if (c.get() == NULL) {
64 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
65 }
66 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
67 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
68 }
69}
70
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070071void SetJniGlobalsMax(size_t max) {
72 if (max != 0) {
73 gGlobalsMax = max;
74 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
75 }
76}
Ian Rogersdf20fe02011-07-20 20:34:16 -070077
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070080 if (obj == NULL) {
81 return NULL;
82 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070084 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070085 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070086 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
87 return reinterpret_cast<jweak>(ref);
88}
89
Mathieu Chartier66f19252012-09-18 08:57:04 -070090static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070092 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070093 const DexFile::TypeList* params = mh.GetParameterTypeList();
94 if (params == NULL) {
95 return; // No arguments so nothing to check.
96 }
97 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070098 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070099 for (uint32_t i = 0; i < num_params; i++) {
100 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
101 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
102 if (param_type == NULL) {
103 Thread* self = Thread::Current();
104 CHECK(self->IsExceptionPending());
105 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
106 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
107 << self->GetException()->Dump();
108 self->ClearException();
109 ++error_count;
110 } else if (!param_type->IsPrimitive()) {
111 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700112 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700114 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
115 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
116 ++error_count;
117 }
118 }
119 }
120 if (error_count > 0) {
121 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
122 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700123 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700124 }
125}
Elliott Hughesb264f082012-04-06 17:10:10 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700128 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700131 CheckMethodArguments(method, args);
132 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700133 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700135 return result;
136}
137
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
139 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700142 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700143 MethodHelper mh(method);
144 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 arg_array.BuildArgArray(soa, args);
146 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700147}
148
Mathieu Chartier66f19252012-09-18 08:57:04 -0700149static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700151 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700152}
153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
155 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700158 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700159 MethodHelper mh(method);
160 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 arg_array.BuildArgArray(soa, args);
162 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700163}
164
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
166 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700169 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 MethodHelper mh(method);
171 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 arg_array.BuildArgArray(soa, args);
173 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700174}
175
Elliott Hughes6b436852011-08-12 10:16:44 -0700176// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
177// separated with slashes but aren't wrapped with "L;" like regular descriptors
178// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
179// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
180// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700181static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700182 std::string result;
183 // Add the missing "L;" if necessary.
184 if (name[0] == '[') {
185 result = name;
186 } else {
187 result += 'L';
188 result += name;
189 result += ';';
190 }
191 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700192 if (result.find('.') != std::string::npos) {
193 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
194 << "\"" << name << "\"";
195 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700196 }
197 return result;
198}
199
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
201 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800204 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700205}
206
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
208 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700210 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700211 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700212 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700213 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700214
Mathieu Chartier66f19252012-09-18 08:57:04 -0700215 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700216 if (is_static) {
217 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700218 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700219 method = c->FindVirtualMethod(name, sig);
220 if (method == NULL) {
221 // No virtual method matching the signature. Search declared
222 // private methods and constructors.
223 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700224 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700225 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700226
Elliott Hughescdf53122011-08-19 15:46:09 -0700227 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700229 return NULL;
230 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700231
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700233}
234
Ian Rogersef28b142012-11-30 14:22:18 -0800235static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800237 AbstractMethod* method = soa.Self()->GetCurrentMethod();
238 if (method == NULL ||
239 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
240 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700241 }
242 return method->GetDeclaringClass()->GetClassLoader();
243}
244
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
246 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700249 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700250 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700251 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700252
253 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700254 Class* field_type;
255 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
256 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800257 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700259 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700261 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 if (field_type == NULL) {
263 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 DCHECK(soa.Self()->IsExceptionPending());
265 soa.Self()->ClearException();
266 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700267 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800268 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 return NULL;
270 }
271 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800272 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700276 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700278 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800279 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700280 return NULL;
281 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700283}
284
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700288 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700289 vm->pin_table.Add(array);
290}
291
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700295 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700296 vm->pin_table.Remove(array);
297}
298
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
300 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700302 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700304 "%s offset=%d length=%d %s.length=%d",
305 type.c_str(), start, length, identifier, array->GetLength());
306}
Ian Rogers0571d352011-11-03 19:51:38 -0700307
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
309 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700312 "offset=%d length=%d string.length()=%d", start, length, array_length);
313}
Elliott Hughes814e4032011-08-23 12:07:56 -0700314
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 // Turn the const char* into a java.lang.String.
318 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
319 if (msg != NULL && s.get() == NULL) {
320 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700321 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700322
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 // Choose an appropriate constructor and set up the arguments.
324 jvalue args[2];
325 const char* signature;
326 if (msg == NULL && cause == NULL) {
327 signature = "()V";
328 } else if (msg != NULL && cause == NULL) {
329 signature = "(Ljava/lang/String;)V";
330 args[0].l = s.get();
331 } else if (msg == NULL && cause != NULL) {
332 signature = "(Ljava/lang/Throwable;)V";
333 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700334 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
336 args[0].l = s.get();
337 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700338 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
340 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800341 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 LOG(ERROR) << "No <init>" << signature << " in "
343 << PrettyClass(soa.Decode<Class*>(exception_class));
344 return JNI_ERR;
345 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700346
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
348 if (exception.get() == NULL) {
349 return JNI_ERR;
350 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700351
Ian Rogersef28b142012-11-30 14:22:18 -0800352 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700354
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700356}
357
Elliott Hughes462c9442012-03-23 18:47:50 -0700358static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700359 if (vm == NULL || p_env == NULL) {
360 return JNI_ERR;
361 }
362
Elliott Hughes462c9442012-03-23 18:47:50 -0700363 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700364 Thread* self = Thread::Current();
365 if (self != NULL) {
366 *p_env = self->GetJniEnv();
367 return JNI_OK;
368 }
369
370 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
371
372 // No threads allowed in zygote mode.
373 if (runtime->IsZygote()) {
374 LOG(ERROR) << "Attempt to attach a thread in the zygote";
375 return JNI_ERR;
376 }
377
Elliott Hughes462c9442012-03-23 18:47:50 -0700378 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
379 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700380 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700381 if (args != NULL) {
382 CHECK_GE(args->version, JNI_VERSION_1_2);
383 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700384 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700385 }
Elliott Hughes75770752011-08-24 17:52:38 -0700386
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800387 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700388 *p_env = NULL;
389 return JNI_ERR;
390 } else {
391 *p_env = Thread::Current()->GetJniEnv();
392 return JNI_OK;
393 }
Elliott Hughes75770752011-08-24 17:52:38 -0700394}
395
Elliott Hughes79082e32011-08-25 12:07:32 -0700396class SharedLibrary {
397 public:
398 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
399 : path_(path),
400 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700401 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700402 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700403 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700404 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700405 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700406 }
407
Elliott Hughes79082e32011-08-25 12:07:32 -0700408 Object* GetClassLoader() {
409 return class_loader_;
410 }
411
412 std::string GetPath() {
413 return path_;
414 }
415
416 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700417 * Check the result of an earlier call to JNI_OnLoad on this library.
418 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700419 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 bool CheckOnLoadResult()
421 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700423 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
425 bool okay;
426 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700427 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700428
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700429 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
430 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
431 // caller can continue.
432 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
433 okay = true;
434 } else {
435 while (jni_on_load_result_ == kPending) {
436 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700437 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700439
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 okay = (jni_on_load_result_ == kOkay);
441 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
442 << (okay ? "succeeded" : "failed") << "]";
443 }
444 }
445 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700446 return okay;
447 }
448
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700449 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700450 Thread* self = Thread::Current();
451 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700452
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700454 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700455
456 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700457 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 }
459
460 void* FindSymbol(const std::string& symbol_name) {
461 return dlsym(handle_, symbol_name.c_str());
462 }
463
464 private:
465 enum JNI_OnLoadState {
466 kPending,
467 kFailed,
468 kOkay,
469 };
470
471 // Path to library "/system/lib/libjni.so".
472 std::string path_;
473
474 // The void* returned by dlopen(3).
475 void* handle_;
476
477 // The ClassLoader this library is associated with.
478 Object* class_loader_;
479
480 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700483 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700485 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700487 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700488};
489
Elliott Hughes79082e32011-08-25 12:07:32 -0700490// This exists mainly to keep implementation details out of the header file.
491class Libraries {
492 public:
493 Libraries() {
494 }
495
496 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700497 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 }
499
Elliott Hughesae80b492012-04-24 10:43:17 -0700500 void Dump(std::ostream& os) const {
501 bool first = true;
502 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
503 if (!first) {
504 os << ' ';
505 }
506 first = false;
507 os << it->first;
508 }
509 }
510
511 size_t size() const {
512 return libraries_.size();
513 }
514
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700516 It it = libraries_.find(path);
517 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700521 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700522 }
523
524 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700525 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700527 std::string jni_short_name(JniShortName(m));
528 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700529 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
531 SharedLibrary* library = it->second;
532 if (library->GetClassLoader() != declaring_class_loader) {
533 // We only search libraries loaded by the appropriate ClassLoader.
534 continue;
535 }
536 // Try the short name then the long name...
537 void* fn = library->FindSymbol(jni_short_name);
538 if (fn == NULL) {
539 fn = library->FindSymbol(jni_long_name);
540 }
541 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800542 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
543 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 return fn;
545 }
546 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700548 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700549 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 return NULL;
552 }
553
554 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700555 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700556
Elliott Hughesa0e18062012-04-13 15:59:59 -0700557 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700558};
559
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
561 jvalue* args) {
562 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700563 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700564 MethodHelper mh(method);
565 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 arg_array.BuildArgArray(soa, args);
567 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700568}
569
Mathieu Chartier66f19252012-09-18 08:57:04 -0700570JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800574}
575
Elliott Hughescdf53122011-08-19 15:46:09 -0700576class JNI {
577 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700578 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700579 return JNI_VERSION_1_6;
580 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700581
Ian Rogers25e8b912012-09-07 11:31:36 -0700582 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700583 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700584 return NULL;
585 }
586
Elliott Hughescdf53122011-08-19 15:46:09 -0700587 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700589 Runtime* runtime = Runtime::Current();
590 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700592 Class* c = NULL;
593 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800594 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800595 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700596 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800597 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700598 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700600 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700601
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700604 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700606 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 ScopedObjectAccess soa(env);
610 Field* field = soa.Decode<Field*>(java_field);
611 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700616 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 ScopedObjectAccess soa(env);
622 Field* field = soa.DecodeField(fid);
623 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughes37f7a402011-08-22 18:56:01 -0700626 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 ScopedObjectAccess soa(env);
628 Object* o = soa.Decode<Object*>(java_object);
629 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700630 }
631
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700632 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700633 ScopedObjectAccess soa(env);
634 Class* c = soa.Decode<Class*>(java_class);
635 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 ScopedObjectAccess soa(env);
640 Class* c1 = soa.Decode<Class*>(java_class1);
641 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700642 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700644
Elliott Hughese84278b2012-03-22 10:06:53 -0700645 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700647 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700649 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 return JNI_TRUE;
651 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 Object* obj = soa.Decode<Object*>(jobj);
653 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700654 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700655 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 ScopedObjectAccess soa(env);
660 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 if (exception == NULL) {
662 return JNI_ERR;
663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 return JNI_OK;
666 }
667
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700668 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700669 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 }
671
672 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700673 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
676 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700677 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 }
679
680 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700682
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700684 Throwable* original_exception = self->GetException();
685 self->ClearException();
686
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700688 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
689 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
690 if (mid == NULL) {
691 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700692 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 } else {
694 env->CallVoidMethod(exception.get(), mid);
695 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700696 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 << " thrown while calling printStackTrace";
698 self->ClearException();
699 }
700 }
701
702 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 ScopedObjectAccess soa(env);
707 Object* exception = soa.Self()->GetException();
708 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 }
710
Ian Rogers25e8b912012-09-07 11:31:36 -0700711 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 LOG(FATAL) << "JNI FatalError called: " << msg;
713 }
714
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700715 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800716 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700717 return JNI_ERR;
718 }
Ian Rogersef28b142012-11-30 14:22:18 -0800719 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 return JNI_OK;
721 }
722
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700723 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
725 Object* survivor = soa.Decode<Object*>(java_survivor);
726 soa.Env()->PopFrame();
727 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
729
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700730 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800731 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 }
733
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 if (obj == NULL) {
736 return NULL;
737 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700738 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700742 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 return reinterpret_cast<jobject>(ref);
745 }
746
747 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 if (obj == NULL) {
749 return;
750 }
Ian Rogersef28b142012-11-30 14:22:18 -0800751 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800753 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
754 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700755
756 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
757 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
758 << "failed to find entry";
759 }
760 }
761
762 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 ScopedObjectAccess soa(env);
764 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
766
767 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 if (obj == NULL) {
769 return;
770 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700771 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700774 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775
776 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
777 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
778 << "failed to find entry";
779 }
780 }
781
782 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 if (obj == NULL) {
784 return NULL;
785 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700786 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700788
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 uint32_t cookie = soa.Env()->local_ref_cookie;
790 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return reinterpret_cast<jobject>(ref);
792 }
793
794 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 if (obj == NULL) {
796 return;
797 }
Ian Rogersef28b142012-11-30 14:22:18 -0800798 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700799
Ian Rogersef28b142012-11-30 14:22:18 -0800800 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 if (!locals.Remove(cookie, obj)) {
802 // Attempting to delete a local reference that is not in the
803 // topmost local reference frame is a no-op. DeleteLocalRef returns
804 // void and doesn't throw any exceptions, but we should probably
805 // complain about it so the user will notice that things aren't
806 // going quite the way they expect.
807 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
808 << "failed to find entry";
809 }
810 }
811
812 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700814 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700817 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
819 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700820 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700821 return NULL;
822 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700823 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 }
825
Elliott Hughese84278b2012-03-22 10:06:53 -0700826 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700828 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700829 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 va_end(args);
831 return result;
832 }
833
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(env);
836 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700837 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700838 return NULL;
839 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700841 if (result == NULL) {
842 return NULL;
843 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700847 return local_result;
848 } else {
849 return NULL;
850 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 }
852
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 ScopedObjectAccess soa(env);
855 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700856 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700857 return NULL;
858 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700859 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700860 if (result == NULL) {
861 return NULL;
862 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700866 return local_result;
867 } else {
868 return NULL;
869 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
874 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
877 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
879 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 va_list ap;
884 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 ScopedObjectAccess soa(env);
893 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
894 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
899 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
900 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_list ap;
905 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700906 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700909 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913 ScopedObjectAccess soa(env);
914 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_list ap;
925 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700928 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
933 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 ScopedObjectAccess soa(env);
938 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 va_list ap;
943 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700944 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700947 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
957 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700963 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700966 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
976 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700985 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(env);
995 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_list ap;
1000 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001001 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001004 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
1014 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001020 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001023 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
1028 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
1033 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_list ap;
1038 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001039 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001042 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001058 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001059 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 ScopedObjectAccess soa(env);
1065 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
1070 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001073 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001076 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1078 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 va_end(ap);
1080 return local_result;
1081 }
1082
1083 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001084 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1087 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
1090 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001091 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
1093 JValue result(InvokeWithJValues(soa, obj, mid, args));
1094 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
1097 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001098 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
1107 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001108 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
1113 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001114 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
1116 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001119 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001122 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001125 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
1128 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001129 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
1134 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001135 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001140 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001146 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
1149 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001150 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
1155 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001156 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001161 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001164 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001167 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 ScopedObjectAccess soa(env);
1173 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001177 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 ScopedObjectAccess soa(env);
1179 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001182 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001185 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001188 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
1194 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001198 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 ScopedObjectAccess soa(env);
1200 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001203 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001206 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001209 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
1215 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001219 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
1221 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001227 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001230 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
1233 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(env);
1236 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
1242 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001248 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001251 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
1254 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 ScopedObjectAccess soa(env);
1257 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001261 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env);
1263 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001269 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_end(ap);
1272 }
1273
1274 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env);
1277 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001281 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001286 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001290
1291
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001292 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001296
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001297 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 ScopedObjectAccess soa(env);
1299 Object* o = soa.Decode<Object*>(obj);
1300 Field* f = soa.DecodeField(fid);
1301 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001303
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001304 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
1306 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001307 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 ScopedObjectAccess soa(env);
1312 Object* o = soa.Decode<Object*>(java_object);
1313 Object* v = soa.Decode<Object*>(java_value);
1314 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001315 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001318 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 ScopedObjectAccess soa(env);
1320 Object* v = soa.Decode<Object*>(java_value);
1321 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001322 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001325#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env); \
1327 Object* o = soa.Decode<Object*>(instance); \
1328 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001329 return f->fn(o)
1330
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001331#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1332 ScopedObjectAccess soa(env); \
1333 Field* f = soa.DecodeField(fid); \
1334 return f->fn(f->GetDeclaringClass())
1335
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001336#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 ScopedObjectAccess soa(env); \
1338 Object* o = soa.Decode<Object*>(instance); \
1339 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340 f->fn(o, value)
1341
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001342#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1343 ScopedObjectAccess soa(env); \
1344 Field* f = soa.DecodeField(fid); \
1345 f->fn(f->GetDeclaringClass(), value)
1346
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1348 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1352 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1356 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1360 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1364 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1368 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1372 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1376 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001380 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001384 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001388 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001392 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001396 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001400 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 }
1402
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001403 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001404 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001408 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 }
1410
1411 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1412 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1413 }
1414
1415 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1416 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1417 }
1418
1419 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1420 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1421 }
1422
1423 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1424 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1425 }
1426
1427 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1428 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1429 }
1430
1431 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1432 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1433 }
1434
1435 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1436 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1437 }
1438
1439 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1440 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1441 }
1442
1443 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001444 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 }
1446
1447 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001448 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 }
1450
1451 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001452 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 }
1454
1455 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001456 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 }
1458
1459 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001460 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 }
1462
1463 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001464 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 }
1466
1467 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001468 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 }
1470
1471 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001472 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001477 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001478 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1480 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 va_end(ap);
1482 return local_result;
1483 }
1484
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001486 ScopedObjectAccess soa(env);
1487 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1488 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
1493 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1494 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001499 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001500 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001503 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001506 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 ScopedObjectAccess soa(env);
1508 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001511 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001512 ScopedObjectAccess soa(env);
1513 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001518 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001519 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001520 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001522 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001525 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 ScopedObjectAccess soa(env);
1527 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
1532 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001537 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001538 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001541 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545 ScopedObjectAccess soa(env);
1546 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 ScopedObjectAccess soa(env);
1551 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001557 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001560 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001564 ScopedObjectAccess soa(env);
1565 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001569 ScopedObjectAccess soa(env);
1570 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001576 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001579 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
1584 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 ScopedObjectAccess soa(env);
1589 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001595 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001596 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001598 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
1603 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 ScopedObjectAccess soa(env);
1608 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001614 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001615 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001617 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 ScopedObjectAccess soa(env);
1622 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 ScopedObjectAccess soa(env);
1627 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001633 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001634 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001636 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 ScopedObjectAccess soa(env);
1641 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
1646 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001652 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_end(ap);
1655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 ScopedObjectAccess soa(env);
1659 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
1664 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes814e4032011-08-23 12:07:56 -07001667 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001669 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
1673 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 if (utf == NULL) {
1675 return NULL;
1676 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001678 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes814e4032011-08-23 12:07:56 -07001682 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
1684 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001685 }
1686
1687 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
1689 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001690 }
1691
Elliott Hughesb465ab02011-08-24 11:21:21 -07001692 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 ScopedObjectAccess soa(env);
1694 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001695 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001697 } else {
1698 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1699 memcpy(buf, chars + start, length * sizeof(jchar));
1700 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001701 }
1702
Elliott Hughesb465ab02011-08-24 11:21:21 -07001703 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001704 ScopedObjectAccess soa(env);
1705 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001706 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001708 } else {
1709 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1710 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1711 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001712 }
1713
Elliott Hughes75770752011-08-24 17:52:38 -07001714 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
1716 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001717 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001718 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001719 if (is_copy != NULL) {
1720 *is_copy = JNI_FALSE;
1721 }
1722 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
1727 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes75770752011-08-24 17:52:38 -07001730 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001731 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes75770752011-08-24 17:52:38 -07001734 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001736 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes75770752011-08-24 17:52:38 -07001739 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001740 if (java_string == NULL) {
1741 return NULL;
1742 }
1743 if (is_copy != NULL) {
1744 *is_copy = JNI_TRUE;
1745 }
Ian Rogersef28b142012-11-30 14:22:18 -08001746 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001748 size_t byte_count = s->GetUtfLength();
1749 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001750 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001751 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1752 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1753 bytes[byte_count] = '\0';
1754 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001755 }
1756
Elliott Hughes75770752011-08-24 17:52:38 -07001757 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001758 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001759 }
1760
Elliott Hughesbd935992011-08-22 11:59:34 -07001761 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 ScopedObjectAccess soa(env);
1763 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001764 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001765 Array* array = obj->AsArray();
1766 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes814e4032011-08-23 12:07:56 -07001769 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
1771 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1772 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
1775 static void SetObjectArrayElement(JNIEnv* env,
1776 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
1778 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1779 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 array->Set(index, value);
1781 }
1782
1783 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
1785 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
1788 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
1793 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ScopedObjectAccess soa(env);
1795 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
1798 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
1800 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
1803 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ScopedObjectAccess soa(env);
1805 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
1808 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001809 ScopedObjectAccess soa(env);
1810 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
1813 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 ScopedObjectAccess soa(env);
1815 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 CHECK_GE(length, 0); // TODO: ReportJniError
1821
1822 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 std::string descriptor;
1825 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001826 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001827
1828 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001829 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1830 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 return NULL;
1832 }
1833
Elliott Hughes75770752011-08-24 17:52:38 -07001834 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001836 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001839 for (jsize i = 0; i < length; ++i) {
1840 result->Set(i, initial_object);
1841 }
1842 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
1846 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
1848 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Ian Rogersa15e67d2012-02-28 13:51:55 -08001851 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
1853 Array* array = soa.Decode<Array*>(java_array);
1854 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001855 if (is_copy != NULL) {
1856 *is_copy = JNI_FALSE;
1857 }
1858 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001862 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
1877 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ScopedObjectAccess soa(env);
1882 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes75770752011-08-24 17:52:38 -07001885 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
1887 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
1892 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
1897 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
1902 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001905 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001906 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001910 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001914 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001917 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001918 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001921 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001922 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001926 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001929 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001930 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001933 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001934 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
1939 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
1944 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ScopedObjectAccess soa(env);
1949 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes814e4032011-08-23 12:07:56 -07001952 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
1954 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
1959 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 ScopedObjectAccess soa(env);
1964 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes814e4032011-08-23 12:07:56 -07001967 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
1969 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes814e4032011-08-23 12:07:56 -07001972 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
1974 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes814e4032011-08-23 12:07:56 -07001977 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
1979 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes814e4032011-08-23 12:07:56 -07001982 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
1984 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 ScopedObjectAccess soa(env);
1989 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 ScopedObjectAccess soa(env);
1994 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes814e4032011-08-23 12:07:56 -07001997 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ScopedObjectAccess soa(env);
1999 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes814e4032011-08-23 12:07:56 -07002002 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002003 ScopedObjectAccess soa(env);
2004 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes814e4032011-08-23 12:07:56 -07002007 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
2009 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002013 ScopedObjectAccess soa(env);
2014 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes5174fe62011-08-23 15:12:35 -07002017 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002018 ScopedObjectAccess soa(env);
2019 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002020
Elliott Hughes5174fe62011-08-23 15:12:35 -07002021 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 const char* name = methods[i].name;
2023 const char* sig = methods[i].signature;
2024
2025 if (*sig == '!') {
2026 // TODO: fast jni. it's too noisy to log all these.
2027 ++sig;
2028 }
2029
Mathieu Chartier66f19252012-09-18 08:57:04 -07002030 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002031 if (m == NULL) {
2032 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002034 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002035 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002038 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002039 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 return JNI_ERR;
2042 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002043
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002044 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002045
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002046 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048 return JNI_OK;
2049 }
2050
Elliott Hughes5174fe62011-08-23 15:12:35 -07002051 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002052 ScopedObjectAccess soa(env);
2053 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002054
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002055 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002056
2057 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002058 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002059 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002061 }
2062 }
2063 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002064 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002065 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002066 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002067 }
2068 }
2069
2070 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2074 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2075 ScopedObjectAccess soa(env);
2076 Object* o = soa.Decode<Object*>(java_object);
2077 o->MonitorEnter(soa.Self());
2078 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002079 return JNI_ERR;
2080 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002082 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 static jint MonitorExit(JNIEnv* env, jobject java_object)
2086 UNLOCK_FUNCTION(monitor_lock_) {
2087 ScopedObjectAccess soa(env);
2088 Object* o = soa.Decode<Object*>(java_object);
2089 o->MonitorExit(soa.Self());
2090 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002091 return JNI_ERR;
2092 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002093 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002094 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
2097 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 Runtime* runtime = Runtime::Current();
2099 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002100 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 } else {
2102 *vm = NULL;
2103 }
2104 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2105 }
2106
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002108 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002109 CHECK(address != NULL); // TODO: ReportJniError
2110 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002111
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002113 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2114 CHECK_LE(capacity, 0xffffffff);
2115 jint address_arg = reinterpret_cast<jint>(address);
2116 jint capacity_arg = static_cast<jint>(capacity);
2117
Elliott Hugheseac76672012-05-24 21:56:51 -07002118 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2119 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2120 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002121 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughesb465ab02011-08-24 11:21:21 -07002124 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002125 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughesb465ab02011-08-24 11:21:21 -07002128 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002129 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughesb465ab02011-08-24 11:21:21 -07002132 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes75770752011-08-24 17:52:38 -07002133 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002134
2135 // Do we definitely know what kind of reference this is?
2136 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2137 IndirectRefKind kind = GetIndirectRefKind(ref);
2138 switch (kind) {
2139 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002140 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002141 return JNILocalRefType;
2142 }
2143 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002144 case kGlobal:
2145 return JNIGlobalRefType;
2146 case kWeakGlobal:
2147 return JNIWeakGlobalRefType;
2148 case kSirtOrInvalid:
2149 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002150 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002151 return JNILocalRefType;
2152 }
2153
Ian Rogersef28b142012-11-30 14:22:18 -08002154 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002155 return JNIInvalidRefType;
2156 }
2157
Elliott Hughesb465ab02011-08-24 11:21:21 -07002158 // If we're handing out direct pointers, check whether it's a direct pointer
2159 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002160 {
2161 ScopedObjectAccess soa(env);
2162 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2163 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2164 return JNILocalRefType;
2165 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002166 }
2167 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002168 return JNIInvalidRefType;
2169 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002170 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2171 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002173
2174 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002175 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2176 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 // TODO: we should try to expand the table if necessary.
2178 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2179 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2180 return JNI_ERR;
2181 }
2182 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002183 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2185 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002186 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 soa.Self()->ThrowOutOfMemoryError(caller);
2188 }
2189 return okay ? JNI_OK : JNI_ERR;
2190 }
2191
2192 template<typename JniT, typename ArtT>
2193 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 CHECK_GE(length, 0); // TODO: ReportJniError
Ian Rogers50b35e22012-10-04 10:09:15 -07002196 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 return soa.AddLocalReference<JniT>(result);
2198 }
2199
2200 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2201 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2202 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2205 PinPrimitiveArray(soa, array);
2206 if (is_copy != NULL) {
2207 *is_copy = JNI_FALSE;
2208 }
2209 return array->GetData();
2210 }
2211
2212 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002213 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002214 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002215 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002216 Array* array = soa.Decode<Array*>(java_array);
2217 UnpinPrimitiveArray(soa, array);
2218 }
2219 }
2220
2221 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2222 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2223 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2226 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2227 ThrowAIOOBE(soa, array, start, length, "src");
2228 } else {
2229 JavaT* data = array->GetData();
2230 memcpy(buf, data + start, length * sizeof(JavaT));
2231 }
2232 }
2233
2234 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2235 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2236 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002238 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2239 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2240 ThrowAIOOBE(soa, array, start, length, "dst");
2241 } else {
2242 JavaT* data = array->GetData();
2243 memcpy(data + start, buf, length * sizeof(JavaT));
2244 }
2245 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002246};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002247
Elliott Hughes88c5c352012-03-15 18:49:48 -07002248const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002249 NULL, // reserved0.
2250 NULL, // reserved1.
2251 NULL, // reserved2.
2252 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 JNI::GetVersion,
2254 JNI::DefineClass,
2255 JNI::FindClass,
2256 JNI::FromReflectedMethod,
2257 JNI::FromReflectedField,
2258 JNI::ToReflectedMethod,
2259 JNI::GetSuperclass,
2260 JNI::IsAssignableFrom,
2261 JNI::ToReflectedField,
2262 JNI::Throw,
2263 JNI::ThrowNew,
2264 JNI::ExceptionOccurred,
2265 JNI::ExceptionDescribe,
2266 JNI::ExceptionClear,
2267 JNI::FatalError,
2268 JNI::PushLocalFrame,
2269 JNI::PopLocalFrame,
2270 JNI::NewGlobalRef,
2271 JNI::DeleteGlobalRef,
2272 JNI::DeleteLocalRef,
2273 JNI::IsSameObject,
2274 JNI::NewLocalRef,
2275 JNI::EnsureLocalCapacity,
2276 JNI::AllocObject,
2277 JNI::NewObject,
2278 JNI::NewObjectV,
2279 JNI::NewObjectA,
2280 JNI::GetObjectClass,
2281 JNI::IsInstanceOf,
2282 JNI::GetMethodID,
2283 JNI::CallObjectMethod,
2284 JNI::CallObjectMethodV,
2285 JNI::CallObjectMethodA,
2286 JNI::CallBooleanMethod,
2287 JNI::CallBooleanMethodV,
2288 JNI::CallBooleanMethodA,
2289 JNI::CallByteMethod,
2290 JNI::CallByteMethodV,
2291 JNI::CallByteMethodA,
2292 JNI::CallCharMethod,
2293 JNI::CallCharMethodV,
2294 JNI::CallCharMethodA,
2295 JNI::CallShortMethod,
2296 JNI::CallShortMethodV,
2297 JNI::CallShortMethodA,
2298 JNI::CallIntMethod,
2299 JNI::CallIntMethodV,
2300 JNI::CallIntMethodA,
2301 JNI::CallLongMethod,
2302 JNI::CallLongMethodV,
2303 JNI::CallLongMethodA,
2304 JNI::CallFloatMethod,
2305 JNI::CallFloatMethodV,
2306 JNI::CallFloatMethodA,
2307 JNI::CallDoubleMethod,
2308 JNI::CallDoubleMethodV,
2309 JNI::CallDoubleMethodA,
2310 JNI::CallVoidMethod,
2311 JNI::CallVoidMethodV,
2312 JNI::CallVoidMethodA,
2313 JNI::CallNonvirtualObjectMethod,
2314 JNI::CallNonvirtualObjectMethodV,
2315 JNI::CallNonvirtualObjectMethodA,
2316 JNI::CallNonvirtualBooleanMethod,
2317 JNI::CallNonvirtualBooleanMethodV,
2318 JNI::CallNonvirtualBooleanMethodA,
2319 JNI::CallNonvirtualByteMethod,
2320 JNI::CallNonvirtualByteMethodV,
2321 JNI::CallNonvirtualByteMethodA,
2322 JNI::CallNonvirtualCharMethod,
2323 JNI::CallNonvirtualCharMethodV,
2324 JNI::CallNonvirtualCharMethodA,
2325 JNI::CallNonvirtualShortMethod,
2326 JNI::CallNonvirtualShortMethodV,
2327 JNI::CallNonvirtualShortMethodA,
2328 JNI::CallNonvirtualIntMethod,
2329 JNI::CallNonvirtualIntMethodV,
2330 JNI::CallNonvirtualIntMethodA,
2331 JNI::CallNonvirtualLongMethod,
2332 JNI::CallNonvirtualLongMethodV,
2333 JNI::CallNonvirtualLongMethodA,
2334 JNI::CallNonvirtualFloatMethod,
2335 JNI::CallNonvirtualFloatMethodV,
2336 JNI::CallNonvirtualFloatMethodA,
2337 JNI::CallNonvirtualDoubleMethod,
2338 JNI::CallNonvirtualDoubleMethodV,
2339 JNI::CallNonvirtualDoubleMethodA,
2340 JNI::CallNonvirtualVoidMethod,
2341 JNI::CallNonvirtualVoidMethodV,
2342 JNI::CallNonvirtualVoidMethodA,
2343 JNI::GetFieldID,
2344 JNI::GetObjectField,
2345 JNI::GetBooleanField,
2346 JNI::GetByteField,
2347 JNI::GetCharField,
2348 JNI::GetShortField,
2349 JNI::GetIntField,
2350 JNI::GetLongField,
2351 JNI::GetFloatField,
2352 JNI::GetDoubleField,
2353 JNI::SetObjectField,
2354 JNI::SetBooleanField,
2355 JNI::SetByteField,
2356 JNI::SetCharField,
2357 JNI::SetShortField,
2358 JNI::SetIntField,
2359 JNI::SetLongField,
2360 JNI::SetFloatField,
2361 JNI::SetDoubleField,
2362 JNI::GetStaticMethodID,
2363 JNI::CallStaticObjectMethod,
2364 JNI::CallStaticObjectMethodV,
2365 JNI::CallStaticObjectMethodA,
2366 JNI::CallStaticBooleanMethod,
2367 JNI::CallStaticBooleanMethodV,
2368 JNI::CallStaticBooleanMethodA,
2369 JNI::CallStaticByteMethod,
2370 JNI::CallStaticByteMethodV,
2371 JNI::CallStaticByteMethodA,
2372 JNI::CallStaticCharMethod,
2373 JNI::CallStaticCharMethodV,
2374 JNI::CallStaticCharMethodA,
2375 JNI::CallStaticShortMethod,
2376 JNI::CallStaticShortMethodV,
2377 JNI::CallStaticShortMethodA,
2378 JNI::CallStaticIntMethod,
2379 JNI::CallStaticIntMethodV,
2380 JNI::CallStaticIntMethodA,
2381 JNI::CallStaticLongMethod,
2382 JNI::CallStaticLongMethodV,
2383 JNI::CallStaticLongMethodA,
2384 JNI::CallStaticFloatMethod,
2385 JNI::CallStaticFloatMethodV,
2386 JNI::CallStaticFloatMethodA,
2387 JNI::CallStaticDoubleMethod,
2388 JNI::CallStaticDoubleMethodV,
2389 JNI::CallStaticDoubleMethodA,
2390 JNI::CallStaticVoidMethod,
2391 JNI::CallStaticVoidMethodV,
2392 JNI::CallStaticVoidMethodA,
2393 JNI::GetStaticFieldID,
2394 JNI::GetStaticObjectField,
2395 JNI::GetStaticBooleanField,
2396 JNI::GetStaticByteField,
2397 JNI::GetStaticCharField,
2398 JNI::GetStaticShortField,
2399 JNI::GetStaticIntField,
2400 JNI::GetStaticLongField,
2401 JNI::GetStaticFloatField,
2402 JNI::GetStaticDoubleField,
2403 JNI::SetStaticObjectField,
2404 JNI::SetStaticBooleanField,
2405 JNI::SetStaticByteField,
2406 JNI::SetStaticCharField,
2407 JNI::SetStaticShortField,
2408 JNI::SetStaticIntField,
2409 JNI::SetStaticLongField,
2410 JNI::SetStaticFloatField,
2411 JNI::SetStaticDoubleField,
2412 JNI::NewString,
2413 JNI::GetStringLength,
2414 JNI::GetStringChars,
2415 JNI::ReleaseStringChars,
2416 JNI::NewStringUTF,
2417 JNI::GetStringUTFLength,
2418 JNI::GetStringUTFChars,
2419 JNI::ReleaseStringUTFChars,
2420 JNI::GetArrayLength,
2421 JNI::NewObjectArray,
2422 JNI::GetObjectArrayElement,
2423 JNI::SetObjectArrayElement,
2424 JNI::NewBooleanArray,
2425 JNI::NewByteArray,
2426 JNI::NewCharArray,
2427 JNI::NewShortArray,
2428 JNI::NewIntArray,
2429 JNI::NewLongArray,
2430 JNI::NewFloatArray,
2431 JNI::NewDoubleArray,
2432 JNI::GetBooleanArrayElements,
2433 JNI::GetByteArrayElements,
2434 JNI::GetCharArrayElements,
2435 JNI::GetShortArrayElements,
2436 JNI::GetIntArrayElements,
2437 JNI::GetLongArrayElements,
2438 JNI::GetFloatArrayElements,
2439 JNI::GetDoubleArrayElements,
2440 JNI::ReleaseBooleanArrayElements,
2441 JNI::ReleaseByteArrayElements,
2442 JNI::ReleaseCharArrayElements,
2443 JNI::ReleaseShortArrayElements,
2444 JNI::ReleaseIntArrayElements,
2445 JNI::ReleaseLongArrayElements,
2446 JNI::ReleaseFloatArrayElements,
2447 JNI::ReleaseDoubleArrayElements,
2448 JNI::GetBooleanArrayRegion,
2449 JNI::GetByteArrayRegion,
2450 JNI::GetCharArrayRegion,
2451 JNI::GetShortArrayRegion,
2452 JNI::GetIntArrayRegion,
2453 JNI::GetLongArrayRegion,
2454 JNI::GetFloatArrayRegion,
2455 JNI::GetDoubleArrayRegion,
2456 JNI::SetBooleanArrayRegion,
2457 JNI::SetByteArrayRegion,
2458 JNI::SetCharArrayRegion,
2459 JNI::SetShortArrayRegion,
2460 JNI::SetIntArrayRegion,
2461 JNI::SetLongArrayRegion,
2462 JNI::SetFloatArrayRegion,
2463 JNI::SetDoubleArrayRegion,
2464 JNI::RegisterNatives,
2465 JNI::UnregisterNatives,
2466 JNI::MonitorEnter,
2467 JNI::MonitorExit,
2468 JNI::GetJavaVM,
2469 JNI::GetStringRegion,
2470 JNI::GetStringUTFRegion,
2471 JNI::GetPrimitiveArrayCritical,
2472 JNI::ReleasePrimitiveArrayCritical,
2473 JNI::GetStringCritical,
2474 JNI::ReleaseStringCritical,
2475 JNI::NewWeakGlobalRef,
2476 JNI::DeleteWeakGlobalRef,
2477 JNI::ExceptionCheck,
2478 JNI::NewDirectByteBuffer,
2479 JNI::GetDirectBufferAddress,
2480 JNI::GetDirectBufferCapacity,
2481 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002482};
2483
Elliott Hughes75770752011-08-24 17:52:38 -07002484JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002485 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002486 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002487 local_ref_cookie(IRT_FIRST_SEGMENT),
2488 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002489 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002490 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002491 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002492 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002493 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002494 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002495 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002496 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2497 // errors will ensue.
2498 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2499 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002500}
2501
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002502JNIEnvExt::~JNIEnvExt() {
2503}
2504
Elliott Hughes88c5c352012-03-15 18:49:48 -07002505void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2506 check_jni = enabled;
2507 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002508}
2509
Elliott Hughes73e66f72012-05-09 09:34:45 -07002510void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2511 locals.Dump(os);
2512 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002513}
2514
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002515void JNIEnvExt::PushFrame(int /*capacity*/) {
2516 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002517 stacked_local_ref_cookies.push_back(local_ref_cookie);
2518 local_ref_cookie = locals.GetSegmentState();
2519}
2520
2521void JNIEnvExt::PopFrame() {
2522 locals.SetSegmentState(local_ref_cookie);
2523 local_ref_cookie = stacked_local_ref_cookies.back();
2524 stacked_local_ref_cookies.pop_back();
2525}
2526
Carl Shapiroea4dca82011-08-01 13:45:38 -07002527// JNI Invocation interface.
2528
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002529extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2530 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2531 if (args->version < JNI_VERSION_1_2) {
2532 return JNI_EVERSION;
2533 }
2534 Runtime::Options options;
2535 for (int i = 0; i < args->nOptions; ++i) {
2536 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002537 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002538 }
2539 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002540 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002541 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002544 runtime->Start();
2545 *p_env = Thread::Current()->GetJniEnv();
2546 *p_vm = runtime->GetJavaVM();
2547 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002548}
2549
Elliott Hughesf2682d52011-08-15 16:37:04 -07002550extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002551 Runtime* runtime = Runtime::Current();
2552 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002553 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002554 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002555 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002556 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
2558 return JNI_OK;
2559}
2560
2561// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002562extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 return JNI_ERR;
2564}
2565
Elliott Hughescdf53122011-08-19 15:46:09 -07002566class JII {
2567 public:
2568 static jint DestroyJavaVM(JavaVM* vm) {
2569 if (vm == NULL) {
2570 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002571 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002572 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2573 delete raw_vm->runtime;
2574 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576
Elliott Hughescdf53122011-08-19 15:46:09 -07002577 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002578 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002579 }
2580
2581 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002582 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002583 }
2584
2585 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002586 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002587 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002589 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2590 Runtime* runtime = raw_vm->runtime;
2591 runtime->DetachCurrentThread();
2592 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002593 }
2594
2595 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2596 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2597 return JNI_EVERSION;
2598 }
2599 if (vm == NULL || env == NULL) {
2600 return JNI_ERR;
2601 }
2602 Thread* thread = Thread::Current();
2603 if (thread == NULL) {
2604 *env = NULL;
2605 return JNI_EDETACHED;
2606 }
2607 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 return JNI_OK;
2609 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002610};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611
Elliott Hughes88c5c352012-03-15 18:49:48 -07002612const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002613 NULL, // reserved0
2614 NULL, // reserved1
2615 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002616 JII::DestroyJavaVM,
2617 JII::AttachCurrentThread,
2618 JII::DetachCurrentThread,
2619 JII::GetEnv,
2620 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002621};
2622
Elliott Hughesa0957642011-09-02 14:27:33 -07002623JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002624 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002625 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002626 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002627 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002628 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002629 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002630 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002631 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002632 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002633 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002634 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002635 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002636 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002637 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002638 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002639 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002640 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002641 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002642 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002643}
2644
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002645JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002646 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002647}
2648
Elliott Hughes88c5c352012-03-15 18:49:48 -07002649void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2650 check_jni = enabled;
2651 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002652}
2653
Elliott Hughesae80b492012-04-24 10:43:17 -07002654void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2655 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2656 if (force_copy) {
2657 os << " (with forcecopy)";
2658 }
2659 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002660 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002661 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002662 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002663 os << "; pins=" << pin_table.Size();
2664 }
2665 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002666 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002667 os << "; globals=" << globals.Capacity();
2668 }
2669 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002670 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002671 if (weak_globals.Capacity() > 0) {
2672 os << " (plus " << weak_globals.Capacity() << " weak)";
2673 }
2674 }
2675 os << '\n';
2676
2677 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002678 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002679 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2680 }
2681}
2682
Elliott Hughes73e66f72012-05-09 09:34:45 -07002683void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002684 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002685 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002686 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002687 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002688 }
2689 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002690 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002691 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002692 }
2693 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002694 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002695 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002696 }
2697}
2698
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002699bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2700 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002701 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002702
2703 // See if we've already loaded this library. If we have, and the class loader
2704 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002705 // TODO: for better results we should canonicalize the pathname (or even compare
2706 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002707 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002708 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002709 {
2710 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002711 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002712 library = libraries->Get(path);
2713 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002714 if (library != NULL) {
2715 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002716 // The library will be associated with class_loader. The JNI
2717 // spec says we can't load the same library into more than one
2718 // class loader.
2719 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2720 "ClassLoader %p; can't open in ClassLoader %p",
2721 path.c_str(), library->GetClassLoader(), class_loader);
2722 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 return false;
2724 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002725 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2726 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002727 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002728 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2729 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002730 return false;
2731 }
2732 return true;
2733 }
2734
2735 // Open the shared library. Because we're using a full path, the system
2736 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2737 // resolve this library's dependencies though.)
2738
2739 // Failures here are expected when java.library.path has several entries
2740 // and we have to hunt for the lib.
2741
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002742 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2743 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2744 // dlopen) becomes zero from dlclose.
2745
Elliott Hughescdf53122011-08-19 15:46:09 -07002746 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002747 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002748 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2749 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2750 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002751
Elliott Hughes84b2f142012-09-27 09:16:28 -07002752 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002753
2754 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002755 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002756 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 return false;
2758 }
2759
2760 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002761 // TODO: move the locking (and more of this logic) into Libraries.
2762 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002763 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002764 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002765 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002766 if (library == NULL) { // We won race to get libraries_lock
2767 library = new SharedLibrary(path, handle, class_loader);
2768 libraries->Put(path, library);
2769 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002771 }
2772 if (!created_library) {
2773 LOG(INFO) << "WOW: we lost a race to add shared library: "
2774 << "\"" << path << "\" ClassLoader=" << class_loader;
2775 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002776 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002777
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002778 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002779
2780 bool result = true;
2781 void* sym = dlsym(handle, "JNI_OnLoad");
2782 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002783 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002784 } else {
2785 // Call JNI_OnLoad. We have to override the current class
2786 // loader, which will always be "null" since the stuff at the
2787 // top of the stack is around Runtime.loadLibrary(). (See
2788 // the comments in the JNI FindClass function.)
2789 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2790 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002791 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 self->SetClassLoaderOverride(class_loader);
2793
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002794 int version = 0;
2795 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002796 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002797 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002798 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002799 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002800
Brian Carlstromaded5f72011-10-07 17:15:04 -07002801 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002802
2803 if (version != JNI_VERSION_1_2 &&
2804 version != JNI_VERSION_1_4 &&
2805 version != JNI_VERSION_1_6) {
2806 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2807 << "bad version: " << version;
2808 // It's unwise to call dlclose() here, but we can mark it
2809 // as bad and ensure that future load attempts will fail.
2810 // We don't know how far JNI_OnLoad got, so there could
2811 // be some partially-initialized stuff accessible through
2812 // newly-registered native method calls. We could try to
2813 // unregister them, but that doesn't seem worthwhile.
2814 result = false;
2815 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002816 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2817 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002818 }
2819 }
2820
2821 library->SetResult(result);
2822 return result;
2823}
2824
Mathieu Chartier66f19252012-09-18 08:57:04 -07002825void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002826 CHECK(m->IsNative());
2827
2828 Class* c = m->GetDeclaringClass();
2829
2830 // If this is a static method, it could be called before the class
2831 // has been initialized.
2832 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002833 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002834 return NULL;
2835 }
2836 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002837 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002838 }
2839
Brian Carlstrom16192862011-09-12 17:50:06 -07002840 std::string detail;
2841 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002842 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002843 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002844 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002845 native_method = libraries->FindNativeMethod(m, detail);
2846 }
2847 // throwing can cause libraries_lock to be reacquired
2848 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002849 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002850 }
2851 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002852}
2853
Elliott Hughes410c0c82011-09-01 17:58:25 -07002854void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002855 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002856 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002857 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002858 globals.VisitRoots(visitor, arg);
2859 }
2860 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002861 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002862 pin_table.VisitRoots(visitor, arg);
2863 }
2864 // The weak_globals table is visited by the GC itself (because it mutates the table).
2865}
2866
Ian Rogersdf20fe02011-07-20 20:34:16 -07002867} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002868
2869std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2870 switch (rhs) {
2871 case JNIInvalidRefType:
2872 os << "JNIInvalidRefType";
2873 return os;
2874 case JNILocalRefType:
2875 os << "JNILocalRefType";
2876 return os;
2877 case JNIGlobalRefType:
2878 os << "JNIGlobalRefType";
2879 return os;
2880 case JNIWeakGlobalRefType:
2881 os << "JNIWeakGlobalRefType";
2882 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002883 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002884 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002885 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002886 }
2887}