blob: 84b5144a3a80276613bf65647c2146273374d1ba [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 Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "class_loader.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070031#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070033#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080034#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070035#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070038#include "ScopedLocalRef.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
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070060void SetJniGlobalsMax(size_t max) {
61 if (max != 0) {
62 gGlobalsMax = max;
63 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
64 }
65}
Ian Rogersdf20fe02011-07-20 20:34:16 -070066
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070069 if (obj == NULL) {
70 return NULL;
71 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070073 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070074 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070075 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
76 return reinterpret_cast<jweak>(ref);
77}
78
Mathieu Chartier66f19252012-09-18 08:57:04 -070079static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070081 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070082 const DexFile::TypeList* params = mh.GetParameterTypeList();
83 if (params == NULL) {
84 return; // No arguments so nothing to check.
85 }
86 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070087 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070088 for (uint32_t i = 0; i < num_params; i++) {
89 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
90 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
91 if (param_type == NULL) {
92 Thread* self = Thread::Current();
93 CHECK(self->IsExceptionPending());
94 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
95 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
96 << self->GetException()->Dump();
97 self->ClearException();
98 ++error_count;
99 } else if (!param_type->IsPrimitive()) {
100 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700101 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700103 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
104 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
105 ++error_count;
106 }
107 }
108 }
109 if (error_count > 0) {
110 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
111 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700112 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 }
114}
Elliott Hughesb264f082012-04-06 17:10:10 -0700115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700117 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700120 CheckMethodArguments(method, args);
121 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700122 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700124 return result;
125}
126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
128 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700131 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700132 MethodHelper mh(method);
133 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 arg_array.BuildArgArray(soa, args);
135 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700136}
137
Mathieu Chartier66f19252012-09-18 08:57:04 -0700138static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700140 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700141}
142
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
144 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700147 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700148 MethodHelper mh(method);
149 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 arg_array.BuildArgArray(soa, args);
151 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700152}
153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
155 jobject obj, jmethodID mid, va_list 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());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700163}
164
Elliott Hughes6b436852011-08-12 10:16:44 -0700165// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
166// separated with slashes but aren't wrapped with "L;" like regular descriptors
167// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
168// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
169// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700170static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700171 std::string result;
172 // Add the missing "L;" if necessary.
173 if (name[0] == '[') {
174 result = name;
175 } else {
176 result += 'L';
177 result += name;
178 result += ';';
179 }
180 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700181 if (result.find('.') != std::string::npos) {
182 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
183 << "\"" << name << "\"";
184 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700185 }
186 return result;
187}
188
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
190 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800193 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700194}
195
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
197 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700200 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700201 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700202 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700203
Mathieu Chartier66f19252012-09-18 08:57:04 -0700204 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700205 if (is_static) {
206 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700207 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700208 method = c->FindVirtualMethod(name, sig);
209 if (method == NULL) {
210 // No virtual method matching the signature. Search declared
211 // private methods and constructors.
212 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700213 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700214 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700215
Elliott Hughescdf53122011-08-19 15:46:09 -0700216 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700218 return NULL;
219 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700220
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700222}
223
Ian Rogersef28b142012-11-30 14:22:18 -0800224static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800226 AbstractMethod* method = soa.Self()->GetCurrentMethod();
227 if (method == NULL ||
228 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
229 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700230 }
231 return method->GetDeclaringClass()->GetClassLoader();
232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
235 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
242 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 Class* field_type;
244 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
245 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800246 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700248 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700250 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700251 if (field_type == NULL) {
252 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 DCHECK(soa.Self()->IsExceptionPending());
254 soa.Self()->ClearException();
255 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700256 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800257 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 return NULL;
259 }
260 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700265 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700267 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800268 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700269 return NULL;
270 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700272}
273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700277 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700278 vm->pin_table.Add(array);
279}
280
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700284 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700285 vm->pin_table.Remove(array);
286}
287
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
289 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700291 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700293 "%s offset=%d length=%d %s.length=%d",
294 type.c_str(), start, length, identifier, array->GetLength());
295}
Ian Rogers0571d352011-11-03 19:51:38 -0700296
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
298 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700301 "offset=%d length=%d string.length()=%d", start, length, array_length);
302}
Elliott Hughes814e4032011-08-23 12:07:56 -0700303
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700305 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 // Turn the const char* into a java.lang.String.
307 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
308 if (msg != NULL && s.get() == NULL) {
309 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700310 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700311
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 // Choose an appropriate constructor and set up the arguments.
313 jvalue args[2];
314 const char* signature;
315 if (msg == NULL && cause == NULL) {
316 signature = "()V";
317 } else if (msg != NULL && cause == NULL) {
318 signature = "(Ljava/lang/String;)V";
319 args[0].l = s.get();
320 } else if (msg == NULL && cause != NULL) {
321 signature = "(Ljava/lang/Throwable;)V";
322 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700323 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
325 args[0].l = s.get();
326 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700327 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
329 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800330 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 LOG(ERROR) << "No <init>" << signature << " in "
332 << PrettyClass(soa.Decode<Class*>(exception_class));
333 return JNI_ERR;
334 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700335
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
337 if (exception.get() == NULL) {
338 return JNI_ERR;
339 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700340
Ian Rogersef28b142012-11-30 14:22:18 -0800341 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700343
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700345}
346
Elliott Hughes462c9442012-03-23 18:47:50 -0700347static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700348 if (vm == NULL || p_env == NULL) {
349 return JNI_ERR;
350 }
351
Elliott Hughes462c9442012-03-23 18:47:50 -0700352 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700353 Thread* self = Thread::Current();
354 if (self != NULL) {
355 *p_env = self->GetJniEnv();
356 return JNI_OK;
357 }
358
359 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
360
361 // No threads allowed in zygote mode.
362 if (runtime->IsZygote()) {
363 LOG(ERROR) << "Attempt to attach a thread in the zygote";
364 return JNI_ERR;
365 }
366
Elliott Hughes462c9442012-03-23 18:47:50 -0700367 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
368 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700369 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700370 if (args != NULL) {
371 CHECK_GE(args->version, JNI_VERSION_1_2);
372 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700373 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700374 }
Elliott Hughes75770752011-08-24 17:52:38 -0700375
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800376 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700377 *p_env = NULL;
378 return JNI_ERR;
379 } else {
380 *p_env = Thread::Current()->GetJniEnv();
381 return JNI_OK;
382 }
Elliott Hughes75770752011-08-24 17:52:38 -0700383}
384
Elliott Hughes79082e32011-08-25 12:07:32 -0700385class SharedLibrary {
386 public:
387 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
388 : path_(path),
389 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700390 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700391 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700392 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700393 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700394 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700395 }
396
Elliott Hughes79082e32011-08-25 12:07:32 -0700397 Object* GetClassLoader() {
398 return class_loader_;
399 }
400
401 std::string GetPath() {
402 return path_;
403 }
404
405 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700406 * Check the result of an earlier call to JNI_OnLoad on this library.
407 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700408 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 bool CheckOnLoadResult()
410 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700412 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700413 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
414 bool okay;
415 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700416 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700417
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700418 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
419 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
420 // caller can continue.
421 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
422 okay = true;
423 } else {
424 while (jni_on_load_result_ == kPending) {
425 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700426 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700428
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700429 okay = (jni_on_load_result_ == kOkay);
430 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
431 << (okay ? "succeeded" : "failed") << "]";
432 }
433 }
434 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700435 return okay;
436 }
437
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700439 Thread* self = Thread::Current();
440 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700441
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700444
445 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700446 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 }
448
449 void* FindSymbol(const std::string& symbol_name) {
450 return dlsym(handle_, symbol_name.c_str());
451 }
452
453 private:
454 enum JNI_OnLoadState {
455 kPending,
456 kFailed,
457 kOkay,
458 };
459
460 // Path to library "/system/lib/libjni.so".
461 std::string path_;
462
463 // The void* returned by dlopen(3).
464 void* handle_;
465
466 // The ClassLoader this library is associated with.
467 Object* class_loader_;
468
469 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700472 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700473 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700474 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700476 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700477};
478
Elliott Hughes79082e32011-08-25 12:07:32 -0700479// This exists mainly to keep implementation details out of the header file.
480class Libraries {
481 public:
482 Libraries() {
483 }
484
485 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700486 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 }
488
Elliott Hughesae80b492012-04-24 10:43:17 -0700489 void Dump(std::ostream& os) const {
490 bool first = true;
491 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
492 if (!first) {
493 os << ' ';
494 }
495 first = false;
496 os << it->first;
497 }
498 }
499
500 size_t size() const {
501 return libraries_.size();
502 }
503
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700505 It it = libraries_.find(path);
506 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700507 }
508
509 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700510 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 }
512
513 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700514 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 std::string jni_short_name(JniShortName(m));
517 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700518 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
520 SharedLibrary* library = it->second;
521 if (library->GetClassLoader() != declaring_class_loader) {
522 // We only search libraries loaded by the appropriate ClassLoader.
523 continue;
524 }
525 // Try the short name then the long name...
526 void* fn = library->FindSymbol(jni_short_name);
527 if (fn == NULL) {
528 fn = library->FindSymbol(jni_long_name);
529 }
530 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800531 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
532 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700533 return fn;
534 }
535 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700536 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700537 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700538 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700539 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700540 return NULL;
541 }
542
543 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700544 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700545
Elliott Hughesa0e18062012-04-13 15:59:59 -0700546 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700547};
548
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
550 jvalue* args) {
551 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700552 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700553 MethodHelper mh(method);
554 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 arg_array.BuildArgArray(soa, args);
556 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700557}
558
Mathieu Chartier66f19252012-09-18 08:57:04 -0700559JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800563}
564
Elliott Hughescdf53122011-08-19 15:46:09 -0700565class JNI {
566 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700567 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700568 return JNI_VERSION_1_6;
569 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700570
Ian Rogers25e8b912012-09-07 11:31:36 -0700571 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700572 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700573 return NULL;
574 }
575
Elliott Hughescdf53122011-08-19 15:46:09 -0700576 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700578 Runtime* runtime = Runtime::Current();
579 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700580 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700581 Class* c = NULL;
582 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800583 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800584 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700585 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800586 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700587 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700589 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700590
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700592 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700593 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700594 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700595 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700596
Elliott Hughescdf53122011-08-19 15:46:09 -0700597 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 ScopedObjectAccess soa(env);
599 Field* field = soa.Decode<Field*>(java_field);
600 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700605 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610 ScopedObjectAccess soa(env);
611 Field* field = soa.DecodeField(fid);
612 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700614
Elliott Hughes37f7a402011-08-22 18:56:01 -0700615 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 ScopedObjectAccess soa(env);
617 Object* o = soa.Decode<Object*>(java_object);
618 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700619 }
620
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700621 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
623 Class* c = soa.Decode<Class*>(java_class);
624 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700626
Elliott Hughes37f7a402011-08-22 18:56:01 -0700627 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 ScopedObjectAccess soa(env);
629 Class* c1 = soa.Decode<Class*>(java_class1);
630 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700631 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughese84278b2012-03-22 10:06:53 -0700634 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800636 if (java_class == NULL) {
637 JniAbortF("IsInstanceOf", "null class (second argument)");
638 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700639 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700640 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700641 return JNI_TRUE;
642 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 Object* obj = soa.Decode<Object*>(jobj);
644 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700645 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughes37f7a402011-08-22 18:56:01 -0700649 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 ScopedObjectAccess soa(env);
651 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 if (exception == NULL) {
653 return JNI_ERR;
654 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 return JNI_OK;
657 }
658
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700659 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700660 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 }
662
663 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700664 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 }
666
667 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700668 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 }
670
671 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700673
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 Throwable* original_exception = self->GetException();
676 self->ClearException();
677
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
680 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
681 if (mid == NULL) {
682 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700683 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700684 } else {
685 env->CallVoidMethod(exception.get(), mid);
686 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700687 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700688 << " thrown while calling printStackTrace";
689 self->ClearException();
690 }
691 }
692
693 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedObjectAccess soa(env);
698 Object* exception = soa.Self()->GetException();
699 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
701
Ian Rogers25e8b912012-09-07 11:31:36 -0700702 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 LOG(FATAL) << "JNI FatalError called: " << msg;
704 }
705
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700706 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800707 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700708 return JNI_ERR;
709 }
Ian Rogersef28b142012-11-30 14:22:18 -0800710 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 return JNI_OK;
712 }
713
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700714 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(env);
716 Object* survivor = soa.Decode<Object*>(java_survivor);
717 soa.Env()->PopFrame();
718 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
720
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700721 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800722 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 }
724
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 if (obj == NULL) {
727 return NULL;
728 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700729 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700733 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 return reinterpret_cast<jobject>(ref);
736 }
737
738 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 if (obj == NULL) {
740 return;
741 }
Ian Rogersef28b142012-11-30 14:22:18 -0800742 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800744 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
745 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746
747 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
748 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
749 << "failed to find entry";
750 }
751 }
752
753 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
755 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
758 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 if (obj == NULL) {
760 return;
761 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700762 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700765 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766
767 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
768 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
769 << "failed to find entry";
770 }
771 }
772
773 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 if (obj == NULL) {
775 return NULL;
776 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700777 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700779
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 uint32_t cookie = soa.Env()->local_ref_cookie;
781 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 return reinterpret_cast<jobject>(ref);
783 }
784
785 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 if (obj == NULL) {
787 return;
788 }
Ian Rogersef28b142012-11-30 14:22:18 -0800789 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700790
Ian Rogersef28b142012-11-30 14:22:18 -0800791 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 if (!locals.Remove(cookie, obj)) {
793 // Attempting to delete a local reference that is not in the
794 // topmost local reference frame is a no-op. DeleteLocalRef returns
795 // void and doesn't throw any exceptions, but we should probably
796 // complain about it so the user will notice that things aren't
797 // going quite the way they expect.
798 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
799 << "failed to find entry";
800 }
801 }
802
803 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700805 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 }
807
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700808 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 ScopedObjectAccess soa(env);
810 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700811 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700812 return NULL;
813 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700814 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughese84278b2012-03-22 10:06:53 -0700817 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700820 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 va_end(args);
822 return result;
823 }
824
Elliott Hughes72025e52011-08-23 17:50:30 -0700825 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(env);
827 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700828 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700829 return NULL;
830 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700831 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700832 if (result == NULL) {
833 return NULL;
834 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700838 return local_result;
839 } else {
840 return NULL;
841 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700847 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700848 return NULL;
849 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700850 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700851 if (result == NULL) {
852 return NULL;
853 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700857 return local_result;
858 } else {
859 return NULL;
860 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(env);
865 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
868 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700869 ScopedObjectAccess soa(env);
870 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 }
872
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 va_list ap;
875 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700876 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883 ScopedObjectAccess soa(env);
884 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
885 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 }
887
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 ScopedObjectAccess soa(env);
890 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
891 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 va_list ap;
896 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700897 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700900 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 ScopedObjectAccess soa(env);
905 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 ScopedObjectAccess soa(env);
910 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_list ap;
916 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700919 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
929 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_list ap;
934 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700935 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700938 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 ScopedObjectAccess soa(env);
943 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
948 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 va_list ap;
953 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700954 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700957 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 ScopedObjectAccess soa(env);
962 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(env);
967 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 va_list ap;
973 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700976 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
981 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
986 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700992 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700995 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
1000 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_list ap;
1010 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001011 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001014 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
1019 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 ScopedObjectAccess soa(env);
1024 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001030 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001033 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
1043 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_list ap;
1048 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001049 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001050 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001064 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1069 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 va_end(ap);
1071 return local_result;
1072 }
1073
1074 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001075 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1078 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
1081 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001082 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 ScopedObjectAccess soa(env);
1084 JValue result(InvokeWithJValues(soa, obj, mid, args));
1085 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
1088 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001089 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001092 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001095 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
1098 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001099 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
1104 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001105 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001110 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001113 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001116 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
1119 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001120 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 ScopedObjectAccess soa(env);
1122 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
1125 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001126 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 ScopedObjectAccess soa(env);
1128 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001137 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
1140 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001141 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
1146 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001147 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
1149 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001155 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001158 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
1161 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001162 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
1167 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001168 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
1170 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001173 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001176 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001179 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
1182 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001183 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
1185 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
1188 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001189 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
1191 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
1203 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001204 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
1206 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
1209 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001210 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001215 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001218 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001221 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001225 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
1227 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001231 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 ScopedObjectAccess soa(env);
1233 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001236 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001239 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001242 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001246 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
1248 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
1251 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001252 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 ScopedObjectAccess soa(env);
1254 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001257 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001260 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 }
1264
1265 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
1268 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001272 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
1274 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001277 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env);
1279 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001281
1282
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001283 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 ScopedObjectAccess soa(env);
1285 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001287
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001288 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
1290 Object* o = soa.Decode<Object*>(obj);
1291 Field* f = soa.DecodeField(fid);
1292 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001294
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001295 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 ScopedObjectAccess soa(env);
1297 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001298 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001301 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 ScopedObjectAccess soa(env);
1303 Object* o = soa.Decode<Object*>(java_object);
1304 Object* v = soa.Decode<Object*>(java_value);
1305 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001309 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 ScopedObjectAccess soa(env);
1311 Object* v = soa.Decode<Object*>(java_value);
1312 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001313 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001316#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 ScopedObjectAccess soa(env); \
1318 Object* o = soa.Decode<Object*>(instance); \
1319 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001320 return f->fn(o)
1321
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001322#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1323 ScopedObjectAccess soa(env); \
1324 Field* f = soa.DecodeField(fid); \
1325 return f->fn(f->GetDeclaringClass())
1326
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001327#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001328 ScopedObjectAccess soa(env); \
1329 Object* o = soa.Decode<Object*>(instance); \
1330 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001331 f->fn(o, value)
1332
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001333#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1334 ScopedObjectAccess soa(env); \
1335 Field* f = soa.DecodeField(fid); \
1336 f->fn(f->GetDeclaringClass(), value)
1337
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1339 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1343 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1347 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001350 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1351 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001354 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1355 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1359 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1363 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001366 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1367 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001370 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001371 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001374 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001375 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001379 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001382 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001383 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001386 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001387 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001390 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001391 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 }
1393
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001395 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 }
1397
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001398 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001399 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 }
1401
1402 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1403 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1404 }
1405
1406 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1407 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1408 }
1409
1410 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1411 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1412 }
1413
1414 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1415 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1416 }
1417
1418 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1419 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1420 }
1421
1422 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1423 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1424 }
1425
1426 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1427 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1428 }
1429
1430 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1431 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1432 }
1433
1434 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001435 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 }
1437
1438 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001439 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 }
1441
1442 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001443 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 }
1445
1446 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001447 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 }
1449
1450 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001451 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 }
1453
1454 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001455 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 }
1457
1458 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001459 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 }
1461
1462 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001463 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001466 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001468 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001469 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1471 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 va_end(ap);
1473 return local_result;
1474 }
1475
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env);
1478 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1479 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001482 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
1484 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1485 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001488 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001491 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001494 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env);
1499 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001502 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001503 ScopedObjectAccess soa(env);
1504 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001507 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001509 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001510 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001513 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
1518 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001521 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
1523 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001526 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001528 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001529 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001532 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 ScopedObjectAccess soa(env);
1537 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
1542 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001545 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001548 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001551 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
1556 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001560 ScopedObjectAccess soa(env);
1561 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001567 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001570 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001574 ScopedObjectAccess soa(env);
1575 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env);
1580 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001586 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001589 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env);
1594 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001598 ScopedObjectAccess soa(env);
1599 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001602 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001605 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001606 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001608 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001612 ScopedObjectAccess soa(env);
1613 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001617 ScopedObjectAccess soa(env);
1618 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001624 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001627 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 ScopedObjectAccess soa(env);
1632 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 ScopedObjectAccess soa(env);
1637 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001643 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_end(ap);
1646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
1650 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 ScopedObjectAccess soa(env);
1655 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes814e4032011-08-23 12:07:56 -07001658 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001660 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
1664 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 if (utf == NULL) {
1666 return NULL;
1667 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001669 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes814e4032011-08-23 12:07:56 -07001673 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 ScopedObjectAccess soa(env);
1675 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001676 }
1677
1678 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
1680 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001681 }
1682
Elliott Hughesb465ab02011-08-24 11:21:21 -07001683 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
1685 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001686 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001688 } else {
1689 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1690 memcpy(buf, chars + start, length * sizeof(jchar));
1691 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001692 }
1693
Elliott Hughesb465ab02011-08-24 11:21:21 -07001694 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001695 ScopedObjectAccess soa(env);
1696 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001697 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001699 } else {
1700 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1701 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1702 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001703 }
1704
Elliott Hughes75770752011-08-24 17:52:38 -07001705 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 ScopedObjectAccess soa(env);
1707 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001708 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001710 if (is_copy != NULL) {
1711 *is_copy = JNI_FALSE;
1712 }
1713 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001714 }
1715
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001716 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
1718 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes75770752011-08-24 17:52:38 -07001721 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001722 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes75770752011-08-24 17:52:38 -07001725 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001727 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes75770752011-08-24 17:52:38 -07001730 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001731 if (java_string == NULL) {
1732 return NULL;
1733 }
1734 if (is_copy != NULL) {
1735 *is_copy = JNI_TRUE;
1736 }
Ian Rogersef28b142012-11-30 14:22:18 -08001737 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001739 size_t byte_count = s->GetUtfLength();
1740 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001741 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001742 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1743 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1744 bytes[byte_count] = '\0';
1745 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001746 }
1747
Elliott Hughes75770752011-08-24 17:52:38 -07001748 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001749 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001750 }
1751
Elliott Hughesbd935992011-08-22 11:59:34 -07001752 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
1754 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001755 if (!obj->IsArrayInstance()) {
1756 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1757 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001758 Array* array = obj->AsArray();
1759 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes814e4032011-08-23 12:07:56 -07001762 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
1764 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1765 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
1768 static void SetObjectArrayElement(JNIEnv* env,
1769 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
1771 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1772 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 array->Set(index, value);
1774 }
1775
1776 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
1778 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
1781 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
1783 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
1786 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001787 ScopedObjectAccess soa(env);
1788 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
1791 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 ScopedObjectAccess soa(env);
1793 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
1796 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
1798 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
1801 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
1803 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
1806 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
1808 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
1811 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001812 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001813 if (length < 0) {
1814 JniAbortF("NewObjectArray", "negative array length: %d", length);
1815 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001816
1817 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 std::string descriptor;
1820 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001821 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822
1823 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001824 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1825 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 return NULL;
1827 }
1828
Elliott Hughes75770752011-08-24 17:52:38 -07001829 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001831 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001832 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001834 for (jsize i = 0; i < length; ++i) {
1835 result->Set(i, initial_object);
1836 }
1837 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
1841 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
1843 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Ian Rogersa15e67d2012-02-28 13:51:55 -08001846 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
1848 Array* array = soa.Decode<Array*>(java_array);
1849 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001850 if (is_copy != NULL) {
1851 *is_copy = JNI_FALSE;
1852 }
1853 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001857 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 }
1859
Elliott Hughes75770752011-08-24 17:52:38 -07001860 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 ScopedObjectAccess soa(env);
1862 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
1877 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ScopedObjectAccess soa(env);
1882 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes75770752011-08-24 17:52:38 -07001885 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
1887 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
1892 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
1897 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001900 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001901 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001904 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001905 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001908 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001909 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001912 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001913 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001916 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001917 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001920 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001921 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001924 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001925 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001928 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001929 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes814e4032011-08-23 12:07:56 -07001932 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
1934 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
1939 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
1944 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ScopedObjectAccess soa(env);
1949 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes814e4032011-08-23 12:07:56 -07001952 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
1954 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
1959 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 ScopedObjectAccess soa(env);
1964 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes814e4032011-08-23 12:07:56 -07001967 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
1969 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes814e4032011-08-23 12:07:56 -07001972 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
1974 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes814e4032011-08-23 12:07:56 -07001977 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
1979 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes814e4032011-08-23 12:07:56 -07001982 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
1984 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 ScopedObjectAccess soa(env);
1989 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 ScopedObjectAccess soa(env);
1994 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes814e4032011-08-23 12:07:56 -07001997 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ScopedObjectAccess soa(env);
1999 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes814e4032011-08-23 12:07:56 -07002002 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002003 ScopedObjectAccess soa(env);
2004 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes814e4032011-08-23 12:07:56 -07002007 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
2009 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes5174fe62011-08-23 15:12:35 -07002012 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002013 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2014 }
2015
2016 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, size_t method_count, bool return_errors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
2018 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002019
Elliott Hughesc8fece32013-01-02 11:27:23 -08002020 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 const char* name = methods[i].name;
2022 const char* sig = methods[i].signature;
2023
2024 if (*sig == '!') {
2025 // TODO: fast jni. it's too noisy to log all these.
2026 ++sig;
2027 }
2028
Mathieu Chartier66f19252012-09-18 08:57:04 -07002029 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002030 if (m == NULL) {
2031 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002033 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002034 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2035 << PrettyDescriptor(c) << "." << 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()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002039 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2040 << PrettyDescriptor(c) << "." << name << sig
2041 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 return JNI_ERR;
2044 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002045
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002046 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002047
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050 return JNI_OK;
2051 }
2052
Elliott Hughes5174fe62011-08-23 15:12:35 -07002053 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002054 ScopedObjectAccess soa(env);
2055 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002056
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002057 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002058
2059 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002060 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002061 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002063 }
2064 }
2065 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002066 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002067 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002069 }
2070 }
2071
2072 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2076 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2077 ScopedObjectAccess soa(env);
2078 Object* o = soa.Decode<Object*>(java_object);
2079 o->MonitorEnter(soa.Self());
2080 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002081 return JNI_ERR;
2082 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002084 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 static jint MonitorExit(JNIEnv* env, jobject java_object)
2088 UNLOCK_FUNCTION(monitor_lock_) {
2089 ScopedObjectAccess soa(env);
2090 Object* o = soa.Decode<Object*>(java_object);
2091 o->MonitorExit(soa.Self());
2092 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002093 return JNI_ERR;
2094 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002096 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
2099 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 Runtime* runtime = Runtime::Current();
2101 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002102 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 } else {
2104 *vm = NULL;
2105 }
2106 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2107 }
2108
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002110 if (capacity < 0) {
2111 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2112 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002113 if (address == NULL && capacity != 0) {
2114 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002115 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002116
Elliott Hughes96a98872012-12-19 14:21:15 -08002117 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002118 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2119 CHECK_LE(capacity, 0xffffffff);
2120 jint address_arg = reinterpret_cast<jint>(address);
2121 jint capacity_arg = static_cast<jint>(capacity);
2122
Elliott Hugheseac76672012-05-24 21:56:51 -07002123 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2124 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2125 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002126 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughesb465ab02011-08-24 11:21:21 -07002129 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002130 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughesb465ab02011-08-24 11:21:21 -07002133 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002134 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughesb465ab02011-08-24 11:21:21 -07002137 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002138 if (java_object == NULL) {
2139 JniAbortF("GetObjectRefType", "null object");
2140 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002141
2142 // Do we definitely know what kind of reference this is?
2143 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2144 IndirectRefKind kind = GetIndirectRefKind(ref);
2145 switch (kind) {
2146 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002147 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002148 return JNILocalRefType;
2149 }
2150 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002151 case kGlobal:
2152 return JNIGlobalRefType;
2153 case kWeakGlobal:
2154 return JNIWeakGlobalRefType;
2155 case kSirtOrInvalid:
2156 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002157 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002158 return JNILocalRefType;
2159 }
2160
Ian Rogersef28b142012-11-30 14:22:18 -08002161 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002162 return JNIInvalidRefType;
2163 }
2164
Elliott Hughesb465ab02011-08-24 11:21:21 -07002165 // If we're handing out direct pointers, check whether it's a direct pointer
2166 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002167 {
2168 ScopedObjectAccess soa(env);
2169 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2170 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2171 return JNILocalRefType;
2172 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002173 }
2174 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175 return JNIInvalidRefType;
2176 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002177 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2178 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180
2181 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002182 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2183 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 // TODO: we should try to expand the table if necessary.
2185 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2186 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2187 return JNI_ERR;
2188 }
2189 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002190 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2192 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002193 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 soa.Self()->ThrowOutOfMemoryError(caller);
2195 }
2196 return okay ? JNI_OK : JNI_ERR;
2197 }
2198
2199 template<typename JniT, typename ArtT>
2200 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002202 if (length < 0) {
2203 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2204 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002205 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 return soa.AddLocalReference<JniT>(result);
2207 }
2208
2209 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2210 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2211 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2214 PinPrimitiveArray(soa, array);
2215 if (is_copy != NULL) {
2216 *is_copy = JNI_FALSE;
2217 }
2218 return array->GetData();
2219 }
2220
2221 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002222 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002224 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 Array* array = soa.Decode<Array*>(java_array);
2226 UnpinPrimitiveArray(soa, array);
2227 }
2228 }
2229
2230 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2231 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2232 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2235 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2236 ThrowAIOOBE(soa, array, start, length, "src");
2237 } else {
2238 JavaT* data = array->GetData();
2239 memcpy(buf, data + start, length * sizeof(JavaT));
2240 }
2241 }
2242
2243 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2244 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2245 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2248 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2249 ThrowAIOOBE(soa, array, start, length, "dst");
2250 } else {
2251 JavaT* data = array->GetData();
2252 memcpy(data + start, buf, length * sizeof(JavaT));
2253 }
2254 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002255};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002256
Elliott Hughes88c5c352012-03-15 18:49:48 -07002257const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002258 NULL, // reserved0.
2259 NULL, // reserved1.
2260 NULL, // reserved2.
2261 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 JNI::GetVersion,
2263 JNI::DefineClass,
2264 JNI::FindClass,
2265 JNI::FromReflectedMethod,
2266 JNI::FromReflectedField,
2267 JNI::ToReflectedMethod,
2268 JNI::GetSuperclass,
2269 JNI::IsAssignableFrom,
2270 JNI::ToReflectedField,
2271 JNI::Throw,
2272 JNI::ThrowNew,
2273 JNI::ExceptionOccurred,
2274 JNI::ExceptionDescribe,
2275 JNI::ExceptionClear,
2276 JNI::FatalError,
2277 JNI::PushLocalFrame,
2278 JNI::PopLocalFrame,
2279 JNI::NewGlobalRef,
2280 JNI::DeleteGlobalRef,
2281 JNI::DeleteLocalRef,
2282 JNI::IsSameObject,
2283 JNI::NewLocalRef,
2284 JNI::EnsureLocalCapacity,
2285 JNI::AllocObject,
2286 JNI::NewObject,
2287 JNI::NewObjectV,
2288 JNI::NewObjectA,
2289 JNI::GetObjectClass,
2290 JNI::IsInstanceOf,
2291 JNI::GetMethodID,
2292 JNI::CallObjectMethod,
2293 JNI::CallObjectMethodV,
2294 JNI::CallObjectMethodA,
2295 JNI::CallBooleanMethod,
2296 JNI::CallBooleanMethodV,
2297 JNI::CallBooleanMethodA,
2298 JNI::CallByteMethod,
2299 JNI::CallByteMethodV,
2300 JNI::CallByteMethodA,
2301 JNI::CallCharMethod,
2302 JNI::CallCharMethodV,
2303 JNI::CallCharMethodA,
2304 JNI::CallShortMethod,
2305 JNI::CallShortMethodV,
2306 JNI::CallShortMethodA,
2307 JNI::CallIntMethod,
2308 JNI::CallIntMethodV,
2309 JNI::CallIntMethodA,
2310 JNI::CallLongMethod,
2311 JNI::CallLongMethodV,
2312 JNI::CallLongMethodA,
2313 JNI::CallFloatMethod,
2314 JNI::CallFloatMethodV,
2315 JNI::CallFloatMethodA,
2316 JNI::CallDoubleMethod,
2317 JNI::CallDoubleMethodV,
2318 JNI::CallDoubleMethodA,
2319 JNI::CallVoidMethod,
2320 JNI::CallVoidMethodV,
2321 JNI::CallVoidMethodA,
2322 JNI::CallNonvirtualObjectMethod,
2323 JNI::CallNonvirtualObjectMethodV,
2324 JNI::CallNonvirtualObjectMethodA,
2325 JNI::CallNonvirtualBooleanMethod,
2326 JNI::CallNonvirtualBooleanMethodV,
2327 JNI::CallNonvirtualBooleanMethodA,
2328 JNI::CallNonvirtualByteMethod,
2329 JNI::CallNonvirtualByteMethodV,
2330 JNI::CallNonvirtualByteMethodA,
2331 JNI::CallNonvirtualCharMethod,
2332 JNI::CallNonvirtualCharMethodV,
2333 JNI::CallNonvirtualCharMethodA,
2334 JNI::CallNonvirtualShortMethod,
2335 JNI::CallNonvirtualShortMethodV,
2336 JNI::CallNonvirtualShortMethodA,
2337 JNI::CallNonvirtualIntMethod,
2338 JNI::CallNonvirtualIntMethodV,
2339 JNI::CallNonvirtualIntMethodA,
2340 JNI::CallNonvirtualLongMethod,
2341 JNI::CallNonvirtualLongMethodV,
2342 JNI::CallNonvirtualLongMethodA,
2343 JNI::CallNonvirtualFloatMethod,
2344 JNI::CallNonvirtualFloatMethodV,
2345 JNI::CallNonvirtualFloatMethodA,
2346 JNI::CallNonvirtualDoubleMethod,
2347 JNI::CallNonvirtualDoubleMethodV,
2348 JNI::CallNonvirtualDoubleMethodA,
2349 JNI::CallNonvirtualVoidMethod,
2350 JNI::CallNonvirtualVoidMethodV,
2351 JNI::CallNonvirtualVoidMethodA,
2352 JNI::GetFieldID,
2353 JNI::GetObjectField,
2354 JNI::GetBooleanField,
2355 JNI::GetByteField,
2356 JNI::GetCharField,
2357 JNI::GetShortField,
2358 JNI::GetIntField,
2359 JNI::GetLongField,
2360 JNI::GetFloatField,
2361 JNI::GetDoubleField,
2362 JNI::SetObjectField,
2363 JNI::SetBooleanField,
2364 JNI::SetByteField,
2365 JNI::SetCharField,
2366 JNI::SetShortField,
2367 JNI::SetIntField,
2368 JNI::SetLongField,
2369 JNI::SetFloatField,
2370 JNI::SetDoubleField,
2371 JNI::GetStaticMethodID,
2372 JNI::CallStaticObjectMethod,
2373 JNI::CallStaticObjectMethodV,
2374 JNI::CallStaticObjectMethodA,
2375 JNI::CallStaticBooleanMethod,
2376 JNI::CallStaticBooleanMethodV,
2377 JNI::CallStaticBooleanMethodA,
2378 JNI::CallStaticByteMethod,
2379 JNI::CallStaticByteMethodV,
2380 JNI::CallStaticByteMethodA,
2381 JNI::CallStaticCharMethod,
2382 JNI::CallStaticCharMethodV,
2383 JNI::CallStaticCharMethodA,
2384 JNI::CallStaticShortMethod,
2385 JNI::CallStaticShortMethodV,
2386 JNI::CallStaticShortMethodA,
2387 JNI::CallStaticIntMethod,
2388 JNI::CallStaticIntMethodV,
2389 JNI::CallStaticIntMethodA,
2390 JNI::CallStaticLongMethod,
2391 JNI::CallStaticLongMethodV,
2392 JNI::CallStaticLongMethodA,
2393 JNI::CallStaticFloatMethod,
2394 JNI::CallStaticFloatMethodV,
2395 JNI::CallStaticFloatMethodA,
2396 JNI::CallStaticDoubleMethod,
2397 JNI::CallStaticDoubleMethodV,
2398 JNI::CallStaticDoubleMethodA,
2399 JNI::CallStaticVoidMethod,
2400 JNI::CallStaticVoidMethodV,
2401 JNI::CallStaticVoidMethodA,
2402 JNI::GetStaticFieldID,
2403 JNI::GetStaticObjectField,
2404 JNI::GetStaticBooleanField,
2405 JNI::GetStaticByteField,
2406 JNI::GetStaticCharField,
2407 JNI::GetStaticShortField,
2408 JNI::GetStaticIntField,
2409 JNI::GetStaticLongField,
2410 JNI::GetStaticFloatField,
2411 JNI::GetStaticDoubleField,
2412 JNI::SetStaticObjectField,
2413 JNI::SetStaticBooleanField,
2414 JNI::SetStaticByteField,
2415 JNI::SetStaticCharField,
2416 JNI::SetStaticShortField,
2417 JNI::SetStaticIntField,
2418 JNI::SetStaticLongField,
2419 JNI::SetStaticFloatField,
2420 JNI::SetStaticDoubleField,
2421 JNI::NewString,
2422 JNI::GetStringLength,
2423 JNI::GetStringChars,
2424 JNI::ReleaseStringChars,
2425 JNI::NewStringUTF,
2426 JNI::GetStringUTFLength,
2427 JNI::GetStringUTFChars,
2428 JNI::ReleaseStringUTFChars,
2429 JNI::GetArrayLength,
2430 JNI::NewObjectArray,
2431 JNI::GetObjectArrayElement,
2432 JNI::SetObjectArrayElement,
2433 JNI::NewBooleanArray,
2434 JNI::NewByteArray,
2435 JNI::NewCharArray,
2436 JNI::NewShortArray,
2437 JNI::NewIntArray,
2438 JNI::NewLongArray,
2439 JNI::NewFloatArray,
2440 JNI::NewDoubleArray,
2441 JNI::GetBooleanArrayElements,
2442 JNI::GetByteArrayElements,
2443 JNI::GetCharArrayElements,
2444 JNI::GetShortArrayElements,
2445 JNI::GetIntArrayElements,
2446 JNI::GetLongArrayElements,
2447 JNI::GetFloatArrayElements,
2448 JNI::GetDoubleArrayElements,
2449 JNI::ReleaseBooleanArrayElements,
2450 JNI::ReleaseByteArrayElements,
2451 JNI::ReleaseCharArrayElements,
2452 JNI::ReleaseShortArrayElements,
2453 JNI::ReleaseIntArrayElements,
2454 JNI::ReleaseLongArrayElements,
2455 JNI::ReleaseFloatArrayElements,
2456 JNI::ReleaseDoubleArrayElements,
2457 JNI::GetBooleanArrayRegion,
2458 JNI::GetByteArrayRegion,
2459 JNI::GetCharArrayRegion,
2460 JNI::GetShortArrayRegion,
2461 JNI::GetIntArrayRegion,
2462 JNI::GetLongArrayRegion,
2463 JNI::GetFloatArrayRegion,
2464 JNI::GetDoubleArrayRegion,
2465 JNI::SetBooleanArrayRegion,
2466 JNI::SetByteArrayRegion,
2467 JNI::SetCharArrayRegion,
2468 JNI::SetShortArrayRegion,
2469 JNI::SetIntArrayRegion,
2470 JNI::SetLongArrayRegion,
2471 JNI::SetFloatArrayRegion,
2472 JNI::SetDoubleArrayRegion,
2473 JNI::RegisterNatives,
2474 JNI::UnregisterNatives,
2475 JNI::MonitorEnter,
2476 JNI::MonitorExit,
2477 JNI::GetJavaVM,
2478 JNI::GetStringRegion,
2479 JNI::GetStringUTFRegion,
2480 JNI::GetPrimitiveArrayCritical,
2481 JNI::ReleasePrimitiveArrayCritical,
2482 JNI::GetStringCritical,
2483 JNI::ReleaseStringCritical,
2484 JNI::NewWeakGlobalRef,
2485 JNI::DeleteWeakGlobalRef,
2486 JNI::ExceptionCheck,
2487 JNI::NewDirectByteBuffer,
2488 JNI::GetDirectBufferAddress,
2489 JNI::GetDirectBufferCapacity,
2490 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002491};
2492
Elliott Hughes75770752011-08-24 17:52:38 -07002493JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002494 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002495 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002496 local_ref_cookie(IRT_FIRST_SEGMENT),
2497 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002498 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002499 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002500 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002501 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002502 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002503 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002504 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002505 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2506 // errors will ensue.
2507 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2508 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002509}
2510
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002511JNIEnvExt::~JNIEnvExt() {
2512}
2513
Elliott Hughes88c5c352012-03-15 18:49:48 -07002514void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2515 check_jni = enabled;
2516 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002517}
2518
Elliott Hughes73e66f72012-05-09 09:34:45 -07002519void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2520 locals.Dump(os);
2521 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002522}
2523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002524void JNIEnvExt::PushFrame(int /*capacity*/) {
2525 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002526 stacked_local_ref_cookies.push_back(local_ref_cookie);
2527 local_ref_cookie = locals.GetSegmentState();
2528}
2529
2530void JNIEnvExt::PopFrame() {
2531 locals.SetSegmentState(local_ref_cookie);
2532 local_ref_cookie = stacked_local_ref_cookies.back();
2533 stacked_local_ref_cookies.pop_back();
2534}
2535
Carl Shapiroea4dca82011-08-01 13:45:38 -07002536// JNI Invocation interface.
2537
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002538extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2539 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2540 if (args->version < JNI_VERSION_1_2) {
2541 return JNI_EVERSION;
2542 }
2543 Runtime::Options options;
2544 for (int i = 0; i < args->nOptions; ++i) {
2545 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002546 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 }
2548 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002549 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002550 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002551 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002552 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002553 runtime->Start();
2554 *p_env = Thread::Current()->GetJniEnv();
2555 *p_vm = runtime->GetJavaVM();
2556 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557}
2558
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 Runtime* runtime = Runtime::Current();
2561 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002562 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002564 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002565 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566 }
2567 return JNI_OK;
2568}
2569
2570// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002571extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002572 return JNI_ERR;
2573}
2574
Elliott Hughescdf53122011-08-19 15:46:09 -07002575class JII {
2576 public:
2577 static jint DestroyJavaVM(JavaVM* vm) {
2578 if (vm == NULL) {
2579 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002580 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002581 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2582 delete raw_vm->runtime;
2583 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002585
Elliott Hughescdf53122011-08-19 15:46:09 -07002586 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002587 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 }
2589
2590 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002591 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002592 }
2593
2594 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002595 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002597 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002598 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2599 Runtime* runtime = raw_vm->runtime;
2600 runtime->DetachCurrentThread();
2601 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002602 }
2603
2604 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2605 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2606 return JNI_EVERSION;
2607 }
2608 if (vm == NULL || env == NULL) {
2609 return JNI_ERR;
2610 }
2611 Thread* thread = Thread::Current();
2612 if (thread == NULL) {
2613 *env = NULL;
2614 return JNI_EDETACHED;
2615 }
2616 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 return JNI_OK;
2618 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002619};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002620
Elliott Hughes88c5c352012-03-15 18:49:48 -07002621const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002622 NULL, // reserved0
2623 NULL, // reserved1
2624 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002625 JII::DestroyJavaVM,
2626 JII::AttachCurrentThread,
2627 JII::DetachCurrentThread,
2628 JII::GetEnv,
2629 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002630};
2631
Elliott Hughesa0957642011-09-02 14:27:33 -07002632JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002633 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002634 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002635 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002636 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002637 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002638 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002639 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002640 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002641 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002642 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002643 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002644 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002645 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002646 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002647 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002648 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002649 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002650 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002651 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002652}
2653
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002654JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002655 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002656}
2657
Elliott Hughes88c5c352012-03-15 18:49:48 -07002658void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2659 check_jni = enabled;
2660 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002661}
2662
Elliott Hughesae80b492012-04-24 10:43:17 -07002663void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2664 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2665 if (force_copy) {
2666 os << " (with forcecopy)";
2667 }
2668 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002669 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002670 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002671 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002672 os << "; pins=" << pin_table.Size();
2673 }
2674 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002675 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002676 os << "; globals=" << globals.Capacity();
2677 }
2678 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002679 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002680 if (weak_globals.Capacity() > 0) {
2681 os << " (plus " << weak_globals.Capacity() << " weak)";
2682 }
2683 }
2684 os << '\n';
2685
2686 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002687 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002688 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2689 }
2690}
2691
Elliott Hughes73e66f72012-05-09 09:34:45 -07002692void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002693 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002694 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002695 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002696 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002697 }
2698 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002699 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002700 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002701 }
2702 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002703 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002704 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002705 }
2706}
2707
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002708bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2709 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002710 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002711
2712 // See if we've already loaded this library. If we have, and the class loader
2713 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002714 // TODO: for better results we should canonicalize the pathname (or even compare
2715 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002716 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002717 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002718 {
2719 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002720 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002721 library = libraries->Get(path);
2722 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 if (library != NULL) {
2724 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002725 // The library will be associated with class_loader. The JNI
2726 // spec says we can't load the same library into more than one
2727 // class loader.
2728 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2729 "ClassLoader %p; can't open in ClassLoader %p",
2730 path.c_str(), library->GetClassLoader(), class_loader);
2731 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002732 return false;
2733 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002734 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2735 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002736 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002737 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2738 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002739 return false;
2740 }
2741 return true;
2742 }
2743
2744 // Open the shared library. Because we're using a full path, the system
2745 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2746 // resolve this library's dependencies though.)
2747
2748 // Failures here are expected when java.library.path has several entries
2749 // and we have to hunt for the lib.
2750
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002751 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2752 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2753 // dlopen) becomes zero from dlclose.
2754
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002756 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002757 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2758 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2759 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002760
Elliott Hughes84b2f142012-09-27 09:16:28 -07002761 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002762
2763 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002764 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002765 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002766 return false;
2767 }
2768
2769 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002770 // TODO: move the locking (and more of this logic) into Libraries.
2771 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002772 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002773 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002775 if (library == NULL) { // We won race to get libraries_lock
2776 library = new SharedLibrary(path, handle, class_loader);
2777 libraries->Put(path, library);
2778 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002779 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002780 }
2781 if (!created_library) {
2782 LOG(INFO) << "WOW: we lost a race to add shared library: "
2783 << "\"" << path << "\" ClassLoader=" << class_loader;
2784 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002785 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002786
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002787 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002788
2789 bool result = true;
2790 void* sym = dlsym(handle, "JNI_OnLoad");
2791 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002792 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 } else {
2794 // Call JNI_OnLoad. We have to override the current class
2795 // loader, which will always be "null" since the stuff at the
2796 // top of the stack is around Runtime.loadLibrary(). (See
2797 // the comments in the JNI FindClass function.)
2798 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2799 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002800 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002801 self->SetClassLoaderOverride(class_loader);
2802
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002803 int version = 0;
2804 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002805 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002806 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002807 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002808 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002809
Brian Carlstromaded5f72011-10-07 17:15:04 -07002810 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002811
2812 if (version != JNI_VERSION_1_2 &&
2813 version != JNI_VERSION_1_4 &&
2814 version != JNI_VERSION_1_6) {
2815 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2816 << "bad version: " << version;
2817 // It's unwise to call dlclose() here, but we can mark it
2818 // as bad and ensure that future load attempts will fail.
2819 // We don't know how far JNI_OnLoad got, so there could
2820 // be some partially-initialized stuff accessible through
2821 // newly-registered native method calls. We could try to
2822 // unregister them, but that doesn't seem worthwhile.
2823 result = false;
2824 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002825 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2826 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002827 }
2828 }
2829
2830 library->SetResult(result);
2831 return result;
2832}
2833
Mathieu Chartier66f19252012-09-18 08:57:04 -07002834void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002835 CHECK(m->IsNative());
2836
2837 Class* c = m->GetDeclaringClass();
2838
2839 // If this is a static method, it could be called before the class
2840 // has been initialized.
2841 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002842 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002843 return NULL;
2844 }
2845 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002846 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002847 }
2848
Brian Carlstrom16192862011-09-12 17:50:06 -07002849 std::string detail;
2850 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002851 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002852 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002853 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002854 native_method = libraries->FindNativeMethod(m, detail);
2855 }
2856 // throwing can cause libraries_lock to be reacquired
2857 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002858 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002859 }
2860 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002861}
2862
Elliott Hughes410c0c82011-09-01 17:58:25 -07002863void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002864 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002865 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002866 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002867 globals.VisitRoots(visitor, arg);
2868 }
2869 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002870 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002871 pin_table.VisitRoots(visitor, arg);
2872 }
2873 // The weak_globals table is visited by the GC itself (because it mutates the table).
2874}
2875
Elliott Hughesc8fece32013-01-02 11:27:23 -08002876void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2877 size_t method_count) {
2878 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2879 if (c.get() == NULL) {
2880 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2881 }
2882 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2883}
2884
Ian Rogersdf20fe02011-07-20 20:34:16 -07002885} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002886
2887std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2888 switch (rhs) {
2889 case JNIInvalidRefType:
2890 os << "JNIInvalidRefType";
2891 return os;
2892 case JNILocalRefType:
2893 os << "JNILocalRefType";
2894 return os;
2895 case JNIGlobalRefType:
2896 os << "JNIGlobalRefType";
2897 return os;
2898 case JNIWeakGlobalRefType:
2899 os << "JNIWeakGlobalRefType";
2900 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002901 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002902 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002903 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002904 }
2905}