blob: 8a16bc784d50b8cd7e6154452231970390eff6ba [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 Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070027#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070028#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "mutex.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070031#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070037#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070038#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070039#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070040#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070041#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070042
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070043namespace art {
44
Elliott Hughes2ced6a52011-10-16 18:44:48 -070045static const size_t kMonitorsInitial = 32; // Arbitrary.
46static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
47
48static const size_t kLocalsInitial = 64; // Arbitrary.
49static const size_t kLocalsMax = 512; // Arbitrary sanity check.
50
51static const size_t kPinTableInitial = 16; // Arbitrary.
52static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
53
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070054static size_t gGlobalsInitial = 512; // Arbitrary.
55static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070056
57static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
58static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
59
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
61 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070062 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
63 if (c.get() == NULL) {
64 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
65 }
66 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
67 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
68 }
69}
70
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070071void SetJniGlobalsMax(size_t max) {
72 if (max != 0) {
73 gGlobalsMax = max;
74 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
75 }
76}
Ian Rogersdf20fe02011-07-20 20:34:16 -070077
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070080 if (obj == NULL) {
81 return NULL;
82 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070084 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070085 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070086 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
87 return reinterpret_cast<jweak>(ref);
88}
89
Mathieu Chartier66f19252012-09-18 08:57:04 -070090static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070092 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070093 const DexFile::TypeList* params = mh.GetParameterTypeList();
94 if (params == NULL) {
95 return; // No arguments so nothing to check.
96 }
97 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070098 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070099 for (uint32_t i = 0; i < num_params; i++) {
100 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
101 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
102 if (param_type == NULL) {
103 Thread* self = Thread::Current();
104 CHECK(self->IsExceptionPending());
105 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
106 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
107 << self->GetException()->Dump();
108 self->ClearException();
109 ++error_count;
110 } else if (!param_type->IsPrimitive()) {
111 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700112 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700114 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
115 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
116 ++error_count;
117 }
118 }
119 }
120 if (error_count > 0) {
121 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
122 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700123 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700124 }
125}
Elliott Hughesb264f082012-04-06 17:10:10 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700128 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700131 CheckMethodArguments(method, args);
132 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700133 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700135 return result;
136}
137
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
139 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700142 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700143 MethodHelper mh(method);
144 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 arg_array.BuildArgArray(soa, args);
146 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700147}
148
Mathieu Chartier66f19252012-09-18 08:57:04 -0700149static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700151 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700152}
153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
155 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700158 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700159 MethodHelper mh(method);
160 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 arg_array.BuildArgArray(soa, args);
162 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700163}
164
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
166 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700169 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 MethodHelper mh(method);
171 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 arg_array.BuildArgArray(soa, args);
173 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700174}
175
Elliott Hughes6b436852011-08-12 10:16:44 -0700176// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
177// separated with slashes but aren't wrapped with "L;" like regular descriptors
178// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
179// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
180// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700181static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700182 std::string result;
183 // Add the missing "L;" if necessary.
184 if (name[0] == '[') {
185 result = name;
186 } else {
187 result += 'L';
188 result += name;
189 result += ';';
190 }
191 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700192 if (result.find('.') != std::string::npos) {
193 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
194 << "\"" << name << "\"";
195 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700196 }
197 return result;
198}
199
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
201 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800204 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700205}
206
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
208 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700210 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700211 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700212 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700213 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700214
Mathieu Chartier66f19252012-09-18 08:57:04 -0700215 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700216 if (is_static) {
217 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700218 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700219 method = c->FindVirtualMethod(name, sig);
220 if (method == NULL) {
221 // No virtual method matching the signature. Search declared
222 // private methods and constructors.
223 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700224 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700225 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700226
Elliott Hughescdf53122011-08-19 15:46:09 -0700227 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700229 return NULL;
230 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700231
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700233}
234
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235static ClassLoader* GetClassLoader(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700237 AbstractMethod* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700238 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
239 return self->GetClassLoaderOverride();
240 }
241 return method->GetDeclaringClass()->GetClassLoader();
242}
243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
245 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700248 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700249 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700250 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700251
252 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700253 Class* field_type;
254 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
255 if (sig[1] != '\0') {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 ClassLoader* cl = GetClassLoader(soa.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700257 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700258 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700260 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 if (field_type == NULL) {
262 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 DCHECK(soa.Self()->IsExceptionPending());
264 soa.Self()->ClearException();
265 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700266 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800267 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 return NULL;
269 }
270 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700274 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700275 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700277 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800278 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700279 return NULL;
280 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700282}
283
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700287 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700288 vm->pin_table.Add(array);
289}
290
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700294 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700295 vm->pin_table.Remove(array);
296}
297
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
299 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700301 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700303 "%s offset=%d length=%d %s.length=%d",
304 type.c_str(), start, length, identifier, array->GetLength());
305}
Ian Rogers0571d352011-11-03 19:51:38 -0700306
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
308 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700311 "offset=%d length=%d string.length()=%d", start, length, array_length);
312}
Elliott Hughes814e4032011-08-23 12:07:56 -0700313
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 ScopedObjectAccess soa(env);
317
318 // Turn the const char* into a java.lang.String.
319 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
320 if (msg != NULL && s.get() == NULL) {
321 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700322 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 // Choose an appropriate constructor and set up the arguments.
325 jvalue args[2];
326 const char* signature;
327 if (msg == NULL && cause == NULL) {
328 signature = "()V";
329 } else if (msg != NULL && cause == NULL) {
330 signature = "(Ljava/lang/String;)V";
331 args[0].l = s.get();
332 } else if (msg == NULL && cause != NULL) {
333 signature = "(Ljava/lang/Throwable;)V";
334 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700335 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
337 args[0].l = s.get();
338 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700339 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
341 if (mid == NULL) {
342 LOG(ERROR) << "No <init>" << signature << " in "
343 << PrettyClass(soa.Decode<Class*>(exception_class));
344 return JNI_ERR;
345 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700346
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
348 if (exception.get() == NULL) {
349 return JNI_ERR;
350 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700353
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700355}
356
Elliott Hughes462c9442012-03-23 18:47:50 -0700357static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700358 if (vm == NULL || p_env == NULL) {
359 return JNI_ERR;
360 }
361
Elliott Hughes462c9442012-03-23 18:47:50 -0700362 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700363 Thread* self = Thread::Current();
364 if (self != NULL) {
365 *p_env = self->GetJniEnv();
366 return JNI_OK;
367 }
368
369 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
370
371 // No threads allowed in zygote mode.
372 if (runtime->IsZygote()) {
373 LOG(ERROR) << "Attempt to attach a thread in the zygote";
374 return JNI_ERR;
375 }
376
Elliott Hughes462c9442012-03-23 18:47:50 -0700377 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
378 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700379 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700380 if (args != NULL) {
381 CHECK_GE(args->version, JNI_VERSION_1_2);
382 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700383 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700384 }
Elliott Hughes75770752011-08-24 17:52:38 -0700385
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800386 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700387 *p_env = NULL;
388 return JNI_ERR;
389 } else {
390 *p_env = Thread::Current()->GetJniEnv();
391 return JNI_OK;
392 }
Elliott Hughes75770752011-08-24 17:52:38 -0700393}
394
Elliott Hughes79082e32011-08-25 12:07:32 -0700395class SharedLibrary {
396 public:
397 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
398 : path_(path),
399 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700400 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700401 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700402 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700403 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700404 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700405 }
406
Elliott Hughes79082e32011-08-25 12:07:32 -0700407 Object* GetClassLoader() {
408 return class_loader_;
409 }
410
411 std::string GetPath() {
412 return path_;
413 }
414
415 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700416 * Check the result of an earlier call to JNI_OnLoad on this library.
417 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700418 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700419 bool CheckOnLoadResult()
420 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700422 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700423 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
424 bool okay;
425 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700426 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700427
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
429 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
430 // caller can continue.
431 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
432 okay = true;
433 } else {
434 while (jni_on_load_result_ == kPending) {
435 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700436 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700438
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 okay = (jni_on_load_result_ == kOkay);
440 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
441 << (okay ? "succeeded" : "failed") << "]";
442 }
443 }
444 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 return okay;
446 }
447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700449 Thread* self = Thread::Current();
450 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700451
Elliott Hughes79082e32011-08-25 12:07:32 -0700452 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700453 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700454
455 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700456 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 }
458
459 void* FindSymbol(const std::string& symbol_name) {
460 return dlsym(handle_, symbol_name.c_str());
461 }
462
463 private:
464 enum JNI_OnLoadState {
465 kPending,
466 kFailed,
467 kOkay,
468 };
469
470 // Path to library "/system/lib/libjni.so".
471 std::string path_;
472
473 // The void* returned by dlopen(3).
474 void* handle_;
475
476 // The ClassLoader this library is associated with.
477 Object* class_loader_;
478
479 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700482 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700484 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700486 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700487};
488
Elliott Hughes79082e32011-08-25 12:07:32 -0700489// This exists mainly to keep implementation details out of the header file.
490class Libraries {
491 public:
492 Libraries() {
493 }
494
495 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700496 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
Elliott Hughesae80b492012-04-24 10:43:17 -0700499 void Dump(std::ostream& os) const {
500 bool first = true;
501 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
502 if (!first) {
503 os << ' ';
504 }
505 first = false;
506 os << it->first;
507 }
508 }
509
510 size_t size() const {
511 return libraries_.size();
512 }
513
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700515 It it = libraries_.find(path);
516 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
519 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700520 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 }
522
523 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700524 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 std::string jni_short_name(JniShortName(m));
527 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700528 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
530 SharedLibrary* library = it->second;
531 if (library->GetClassLoader() != declaring_class_loader) {
532 // We only search libraries loaded by the appropriate ClassLoader.
533 continue;
534 }
535 // Try the short name then the long name...
536 void* fn = library->FindSymbol(jni_short_name);
537 if (fn == NULL) {
538 fn = library->FindSymbol(jni_long_name);
539 }
540 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800541 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
542 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 return fn;
544 }
545 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700547 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700548 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 return NULL;
551 }
552
553 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700554 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700555
Elliott Hughesa0e18062012-04-13 15:59:59 -0700556 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557};
558
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
560 jvalue* args) {
561 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700562 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700563 MethodHelper mh(method);
564 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 arg_array.BuildArgArray(soa, args);
566 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700567}
568
Mathieu Chartier66f19252012-09-18 08:57:04 -0700569JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700572 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800573}
574
Elliott Hughescdf53122011-08-19 15:46:09 -0700575class JNI {
576 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700577 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700578 return JNI_VERSION_1_6;
579 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700580
Ian Rogers25e8b912012-09-07 11:31:36 -0700581 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700582 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700583 return NULL;
584 }
585
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700588 Runtime* runtime = Runtime::Current();
589 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700590 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700591 Class* c = NULL;
592 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800594 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700595 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800596 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700597 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700599 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700600
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700603 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700605 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700606
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608 ScopedObjectAccess soa(env);
609 Field* field = soa.Decode<Field*>(java_field);
610 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700611 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700615 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 ScopedObjectAccess soa(env);
621 Field* field = soa.DecodeField(fid);
622 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughes37f7a402011-08-22 18:56:01 -0700625 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 ScopedObjectAccess soa(env);
627 Object* o = soa.Decode<Object*>(java_object);
628 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 }
630
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700631 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 ScopedObjectAccess soa(env);
633 Class* c = soa.Decode<Class*>(java_class);
634 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
639 Class* c1 = soa.Decode<Class*>(java_class1);
640 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700641 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughese84278b2012-03-22 10:06:53 -0700644 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700646 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700647 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700648 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700649 return JNI_TRUE;
650 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 Object* obj = soa.Decode<Object*>(jobj);
652 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700653 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
659 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 if (exception == NULL) {
661 return JNI_ERR;
662 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 return JNI_OK;
665 }
666
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700667 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700668 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 }
670
671 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700672 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 }
674
675 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700676 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 }
678
679 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700681
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700683 Throwable* original_exception = self->GetException();
684 self->ClearException();
685
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
688 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
689 if (mid == NULL) {
690 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700691 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700692 } else {
693 env->CallVoidMethod(exception.get(), mid);
694 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700695 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700696 << " thrown while calling printStackTrace";
697 self->ClearException();
698 }
699 }
700
701 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700703
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 ScopedObjectAccess soa(env);
706 Object* exception = soa.Self()->GetException();
707 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
709
Ian Rogers25e8b912012-09-07 11:31:36 -0700710 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 LOG(FATAL) << "JNI FatalError called: " << msg;
712 }
713
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700714 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(env);
716 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700717 return JNI_ERR;
718 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 return JNI_OK;
721 }
722
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700723 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
725 Object* survivor = soa.Decode<Object*>(java_survivor);
726 soa.Env()->PopFrame();
727 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
729
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700730 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700731 ScopedObjectAccess soa(env);
732 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 }
734
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 if (obj == NULL) {
737 return NULL;
738 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700739 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700743 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 return reinterpret_cast<jobject>(ref);
746 }
747
748 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 if (obj == NULL) {
750 return;
751 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700752 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 IndirectReferenceTable& globals = vm->globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700755 MutexLock mu(soa.Self(), vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700756
757 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
758 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
759 << "failed to find entry";
760 }
761 }
762
763 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 ScopedObjectAccess soa(env);
765 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767
768 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 if (obj == NULL) {
770 return;
771 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700772 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700775 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700776
777 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
778 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
779 << "failed to find entry";
780 }
781 }
782
783 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 if (obj == NULL) {
785 return NULL;
786 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700787 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700789
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 uint32_t cookie = soa.Env()->local_ref_cookie;
791 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 return reinterpret_cast<jobject>(ref);
793 }
794
795 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 if (obj == NULL) {
797 return;
798 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700799 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 if (!locals.Remove(cookie, obj)) {
804 // Attempting to delete a local reference that is not in the
805 // topmost local reference frame is a no-op. DeleteLocalRef returns
806 // void and doesn't throw any exceptions, but we should probably
807 // complain about it so the user will notice that things aren't
808 // going quite the way they expect.
809 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
810 << "failed to find entry";
811 }
812 }
813
814 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700815 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700816 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 }
818
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700819 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 ScopedObjectAccess soa(env);
821 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700822 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700823 return NULL;
824 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700825 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 }
827
Elliott Hughese84278b2012-03-22 10:06:53 -0700828 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700831 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 va_end(args);
833 return result;
834 }
835
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(env);
838 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700839 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700840 return NULL;
841 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700842 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700843 if (result == NULL) {
844 return NULL;
845 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700849 return local_result;
850 } else {
851 return NULL;
852 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
854
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 ScopedObjectAccess soa(env);
857 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700858 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700859 return NULL;
860 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700861 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700862 if (result == NULL) {
863 return NULL;
864 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700866 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700868 return local_result;
869 } else {
870 return NULL;
871 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(env);
876 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 }
878
879 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 ScopedObjectAccess soa(env);
881 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 va_list ap;
886 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700887 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 ScopedObjectAccess soa(env);
895 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
896 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 ScopedObjectAccess soa(env);
901 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
902 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 }
904
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 va_list ap;
907 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700908 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700911 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
916 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
921 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 va_list ap;
927 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700930 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 ScopedObjectAccess soa(env);
935 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 ScopedObjectAccess soa(env);
940 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 va_list ap;
945 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700946 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700949 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
954 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 ScopedObjectAccess soa(env);
959 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_list ap;
964 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700965 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700968 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(env);
973 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
978 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 va_list ap;
984 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700987 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 ScopedObjectAccess soa(env);
992 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
997 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_list ap;
1002 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001003 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001006 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 ScopedObjectAccess soa(env);
1016 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_list ap;
1021 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001022 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 ScopedObjectAccess soa(env);
1035 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001041 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001044 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
1049 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
1054 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001060 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001061 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066 ScopedObjectAccess soa(env);
1067 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 ScopedObjectAccess soa(env);
1072 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001075 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001078 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1080 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 va_end(ap);
1082 return local_result;
1083 }
1084
1085 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001086 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1089 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
1092 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001093 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(env);
1095 JValue result(InvokeWithJValues(soa, obj, mid, args));
1096 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
1099 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001100 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001103 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001106 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
1109 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001110 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
1112 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
1115 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001116 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
1118 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001121 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001124 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001127 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
1130 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
1133 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
1136 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001137 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001142 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001148 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
1151 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
1157 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001158 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 ScopedObjectAccess soa(env);
1160 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001163 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001166 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001169 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
1172 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001173 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
1175 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
1178 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001179 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001184 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001187 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001190 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
1196 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001200 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
1202 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001205 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001208 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001211 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001215 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 ScopedObjectAccess soa(env);
1217 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001221 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001222 ScopedObjectAccess soa(env);
1223 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001226 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001229 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001232 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001236 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
1241 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001242 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
1244 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001247 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001250 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001253 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001257 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 ScopedObjectAccess soa(env);
1259 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001263 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
1265 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
1274 }
1275
1276 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env);
1279 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 ScopedObjectAccess soa(env);
1285 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001288 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
1290 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001292
1293
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001294 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001298
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001299 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(env);
1301 Object* o = soa.Decode<Object*>(obj);
1302 Field* f = soa.DecodeField(fid);
1303 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001305
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 ScopedObjectAccess soa(env);
1308 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001309 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001312 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 ScopedObjectAccess soa(env);
1314 Object* o = soa.Decode<Object*>(java_object);
1315 Object* v = soa.Decode<Object*>(java_value);
1316 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001317 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001320 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 Object* v = soa.Decode<Object*>(java_value);
1323 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001324 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001327#define GET_PRIMITIVE_FIELD(fn, instance) \
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 return f->fn(o)
1332
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001333#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1334 ScopedObjectAccess soa(env); \
1335 Field* f = soa.DecodeField(fid); \
1336 return f->fn(f->GetDeclaringClass())
1337
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env); \
1340 Object* o = soa.Decode<Object*>(instance); \
1341 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 f->fn(o, value)
1343
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001344#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1345 ScopedObjectAccess soa(env); \
1346 Field* f = soa.DecodeField(fid); \
1347 f->fn(f->GetDeclaringClass(), value)
1348
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1350 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1354 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1358 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1362 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1366 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1370 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1374 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1378 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001381 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001382 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001386 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001389 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001390 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001394 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001397 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001398 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001401 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001402 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001406 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 }
1408
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001409 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001410 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 }
1412
1413 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1414 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1415 }
1416
1417 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1418 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1419 }
1420
1421 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1422 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1423 }
1424
1425 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1426 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1427 }
1428
1429 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1430 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1431 }
1432
1433 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1434 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1435 }
1436
1437 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1438 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1439 }
1440
1441 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1442 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1443 }
1444
1445 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001446 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 }
1448
1449 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001450 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 }
1452
1453 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001454 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 }
1456
1457 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001458 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 }
1460
1461 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001462 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 }
1464
1465 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001466 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 }
1468
1469 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001470 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 }
1472
1473 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001474 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001477 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001479 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001480 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1482 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 va_end(ap);
1484 return local_result;
1485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 ScopedObjectAccess soa(env);
1489 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1490 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001493 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001494 ScopedObjectAccess soa(env);
1495 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1496 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001501 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001502 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001503 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001505 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
1510 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001513 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
1515 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001520 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001521 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001524 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001527 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001528 ScopedObjectAccess soa(env);
1529 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
1534 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001540 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001543 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 ScopedObjectAccess soa(env);
1548 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001551 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001552 ScopedObjectAccess soa(env);
1553 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001559 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001560 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001562 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001566 ScopedObjectAccess soa(env);
1567 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001571 ScopedObjectAccess soa(env);
1572 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001578 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001581 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001585 ScopedObjectAccess soa(env);
1586 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001590 ScopedObjectAccess soa(env);
1591 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001597 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001598 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001600 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001604 ScopedObjectAccess soa(env);
1605 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001609 ScopedObjectAccess soa(env);
1610 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001616 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001617 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001619 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001623 ScopedObjectAccess soa(env);
1624 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001628 ScopedObjectAccess soa(env);
1629 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001635 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001638 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 ScopedObjectAccess soa(env);
1643 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001647 ScopedObjectAccess soa(env);
1648 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001654 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 va_end(ap);
1657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 ScopedObjectAccess soa(env);
1661 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001664 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
1666 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes814e4032011-08-23 12:07:56 -07001669 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001671 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001672 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
1675 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 if (utf == NULL) {
1677 return NULL;
1678 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001680 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes814e4032011-08-23 12:07:56 -07001684 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
1686 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001687 }
1688
1689 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001690 ScopedObjectAccess soa(env);
1691 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001692 }
1693
Elliott Hughesb465ab02011-08-24 11:21:21 -07001694 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* 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 memcpy(buf, chars + start, length * sizeof(jchar));
1702 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001703 }
1704
Elliott Hughesb465ab02011-08-24 11:21:21 -07001705 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 ScopedObjectAccess soa(env);
1707 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001708 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001710 } else {
1711 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1712 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1713 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001714 }
1715
Elliott Hughes75770752011-08-24 17:52:38 -07001716 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
1718 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001719 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001720 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001721 if (is_copy != NULL) {
1722 *is_copy = JNI_FALSE;
1723 }
1724 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 ScopedObjectAccess soa(env);
1729 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes75770752011-08-24 17:52:38 -07001732 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001734 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes75770752011-08-24 17:52:38 -07001737 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001739 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes75770752011-08-24 17:52:38 -07001742 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001744 if (java_string == NULL) {
1745 return NULL;
1746 }
1747 if (is_copy != NULL) {
1748 *is_copy = JNI_TRUE;
1749 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001751 size_t byte_count = s->GetUtfLength();
1752 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001753 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001754 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1755 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1756 bytes[byte_count] = '\0';
1757 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001758 }
1759
Elliott Hughes75770752011-08-24 17:52:38 -07001760 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001761 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001762 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001763 }
1764
Elliott Hughesbd935992011-08-22 11:59:34 -07001765 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
1767 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001768 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001769 Array* array = obj->AsArray();
1770 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes814e4032011-08-23 12:07:56 -07001773 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 ScopedObjectAccess soa(env);
1775 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1776 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
1779 static void SetObjectArrayElement(JNIEnv* env,
1780 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
1782 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1783 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 array->Set(index, value);
1785 }
1786
1787 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
1789 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
1792 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
1794 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
1797 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
1799 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
1804 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
1807 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
1809 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
1812 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
1814 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
1819 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 CHECK_GE(length, 0); // TODO: ReportJniError
1825
1826 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 std::string descriptor;
1829 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001830 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831
1832 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001833 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1834 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 return NULL;
1836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001840 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001841 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001843 for (jsize i = 0; i < length; ++i) {
1844 result->Set(i, initial_object);
1845 }
1846 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
1850 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
1852 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Ian Rogersa15e67d2012-02-28 13:51:55 -08001855 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
1857 Array* array = soa.Decode<Array*>(java_array);
1858 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001859 if (is_copy != NULL) {
1860 *is_copy = JNI_FALSE;
1861 }
1862 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
1877 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 ScopedObjectAccess soa(env);
1882 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes75770752011-08-24 17:52:38 -07001885 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
1887 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
1892 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
1897 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
1902 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes75770752011-08-24 17:52:38 -07001905 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
1907 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
1912 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001915 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
1917 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001920 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
1922 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 ScopedObjectAccess soa(env);
1927 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001930 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
1932 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 ScopedObjectAccess soa(env);
1937 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001940 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 ScopedObjectAccess soa(env);
1942 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001945 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
1947 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes814e4032011-08-23 12:07:56 -07001950 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
1952 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
1957 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
1962 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 ScopedObjectAccess soa(env);
1977 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
1982 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ScopedObjectAccess soa(env);
1987 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
1992 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
1997 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes814e4032011-08-23 12:07:56 -07002000 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
2002 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes814e4032011-08-23 12:07:56 -07002005 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
2007 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ScopedObjectAccess soa(env);
2012 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(env);
2017 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
2022 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002026 ScopedObjectAccess soa(env);
2027 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes5174fe62011-08-23 15:12:35 -07002030 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002031 ScopedObjectAccess soa(env);
2032 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002033
Elliott Hughes5174fe62011-08-23 15:12:35 -07002034 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 const char* name = methods[i].name;
2036 const char* sig = methods[i].signature;
2037
2038 if (*sig == '!') {
2039 // TODO: fast jni. it's too noisy to log all these.
2040 ++sig;
2041 }
2042
Mathieu Chartier66f19252012-09-18 08:57:04 -07002043 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002044 if (m == NULL) {
2045 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002047 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002048 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002051 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002052 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002053 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 return JNI_ERR;
2055 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002056
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002057 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002058
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061 return JNI_OK;
2062 }
2063
Elliott Hughes5174fe62011-08-23 15:12:35 -07002064 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
2066 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002067
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002068 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002069
2070 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002071 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002072 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002074 }
2075 }
2076 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002077 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002078 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002080 }
2081 }
2082
2083 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2087 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2088 ScopedObjectAccess soa(env);
2089 Object* o = soa.Decode<Object*>(java_object);
2090 o->MonitorEnter(soa.Self());
2091 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002092 return JNI_ERR;
2093 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002095 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002098 static jint MonitorExit(JNIEnv* env, jobject java_object)
2099 UNLOCK_FUNCTION(monitor_lock_) {
2100 ScopedObjectAccess soa(env);
2101 Object* o = soa.Decode<Object*>(java_object);
2102 o->MonitorExit(soa.Self());
2103 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002104 return JNI_ERR;
2105 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002107 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
2110 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 Runtime* runtime = Runtime::Current();
2113 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002114 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 } else {
2116 *vm = NULL;
2117 }
2118 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2119 }
2120
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002123
2124 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002125 CHECK(address != NULL); // TODO: ReportJniError
2126 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002127
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002129 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2130 CHECK_LE(capacity, 0xffffffff);
2131 jint address_arg = reinterpret_cast<jint>(address);
2132 jint capacity_arg = static_cast<jint>(capacity);
2133
Elliott Hugheseac76672012-05-24 21:56:51 -07002134 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2135 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2136 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughesb465ab02011-08-24 11:21:21 -07002140 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002142 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughesb465ab02011-08-24 11:21:21 -07002145 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002147 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughesb465ab02011-08-24 11:21:21 -07002150 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002152
Elliott Hughes75770752011-08-24 17:52:38 -07002153 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002154
2155 // Do we definitely know what kind of reference this is?
2156 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2157 IndirectRefKind kind = GetIndirectRefKind(ref);
2158 switch (kind) {
2159 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002160 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002161 return JNILocalRefType;
2162 }
2163 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002164 case kGlobal:
2165 return JNIGlobalRefType;
2166 case kWeakGlobal:
2167 return JNIWeakGlobalRefType;
2168 case kSirtOrInvalid:
2169 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002170 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002171 return JNILocalRefType;
2172 }
2173
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002174 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002175 return JNIInvalidRefType;
2176 }
2177
Elliott Hughesb465ab02011-08-24 11:21:21 -07002178 // If we're handing out direct pointers, check whether it's a direct pointer
2179 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2181 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002182 return JNILocalRefType;
2183 }
2184 }
2185
2186 return JNIInvalidRefType;
2187 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002188 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2189 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191
2192 private:
2193 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2194 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 // TODO: we should try to expand the table if necessary.
2197 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2198 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2199 return JNI_ERR;
2200 }
2201 // TODO: this isn't quite right, since "capacity" includes holes.
2202 size_t capacity = soa.Env()->locals.Capacity();
2203 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2204 if (!okay) {
2205 soa.Self()->ThrowOutOfMemoryError(caller);
2206 }
2207 return okay ? JNI_OK : JNI_ERR;
2208 }
2209
2210 template<typename JniT, typename ArtT>
2211 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 CHECK_GE(length, 0); // TODO: ReportJniError
Ian Rogers50b35e22012-10-04 10:09:15 -07002214 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 return soa.AddLocalReference<JniT>(result);
2216 }
2217
2218 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2219 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2220 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2223 PinPrimitiveArray(soa, array);
2224 if (is_copy != NULL) {
2225 *is_copy = JNI_FALSE;
2226 }
2227 return array->GetData();
2228 }
2229
2230 template <typename ArrayT>
2231 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2232 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 if (mode != JNI_COMMIT) {
2235 Array* array = soa.Decode<Array*>(java_array);
2236 UnpinPrimitiveArray(soa, array);
2237 }
2238 }
2239
2240 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2241 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2242 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2245 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2246 ThrowAIOOBE(soa, array, start, length, "src");
2247 } else {
2248 JavaT* data = array->GetData();
2249 memcpy(buf, data + start, length * sizeof(JavaT));
2250 }
2251 }
2252
2253 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2254 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2255 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002257 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2258 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2259 ThrowAIOOBE(soa, array, start, length, "dst");
2260 } else {
2261 JavaT* data = array->GetData();
2262 memcpy(data + start, buf, length * sizeof(JavaT));
2263 }
2264 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002265};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002266
Elliott Hughes88c5c352012-03-15 18:49:48 -07002267const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002268 NULL, // reserved0.
2269 NULL, // reserved1.
2270 NULL, // reserved2.
2271 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 JNI::GetVersion,
2273 JNI::DefineClass,
2274 JNI::FindClass,
2275 JNI::FromReflectedMethod,
2276 JNI::FromReflectedField,
2277 JNI::ToReflectedMethod,
2278 JNI::GetSuperclass,
2279 JNI::IsAssignableFrom,
2280 JNI::ToReflectedField,
2281 JNI::Throw,
2282 JNI::ThrowNew,
2283 JNI::ExceptionOccurred,
2284 JNI::ExceptionDescribe,
2285 JNI::ExceptionClear,
2286 JNI::FatalError,
2287 JNI::PushLocalFrame,
2288 JNI::PopLocalFrame,
2289 JNI::NewGlobalRef,
2290 JNI::DeleteGlobalRef,
2291 JNI::DeleteLocalRef,
2292 JNI::IsSameObject,
2293 JNI::NewLocalRef,
2294 JNI::EnsureLocalCapacity,
2295 JNI::AllocObject,
2296 JNI::NewObject,
2297 JNI::NewObjectV,
2298 JNI::NewObjectA,
2299 JNI::GetObjectClass,
2300 JNI::IsInstanceOf,
2301 JNI::GetMethodID,
2302 JNI::CallObjectMethod,
2303 JNI::CallObjectMethodV,
2304 JNI::CallObjectMethodA,
2305 JNI::CallBooleanMethod,
2306 JNI::CallBooleanMethodV,
2307 JNI::CallBooleanMethodA,
2308 JNI::CallByteMethod,
2309 JNI::CallByteMethodV,
2310 JNI::CallByteMethodA,
2311 JNI::CallCharMethod,
2312 JNI::CallCharMethodV,
2313 JNI::CallCharMethodA,
2314 JNI::CallShortMethod,
2315 JNI::CallShortMethodV,
2316 JNI::CallShortMethodA,
2317 JNI::CallIntMethod,
2318 JNI::CallIntMethodV,
2319 JNI::CallIntMethodA,
2320 JNI::CallLongMethod,
2321 JNI::CallLongMethodV,
2322 JNI::CallLongMethodA,
2323 JNI::CallFloatMethod,
2324 JNI::CallFloatMethodV,
2325 JNI::CallFloatMethodA,
2326 JNI::CallDoubleMethod,
2327 JNI::CallDoubleMethodV,
2328 JNI::CallDoubleMethodA,
2329 JNI::CallVoidMethod,
2330 JNI::CallVoidMethodV,
2331 JNI::CallVoidMethodA,
2332 JNI::CallNonvirtualObjectMethod,
2333 JNI::CallNonvirtualObjectMethodV,
2334 JNI::CallNonvirtualObjectMethodA,
2335 JNI::CallNonvirtualBooleanMethod,
2336 JNI::CallNonvirtualBooleanMethodV,
2337 JNI::CallNonvirtualBooleanMethodA,
2338 JNI::CallNonvirtualByteMethod,
2339 JNI::CallNonvirtualByteMethodV,
2340 JNI::CallNonvirtualByteMethodA,
2341 JNI::CallNonvirtualCharMethod,
2342 JNI::CallNonvirtualCharMethodV,
2343 JNI::CallNonvirtualCharMethodA,
2344 JNI::CallNonvirtualShortMethod,
2345 JNI::CallNonvirtualShortMethodV,
2346 JNI::CallNonvirtualShortMethodA,
2347 JNI::CallNonvirtualIntMethod,
2348 JNI::CallNonvirtualIntMethodV,
2349 JNI::CallNonvirtualIntMethodA,
2350 JNI::CallNonvirtualLongMethod,
2351 JNI::CallNonvirtualLongMethodV,
2352 JNI::CallNonvirtualLongMethodA,
2353 JNI::CallNonvirtualFloatMethod,
2354 JNI::CallNonvirtualFloatMethodV,
2355 JNI::CallNonvirtualFloatMethodA,
2356 JNI::CallNonvirtualDoubleMethod,
2357 JNI::CallNonvirtualDoubleMethodV,
2358 JNI::CallNonvirtualDoubleMethodA,
2359 JNI::CallNonvirtualVoidMethod,
2360 JNI::CallNonvirtualVoidMethodV,
2361 JNI::CallNonvirtualVoidMethodA,
2362 JNI::GetFieldID,
2363 JNI::GetObjectField,
2364 JNI::GetBooleanField,
2365 JNI::GetByteField,
2366 JNI::GetCharField,
2367 JNI::GetShortField,
2368 JNI::GetIntField,
2369 JNI::GetLongField,
2370 JNI::GetFloatField,
2371 JNI::GetDoubleField,
2372 JNI::SetObjectField,
2373 JNI::SetBooleanField,
2374 JNI::SetByteField,
2375 JNI::SetCharField,
2376 JNI::SetShortField,
2377 JNI::SetIntField,
2378 JNI::SetLongField,
2379 JNI::SetFloatField,
2380 JNI::SetDoubleField,
2381 JNI::GetStaticMethodID,
2382 JNI::CallStaticObjectMethod,
2383 JNI::CallStaticObjectMethodV,
2384 JNI::CallStaticObjectMethodA,
2385 JNI::CallStaticBooleanMethod,
2386 JNI::CallStaticBooleanMethodV,
2387 JNI::CallStaticBooleanMethodA,
2388 JNI::CallStaticByteMethod,
2389 JNI::CallStaticByteMethodV,
2390 JNI::CallStaticByteMethodA,
2391 JNI::CallStaticCharMethod,
2392 JNI::CallStaticCharMethodV,
2393 JNI::CallStaticCharMethodA,
2394 JNI::CallStaticShortMethod,
2395 JNI::CallStaticShortMethodV,
2396 JNI::CallStaticShortMethodA,
2397 JNI::CallStaticIntMethod,
2398 JNI::CallStaticIntMethodV,
2399 JNI::CallStaticIntMethodA,
2400 JNI::CallStaticLongMethod,
2401 JNI::CallStaticLongMethodV,
2402 JNI::CallStaticLongMethodA,
2403 JNI::CallStaticFloatMethod,
2404 JNI::CallStaticFloatMethodV,
2405 JNI::CallStaticFloatMethodA,
2406 JNI::CallStaticDoubleMethod,
2407 JNI::CallStaticDoubleMethodV,
2408 JNI::CallStaticDoubleMethodA,
2409 JNI::CallStaticVoidMethod,
2410 JNI::CallStaticVoidMethodV,
2411 JNI::CallStaticVoidMethodA,
2412 JNI::GetStaticFieldID,
2413 JNI::GetStaticObjectField,
2414 JNI::GetStaticBooleanField,
2415 JNI::GetStaticByteField,
2416 JNI::GetStaticCharField,
2417 JNI::GetStaticShortField,
2418 JNI::GetStaticIntField,
2419 JNI::GetStaticLongField,
2420 JNI::GetStaticFloatField,
2421 JNI::GetStaticDoubleField,
2422 JNI::SetStaticObjectField,
2423 JNI::SetStaticBooleanField,
2424 JNI::SetStaticByteField,
2425 JNI::SetStaticCharField,
2426 JNI::SetStaticShortField,
2427 JNI::SetStaticIntField,
2428 JNI::SetStaticLongField,
2429 JNI::SetStaticFloatField,
2430 JNI::SetStaticDoubleField,
2431 JNI::NewString,
2432 JNI::GetStringLength,
2433 JNI::GetStringChars,
2434 JNI::ReleaseStringChars,
2435 JNI::NewStringUTF,
2436 JNI::GetStringUTFLength,
2437 JNI::GetStringUTFChars,
2438 JNI::ReleaseStringUTFChars,
2439 JNI::GetArrayLength,
2440 JNI::NewObjectArray,
2441 JNI::GetObjectArrayElement,
2442 JNI::SetObjectArrayElement,
2443 JNI::NewBooleanArray,
2444 JNI::NewByteArray,
2445 JNI::NewCharArray,
2446 JNI::NewShortArray,
2447 JNI::NewIntArray,
2448 JNI::NewLongArray,
2449 JNI::NewFloatArray,
2450 JNI::NewDoubleArray,
2451 JNI::GetBooleanArrayElements,
2452 JNI::GetByteArrayElements,
2453 JNI::GetCharArrayElements,
2454 JNI::GetShortArrayElements,
2455 JNI::GetIntArrayElements,
2456 JNI::GetLongArrayElements,
2457 JNI::GetFloatArrayElements,
2458 JNI::GetDoubleArrayElements,
2459 JNI::ReleaseBooleanArrayElements,
2460 JNI::ReleaseByteArrayElements,
2461 JNI::ReleaseCharArrayElements,
2462 JNI::ReleaseShortArrayElements,
2463 JNI::ReleaseIntArrayElements,
2464 JNI::ReleaseLongArrayElements,
2465 JNI::ReleaseFloatArrayElements,
2466 JNI::ReleaseDoubleArrayElements,
2467 JNI::GetBooleanArrayRegion,
2468 JNI::GetByteArrayRegion,
2469 JNI::GetCharArrayRegion,
2470 JNI::GetShortArrayRegion,
2471 JNI::GetIntArrayRegion,
2472 JNI::GetLongArrayRegion,
2473 JNI::GetFloatArrayRegion,
2474 JNI::GetDoubleArrayRegion,
2475 JNI::SetBooleanArrayRegion,
2476 JNI::SetByteArrayRegion,
2477 JNI::SetCharArrayRegion,
2478 JNI::SetShortArrayRegion,
2479 JNI::SetIntArrayRegion,
2480 JNI::SetLongArrayRegion,
2481 JNI::SetFloatArrayRegion,
2482 JNI::SetDoubleArrayRegion,
2483 JNI::RegisterNatives,
2484 JNI::UnregisterNatives,
2485 JNI::MonitorEnter,
2486 JNI::MonitorExit,
2487 JNI::GetJavaVM,
2488 JNI::GetStringRegion,
2489 JNI::GetStringUTFRegion,
2490 JNI::GetPrimitiveArrayCritical,
2491 JNI::ReleasePrimitiveArrayCritical,
2492 JNI::GetStringCritical,
2493 JNI::ReleaseStringCritical,
2494 JNI::NewWeakGlobalRef,
2495 JNI::DeleteWeakGlobalRef,
2496 JNI::ExceptionCheck,
2497 JNI::NewDirectByteBuffer,
2498 JNI::GetDirectBufferAddress,
2499 JNI::GetDirectBufferCapacity,
2500 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002501};
2502
Elliott Hughes75770752011-08-24 17:52:38 -07002503JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002504 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002505 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002506 local_ref_cookie(IRT_FIRST_SEGMENT),
2507 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002508 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002509 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002510 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002511 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002512 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002513 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002514 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002515 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2516 // errors will ensue.
2517 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2518 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002519}
2520
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002521JNIEnvExt::~JNIEnvExt() {
2522}
2523
Elliott Hughes88c5c352012-03-15 18:49:48 -07002524void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2525 check_jni = enabled;
2526 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002527}
2528
Elliott Hughes73e66f72012-05-09 09:34:45 -07002529void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2530 locals.Dump(os);
2531 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002532}
2533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002534void JNIEnvExt::PushFrame(int /*capacity*/) {
2535 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002536 stacked_local_ref_cookies.push_back(local_ref_cookie);
2537 local_ref_cookie = locals.GetSegmentState();
2538}
2539
2540void JNIEnvExt::PopFrame() {
2541 locals.SetSegmentState(local_ref_cookie);
2542 local_ref_cookie = stacked_local_ref_cookies.back();
2543 stacked_local_ref_cookies.pop_back();
2544}
2545
Carl Shapiroea4dca82011-08-01 13:45:38 -07002546// JNI Invocation interface.
2547
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002548extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2549 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2550 if (args->version < JNI_VERSION_1_2) {
2551 return JNI_EVERSION;
2552 }
2553 Runtime::Options options;
2554 for (int i = 0; i < args->nOptions; ++i) {
2555 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002556 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
2558 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002559 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002562 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002563 runtime->Start();
2564 *p_env = Thread::Current()->GetJniEnv();
2565 *p_vm = runtime->GetJavaVM();
2566 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002567}
2568
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 Runtime* runtime = Runtime::Current();
2571 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002572 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002574 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002575 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 }
2577 return JNI_OK;
2578}
2579
2580// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002581extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 return JNI_ERR;
2583}
2584
Elliott Hughescdf53122011-08-19 15:46:09 -07002585class JII {
2586 public:
2587 static jint DestroyJavaVM(JavaVM* vm) {
2588 if (vm == NULL) {
2589 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002590 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002591 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2592 delete raw_vm->runtime;
2593 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002597 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 }
2599
2600 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002601 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002602 }
2603
2604 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002605 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002606 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002607 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002608 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2609 Runtime* runtime = raw_vm->runtime;
2610 runtime->DetachCurrentThread();
2611 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002612 }
2613
2614 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2615 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2616 return JNI_EVERSION;
2617 }
2618 if (vm == NULL || env == NULL) {
2619 return JNI_ERR;
2620 }
2621 Thread* thread = Thread::Current();
2622 if (thread == NULL) {
2623 *env = NULL;
2624 return JNI_EDETACHED;
2625 }
2626 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 return JNI_OK;
2628 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002629};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002630
Elliott Hughes88c5c352012-03-15 18:49:48 -07002631const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002632 NULL, // reserved0
2633 NULL, // reserved1
2634 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002635 JII::DestroyJavaVM,
2636 JII::AttachCurrentThread,
2637 JII::DetachCurrentThread,
2638 JII::GetEnv,
2639 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002640};
2641
Elliott Hughesa0957642011-09-02 14:27:33 -07002642JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002643 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002644 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002645 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002646 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002647 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002648 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002649 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002650 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002651 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002652 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002653 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002654 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002655 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002656 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002657 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002658 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002659 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002660 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002661 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002662}
2663
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002664JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002665 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002666}
2667
Elliott Hughes88c5c352012-03-15 18:49:48 -07002668void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2669 check_jni = enabled;
2670 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002671}
2672
Elliott Hughesae80b492012-04-24 10:43:17 -07002673void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2674 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2675 if (force_copy) {
2676 os << " (with forcecopy)";
2677 }
2678 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002679 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002680 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002681 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002682 os << "; pins=" << pin_table.Size();
2683 }
2684 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002685 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002686 os << "; globals=" << globals.Capacity();
2687 }
2688 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002689 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002690 if (weak_globals.Capacity() > 0) {
2691 os << " (plus " << weak_globals.Capacity() << " weak)";
2692 }
2693 }
2694 os << '\n';
2695
2696 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002697 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002698 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2699 }
2700}
2701
Elliott Hughes73e66f72012-05-09 09:34:45 -07002702void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002703 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002704 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002705 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002706 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002707 }
2708 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002709 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002710 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002711 }
2712 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002713 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002714 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002715 }
2716}
2717
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002718bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2719 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002720 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002721
2722 // See if we've already loaded this library. If we have, and the class loader
2723 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002724 // TODO: for better results we should canonicalize the pathname (or even compare
2725 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002726 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002727 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002728 {
2729 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002730 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002731 library = libraries->Get(path);
2732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 if (library != NULL) {
2734 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 // The library will be associated with class_loader. The JNI
2736 // spec says we can't load the same library into more than one
2737 // class loader.
2738 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2739 "ClassLoader %p; can't open in ClassLoader %p",
2740 path.c_str(), library->GetClassLoader(), class_loader);
2741 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 return false;
2743 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002744 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2745 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002746 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002747 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2748 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002749 return false;
2750 }
2751 return true;
2752 }
2753
2754 // Open the shared library. Because we're using a full path, the system
2755 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2756 // resolve this library's dependencies though.)
2757
2758 // Failures here are expected when java.library.path has several entries
2759 // and we have to hunt for the lib.
2760
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002761 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2762 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2763 // dlopen) becomes zero from dlclose.
2764
Elliott Hughescdf53122011-08-19 15:46:09 -07002765 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002766 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002767 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2768 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2769 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002770
Elliott Hughes84b2f142012-09-27 09:16:28 -07002771 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002772
2773 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002774 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002775 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002776 return false;
2777 }
2778
2779 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002780 // TODO: move the locking (and more of this logic) into Libraries.
2781 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002782 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002783 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002784 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002785 if (library == NULL) { // We won race to get libraries_lock
2786 library = new SharedLibrary(path, handle, class_loader);
2787 libraries->Put(path, library);
2788 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002789 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002790 }
2791 if (!created_library) {
2792 LOG(INFO) << "WOW: we lost a race to add shared library: "
2793 << "\"" << path << "\" ClassLoader=" << class_loader;
2794 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002795 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002796
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002797 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002798
2799 bool result = true;
2800 void* sym = dlsym(handle, "JNI_OnLoad");
2801 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002802 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002803 } else {
2804 // Call JNI_OnLoad. We have to override the current class
2805 // loader, which will always be "null" since the stuff at the
2806 // top of the stack is around Runtime.loadLibrary(). (See
2807 // the comments in the JNI FindClass function.)
2808 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2809 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002810 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002811 self->SetClassLoaderOverride(class_loader);
2812
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002813 int version = 0;
2814 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002815 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002816 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002817 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002818 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002819
Brian Carlstromaded5f72011-10-07 17:15:04 -07002820 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002821
2822 if (version != JNI_VERSION_1_2 &&
2823 version != JNI_VERSION_1_4 &&
2824 version != JNI_VERSION_1_6) {
2825 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2826 << "bad version: " << version;
2827 // It's unwise to call dlclose() here, but we can mark it
2828 // as bad and ensure that future load attempts will fail.
2829 // We don't know how far JNI_OnLoad got, so there could
2830 // be some partially-initialized stuff accessible through
2831 // newly-registered native method calls. We could try to
2832 // unregister them, but that doesn't seem worthwhile.
2833 result = false;
2834 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002835 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2836 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002837 }
2838 }
2839
2840 library->SetResult(result);
2841 return result;
2842}
2843
Mathieu Chartier66f19252012-09-18 08:57:04 -07002844void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002845 CHECK(m->IsNative());
2846
2847 Class* c = m->GetDeclaringClass();
2848
2849 // If this is a static method, it could be called before the class
2850 // has been initialized.
2851 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002852 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002853 return NULL;
2854 }
2855 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002856 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002857 }
2858
Brian Carlstrom16192862011-09-12 17:50:06 -07002859 std::string detail;
2860 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002861 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002862 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002863 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002864 native_method = libraries->FindNativeMethod(m, detail);
2865 }
2866 // throwing can cause libraries_lock to be reacquired
2867 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002868 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002869 }
2870 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002871}
2872
Elliott Hughes410c0c82011-09-01 17:58:25 -07002873void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002874 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002875 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002876 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002877 globals.VisitRoots(visitor, arg);
2878 }
2879 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002880 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002881 pin_table.VisitRoots(visitor, arg);
2882 }
2883 // The weak_globals table is visited by the GC itself (because it mutates the table).
2884}
2885
Ian Rogersdf20fe02011-07-20 20:34:16 -07002886} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002887
2888std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2889 switch (rhs) {
2890 case JNIInvalidRefType:
2891 os << "JNIInvalidRefType";
2892 return os;
2893 case JNILocalRefType:
2894 os << "JNILocalRefType";
2895 return os;
2896 case JNIGlobalRefType:
2897 os << "JNIGlobalRefType";
2898 return os;
2899 case JNIWeakGlobalRefType:
2900 os << "JNIWeakGlobalRefType";
2901 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002902 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002903 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002904 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002905 }
2906}