blob: 8da208ff130d32e2b09b38baf5a301bfe507a89c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "gc/card_table-inl.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070031#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class-inl.h"
34#include "mirror/class_loader.h"
35#include "mirror/field-inl.h"
36#include "mirror/abstract_method-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070041#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070042#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070045#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070048#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070049
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050using namespace art::mirror;
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052namespace art {
53
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054static const size_t kMonitorsInitial = 32; // Arbitrary.
55static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
56
57static const size_t kLocalsInitial = 64; // Arbitrary.
58static const size_t kLocalsMax = 512; // Arbitrary sanity check.
59
60static const size_t kPinTableInitial = 16; // Arbitrary.
61static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
62
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070063static size_t gGlobalsInitial = 512; // Arbitrary.
64static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
66static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
67static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
68
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070069void SetJniGlobalsMax(size_t max) {
70 if (max != 0) {
71 gGlobalsMax = max;
72 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
73 }
74}
Ian Rogersdf20fe02011-07-20 20:34:16 -070075
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070078 if (obj == NULL) {
79 return NULL;
80 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070082 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070083 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070084 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
85 return reinterpret_cast<jweak>(ref);
86}
87
Mathieu Chartier66f19252012-09-18 08:57:04 -070088static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070090 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070091 const DexFile::TypeList* params = mh.GetParameterTypeList();
92 if (params == NULL) {
93 return; // No arguments so nothing to check.
94 }
95 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070096 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070097 for (uint32_t i = 0; i < num_params; i++) {
98 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
99 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
100 if (param_type == NULL) {
101 Thread* self = Thread::Current();
102 CHECK(self->IsExceptionPending());
103 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
104 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
105 << self->GetException()->Dump();
106 self->ClearException();
107 ++error_count;
108 } else if (!param_type->IsPrimitive()) {
109 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700110 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700112 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
113 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
114 ++error_count;
115 }
116 }
117 }
118 if (error_count > 0) {
119 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
120 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700121 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700122 }
123}
Elliott Hughesb264f082012-04-06 17:10:10 -0700124
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700126 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700129 CheckMethodArguments(method, args);
130 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700131 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700133 return result;
134}
135
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
137 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700140 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700141 MethodHelper mh(method);
142 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 arg_array.BuildArgArray(soa, args);
144 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700145}
146
Mathieu Chartier66f19252012-09-18 08:57:04 -0700147static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700149 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700150}
151
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
153 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700156 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 MethodHelper mh(method);
158 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 arg_array.BuildArgArray(soa, args);
160 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
164 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700167 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700168 MethodHelper mh(method);
169 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 arg_array.BuildArgArray(soa, args);
171 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700172}
173
Elliott Hughes6b436852011-08-12 10:16:44 -0700174// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
175// separated with slashes but aren't wrapped with "L;" like regular descriptors
176// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
177// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
178// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700179static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700180 std::string result;
181 // Add the missing "L;" if necessary.
182 if (name[0] == '[') {
183 result = name;
184 } else {
185 result += 'L';
186 result += name;
187 result += ';';
188 }
189 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700190 if (result.find('.') != std::string::npos) {
191 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
192 << "\"" << name << "\"";
193 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700194 }
195 return result;
196}
197
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
199 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800202 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700203}
204
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
206 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700209 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700210 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700211 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700212
Mathieu Chartier66f19252012-09-18 08:57:04 -0700213 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700214 if (is_static) {
215 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700216 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700217 method = c->FindVirtualMethod(name, sig);
218 if (method == NULL) {
219 // No virtual method matching the signature. Search declared
220 // private methods and constructors.
221 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700222 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700223 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700224
Elliott Hughescdf53122011-08-19 15:46:09 -0700225 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700227 return NULL;
228 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700229
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700231}
232
Ian Rogersef28b142012-11-30 14:22:18 -0800233static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800235 AbstractMethod* method = soa.Self()->GetCurrentMethod();
236 if (method == NULL ||
237 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
238 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700239 }
240 return method->GetDeclaringClass()->GetClassLoader();
241}
242
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
244 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700247 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700248 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700249 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700250
251 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700252 Class* field_type;
253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
254 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800255 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700256 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700257 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700259 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 if (field_type == NULL) {
261 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 DCHECK(soa.Self()->IsExceptionPending());
263 soa.Self()->ClearException();
264 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700265 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800266 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 return NULL;
268 }
269 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800272 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700274 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700276 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800277 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700278 return NULL;
279 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700281}
282
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700286 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700287 vm->pin_table.Add(array);
288}
289
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700293 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700294 vm->pin_table.Remove(array);
295}
296
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
298 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700300 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700302 "%s offset=%d length=%d %s.length=%d",
303 type.c_str(), start, length, identifier, array->GetLength());
304}
Ian Rogers0571d352011-11-03 19:51:38 -0700305
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
307 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700310 "offset=%d length=%d string.length()=%d", start, length, array_length);
311}
Elliott Hughes814e4032011-08-23 12:07:56 -0700312
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700314 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315 // Turn the const char* into a java.lang.String.
316 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
317 if (msg != NULL && s.get() == NULL) {
318 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700319 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700320
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 // Choose an appropriate constructor and set up the arguments.
322 jvalue args[2];
323 const char* signature;
324 if (msg == NULL && cause == NULL) {
325 signature = "()V";
326 } else if (msg != NULL && cause == NULL) {
327 signature = "(Ljava/lang/String;)V";
328 args[0].l = s.get();
329 } else if (msg == NULL && cause != NULL) {
330 signature = "(Ljava/lang/Throwable;)V";
331 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700332 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
334 args[0].l = s.get();
335 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
338 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800339 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 LOG(ERROR) << "No <init>" << signature << " in "
341 << PrettyClass(soa.Decode<Class*>(exception_class));
342 return JNI_ERR;
343 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700344
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
346 if (exception.get() == NULL) {
347 return JNI_ERR;
348 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700349
Ian Rogersef28b142012-11-30 14:22:18 -0800350 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700352
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700354}
355
Elliott Hughes462c9442012-03-23 18:47:50 -0700356static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700357 if (vm == NULL || p_env == NULL) {
358 return JNI_ERR;
359 }
360
Elliott Hughes462c9442012-03-23 18:47:50 -0700361 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700362 Thread* self = Thread::Current();
363 if (self != NULL) {
364 *p_env = self->GetJniEnv();
365 return JNI_OK;
366 }
367
368 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
369
370 // No threads allowed in zygote mode.
371 if (runtime->IsZygote()) {
372 LOG(ERROR) << "Attempt to attach a thread in the zygote";
373 return JNI_ERR;
374 }
375
Elliott Hughes462c9442012-03-23 18:47:50 -0700376 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
377 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700378 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700379 if (args != NULL) {
Brian Carlstrom86922152013-03-12 00:59:36 -0700380 if (args->version < JNI_VERSION_1_2) {
381 LOG(WARNING) << "Failed to AttachCurrentThread due to insufficent version "
382 << std::hex << args->version << " < JNI_VERSION_1_2(" << JNI_VERSION_1_2 << ")";
383 return JNI_ERR;
384 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700385 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700386 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700387 }
Elliott Hughes75770752011-08-24 17:52:38 -0700388
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800389 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700390 *p_env = NULL;
391 return JNI_ERR;
392 } else {
393 *p_env = Thread::Current()->GetJniEnv();
394 return JNI_OK;
395 }
Elliott Hughes75770752011-08-24 17:52:38 -0700396}
397
Elliott Hughes79082e32011-08-25 12:07:32 -0700398class SharedLibrary {
399 public:
400 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
401 : path_(path),
402 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700403 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700404 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700405 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700406 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700407 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700408 }
409
Elliott Hughes79082e32011-08-25 12:07:32 -0700410 Object* GetClassLoader() {
411 return class_loader_;
412 }
413
414 std::string GetPath() {
415 return path_;
416 }
417
418 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700419 * Check the result of an earlier call to JNI_OnLoad on this library.
420 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700421 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 bool CheckOnLoadResult()
423 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700425 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
427 bool okay;
428 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700429 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700430
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
432 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
433 // caller can continue.
434 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
435 okay = true;
436 } else {
437 while (jni_on_load_result_ == kPending) {
438 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700439 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700441
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700442 okay = (jni_on_load_result_ == kOkay);
443 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
444 << (okay ? "succeeded" : "failed") << "]";
445 }
446 }
447 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700448 return okay;
449 }
450
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700452 Thread* self = Thread::Current();
453 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700454
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700456 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700457
458 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700459 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700460 }
461
462 void* FindSymbol(const std::string& symbol_name) {
463 return dlsym(handle_, symbol_name.c_str());
464 }
465
466 private:
467 enum JNI_OnLoadState {
468 kPending,
469 kFailed,
470 kOkay,
471 };
472
473 // Path to library "/system/lib/libjni.so".
474 std::string path_;
475
476 // The void* returned by dlopen(3).
477 void* handle_;
478
479 // The ClassLoader this library is associated with.
480 Object* class_loader_;
481
482 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700485 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700487 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700488 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700489 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700490};
491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492// This exists mainly to keep implementation details out of the header file.
493class Libraries {
494 public:
495 Libraries() {
496 }
497
498 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700499 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
Elliott Hughesae80b492012-04-24 10:43:17 -0700502 void Dump(std::ostream& os) const {
503 bool first = true;
504 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
505 if (!first) {
506 os << ' ';
507 }
508 first = false;
509 os << it->first;
510 }
511 }
512
513 size_t size() const {
514 return libraries_.size();
515 }
516
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700518 It it = libraries_.find(path);
519 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 }
521
522 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700523 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700524 }
525
526 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700527 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 std::string jni_short_name(JniShortName(m));
530 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700531 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
533 SharedLibrary* library = it->second;
534 if (library->GetClassLoader() != declaring_class_loader) {
535 // We only search libraries loaded by the appropriate ClassLoader.
536 continue;
537 }
538 // Try the short name then the long name...
539 void* fn = library->FindSymbol(jni_short_name);
540 if (fn == NULL) {
541 fn = library->FindSymbol(jni_long_name);
542 }
543 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800544 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
545 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 return fn;
547 }
548 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700550 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700551 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 return NULL;
554 }
555
556 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700557 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700558
Elliott Hughesa0e18062012-04-13 15:59:59 -0700559 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700560};
561
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
563 jvalue* args) {
564 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700565 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700566 MethodHelper mh(method);
567 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568 arg_array.BuildArgArray(soa, args);
569 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700570}
571
Mathieu Chartier66f19252012-09-18 08:57:04 -0700572JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800576}
577
Elliott Hughescdf53122011-08-19 15:46:09 -0700578class JNI {
579 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700580 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700581 return JNI_VERSION_1_6;
582 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700583
Ian Rogers25e8b912012-09-07 11:31:36 -0700584 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700585 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700586 return NULL;
587 }
588
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700591 Runtime* runtime = Runtime::Current();
592 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700593 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700594 Class* c = NULL;
595 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800596 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800597 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700598 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800599 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700600 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700602 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700603
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700606 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700608 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700609
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
612 Field* field = soa.Decode<Field*>(java_field);
613 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700618 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
624 Field* field = soa.DecodeField(fid);
625 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700627
Elliott Hughes37f7a402011-08-22 18:56:01 -0700628 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 ScopedObjectAccess soa(env);
630 Object* o = soa.Decode<Object*>(java_object);
631 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700632 }
633
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700634 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
636 Class* c = soa.Decode<Class*>(java_class);
637 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Elliott Hughes37f7a402011-08-22 18:56:01 -0700640 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
642 Class* c1 = soa.Decode<Class*>(java_class1);
643 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700644 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughese84278b2012-03-22 10:06:53 -0700647 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800649 if (java_class == NULL) {
650 JniAbortF("IsInstanceOf", "null class (second argument)");
651 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700652 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700653 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 return JNI_TRUE;
655 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 Object* obj = soa.Decode<Object*>(jobj);
657 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700658 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700661
Elliott Hughes37f7a402011-08-22 18:56:01 -0700662 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
664 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 if (exception == NULL) {
666 return JNI_ERR;
667 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 return JNI_OK;
670 }
671
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700672 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700673 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
676 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700677 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 }
679
680 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700681 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 }
683
684 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700686
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700688 Throwable* original_exception = self->GetException();
689 self->ClearException();
690
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700691 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700692 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
693 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
694 if (mid == NULL) {
695 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700696 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 } else {
698 env->CallVoidMethod(exception.get(), mid);
699 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700700 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 << " thrown while calling printStackTrace";
702 self->ClearException();
703 }
704 }
705
706 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
711 Object* exception = soa.Self()->GetException();
712 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
714
Ian Rogers25e8b912012-09-07 11:31:36 -0700715 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 LOG(FATAL) << "JNI FatalError called: " << msg;
717 }
718
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700719 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800720 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700721 return JNI_ERR;
722 }
Ian Rogersef28b142012-11-30 14:22:18 -0800723 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 return JNI_OK;
725 }
726
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700727 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
729 Object* survivor = soa.Decode<Object*>(java_survivor);
730 soa.Env()->PopFrame();
731 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 }
733
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700734 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800735 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 }
737
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 if (obj == NULL) {
740 return NULL;
741 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700742 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700746 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 return reinterpret_cast<jobject>(ref);
749 }
750
751 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 if (obj == NULL) {
753 return;
754 }
Ian Rogersef28b142012-11-30 14:22:18 -0800755 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800757 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
758 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700759
760 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
761 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
762 << "failed to find entry";
763 }
764 }
765
766 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 ScopedObjectAccess soa(env);
768 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 }
770
771 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 if (obj == NULL) {
773 return;
774 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700775 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700778 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779
780 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
781 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
782 << "failed to find entry";
783 }
784 }
785
786 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 if (obj == NULL) {
788 return NULL;
789 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700790 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700791 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700792
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793 uint32_t cookie = soa.Env()->local_ref_cookie;
794 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 return reinterpret_cast<jobject>(ref);
796 }
797
798 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 if (obj == NULL) {
800 return;
801 }
Ian Rogersef28b142012-11-30 14:22:18 -0800802 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700803
Ian Rogersef28b142012-11-30 14:22:18 -0800804 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 if (!locals.Remove(cookie, obj)) {
806 // Attempting to delete a local reference that is not in the
807 // topmost local reference frame is a no-op. DeleteLocalRef returns
808 // void and doesn't throw any exceptions, but we should probably
809 // complain about it so the user will notice that things aren't
810 // going quite the way they expect.
811 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
812 << "failed to find entry";
813 }
814 }
815
816 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700817 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700818 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
820
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700821 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700822 ScopedObjectAccess soa(env);
823 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700824 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700825 return NULL;
826 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700827 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
829
Elliott Hughese84278b2012-03-22 10:06:53 -0700830 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700833 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 va_end(args);
835 return result;
836 }
837
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 ScopedObjectAccess soa(env);
840 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700841 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700842 return NULL;
843 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700844 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700845 if (result == NULL) {
846 return NULL;
847 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700851 return local_result;
852 } else {
853 return NULL;
854 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(env);
859 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700860 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700861 return NULL;
862 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700863 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700864 if (result == NULL) {
865 return NULL;
866 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700869 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700870 return local_result;
871 } else {
872 return NULL;
873 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
878 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
881 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 ScopedObjectAccess soa(env);
883 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_list ap;
888 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700889 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 ScopedObjectAccess soa(env);
897 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
898 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
903 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
904 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_list ap;
909 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700910 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700913 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 ScopedObjectAccess soa(env);
918 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 ScopedObjectAccess soa(env);
923 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 va_list ap;
929 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700932 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 }
934
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 ScopedObjectAccess soa(env);
937 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
942 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_list ap;
947 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700948 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700951 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 ScopedObjectAccess soa(env);
956 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
961 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_list ap;
966 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700967 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700970 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
975 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
980 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_list ap;
986 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700989 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 ScopedObjectAccess soa(env);
994 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_list ap;
1004 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001005 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001008 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
1013 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 ScopedObjectAccess soa(env);
1018 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_list ap;
1023 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001024 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001027 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
1032 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
1037 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001043 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001046 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 ScopedObjectAccess soa(env);
1051 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001062 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001063 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ScopedObjectAccess soa(env);
1069 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(env);
1074 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001077 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001080 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1082 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 va_end(ap);
1084 return local_result;
1085 }
1086
1087 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001088 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(env);
1090 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1091 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
1094 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001095 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
1097 JValue result(InvokeWithJValues(soa, obj, mid, args));
1098 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
1101 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001102 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001105 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001108 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
1111 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001112 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
1117 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001118 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001123 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001126 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
1132 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001133 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(env);
1135 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
1138 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001139 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001144 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
1153 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001154 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 ScopedObjectAccess soa(env);
1156 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
1159 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001160 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001165 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001168 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001171 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
1174 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001175 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(env);
1177 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001186 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001189 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001192 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
1195 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001196 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 ScopedObjectAccess soa(env);
1198 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001207 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001210 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001213 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
1216 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001217 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
1219 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
1222 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
1225 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001228 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001231 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001234 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
1237 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001238 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
1240 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001244 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
1246 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001252 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001255 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001259 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001260 ScopedObjectAccess soa(env);
1261 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001265 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001270 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001273 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 va_end(ap);
1276 }
1277
1278 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 ScopedObjectAccess soa(env);
1281 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001290 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001294
1295
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001296 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 ScopedObjectAccess soa(env);
1298 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001300
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001301 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 ScopedObjectAccess soa(env);
1303 Object* o = soa.Decode<Object*>(obj);
1304 Field* f = soa.DecodeField(fid);
1305 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001307
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001308 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
1310 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001311 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001314 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 ScopedObjectAccess soa(env);
1316 Object* o = soa.Decode<Object*>(java_object);
1317 Object* v = soa.Decode<Object*>(java_value);
1318 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001319 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001322 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001323 ScopedObjectAccess soa(env);
1324 Object* v = soa.Decode<Object*>(java_value);
1325 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001326 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001329#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 ScopedObjectAccess soa(env); \
1331 Object* o = soa.Decode<Object*>(instance); \
1332 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001333 return f->fn(o)
1334
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001335#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1336 ScopedObjectAccess soa(env); \
1337 Field* f = soa.DecodeField(fid); \
1338 return f->fn(f->GetDeclaringClass())
1339
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env); \
1342 Object* o = soa.Decode<Object*>(instance); \
1343 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001344 f->fn(o, value)
1345
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001346#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1347 ScopedObjectAccess soa(env); \
1348 Field* f = soa.DecodeField(fid); \
1349 f->fn(f->GetDeclaringClass(), value)
1350
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1352 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1356 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1360 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1364 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1368 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1372 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1376 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1380 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001384 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001388 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001392 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001396 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001400 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001403 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001404 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001408 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 }
1410
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001412 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 }
1414
1415 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1416 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1417 }
1418
1419 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1420 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1421 }
1422
1423 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1424 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1425 }
1426
1427 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1428 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1429 }
1430
1431 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1432 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1433 }
1434
1435 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1436 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1437 }
1438
1439 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1440 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1441 }
1442
1443 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1444 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1445 }
1446
1447 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001448 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 }
1450
1451 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001452 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 }
1454
1455 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001456 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 }
1458
1459 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001460 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 }
1462
1463 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001464 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 }
1466
1467 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001468 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 }
1470
1471 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001472 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 }
1474
1475 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001476 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001481 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001482 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1484 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 va_end(ap);
1486 return local_result;
1487 }
1488
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001489 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 ScopedObjectAccess soa(env);
1491 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1492 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001496 ScopedObjectAccess soa(env);
1497 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1498 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001501 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001503 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001504 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001507 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001510 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 ScopedObjectAccess soa(env);
1512 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001515 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001516 ScopedObjectAccess soa(env);
1517 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001522 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001523 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001526 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001529 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 ScopedObjectAccess soa(env);
1531 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 ScopedObjectAccess soa(env);
1536 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001541 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001542 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001545 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
1550 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env);
1555 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001561 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001564 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001567 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 ScopedObjectAccess soa(env);
1569 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001573 ScopedObjectAccess soa(env);
1574 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001580 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001583 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001586 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 ScopedObjectAccess soa(env);
1588 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001592 ScopedObjectAccess soa(env);
1593 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001599 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001602 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001606 ScopedObjectAccess soa(env);
1607 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001611 ScopedObjectAccess soa(env);
1612 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001618 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001619 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001621 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 ScopedObjectAccess soa(env);
1626 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001630 ScopedObjectAccess soa(env);
1631 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001637 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001640 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001643 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 ScopedObjectAccess soa(env);
1645 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
1650 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001656 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 va_end(ap);
1659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 ScopedObjectAccess soa(env);
1663 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 ScopedObjectAccess soa(env);
1668 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes814e4032011-08-23 12:07:56 -07001671 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001672 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001673 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
1677 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 if (utf == NULL) {
1679 return NULL;
1680 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001682 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes814e4032011-08-23 12:07:56 -07001686 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
1688 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001689 }
1690
1691 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ScopedObjectAccess soa(env);
1693 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001694 }
1695
Elliott Hughesb465ab02011-08-24 11:21:21 -07001696 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
1698 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001699 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001701 } else {
1702 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1703 memcpy(buf, chars + start, length * sizeof(jchar));
1704 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001705 }
1706
Elliott Hughesb465ab02011-08-24 11:21:21 -07001707 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
1709 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001710 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001712 } else {
1713 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1714 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1715 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001716 }
1717
Elliott Hughes75770752011-08-24 17:52:38 -07001718 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 ScopedObjectAccess soa(env);
1720 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001721 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001723 if (is_copy != NULL) {
1724 *is_copy = JNI_FALSE;
1725 }
1726 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 ScopedObjectAccess soa(env);
1731 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes75770752011-08-24 17:52:38 -07001734 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001735 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes75770752011-08-24 17:52:38 -07001738 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001740 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes75770752011-08-24 17:52:38 -07001743 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
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 Rogersef28b142012-11-30 14:22:18 -08001750 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001752 size_t byte_count = s->GetUtfLength();
1753 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001754 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001755 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1756 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1757 bytes[byte_count] = '\0';
1758 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001759 }
1760
Elliott Hughes75770752011-08-24 17:52:38 -07001761 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
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);
Elliott Hughes96a98872012-12-19 14:21:15 -08001768 if (!obj->IsArrayInstance()) {
1769 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1770 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001771 Array* array = obj->AsArray();
1772 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes814e4032011-08-23 12:07:56 -07001775 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 ScopedObjectAccess soa(env);
1777 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1778 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
1781 static void SetObjectArrayElement(JNIEnv* env,
1782 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
1784 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1785 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 array->Set(index, value);
1787 }
1788
1789 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001790 ScopedObjectAccess soa(env);
1791 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
1794 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
1796 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
1799 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001800 ScopedObjectAccess soa(env);
1801 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
1804 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
1809 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 ScopedObjectAccess soa(env);
1811 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 ScopedObjectAccess soa(env);
1816 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
1819 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 ScopedObjectAccess soa(env);
1821 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
1824 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001825 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001826 if (length < 0) {
1827 JniAbortF("NewObjectArray", "negative array length: %d", length);
1828 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001829
1830 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001831 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 std::string descriptor;
1833 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001834 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835
1836 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001837 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1838 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 return NULL;
1840 }
1841
Elliott Hughes75770752011-08-24 17:52:38 -07001842 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001844 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001847 for (jsize i = 0; i < length; ++i) {
1848 result->Set(i, initial_object);
1849 }
1850 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
1854 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
1856 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Ian Rogersa15e67d2012-02-28 13:51:55 -08001859 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
1861 Array* array = soa.Decode<Array*>(java_array);
1862 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001863 if (is_copy != NULL) {
1864 *is_copy = JNI_FALSE;
1865 }
1866 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001870 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001871 }
1872
Elliott Hughes75770752011-08-24 17:52:38 -07001873 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 ScopedObjectAccess soa(env);
1875 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes75770752011-08-24 17:52:38 -07001878 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
1880 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
Elliott Hughes75770752011-08-24 17:52:38 -07001883 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
1885 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes75770752011-08-24 17:52:38 -07001888 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
1890 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes75770752011-08-24 17:52:38 -07001893 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 ScopedObjectAccess soa(env);
1895 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Elliott Hughes75770752011-08-24 17:52:38 -07001898 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
1900 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes75770752011-08-24 17:52:38 -07001903 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 ScopedObjectAccess soa(env);
1905 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes75770752011-08-24 17:52:38 -07001908 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 ScopedObjectAccess soa(env);
1910 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001914 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001917 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001918 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001921 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001922 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001926 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001929 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001930 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001933 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001934 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001937 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001938 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001941 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001942 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes814e4032011-08-23 12:07:56 -07001945 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
1947 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes814e4032011-08-23 12:07:56 -07001950 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
1952 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
1957 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
1962 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 ScopedObjectAccess soa(env);
1977 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
1982 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ScopedObjectAccess soa(env);
1987 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
1992 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
1997 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes814e4032011-08-23 12:07:56 -07002000 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
2002 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes814e4032011-08-23 12:07:56 -07002005 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
2007 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ScopedObjectAccess soa(env);
2012 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(env);
2017 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
2022 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes5174fe62011-08-23 15:12:35 -07002025 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002026 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2027 }
2028
2029 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, size_t method_count, bool return_errors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(env);
2031 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002032
Elliott Hughesc8fece32013-01-02 11:27:23 -08002033 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 const char* name = methods[i].name;
2035 const char* sig = methods[i].signature;
2036
2037 if (*sig == '!') {
2038 // TODO: fast jni. it's too noisy to log all these.
2039 ++sig;
2040 }
2041
Mathieu Chartier66f19252012-09-18 08:57:04 -07002042 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002043 if (m == NULL) {
2044 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002046 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002047 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2048 << PrettyDescriptor(c) << "." << 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()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002052 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2053 << PrettyDescriptor(c) << "." << name << sig
2054 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002055 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 return JNI_ERR;
2057 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002058
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002059 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002060
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002061 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063 return JNI_OK;
2064 }
2065
Elliott Hughes5174fe62011-08-23 15:12:35 -07002066 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 ScopedObjectAccess soa(env);
2068 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002069
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002070 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002071
2072 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002073 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002074 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002076 }
2077 }
2078 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002079 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002080 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002082 }
2083 }
2084
2085 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2089 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2090 ScopedObjectAccess soa(env);
2091 Object* o = soa.Decode<Object*>(java_object);
2092 o->MonitorEnter(soa.Self());
2093 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002094 return JNI_ERR;
2095 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002097 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 static jint MonitorExit(JNIEnv* env, jobject java_object)
2101 UNLOCK_FUNCTION(monitor_lock_) {
2102 ScopedObjectAccess soa(env);
2103 Object* o = soa.Decode<Object*>(java_object);
2104 o->MonitorExit(soa.Self());
2105 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002106 return JNI_ERR;
2107 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002109 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
2112 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 Runtime* runtime = Runtime::Current();
2114 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002115 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 } else {
2117 *vm = NULL;
2118 }
2119 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2120 }
2121
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002123 if (capacity < 0) {
2124 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2125 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002126 if (address == NULL && capacity != 0) {
2127 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002128 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002129
Elliott Hughes96a98872012-12-19 14:21:15 -08002130 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002131 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2132 CHECK_LE(capacity, 0xffffffff);
2133 jint address_arg = reinterpret_cast<jint>(address);
2134 jint capacity_arg = static_cast<jint>(capacity);
2135
Elliott Hugheseac76672012-05-24 21:56:51 -07002136 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2137 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2138 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002139 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughesb465ab02011-08-24 11:21:21 -07002142 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002143 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughesb465ab02011-08-24 11:21:21 -07002146 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
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) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002151 if (java_object == NULL) {
2152 JniAbortF("GetObjectRefType", "null object");
2153 }
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 Rogersef28b142012-11-30 14:22:18 -08002160 if (static_cast<JNIEnvExt*>(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 Rogersef28b142012-11-30 14:22:18 -08002170 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002171 return JNILocalRefType;
2172 }
2173
Ian Rogersef28b142012-11-30 14:22:18 -08002174 if (!static_cast<JNIEnvExt*>(env)->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 Rogersef28b142012-11-30 14:22:18 -08002180 {
2181 ScopedObjectAccess soa(env);
2182 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2183 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2184 return JNILocalRefType;
2185 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002186 }
2187 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002188 return JNIInvalidRefType;
2189 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002190 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2191 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193
2194 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002195 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2196 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 // TODO: we should try to expand the table if necessary.
2198 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2199 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2200 return JNI_ERR;
2201 }
2202 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002203 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2205 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002206 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002207 soa.Self()->ThrowOutOfMemoryError(caller);
2208 }
2209 return okay ? JNI_OK : JNI_ERR;
2210 }
2211
2212 template<typename JniT, typename ArtT>
2213 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002215 if (length < 0) {
2216 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2217 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002218 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002219 return soa.AddLocalReference<JniT>(result);
2220 }
2221
2222 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2223 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2224 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2227 PinPrimitiveArray(soa, array);
2228 if (is_copy != NULL) {
2229 *is_copy = JNI_FALSE;
2230 }
2231 return array->GetData();
2232 }
2233
2234 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002235 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002237 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002238 Array* array = soa.Decode<Array*>(java_array);
2239 UnpinPrimitiveArray(soa, array);
2240 }
2241 }
2242
2243 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2244 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2245 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2248 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2249 ThrowAIOOBE(soa, array, start, length, "src");
2250 } else {
2251 JavaT* data = array->GetData();
2252 memcpy(buf, data + start, length * sizeof(JavaT));
2253 }
2254 }
2255
2256 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2257 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2258 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2261 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2262 ThrowAIOOBE(soa, array, start, length, "dst");
2263 } else {
2264 JavaT* data = array->GetData();
2265 memcpy(data + start, buf, length * sizeof(JavaT));
2266 }
2267 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002268};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002269
Elliott Hughes88c5c352012-03-15 18:49:48 -07002270const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002271 NULL, // reserved0.
2272 NULL, // reserved1.
2273 NULL, // reserved2.
2274 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 JNI::GetVersion,
2276 JNI::DefineClass,
2277 JNI::FindClass,
2278 JNI::FromReflectedMethod,
2279 JNI::FromReflectedField,
2280 JNI::ToReflectedMethod,
2281 JNI::GetSuperclass,
2282 JNI::IsAssignableFrom,
2283 JNI::ToReflectedField,
2284 JNI::Throw,
2285 JNI::ThrowNew,
2286 JNI::ExceptionOccurred,
2287 JNI::ExceptionDescribe,
2288 JNI::ExceptionClear,
2289 JNI::FatalError,
2290 JNI::PushLocalFrame,
2291 JNI::PopLocalFrame,
2292 JNI::NewGlobalRef,
2293 JNI::DeleteGlobalRef,
2294 JNI::DeleteLocalRef,
2295 JNI::IsSameObject,
2296 JNI::NewLocalRef,
2297 JNI::EnsureLocalCapacity,
2298 JNI::AllocObject,
2299 JNI::NewObject,
2300 JNI::NewObjectV,
2301 JNI::NewObjectA,
2302 JNI::GetObjectClass,
2303 JNI::IsInstanceOf,
2304 JNI::GetMethodID,
2305 JNI::CallObjectMethod,
2306 JNI::CallObjectMethodV,
2307 JNI::CallObjectMethodA,
2308 JNI::CallBooleanMethod,
2309 JNI::CallBooleanMethodV,
2310 JNI::CallBooleanMethodA,
2311 JNI::CallByteMethod,
2312 JNI::CallByteMethodV,
2313 JNI::CallByteMethodA,
2314 JNI::CallCharMethod,
2315 JNI::CallCharMethodV,
2316 JNI::CallCharMethodA,
2317 JNI::CallShortMethod,
2318 JNI::CallShortMethodV,
2319 JNI::CallShortMethodA,
2320 JNI::CallIntMethod,
2321 JNI::CallIntMethodV,
2322 JNI::CallIntMethodA,
2323 JNI::CallLongMethod,
2324 JNI::CallLongMethodV,
2325 JNI::CallLongMethodA,
2326 JNI::CallFloatMethod,
2327 JNI::CallFloatMethodV,
2328 JNI::CallFloatMethodA,
2329 JNI::CallDoubleMethod,
2330 JNI::CallDoubleMethodV,
2331 JNI::CallDoubleMethodA,
2332 JNI::CallVoidMethod,
2333 JNI::CallVoidMethodV,
2334 JNI::CallVoidMethodA,
2335 JNI::CallNonvirtualObjectMethod,
2336 JNI::CallNonvirtualObjectMethodV,
2337 JNI::CallNonvirtualObjectMethodA,
2338 JNI::CallNonvirtualBooleanMethod,
2339 JNI::CallNonvirtualBooleanMethodV,
2340 JNI::CallNonvirtualBooleanMethodA,
2341 JNI::CallNonvirtualByteMethod,
2342 JNI::CallNonvirtualByteMethodV,
2343 JNI::CallNonvirtualByteMethodA,
2344 JNI::CallNonvirtualCharMethod,
2345 JNI::CallNonvirtualCharMethodV,
2346 JNI::CallNonvirtualCharMethodA,
2347 JNI::CallNonvirtualShortMethod,
2348 JNI::CallNonvirtualShortMethodV,
2349 JNI::CallNonvirtualShortMethodA,
2350 JNI::CallNonvirtualIntMethod,
2351 JNI::CallNonvirtualIntMethodV,
2352 JNI::CallNonvirtualIntMethodA,
2353 JNI::CallNonvirtualLongMethod,
2354 JNI::CallNonvirtualLongMethodV,
2355 JNI::CallNonvirtualLongMethodA,
2356 JNI::CallNonvirtualFloatMethod,
2357 JNI::CallNonvirtualFloatMethodV,
2358 JNI::CallNonvirtualFloatMethodA,
2359 JNI::CallNonvirtualDoubleMethod,
2360 JNI::CallNonvirtualDoubleMethodV,
2361 JNI::CallNonvirtualDoubleMethodA,
2362 JNI::CallNonvirtualVoidMethod,
2363 JNI::CallNonvirtualVoidMethodV,
2364 JNI::CallNonvirtualVoidMethodA,
2365 JNI::GetFieldID,
2366 JNI::GetObjectField,
2367 JNI::GetBooleanField,
2368 JNI::GetByteField,
2369 JNI::GetCharField,
2370 JNI::GetShortField,
2371 JNI::GetIntField,
2372 JNI::GetLongField,
2373 JNI::GetFloatField,
2374 JNI::GetDoubleField,
2375 JNI::SetObjectField,
2376 JNI::SetBooleanField,
2377 JNI::SetByteField,
2378 JNI::SetCharField,
2379 JNI::SetShortField,
2380 JNI::SetIntField,
2381 JNI::SetLongField,
2382 JNI::SetFloatField,
2383 JNI::SetDoubleField,
2384 JNI::GetStaticMethodID,
2385 JNI::CallStaticObjectMethod,
2386 JNI::CallStaticObjectMethodV,
2387 JNI::CallStaticObjectMethodA,
2388 JNI::CallStaticBooleanMethod,
2389 JNI::CallStaticBooleanMethodV,
2390 JNI::CallStaticBooleanMethodA,
2391 JNI::CallStaticByteMethod,
2392 JNI::CallStaticByteMethodV,
2393 JNI::CallStaticByteMethodA,
2394 JNI::CallStaticCharMethod,
2395 JNI::CallStaticCharMethodV,
2396 JNI::CallStaticCharMethodA,
2397 JNI::CallStaticShortMethod,
2398 JNI::CallStaticShortMethodV,
2399 JNI::CallStaticShortMethodA,
2400 JNI::CallStaticIntMethod,
2401 JNI::CallStaticIntMethodV,
2402 JNI::CallStaticIntMethodA,
2403 JNI::CallStaticLongMethod,
2404 JNI::CallStaticLongMethodV,
2405 JNI::CallStaticLongMethodA,
2406 JNI::CallStaticFloatMethod,
2407 JNI::CallStaticFloatMethodV,
2408 JNI::CallStaticFloatMethodA,
2409 JNI::CallStaticDoubleMethod,
2410 JNI::CallStaticDoubleMethodV,
2411 JNI::CallStaticDoubleMethodA,
2412 JNI::CallStaticVoidMethod,
2413 JNI::CallStaticVoidMethodV,
2414 JNI::CallStaticVoidMethodA,
2415 JNI::GetStaticFieldID,
2416 JNI::GetStaticObjectField,
2417 JNI::GetStaticBooleanField,
2418 JNI::GetStaticByteField,
2419 JNI::GetStaticCharField,
2420 JNI::GetStaticShortField,
2421 JNI::GetStaticIntField,
2422 JNI::GetStaticLongField,
2423 JNI::GetStaticFloatField,
2424 JNI::GetStaticDoubleField,
2425 JNI::SetStaticObjectField,
2426 JNI::SetStaticBooleanField,
2427 JNI::SetStaticByteField,
2428 JNI::SetStaticCharField,
2429 JNI::SetStaticShortField,
2430 JNI::SetStaticIntField,
2431 JNI::SetStaticLongField,
2432 JNI::SetStaticFloatField,
2433 JNI::SetStaticDoubleField,
2434 JNI::NewString,
2435 JNI::GetStringLength,
2436 JNI::GetStringChars,
2437 JNI::ReleaseStringChars,
2438 JNI::NewStringUTF,
2439 JNI::GetStringUTFLength,
2440 JNI::GetStringUTFChars,
2441 JNI::ReleaseStringUTFChars,
2442 JNI::GetArrayLength,
2443 JNI::NewObjectArray,
2444 JNI::GetObjectArrayElement,
2445 JNI::SetObjectArrayElement,
2446 JNI::NewBooleanArray,
2447 JNI::NewByteArray,
2448 JNI::NewCharArray,
2449 JNI::NewShortArray,
2450 JNI::NewIntArray,
2451 JNI::NewLongArray,
2452 JNI::NewFloatArray,
2453 JNI::NewDoubleArray,
2454 JNI::GetBooleanArrayElements,
2455 JNI::GetByteArrayElements,
2456 JNI::GetCharArrayElements,
2457 JNI::GetShortArrayElements,
2458 JNI::GetIntArrayElements,
2459 JNI::GetLongArrayElements,
2460 JNI::GetFloatArrayElements,
2461 JNI::GetDoubleArrayElements,
2462 JNI::ReleaseBooleanArrayElements,
2463 JNI::ReleaseByteArrayElements,
2464 JNI::ReleaseCharArrayElements,
2465 JNI::ReleaseShortArrayElements,
2466 JNI::ReleaseIntArrayElements,
2467 JNI::ReleaseLongArrayElements,
2468 JNI::ReleaseFloatArrayElements,
2469 JNI::ReleaseDoubleArrayElements,
2470 JNI::GetBooleanArrayRegion,
2471 JNI::GetByteArrayRegion,
2472 JNI::GetCharArrayRegion,
2473 JNI::GetShortArrayRegion,
2474 JNI::GetIntArrayRegion,
2475 JNI::GetLongArrayRegion,
2476 JNI::GetFloatArrayRegion,
2477 JNI::GetDoubleArrayRegion,
2478 JNI::SetBooleanArrayRegion,
2479 JNI::SetByteArrayRegion,
2480 JNI::SetCharArrayRegion,
2481 JNI::SetShortArrayRegion,
2482 JNI::SetIntArrayRegion,
2483 JNI::SetLongArrayRegion,
2484 JNI::SetFloatArrayRegion,
2485 JNI::SetDoubleArrayRegion,
2486 JNI::RegisterNatives,
2487 JNI::UnregisterNatives,
2488 JNI::MonitorEnter,
2489 JNI::MonitorExit,
2490 JNI::GetJavaVM,
2491 JNI::GetStringRegion,
2492 JNI::GetStringUTFRegion,
2493 JNI::GetPrimitiveArrayCritical,
2494 JNI::ReleasePrimitiveArrayCritical,
2495 JNI::GetStringCritical,
2496 JNI::ReleaseStringCritical,
2497 JNI::NewWeakGlobalRef,
2498 JNI::DeleteWeakGlobalRef,
2499 JNI::ExceptionCheck,
2500 JNI::NewDirectByteBuffer,
2501 JNI::GetDirectBufferAddress,
2502 JNI::GetDirectBufferCapacity,
2503 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002504};
2505
Elliott Hughes75770752011-08-24 17:52:38 -07002506JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002507 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002508 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002509 local_ref_cookie(IRT_FIRST_SEGMENT),
2510 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002511 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002512 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002513 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002514 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002515 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002516 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002517 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002518 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2519 // errors will ensue.
2520 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2521 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002522}
2523
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002524JNIEnvExt::~JNIEnvExt() {
2525}
2526
Elliott Hughes88c5c352012-03-15 18:49:48 -07002527void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2528 check_jni = enabled;
2529 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002530}
2531
Elliott Hughes73e66f72012-05-09 09:34:45 -07002532void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2533 locals.Dump(os);
2534 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002535}
2536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002537void JNIEnvExt::PushFrame(int /*capacity*/) {
2538 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002539 stacked_local_ref_cookies.push_back(local_ref_cookie);
2540 local_ref_cookie = locals.GetSegmentState();
2541}
2542
2543void JNIEnvExt::PopFrame() {
2544 locals.SetSegmentState(local_ref_cookie);
2545 local_ref_cookie = stacked_local_ref_cookies.back();
2546 stacked_local_ref_cookies.pop_back();
2547}
2548
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002549Offset JNIEnvExt::SegmentStateOffset() {
2550 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2551 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2552}
2553
Carl Shapiroea4dca82011-08-01 13:45:38 -07002554// JNI Invocation interface.
2555
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002556extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2557 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2558 if (args->version < JNI_VERSION_1_2) {
2559 return JNI_EVERSION;
2560 }
2561 Runtime::Options options;
2562 for (int i = 0; i < args->nOptions; ++i) {
2563 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002564 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 }
2566 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002567 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002568 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002571 bool started = runtime->Start();
2572 if (!started) {
2573 delete Thread::Current()->GetJniEnv();
2574 delete runtime->GetJavaVM();
2575 LOG(WARNING) << "CreateJavaVM failed";
2576 return JNI_ERR;
2577 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002578 *p_env = Thread::Current()->GetJniEnv();
2579 *p_vm = runtime->GetJavaVM();
2580 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581}
2582
Elliott Hughesf2682d52011-08-15 16:37:04 -07002583extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584 Runtime* runtime = Runtime::Current();
2585 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002586 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002587 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002588 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002589 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002590 }
2591 return JNI_OK;
2592}
2593
2594// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002595extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596 return JNI_ERR;
2597}
2598
Elliott Hughescdf53122011-08-19 15:46:09 -07002599class JII {
2600 public:
2601 static jint DestroyJavaVM(JavaVM* vm) {
2602 if (vm == NULL) {
2603 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002604 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002605 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2606 delete raw_vm->runtime;
2607 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609
Elliott Hughescdf53122011-08-19 15:46:09 -07002610 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002611 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002612 }
2613
2614 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002615 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002616 }
2617
2618 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002619 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002620 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002621 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002622 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2623 Runtime* runtime = raw_vm->runtime;
2624 runtime->DetachCurrentThread();
2625 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002626 }
2627
2628 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2629 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2630 return JNI_EVERSION;
2631 }
2632 if (vm == NULL || env == NULL) {
2633 return JNI_ERR;
2634 }
2635 Thread* thread = Thread::Current();
2636 if (thread == NULL) {
2637 *env = NULL;
2638 return JNI_EDETACHED;
2639 }
2640 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 return JNI_OK;
2642 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002643};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002644
Elliott Hughes88c5c352012-03-15 18:49:48 -07002645const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002646 NULL, // reserved0
2647 NULL, // reserved1
2648 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002649 JII::DestroyJavaVM,
2650 JII::AttachCurrentThread,
2651 JII::DetachCurrentThread,
2652 JII::GetEnv,
2653 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002654};
2655
Elliott Hughesa0957642011-09-02 14:27:33 -07002656JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002657 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002658 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002659 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002660 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002661 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002662 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002663 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002664 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002665 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002666 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002667 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002668 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002669 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002670 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002671 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002672 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002673 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002674 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002675 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002676}
2677
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002678JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002679 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002680}
2681
Elliott Hughes88c5c352012-03-15 18:49:48 -07002682void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2683 check_jni = enabled;
2684 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002685}
2686
Elliott Hughesae80b492012-04-24 10:43:17 -07002687void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2688 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2689 if (force_copy) {
2690 os << " (with forcecopy)";
2691 }
2692 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002693 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002694 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002695 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002696 os << "; pins=" << pin_table.Size();
2697 }
2698 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002699 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002700 os << "; globals=" << globals.Capacity();
2701 }
2702 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002703 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002704 if (weak_globals.Capacity() > 0) {
2705 os << " (plus " << weak_globals.Capacity() << " weak)";
2706 }
2707 }
2708 os << '\n';
2709
2710 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002711 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002712 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2713 }
2714}
2715
Elliott Hughes73e66f72012-05-09 09:34:45 -07002716void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002717 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002718 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002719 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002720 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002721 }
2722 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002723 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002724 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002725 }
2726 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002727 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002728 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002729 }
2730}
2731
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002732bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2733 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002734 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002735
2736 // See if we've already loaded this library. If we have, and the class loader
2737 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002738 // TODO: for better results we should canonicalize the pathname (or even compare
2739 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002740 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002741 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002742 {
2743 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002744 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002745 library = libraries->Get(path);
2746 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002747 if (library != NULL) {
2748 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002749 // The library will be associated with class_loader. The JNI
2750 // spec says we can't load the same library into more than one
2751 // class loader.
2752 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2753 "ClassLoader %p; can't open in ClassLoader %p",
2754 path.c_str(), library->GetClassLoader(), class_loader);
2755 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002756 return false;
2757 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002758 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2759 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002760 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002761 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2762 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002763 return false;
2764 }
2765 return true;
2766 }
2767
2768 // Open the shared library. Because we're using a full path, the system
2769 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2770 // resolve this library's dependencies though.)
2771
2772 // Failures here are expected when java.library.path has several entries
2773 // and we have to hunt for the lib.
2774
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002775 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2776 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2777 // dlopen) becomes zero from dlclose.
2778
Elliott Hughescdf53122011-08-19 15:46:09 -07002779 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002780 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002781 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2782 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2783 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002784
Elliott Hughes84b2f142012-09-27 09:16:28 -07002785 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002786
2787 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002788 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002789 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002790 return false;
2791 }
2792
2793 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002794 // TODO: move the locking (and more of this logic) into Libraries.
2795 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002797 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002798 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002799 if (library == NULL) { // We won race to get libraries_lock
2800 library = new SharedLibrary(path, handle, class_loader);
2801 libraries->Put(path, library);
2802 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002803 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002804 }
2805 if (!created_library) {
2806 LOG(INFO) << "WOW: we lost a race to add shared library: "
2807 << "\"" << path << "\" ClassLoader=" << class_loader;
2808 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002809 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002810
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002811 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002812
2813 bool result = true;
2814 void* sym = dlsym(handle, "JNI_OnLoad");
2815 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002816 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002817 } else {
2818 // Call JNI_OnLoad. We have to override the current class
2819 // loader, which will always be "null" since the stuff at the
2820 // top of the stack is around Runtime.loadLibrary(). (See
2821 // the comments in the JNI FindClass function.)
2822 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2823 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002824 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002825 self->SetClassLoaderOverride(class_loader);
2826
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002827 int version = 0;
2828 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002829 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002830 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002831 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002832 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002833
Brian Carlstromaded5f72011-10-07 17:15:04 -07002834 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002835
2836 if (version != JNI_VERSION_1_2 &&
2837 version != JNI_VERSION_1_4 &&
2838 version != JNI_VERSION_1_6) {
2839 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2840 << "bad version: " << version;
2841 // It's unwise to call dlclose() here, but we can mark it
2842 // as bad and ensure that future load attempts will fail.
2843 // We don't know how far JNI_OnLoad got, so there could
2844 // be some partially-initialized stuff accessible through
2845 // newly-registered native method calls. We could try to
2846 // unregister them, but that doesn't seem worthwhile.
2847 result = false;
2848 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002849 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2850 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002851 }
2852 }
2853
2854 library->SetResult(result);
2855 return result;
2856}
2857
Mathieu Chartier66f19252012-09-18 08:57:04 -07002858void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002859 CHECK(m->IsNative());
2860
2861 Class* c = m->GetDeclaringClass();
2862
2863 // If this is a static method, it could be called before the class
2864 // has been initialized.
2865 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002866 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 return NULL;
2868 }
2869 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002870 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002871 }
2872
Brian Carlstrom16192862011-09-12 17:50:06 -07002873 std::string detail;
2874 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002875 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002876 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002877 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002878 native_method = libraries->FindNativeMethod(m, detail);
2879 }
2880 // throwing can cause libraries_lock to be reacquired
2881 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002882 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002883 }
2884 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002885}
2886
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002887void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002888 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002889 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002890 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002891 globals.VisitRoots(visitor, arg);
2892 }
2893 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002894 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002895 pin_table.VisitRoots(visitor, arg);
2896 }
2897 // The weak_globals table is visited by the GC itself (because it mutates the table).
2898}
2899
Elliott Hughesc8fece32013-01-02 11:27:23 -08002900void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2901 size_t method_count) {
2902 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2903 if (c.get() == NULL) {
2904 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2905 }
2906 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2907}
2908
Ian Rogersdf20fe02011-07-20 20:34:16 -07002909} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002910
2911std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2912 switch (rhs) {
2913 case JNIInvalidRefType:
2914 os << "JNIInvalidRefType";
2915 return os;
2916 case JNILocalRefType:
2917 os << "JNILocalRefType";
2918 return os;
2919 case JNIGlobalRefType:
2920 os << "JNIGlobalRefType";
2921 return os;
2922 case JNIWeakGlobalRefType:
2923 os << "JNIWeakGlobalRefType";
2924 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002925 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002926 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002927 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002928 }
2929}