blob: 726c80bbe08d2ecd298c9105397a7fb4751957d7 [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) {
380 CHECK_GE(args->version, JNI_VERSION_1_2);
381 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700382 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700383 }
Elliott Hughes75770752011-08-24 17:52:38 -0700384
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800385 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700386 *p_env = NULL;
387 return JNI_ERR;
388 } else {
389 *p_env = Thread::Current()->GetJniEnv();
390 return JNI_OK;
391 }
Elliott Hughes75770752011-08-24 17:52:38 -0700392}
393
Elliott Hughes79082e32011-08-25 12:07:32 -0700394class SharedLibrary {
395 public:
396 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
397 : path_(path),
398 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700399 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700400 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700401 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700402 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700403 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700404 }
405
Elliott Hughes79082e32011-08-25 12:07:32 -0700406 Object* GetClassLoader() {
407 return class_loader_;
408 }
409
410 std::string GetPath() {
411 return path_;
412 }
413
414 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700415 * Check the result of an earlier call to JNI_OnLoad on this library.
416 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700417 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700418 bool CheckOnLoadResult()
419 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700420 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700421 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
423 bool okay;
424 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700425 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700426
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
428 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
429 // caller can continue.
430 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
431 okay = true;
432 } else {
433 while (jni_on_load_result_ == kPending) {
434 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700435 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700436 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700437
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 okay = (jni_on_load_result_ == kOkay);
439 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
440 << (okay ? "succeeded" : "failed") << "]";
441 }
442 }
443 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 return okay;
445 }
446
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700448 Thread* self = Thread::Current();
449 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700450
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700452 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700453
454 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700455 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 }
457
458 void* FindSymbol(const std::string& symbol_name) {
459 return dlsym(handle_, symbol_name.c_str());
460 }
461
462 private:
463 enum JNI_OnLoadState {
464 kPending,
465 kFailed,
466 kOkay,
467 };
468
469 // Path to library "/system/lib/libjni.so".
470 std::string path_;
471
472 // The void* returned by dlopen(3).
473 void* handle_;
474
475 // The ClassLoader this library is associated with.
476 Object* class_loader_;
477
478 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700481 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700483 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700485 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486};
487
Elliott Hughes79082e32011-08-25 12:07:32 -0700488// This exists mainly to keep implementation details out of the header file.
489class Libraries {
490 public:
491 Libraries() {
492 }
493
494 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700495 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 }
497
Elliott Hughesae80b492012-04-24 10:43:17 -0700498 void Dump(std::ostream& os) const {
499 bool first = true;
500 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
501 if (!first) {
502 os << ' ';
503 }
504 first = false;
505 os << it->first;
506 }
507 }
508
509 size_t size() const {
510 return libraries_.size();
511 }
512
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700514 It it = libraries_.find(path);
515 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 }
517
518 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700519 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 }
521
522 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700523 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 std::string jni_short_name(JniShortName(m));
526 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700527 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
529 SharedLibrary* library = it->second;
530 if (library->GetClassLoader() != declaring_class_loader) {
531 // We only search libraries loaded by the appropriate ClassLoader.
532 continue;
533 }
534 // Try the short name then the long name...
535 void* fn = library->FindSymbol(jni_short_name);
536 if (fn == NULL) {
537 fn = library->FindSymbol(jni_long_name);
538 }
539 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800540 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
541 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 return fn;
543 }
544 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700546 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700547 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 return NULL;
550 }
551
552 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700553 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700554
Elliott Hughesa0e18062012-04-13 15:59:59 -0700555 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700556};
557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
559 jvalue* args) {
560 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700561 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700562 MethodHelper mh(method);
563 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564 arg_array.BuildArgArray(soa, args);
565 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700566}
567
Mathieu Chartier66f19252012-09-18 08:57:04 -0700568JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800572}
573
Elliott Hughescdf53122011-08-19 15:46:09 -0700574class JNI {
575 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700576 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700577 return JNI_VERSION_1_6;
578 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700579
Ian Rogers25e8b912012-09-07 11:31:36 -0700580 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700581 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700582 return NULL;
583 }
584
Elliott Hughescdf53122011-08-19 15:46:09 -0700585 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700587 Runtime* runtime = Runtime::Current();
588 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700590 Class* c = NULL;
591 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800592 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800593 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700594 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800595 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700596 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700598 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700599
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700602 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700604 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700605
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 ScopedObjectAccess soa(env);
608 Field* field = soa.Decode<Field*>(java_field);
609 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700611
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700614 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
620 Field* field = soa.DecodeField(fid);
621 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700623
Elliott Hughes37f7a402011-08-22 18:56:01 -0700624 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 ScopedObjectAccess soa(env);
626 Object* o = soa.Decode<Object*>(java_object);
627 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700628 }
629
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700630 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 ScopedObjectAccess soa(env);
632 Class* c = soa.Decode<Class*>(java_class);
633 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700635
Elliott Hughes37f7a402011-08-22 18:56:01 -0700636 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 ScopedObjectAccess soa(env);
638 Class* c1 = soa.Decode<Class*>(java_class1);
639 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700640 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughese84278b2012-03-22 10:06:53 -0700643 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800645 if (java_class == NULL) {
646 JniAbortF("IsInstanceOf", "null class (second argument)");
647 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700648 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700649 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 return JNI_TRUE;
651 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 Object* obj = soa.Decode<Object*>(jobj);
653 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700654 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700655 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 ScopedObjectAccess soa(env);
660 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 if (exception == NULL) {
662 return JNI_ERR;
663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 return JNI_OK;
666 }
667
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700668 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700669 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 }
671
672 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700673 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
676 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700677 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 }
679
680 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700682
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700684 Throwable* original_exception = self->GetException();
685 self->ClearException();
686
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700688 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
689 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
690 if (mid == NULL) {
691 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700692 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 } else {
694 env->CallVoidMethod(exception.get(), mid);
695 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700696 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 << " thrown while calling printStackTrace";
698 self->ClearException();
699 }
700 }
701
702 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 ScopedObjectAccess soa(env);
707 Object* exception = soa.Self()->GetException();
708 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 }
710
Ian Rogers25e8b912012-09-07 11:31:36 -0700711 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 LOG(FATAL) << "JNI FatalError called: " << msg;
713 }
714
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700715 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800716 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700717 return JNI_ERR;
718 }
Ian Rogersef28b142012-11-30 14:22:18 -0800719 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 return JNI_OK;
721 }
722
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700723 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
725 Object* survivor = soa.Decode<Object*>(java_survivor);
726 soa.Env()->PopFrame();
727 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
729
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700730 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800731 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 }
733
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 if (obj == NULL) {
736 return NULL;
737 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700738 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700742 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 return reinterpret_cast<jobject>(ref);
745 }
746
747 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 if (obj == NULL) {
749 return;
750 }
Ian Rogersef28b142012-11-30 14:22:18 -0800751 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800753 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
754 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700755
756 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
757 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
758 << "failed to find entry";
759 }
760 }
761
762 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 ScopedObjectAccess soa(env);
764 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
766
767 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 if (obj == NULL) {
769 return;
770 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700771 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700774 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775
776 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
777 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
778 << "failed to find entry";
779 }
780 }
781
782 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 if (obj == NULL) {
784 return NULL;
785 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700786 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700788
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 uint32_t cookie = soa.Env()->local_ref_cookie;
790 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return reinterpret_cast<jobject>(ref);
792 }
793
794 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 if (obj == NULL) {
796 return;
797 }
Ian Rogersef28b142012-11-30 14:22:18 -0800798 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700799
Ian Rogersef28b142012-11-30 14:22:18 -0800800 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 if (!locals.Remove(cookie, obj)) {
802 // Attempting to delete a local reference that is not in the
803 // topmost local reference frame is a no-op. DeleteLocalRef returns
804 // void and doesn't throw any exceptions, but we should probably
805 // complain about it so the user will notice that things aren't
806 // going quite the way they expect.
807 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
808 << "failed to find entry";
809 }
810 }
811
812 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700814 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700817 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
819 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700820 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700821 return NULL;
822 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700823 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 }
825
Elliott Hughese84278b2012-03-22 10:06:53 -0700826 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700828 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700829 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 va_end(args);
831 return result;
832 }
833
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(env);
836 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700837 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700838 return NULL;
839 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700841 if (result == NULL) {
842 return NULL;
843 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700847 return local_result;
848 } else {
849 return NULL;
850 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 }
852
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 ScopedObjectAccess soa(env);
855 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700856 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700857 return NULL;
858 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700859 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700860 if (result == NULL) {
861 return NULL;
862 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700866 return local_result;
867 } else {
868 return NULL;
869 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
874 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
877 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
879 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 va_list ap;
884 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 ScopedObjectAccess soa(env);
893 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
894 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
899 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
900 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_list ap;
905 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700906 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700909 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913 ScopedObjectAccess soa(env);
914 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_list ap;
925 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700928 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
933 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 ScopedObjectAccess soa(env);
938 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 va_list ap;
943 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700944 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700947 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
957 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700963 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700966 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
976 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700985 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(env);
995 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_list ap;
1000 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001001 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001004 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
1014 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001020 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001023 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
1028 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
1033 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_list ap;
1038 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001039 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001042 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001058 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001059 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 ScopedObjectAccess soa(env);
1065 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
1070 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001073 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001076 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1078 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 va_end(ap);
1080 return local_result;
1081 }
1082
1083 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001084 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1087 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
1090 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001091 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
1093 JValue result(InvokeWithJValues(soa, obj, mid, args));
1094 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
1097 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001098 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
1107 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001108 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
1113 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001114 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
1116 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001119 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001122 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001125 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
1128 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001129 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
1134 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001135 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001140 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001146 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
1149 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001150 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
1155 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001156 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001161 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001164 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001167 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 ScopedObjectAccess soa(env);
1173 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001177 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 ScopedObjectAccess soa(env);
1179 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001182 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001185 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001188 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
1194 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001198 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 ScopedObjectAccess soa(env);
1200 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001203 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001206 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001209 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
1215 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001219 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
1221 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001227 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001230 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
1233 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(env);
1236 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
1242 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001248 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001251 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
1254 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 ScopedObjectAccess soa(env);
1257 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001261 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env);
1263 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001269 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_end(ap);
1272 }
1273
1274 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env);
1277 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001281 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001286 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001290
1291
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001292 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001296
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001297 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 ScopedObjectAccess soa(env);
1299 Object* o = soa.Decode<Object*>(obj);
1300 Field* f = soa.DecodeField(fid);
1301 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001303
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001304 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
1306 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001307 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 ScopedObjectAccess soa(env);
1312 Object* o = soa.Decode<Object*>(java_object);
1313 Object* v = soa.Decode<Object*>(java_value);
1314 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001315 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001318 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 ScopedObjectAccess soa(env);
1320 Object* v = soa.Decode<Object*>(java_value);
1321 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001322 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001325#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env); \
1327 Object* o = soa.Decode<Object*>(instance); \
1328 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001329 return f->fn(o)
1330
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001331#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1332 ScopedObjectAccess soa(env); \
1333 Field* f = soa.DecodeField(fid); \
1334 return f->fn(f->GetDeclaringClass())
1335
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001336#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 ScopedObjectAccess soa(env); \
1338 Object* o = soa.Decode<Object*>(instance); \
1339 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340 f->fn(o, value)
1341
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001342#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1343 ScopedObjectAccess soa(env); \
1344 Field* f = soa.DecodeField(fid); \
1345 f->fn(f->GetDeclaringClass(), value)
1346
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1348 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1352 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1356 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1360 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1364 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1368 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1372 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1376 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001380 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001384 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001388 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001392 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001396 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001400 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 }
1402
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001403 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001404 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001408 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 }
1410
1411 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1412 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1413 }
1414
1415 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1416 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1417 }
1418
1419 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1420 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1421 }
1422
1423 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1424 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1425 }
1426
1427 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1428 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1429 }
1430
1431 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1432 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1433 }
1434
1435 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1436 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1437 }
1438
1439 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1440 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1441 }
1442
1443 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001444 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 }
1446
1447 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001448 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 }
1450
1451 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001452 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 }
1454
1455 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001456 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 }
1458
1459 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001460 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 }
1462
1463 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001464 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 }
1466
1467 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001468 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 }
1470
1471 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001472 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001477 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001478 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1480 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 va_end(ap);
1482 return local_result;
1483 }
1484
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001486 ScopedObjectAccess soa(env);
1487 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1488 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
1493 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1494 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001499 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001500 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001503 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001506 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 ScopedObjectAccess soa(env);
1508 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001511 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001512 ScopedObjectAccess soa(env);
1513 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001518 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001519 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001520 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001522 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001525 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 ScopedObjectAccess soa(env);
1527 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
1532 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001537 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001538 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001541 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545 ScopedObjectAccess soa(env);
1546 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 ScopedObjectAccess soa(env);
1551 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001557 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001560 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001564 ScopedObjectAccess soa(env);
1565 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001569 ScopedObjectAccess soa(env);
1570 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001576 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001579 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
1584 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 ScopedObjectAccess soa(env);
1589 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001595 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001596 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001598 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
1603 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 ScopedObjectAccess soa(env);
1608 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001614 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001615 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001617 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 ScopedObjectAccess soa(env);
1622 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 ScopedObjectAccess soa(env);
1627 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001633 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001634 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001636 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 ScopedObjectAccess soa(env);
1641 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
1646 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001652 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_end(ap);
1655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 ScopedObjectAccess soa(env);
1659 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
1664 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes814e4032011-08-23 12:07:56 -07001667 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001669 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
1673 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 if (utf == NULL) {
1675 return NULL;
1676 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001678 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes814e4032011-08-23 12:07:56 -07001682 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
1684 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001685 }
1686
1687 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
1689 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001690 }
1691
Elliott Hughesb465ab02011-08-24 11:21:21 -07001692 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 ScopedObjectAccess soa(env);
1694 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001695 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001697 } else {
1698 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1699 memcpy(buf, chars + start, length * sizeof(jchar));
1700 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001701 }
1702
Elliott Hughesb465ab02011-08-24 11:21:21 -07001703 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001704 ScopedObjectAccess soa(env);
1705 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001706 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001708 } else {
1709 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1710 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1711 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001712 }
1713
Elliott Hughes75770752011-08-24 17:52:38 -07001714 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
1716 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001717 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001718 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001719 if (is_copy != NULL) {
1720 *is_copy = JNI_FALSE;
1721 }
1722 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
1727 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes75770752011-08-24 17:52:38 -07001730 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001731 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes75770752011-08-24 17:52:38 -07001734 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001736 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes75770752011-08-24 17:52:38 -07001739 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001740 if (java_string == NULL) {
1741 return NULL;
1742 }
1743 if (is_copy != NULL) {
1744 *is_copy = JNI_TRUE;
1745 }
Ian Rogersef28b142012-11-30 14:22:18 -08001746 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001748 size_t byte_count = s->GetUtfLength();
1749 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001750 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001751 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1752 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1753 bytes[byte_count] = '\0';
1754 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001755 }
1756
Elliott Hughes75770752011-08-24 17:52:38 -07001757 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001758 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001759 }
1760
Elliott Hughesbd935992011-08-22 11:59:34 -07001761 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 ScopedObjectAccess soa(env);
1763 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001764 if (!obj->IsArrayInstance()) {
1765 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1766 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001767 Array* array = obj->AsArray();
1768 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes814e4032011-08-23 12:07:56 -07001771 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 ScopedObjectAccess soa(env);
1773 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1774 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
1777 static void SetObjectArrayElement(JNIEnv* env,
1778 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
1780 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1781 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 array->Set(index, value);
1783 }
1784
1785 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
1787 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
1790 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
1792 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
1795 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
1797 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
1800 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
1802 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
1805 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
1807 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
1815 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 ScopedObjectAccess soa(env);
1817 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
1820 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001822 if (length < 0) {
1823 JniAbortF("NewObjectArray", "negative array length: %d", length);
1824 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001825
1826 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 std::string descriptor;
1829 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001830 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831
1832 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001833 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1834 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 return NULL;
1836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001840 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001841 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001843 for (jsize i = 0; i < length; ++i) {
1844 result->Set(i, initial_object);
1845 }
1846 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
1850 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
1852 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Ian Rogersa15e67d2012-02-28 13:51:55 -08001855 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
1857 Array* array = soa.Decode<Array*>(java_array);
1858 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001859 if (is_copy != NULL) {
1860 *is_copy = JNI_FALSE;
1861 }
1862 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001866 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001870 ScopedObjectAccess soa(env);
1871 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001875 ScopedObjectAccess soa(env);
1876 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes75770752011-08-24 17:52:38 -07001879 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
1881 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes75770752011-08-24 17:52:38 -07001884 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 ScopedObjectAccess soa(env);
1886 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Elliott Hughes75770752011-08-24 17:52:38 -07001889 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
1891 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes75770752011-08-24 17:52:38 -07001894 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
1896 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
Elliott Hughes75770752011-08-24 17:52:38 -07001899 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
1901 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
1906 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001910 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, 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 ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, 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 ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, 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 ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, 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 ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001938 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 ScopedObjectAccess soa(env);
1943 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes814e4032011-08-23 12:07:56 -07001946 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001947 ScopedObjectAccess soa(env);
1948 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes814e4032011-08-23 12:07:56 -07001951 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
1953 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes814e4032011-08-23 12:07:56 -07001956 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
1958 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes814e4032011-08-23 12:07:56 -07001961 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
1963 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
1968 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes814e4032011-08-23 12:07:56 -07001971 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ScopedObjectAccess soa(env);
1973 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 ScopedObjectAccess soa(env);
1978 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes814e4032011-08-23 12:07:56 -07001981 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982 ScopedObjectAccess soa(env);
1983 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes814e4032011-08-23 12:07:56 -07001986 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
1993 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
1998 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
2003 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes814e4032011-08-23 12:07:56 -07002006 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
2008 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes814e4032011-08-23 12:07:56 -07002011 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
2013 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes814e4032011-08-23 12:07:56 -07002016 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
2018 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes5174fe62011-08-23 15:12:35 -07002021 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002022 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2023 }
2024
2025 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 -07002026 ScopedObjectAccess soa(env);
2027 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002028
Elliott Hughesc8fece32013-01-02 11:27:23 -08002029 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 const char* name = methods[i].name;
2031 const char* sig = methods[i].signature;
2032
2033 if (*sig == '!') {
2034 // TODO: fast jni. it's too noisy to log all these.
2035 ++sig;
2036 }
2037
Mathieu Chartier66f19252012-09-18 08:57:04 -07002038 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002039 if (m == NULL) {
2040 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002042 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002043 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2044 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002047 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002048 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2049 << PrettyDescriptor(c) << "." << name << sig
2050 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002051 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 return JNI_ERR;
2053 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002054
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002055 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002056
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059 return JNI_OK;
2060 }
2061
Elliott Hughes5174fe62011-08-23 15:12:35 -07002062 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002063 ScopedObjectAccess soa(env);
2064 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002065
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002066 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002067
2068 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002069 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002070 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002071 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002072 }
2073 }
2074 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002075 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002076 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002078 }
2079 }
2080
2081 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2085 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2086 ScopedObjectAccess soa(env);
2087 Object* o = soa.Decode<Object*>(java_object);
2088 o->MonitorEnter(soa.Self());
2089 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002090 return JNI_ERR;
2091 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002093 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 static jint MonitorExit(JNIEnv* env, jobject java_object)
2097 UNLOCK_FUNCTION(monitor_lock_) {
2098 ScopedObjectAccess soa(env);
2099 Object* o = soa.Decode<Object*>(java_object);
2100 o->MonitorExit(soa.Self());
2101 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002102 return JNI_ERR;
2103 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002105 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
2108 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 Runtime* runtime = Runtime::Current();
2110 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002111 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 } else {
2113 *vm = NULL;
2114 }
2115 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2116 }
2117
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002119 if (capacity < 0) {
2120 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2121 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002122 if (address == NULL && capacity != 0) {
2123 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002124 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002125
Elliott Hughes96a98872012-12-19 14:21:15 -08002126 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002127 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2128 CHECK_LE(capacity, 0xffffffff);
2129 jint address_arg = reinterpret_cast<jint>(address);
2130 jint capacity_arg = static_cast<jint>(capacity);
2131
Elliott Hugheseac76672012-05-24 21:56:51 -07002132 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2133 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2134 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002135 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughesb465ab02011-08-24 11:21:21 -07002138 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002139 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughesb465ab02011-08-24 11:21:21 -07002142 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002143 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughesb465ab02011-08-24 11:21:21 -07002146 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002147 if (java_object == NULL) {
2148 JniAbortF("GetObjectRefType", "null object");
2149 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002150
2151 // Do we definitely know what kind of reference this is?
2152 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2153 IndirectRefKind kind = GetIndirectRefKind(ref);
2154 switch (kind) {
2155 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002156 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002157 return JNILocalRefType;
2158 }
2159 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002160 case kGlobal:
2161 return JNIGlobalRefType;
2162 case kWeakGlobal:
2163 return JNIWeakGlobalRefType;
2164 case kSirtOrInvalid:
2165 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002166 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002167 return JNILocalRefType;
2168 }
2169
Ian Rogersef28b142012-11-30 14:22:18 -08002170 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002171 return JNIInvalidRefType;
2172 }
2173
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174 // If we're handing out direct pointers, check whether it's a direct pointer
2175 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002176 {
2177 ScopedObjectAccess soa(env);
2178 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2179 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2180 return JNILocalRefType;
2181 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002182 }
2183 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002184 return JNIInvalidRefType;
2185 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002186 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2187 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189
2190 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002191 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2192 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 // TODO: we should try to expand the table if necessary.
2194 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2195 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2196 return JNI_ERR;
2197 }
2198 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002199 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2201 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002202 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 soa.Self()->ThrowOutOfMemoryError(caller);
2204 }
2205 return okay ? JNI_OK : JNI_ERR;
2206 }
2207
2208 template<typename JniT, typename ArtT>
2209 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002211 if (length < 0) {
2212 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2213 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002214 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 return soa.AddLocalReference<JniT>(result);
2216 }
2217
2218 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2219 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2220 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2223 PinPrimitiveArray(soa, array);
2224 if (is_copy != NULL) {
2225 *is_copy = JNI_FALSE;
2226 }
2227 return array->GetData();
2228 }
2229
2230 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002231 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002233 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 Array* array = soa.Decode<Array*>(java_array);
2235 UnpinPrimitiveArray(soa, array);
2236 }
2237 }
2238
2239 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2240 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2241 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2244 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2245 ThrowAIOOBE(soa, array, start, length, "src");
2246 } else {
2247 JavaT* data = array->GetData();
2248 memcpy(buf, data + start, length * sizeof(JavaT));
2249 }
2250 }
2251
2252 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2253 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2254 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002256 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2257 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2258 ThrowAIOOBE(soa, array, start, length, "dst");
2259 } else {
2260 JavaT* data = array->GetData();
2261 memcpy(data + start, buf, length * sizeof(JavaT));
2262 }
2263 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002264};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002265
Elliott Hughes88c5c352012-03-15 18:49:48 -07002266const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002267 NULL, // reserved0.
2268 NULL, // reserved1.
2269 NULL, // reserved2.
2270 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 JNI::GetVersion,
2272 JNI::DefineClass,
2273 JNI::FindClass,
2274 JNI::FromReflectedMethod,
2275 JNI::FromReflectedField,
2276 JNI::ToReflectedMethod,
2277 JNI::GetSuperclass,
2278 JNI::IsAssignableFrom,
2279 JNI::ToReflectedField,
2280 JNI::Throw,
2281 JNI::ThrowNew,
2282 JNI::ExceptionOccurred,
2283 JNI::ExceptionDescribe,
2284 JNI::ExceptionClear,
2285 JNI::FatalError,
2286 JNI::PushLocalFrame,
2287 JNI::PopLocalFrame,
2288 JNI::NewGlobalRef,
2289 JNI::DeleteGlobalRef,
2290 JNI::DeleteLocalRef,
2291 JNI::IsSameObject,
2292 JNI::NewLocalRef,
2293 JNI::EnsureLocalCapacity,
2294 JNI::AllocObject,
2295 JNI::NewObject,
2296 JNI::NewObjectV,
2297 JNI::NewObjectA,
2298 JNI::GetObjectClass,
2299 JNI::IsInstanceOf,
2300 JNI::GetMethodID,
2301 JNI::CallObjectMethod,
2302 JNI::CallObjectMethodV,
2303 JNI::CallObjectMethodA,
2304 JNI::CallBooleanMethod,
2305 JNI::CallBooleanMethodV,
2306 JNI::CallBooleanMethodA,
2307 JNI::CallByteMethod,
2308 JNI::CallByteMethodV,
2309 JNI::CallByteMethodA,
2310 JNI::CallCharMethod,
2311 JNI::CallCharMethodV,
2312 JNI::CallCharMethodA,
2313 JNI::CallShortMethod,
2314 JNI::CallShortMethodV,
2315 JNI::CallShortMethodA,
2316 JNI::CallIntMethod,
2317 JNI::CallIntMethodV,
2318 JNI::CallIntMethodA,
2319 JNI::CallLongMethod,
2320 JNI::CallLongMethodV,
2321 JNI::CallLongMethodA,
2322 JNI::CallFloatMethod,
2323 JNI::CallFloatMethodV,
2324 JNI::CallFloatMethodA,
2325 JNI::CallDoubleMethod,
2326 JNI::CallDoubleMethodV,
2327 JNI::CallDoubleMethodA,
2328 JNI::CallVoidMethod,
2329 JNI::CallVoidMethodV,
2330 JNI::CallVoidMethodA,
2331 JNI::CallNonvirtualObjectMethod,
2332 JNI::CallNonvirtualObjectMethodV,
2333 JNI::CallNonvirtualObjectMethodA,
2334 JNI::CallNonvirtualBooleanMethod,
2335 JNI::CallNonvirtualBooleanMethodV,
2336 JNI::CallNonvirtualBooleanMethodA,
2337 JNI::CallNonvirtualByteMethod,
2338 JNI::CallNonvirtualByteMethodV,
2339 JNI::CallNonvirtualByteMethodA,
2340 JNI::CallNonvirtualCharMethod,
2341 JNI::CallNonvirtualCharMethodV,
2342 JNI::CallNonvirtualCharMethodA,
2343 JNI::CallNonvirtualShortMethod,
2344 JNI::CallNonvirtualShortMethodV,
2345 JNI::CallNonvirtualShortMethodA,
2346 JNI::CallNonvirtualIntMethod,
2347 JNI::CallNonvirtualIntMethodV,
2348 JNI::CallNonvirtualIntMethodA,
2349 JNI::CallNonvirtualLongMethod,
2350 JNI::CallNonvirtualLongMethodV,
2351 JNI::CallNonvirtualLongMethodA,
2352 JNI::CallNonvirtualFloatMethod,
2353 JNI::CallNonvirtualFloatMethodV,
2354 JNI::CallNonvirtualFloatMethodA,
2355 JNI::CallNonvirtualDoubleMethod,
2356 JNI::CallNonvirtualDoubleMethodV,
2357 JNI::CallNonvirtualDoubleMethodA,
2358 JNI::CallNonvirtualVoidMethod,
2359 JNI::CallNonvirtualVoidMethodV,
2360 JNI::CallNonvirtualVoidMethodA,
2361 JNI::GetFieldID,
2362 JNI::GetObjectField,
2363 JNI::GetBooleanField,
2364 JNI::GetByteField,
2365 JNI::GetCharField,
2366 JNI::GetShortField,
2367 JNI::GetIntField,
2368 JNI::GetLongField,
2369 JNI::GetFloatField,
2370 JNI::GetDoubleField,
2371 JNI::SetObjectField,
2372 JNI::SetBooleanField,
2373 JNI::SetByteField,
2374 JNI::SetCharField,
2375 JNI::SetShortField,
2376 JNI::SetIntField,
2377 JNI::SetLongField,
2378 JNI::SetFloatField,
2379 JNI::SetDoubleField,
2380 JNI::GetStaticMethodID,
2381 JNI::CallStaticObjectMethod,
2382 JNI::CallStaticObjectMethodV,
2383 JNI::CallStaticObjectMethodA,
2384 JNI::CallStaticBooleanMethod,
2385 JNI::CallStaticBooleanMethodV,
2386 JNI::CallStaticBooleanMethodA,
2387 JNI::CallStaticByteMethod,
2388 JNI::CallStaticByteMethodV,
2389 JNI::CallStaticByteMethodA,
2390 JNI::CallStaticCharMethod,
2391 JNI::CallStaticCharMethodV,
2392 JNI::CallStaticCharMethodA,
2393 JNI::CallStaticShortMethod,
2394 JNI::CallStaticShortMethodV,
2395 JNI::CallStaticShortMethodA,
2396 JNI::CallStaticIntMethod,
2397 JNI::CallStaticIntMethodV,
2398 JNI::CallStaticIntMethodA,
2399 JNI::CallStaticLongMethod,
2400 JNI::CallStaticLongMethodV,
2401 JNI::CallStaticLongMethodA,
2402 JNI::CallStaticFloatMethod,
2403 JNI::CallStaticFloatMethodV,
2404 JNI::CallStaticFloatMethodA,
2405 JNI::CallStaticDoubleMethod,
2406 JNI::CallStaticDoubleMethodV,
2407 JNI::CallStaticDoubleMethodA,
2408 JNI::CallStaticVoidMethod,
2409 JNI::CallStaticVoidMethodV,
2410 JNI::CallStaticVoidMethodA,
2411 JNI::GetStaticFieldID,
2412 JNI::GetStaticObjectField,
2413 JNI::GetStaticBooleanField,
2414 JNI::GetStaticByteField,
2415 JNI::GetStaticCharField,
2416 JNI::GetStaticShortField,
2417 JNI::GetStaticIntField,
2418 JNI::GetStaticLongField,
2419 JNI::GetStaticFloatField,
2420 JNI::GetStaticDoubleField,
2421 JNI::SetStaticObjectField,
2422 JNI::SetStaticBooleanField,
2423 JNI::SetStaticByteField,
2424 JNI::SetStaticCharField,
2425 JNI::SetStaticShortField,
2426 JNI::SetStaticIntField,
2427 JNI::SetStaticLongField,
2428 JNI::SetStaticFloatField,
2429 JNI::SetStaticDoubleField,
2430 JNI::NewString,
2431 JNI::GetStringLength,
2432 JNI::GetStringChars,
2433 JNI::ReleaseStringChars,
2434 JNI::NewStringUTF,
2435 JNI::GetStringUTFLength,
2436 JNI::GetStringUTFChars,
2437 JNI::ReleaseStringUTFChars,
2438 JNI::GetArrayLength,
2439 JNI::NewObjectArray,
2440 JNI::GetObjectArrayElement,
2441 JNI::SetObjectArrayElement,
2442 JNI::NewBooleanArray,
2443 JNI::NewByteArray,
2444 JNI::NewCharArray,
2445 JNI::NewShortArray,
2446 JNI::NewIntArray,
2447 JNI::NewLongArray,
2448 JNI::NewFloatArray,
2449 JNI::NewDoubleArray,
2450 JNI::GetBooleanArrayElements,
2451 JNI::GetByteArrayElements,
2452 JNI::GetCharArrayElements,
2453 JNI::GetShortArrayElements,
2454 JNI::GetIntArrayElements,
2455 JNI::GetLongArrayElements,
2456 JNI::GetFloatArrayElements,
2457 JNI::GetDoubleArrayElements,
2458 JNI::ReleaseBooleanArrayElements,
2459 JNI::ReleaseByteArrayElements,
2460 JNI::ReleaseCharArrayElements,
2461 JNI::ReleaseShortArrayElements,
2462 JNI::ReleaseIntArrayElements,
2463 JNI::ReleaseLongArrayElements,
2464 JNI::ReleaseFloatArrayElements,
2465 JNI::ReleaseDoubleArrayElements,
2466 JNI::GetBooleanArrayRegion,
2467 JNI::GetByteArrayRegion,
2468 JNI::GetCharArrayRegion,
2469 JNI::GetShortArrayRegion,
2470 JNI::GetIntArrayRegion,
2471 JNI::GetLongArrayRegion,
2472 JNI::GetFloatArrayRegion,
2473 JNI::GetDoubleArrayRegion,
2474 JNI::SetBooleanArrayRegion,
2475 JNI::SetByteArrayRegion,
2476 JNI::SetCharArrayRegion,
2477 JNI::SetShortArrayRegion,
2478 JNI::SetIntArrayRegion,
2479 JNI::SetLongArrayRegion,
2480 JNI::SetFloatArrayRegion,
2481 JNI::SetDoubleArrayRegion,
2482 JNI::RegisterNatives,
2483 JNI::UnregisterNatives,
2484 JNI::MonitorEnter,
2485 JNI::MonitorExit,
2486 JNI::GetJavaVM,
2487 JNI::GetStringRegion,
2488 JNI::GetStringUTFRegion,
2489 JNI::GetPrimitiveArrayCritical,
2490 JNI::ReleasePrimitiveArrayCritical,
2491 JNI::GetStringCritical,
2492 JNI::ReleaseStringCritical,
2493 JNI::NewWeakGlobalRef,
2494 JNI::DeleteWeakGlobalRef,
2495 JNI::ExceptionCheck,
2496 JNI::NewDirectByteBuffer,
2497 JNI::GetDirectBufferAddress,
2498 JNI::GetDirectBufferCapacity,
2499 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002500};
2501
Elliott Hughes75770752011-08-24 17:52:38 -07002502JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002503 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002504 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002505 local_ref_cookie(IRT_FIRST_SEGMENT),
2506 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002507 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002508 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002509 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002510 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002511 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002512 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002513 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002514 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2515 // errors will ensue.
2516 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2517 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002518}
2519
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002520JNIEnvExt::~JNIEnvExt() {
2521}
2522
Elliott Hughes88c5c352012-03-15 18:49:48 -07002523void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2524 check_jni = enabled;
2525 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002526}
2527
Elliott Hughes73e66f72012-05-09 09:34:45 -07002528void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2529 locals.Dump(os);
2530 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002531}
2532
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002533void JNIEnvExt::PushFrame(int /*capacity*/) {
2534 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002535 stacked_local_ref_cookies.push_back(local_ref_cookie);
2536 local_ref_cookie = locals.GetSegmentState();
2537}
2538
2539void JNIEnvExt::PopFrame() {
2540 locals.SetSegmentState(local_ref_cookie);
2541 local_ref_cookie = stacked_local_ref_cookies.back();
2542 stacked_local_ref_cookies.pop_back();
2543}
2544
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002545Offset JNIEnvExt::SegmentStateOffset() {
2546 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2547 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2548}
2549
Carl Shapiroea4dca82011-08-01 13:45:38 -07002550// JNI Invocation interface.
2551
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2553 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2554 if (args->version < JNI_VERSION_1_2) {
2555 return JNI_EVERSION;
2556 }
2557 Runtime::Options options;
2558 for (int i = 0; i < args->nOptions; ++i) {
2559 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002560 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
2562 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002563 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002566 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002567 bool started = runtime->Start();
2568 if (!started) {
2569 delete Thread::Current()->GetJniEnv();
2570 delete runtime->GetJavaVM();
2571 LOG(WARNING) << "CreateJavaVM failed";
2572 return JNI_ERR;
2573 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002574 *p_env = Thread::Current()->GetJniEnv();
2575 *p_vm = runtime->GetJavaVM();
2576 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002577}
2578
Elliott Hughesf2682d52011-08-15 16:37:04 -07002579extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580 Runtime* runtime = Runtime::Current();
2581 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002582 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002584 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002585 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 }
2587 return JNI_OK;
2588}
2589
2590// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002591extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 return JNI_ERR;
2593}
2594
Elliott Hughescdf53122011-08-19 15:46:09 -07002595class JII {
2596 public:
2597 static jint DestroyJavaVM(JavaVM* vm) {
2598 if (vm == NULL) {
2599 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002600 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002601 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2602 delete raw_vm->runtime;
2603 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605
Elliott Hughescdf53122011-08-19 15:46:09 -07002606 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002607 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002608 }
2609
2610 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002611 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002612 }
2613
2614 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002615 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002616 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002617 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002618 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2619 Runtime* runtime = raw_vm->runtime;
2620 runtime->DetachCurrentThread();
2621 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002622 }
2623
2624 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2625 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2626 return JNI_EVERSION;
2627 }
2628 if (vm == NULL || env == NULL) {
2629 return JNI_ERR;
2630 }
2631 Thread* thread = Thread::Current();
2632 if (thread == NULL) {
2633 *env = NULL;
2634 return JNI_EDETACHED;
2635 }
2636 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637 return JNI_OK;
2638 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002639};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640
Elliott Hughes88c5c352012-03-15 18:49:48 -07002641const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002642 NULL, // reserved0
2643 NULL, // reserved1
2644 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002645 JII::DestroyJavaVM,
2646 JII::AttachCurrentThread,
2647 JII::DetachCurrentThread,
2648 JII::GetEnv,
2649 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002650};
2651
Elliott Hughesa0957642011-09-02 14:27:33 -07002652JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002653 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002654 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002655 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002656 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002657 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002658 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002659 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002660 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002661 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002662 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002663 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002664 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002665 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002666 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002667 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002668 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002669 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002670 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002671 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002672}
2673
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002674JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002675 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002676}
2677
Elliott Hughes88c5c352012-03-15 18:49:48 -07002678void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2679 check_jni = enabled;
2680 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002681}
2682
Elliott Hughesae80b492012-04-24 10:43:17 -07002683void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2684 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2685 if (force_copy) {
2686 os << " (with forcecopy)";
2687 }
2688 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002689 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002690 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002691 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002692 os << "; pins=" << pin_table.Size();
2693 }
2694 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002695 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002696 os << "; globals=" << globals.Capacity();
2697 }
2698 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002699 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002700 if (weak_globals.Capacity() > 0) {
2701 os << " (plus " << weak_globals.Capacity() << " weak)";
2702 }
2703 }
2704 os << '\n';
2705
2706 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002707 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002708 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2709 }
2710}
2711
Elliott Hughes73e66f72012-05-09 09:34:45 -07002712void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002713 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002714 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002715 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002716 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002717 }
2718 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002719 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002720 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002721 }
2722 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002723 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002724 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002725 }
2726}
2727
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002728bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2729 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002730 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002731
2732 // See if we've already loaded this library. If we have, and the class loader
2733 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002734 // TODO: for better results we should canonicalize the pathname (or even compare
2735 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002736 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002737 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002738 {
2739 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002740 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002741 library = libraries->Get(path);
2742 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002743 if (library != NULL) {
2744 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002745 // The library will be associated with class_loader. The JNI
2746 // spec says we can't load the same library into more than one
2747 // class loader.
2748 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2749 "ClassLoader %p; can't open in ClassLoader %p",
2750 path.c_str(), library->GetClassLoader(), class_loader);
2751 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002752 return false;
2753 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002754 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2755 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002756 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2758 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002759 return false;
2760 }
2761 return true;
2762 }
2763
2764 // Open the shared library. Because we're using a full path, the system
2765 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2766 // resolve this library's dependencies though.)
2767
2768 // Failures here are expected when java.library.path has several entries
2769 // and we have to hunt for the lib.
2770
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002771 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2772 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2773 // dlopen) becomes zero from dlclose.
2774
Elliott Hughescdf53122011-08-19 15:46:09 -07002775 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002776 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002777 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2778 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2779 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002780
Elliott Hughes84b2f142012-09-27 09:16:28 -07002781 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002782
2783 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002784 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002785 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002786 return false;
2787 }
2788
2789 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002790 // TODO: move the locking (and more of this logic) into Libraries.
2791 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002793 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002794 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002795 if (library == NULL) { // We won race to get libraries_lock
2796 library = new SharedLibrary(path, handle, class_loader);
2797 libraries->Put(path, library);
2798 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002799 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002800 }
2801 if (!created_library) {
2802 LOG(INFO) << "WOW: we lost a race to add shared library: "
2803 << "\"" << path << "\" ClassLoader=" << class_loader;
2804 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002805 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002806
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002807 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002808
2809 bool result = true;
2810 void* sym = dlsym(handle, "JNI_OnLoad");
2811 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002812 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 } else {
2814 // Call JNI_OnLoad. We have to override the current class
2815 // loader, which will always be "null" since the stuff at the
2816 // top of the stack is around Runtime.loadLibrary(). (See
2817 // the comments in the JNI FindClass function.)
2818 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2819 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002820 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002821 self->SetClassLoaderOverride(class_loader);
2822
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002823 int version = 0;
2824 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002825 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002826 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002827 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002828 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002829
Brian Carlstromaded5f72011-10-07 17:15:04 -07002830 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002831
2832 if (version != JNI_VERSION_1_2 &&
2833 version != JNI_VERSION_1_4 &&
2834 version != JNI_VERSION_1_6) {
2835 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2836 << "bad version: " << version;
2837 // It's unwise to call dlclose() here, but we can mark it
2838 // as bad and ensure that future load attempts will fail.
2839 // We don't know how far JNI_OnLoad got, so there could
2840 // be some partially-initialized stuff accessible through
2841 // newly-registered native method calls. We could try to
2842 // unregister them, but that doesn't seem worthwhile.
2843 result = false;
2844 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002845 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2846 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002847 }
2848 }
2849
2850 library->SetResult(result);
2851 return result;
2852}
2853
Mathieu Chartier66f19252012-09-18 08:57:04 -07002854void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002855 CHECK(m->IsNative());
2856
2857 Class* c = m->GetDeclaringClass();
2858
2859 // If this is a static method, it could be called before the class
2860 // has been initialized.
2861 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002862 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 return NULL;
2864 }
2865 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002866 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 }
2868
Brian Carlstrom16192862011-09-12 17:50:06 -07002869 std::string detail;
2870 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002871 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002872 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002873 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002874 native_method = libraries->FindNativeMethod(m, detail);
2875 }
2876 // throwing can cause libraries_lock to be reacquired
2877 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002878 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002879 }
2880 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002881}
2882
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002883void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002884 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002885 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002886 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002887 globals.VisitRoots(visitor, arg);
2888 }
2889 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002890 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002891 pin_table.VisitRoots(visitor, arg);
2892 }
2893 // The weak_globals table is visited by the GC itself (because it mutates the table).
2894}
2895
Elliott Hughesc8fece32013-01-02 11:27:23 -08002896void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2897 size_t method_count) {
2898 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2899 if (c.get() == NULL) {
2900 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2901 }
2902 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2903}
2904
Ian Rogersdf20fe02011-07-20 20:34:16 -07002905} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002906
2907std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2908 switch (rhs) {
2909 case JNIInvalidRefType:
2910 os << "JNIInvalidRefType";
2911 return os;
2912 case JNILocalRefType:
2913 os << "JNILocalRefType";
2914 return os;
2915 case JNIGlobalRefType:
2916 os << "JNIGlobalRefType";
2917 return os;
2918 case JNIWeakGlobalRefType:
2919 os << "JNIWeakGlobalRefType";
2920 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002921 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002922 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002923 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002924 }
2925}