blob: 887fcb41481e26266476f04f72b229ca9d8fb233 [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
Elliott Hughes83a25322013-03-14 11:18:53 -070088static bool IsBadJniVersion(int version) {
89 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
90 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
91}
92
Mathieu Chartier66f19252012-09-18 08:57:04 -070093static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070095 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070096 const DexFile::TypeList* params = mh.GetParameterTypeList();
97 if (params == NULL) {
98 return; // No arguments so nothing to check.
99 }
100 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700101 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 for (uint32_t i = 0; i < num_params; i++) {
103 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
104 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
105 if (param_type == NULL) {
106 Thread* self = Thread::Current();
107 CHECK(self->IsExceptionPending());
108 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
109 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
110 << self->GetException()->Dump();
111 self->ClearException();
112 ++error_count;
113 } else if (!param_type->IsPrimitive()) {
114 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700115 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700116 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700117 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
118 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
119 ++error_count;
120 }
121 }
122 }
123 if (error_count > 0) {
124 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
125 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700126 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700127 }
128}
Elliott Hughesb264f082012-04-06 17:10:10 -0700129
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700131 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700134 CheckMethodArguments(method, args);
135 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700136 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700138 return result;
139}
140
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
142 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700145 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700146 MethodHelper mh(method);
147 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 arg_array.BuildArgArray(soa, args);
149 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700150}
151
Mathieu Chartier66f19252012-09-18 08:57:04 -0700152static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700154 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
158 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700161 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
163 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 arg_array.BuildArgArray(soa, args);
165 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700166}
167
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
169 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 MethodHelper mh(method);
174 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 arg_array.BuildArgArray(soa, args);
176 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700177}
178
Elliott Hughes6b436852011-08-12 10:16:44 -0700179// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
180// separated with slashes but aren't wrapped with "L;" like regular descriptors
181// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
182// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
183// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700184static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700185 std::string result;
186 // Add the missing "L;" if necessary.
187 if (name[0] == '[') {
188 result = name;
189 } else {
190 result += 'L';
191 result += name;
192 result += ';';
193 }
194 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700195 if (result.find('.') != std::string::npos) {
196 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
197 << "\"" << name << "\"";
198 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700199 }
200 return result;
201}
202
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
204 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800207 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700208}
209
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700210static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
211 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700214 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700215 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700216 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700217
Mathieu Chartier66f19252012-09-18 08:57:04 -0700218 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700219 if (is_static) {
220 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700221 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700222 method = c->FindVirtualMethod(name, sig);
223 if (method == NULL) {
224 // No virtual method matching the signature. Search declared
225 // private methods and constructors.
226 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700227 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700228 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700229
Elliott Hughescdf53122011-08-19 15:46:09 -0700230 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700232 return NULL;
233 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700234
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700236}
237
Ian Rogersef28b142012-11-30 14:22:18 -0800238static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800240 AbstractMethod* method = soa.Self()->GetCurrentMethod();
241 if (method == NULL ||
242 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
243 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700244 }
245 return method->GetDeclaringClass()->GetClassLoader();
246}
247
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
249 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700252 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700253 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700254 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700255
256 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700257 Class* field_type;
258 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
259 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800260 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700262 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700263 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700264 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 if (field_type == NULL) {
266 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 DCHECK(soa.Self()->IsExceptionPending());
268 soa.Self()->ClearException();
269 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700270 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800271 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272 return NULL;
273 }
274 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700281 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800282 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700283 return NULL;
284 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700286}
287
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700291 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700292 vm->pin_table.Add(array);
293}
294
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700298 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700299 vm->pin_table.Remove(array);
300}
301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
303 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700304 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700305 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700307 "%s offset=%d length=%d %s.length=%d",
308 type.c_str(), start, length, identifier, array->GetLength());
309}
Ian Rogers0571d352011-11-03 19:51:38 -0700310
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
312 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700315 "offset=%d length=%d string.length()=%d", start, length, array_length);
316}
Elliott Hughes814e4032011-08-23 12:07:56 -0700317
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 // Turn the const char* into a java.lang.String.
321 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
322 if (msg != NULL && s.get() == NULL) {
323 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700324 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700325
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 // Choose an appropriate constructor and set up the arguments.
327 jvalue args[2];
328 const char* signature;
329 if (msg == NULL && cause == NULL) {
330 signature = "()V";
331 } else if (msg != NULL && cause == NULL) {
332 signature = "(Ljava/lang/String;)V";
333 args[0].l = s.get();
334 } else if (msg == NULL && cause != NULL) {
335 signature = "(Ljava/lang/Throwable;)V";
336 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700337 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
339 args[0].l = s.get();
340 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700341 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
343 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800344 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 LOG(ERROR) << "No <init>" << signature << " in "
346 << PrettyClass(soa.Decode<Class*>(exception_class));
347 return JNI_ERR;
348 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
351 if (exception.get() == NULL) {
352 return JNI_ERR;
353 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700354
Ian Rogersef28b142012-11-30 14:22:18 -0800355 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700359}
360
Elliott Hughes462c9442012-03-23 18:47:50 -0700361static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700362 if (vm == NULL || p_env == NULL) {
363 return JNI_ERR;
364 }
365
Elliott Hughes462c9442012-03-23 18:47:50 -0700366 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700367 Thread* self = Thread::Current();
368 if (self != NULL) {
369 *p_env = self->GetJniEnv();
370 return JNI_OK;
371 }
372
373 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
374
375 // No threads allowed in zygote mode.
376 if (runtime->IsZygote()) {
377 LOG(ERROR) << "Attempt to attach a thread in the zygote";
378 return JNI_ERR;
379 }
380
Elliott Hughes462c9442012-03-23 18:47:50 -0700381 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
382 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700383 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700384 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700385 if (IsBadJniVersion(args->version)) {
386 LOG(ERROR) << "Bad JNI version passed to "
387 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
388 << args->version;
389 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700390 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700391 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700392 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700393 }
Elliott Hughes75770752011-08-24 17:52:38 -0700394
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800395 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700396 *p_env = NULL;
397 return JNI_ERR;
398 } else {
399 *p_env = Thread::Current()->GetJniEnv();
400 return JNI_OK;
401 }
Elliott Hughes75770752011-08-24 17:52:38 -0700402}
403
Elliott Hughes79082e32011-08-25 12:07:32 -0700404class SharedLibrary {
405 public:
406 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
407 : path_(path),
408 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700409 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700410 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700411 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700412 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700413 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700414 }
415
Elliott Hughes79082e32011-08-25 12:07:32 -0700416 Object* GetClassLoader() {
417 return class_loader_;
418 }
419
420 std::string GetPath() {
421 return path_;
422 }
423
424 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700425 * Check the result of an earlier call to JNI_OnLoad on this library.
426 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700427 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 bool CheckOnLoadResult()
429 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700431 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700432 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
433 bool okay;
434 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700435 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700436
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
438 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
439 // caller can continue.
440 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
441 okay = true;
442 } else {
443 while (jni_on_load_result_ == kPending) {
444 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700445 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 okay = (jni_on_load_result_ == kOkay);
449 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
450 << (okay ? "succeeded" : "failed") << "]";
451 }
452 }
453 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700454 return okay;
455 }
456
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700458 Thread* self = Thread::Current();
459 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700460
Elliott Hughes79082e32011-08-25 12:07:32 -0700461 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700462 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700463
464 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700465 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700466 }
467
468 void* FindSymbol(const std::string& symbol_name) {
469 return dlsym(handle_, symbol_name.c_str());
470 }
471
472 private:
473 enum JNI_OnLoadState {
474 kPending,
475 kFailed,
476 kOkay,
477 };
478
479 // Path to library "/system/lib/libjni.so".
480 std::string path_;
481
482 // The void* returned by dlopen(3).
483 void* handle_;
484
485 // The ClassLoader this library is associated with.
486 Object* class_loader_;
487
488 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700489 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700491 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700493 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700495 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700496};
497
Elliott Hughes79082e32011-08-25 12:07:32 -0700498// This exists mainly to keep implementation details out of the header file.
499class Libraries {
500 public:
501 Libraries() {
502 }
503
504 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700505 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700506 }
507
Elliott Hughesae80b492012-04-24 10:43:17 -0700508 void Dump(std::ostream& os) const {
509 bool first = true;
510 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
511 if (!first) {
512 os << ' ';
513 }
514 first = false;
515 os << it->first;
516 }
517 }
518
519 size_t size() const {
520 return libraries_.size();
521 }
522
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700524 It it = libraries_.find(path);
525 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 }
527
528 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700529 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 }
531
532 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700533 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 std::string jni_short_name(JniShortName(m));
536 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700537 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
539 SharedLibrary* library = it->second;
540 if (library->GetClassLoader() != declaring_class_loader) {
541 // We only search libraries loaded by the appropriate ClassLoader.
542 continue;
543 }
544 // Try the short name then the long name...
545 void* fn = library->FindSymbol(jni_short_name);
546 if (fn == NULL) {
547 fn = library->FindSymbol(jni_long_name);
548 }
549 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800550 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
551 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 return fn;
553 }
554 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700556 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700557 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 return NULL;
560 }
561
562 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700563 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700564
Elliott Hughesa0e18062012-04-13 15:59:59 -0700565 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700566};
567
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
569 jvalue* args) {
570 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700571 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700572 MethodHelper mh(method);
573 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574 arg_array.BuildArgArray(soa, args);
575 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700576}
577
Mathieu Chartier66f19252012-09-18 08:57:04 -0700578JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800582}
583
Elliott Hughescdf53122011-08-19 15:46:09 -0700584class JNI {
585 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700586 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700587 return JNI_VERSION_1_6;
588 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700589
Ian Rogers25e8b912012-09-07 11:31:36 -0700590 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700592 return NULL;
593 }
594
Elliott Hughescdf53122011-08-19 15:46:09 -0700595 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700597 Runtime* runtime = Runtime::Current();
598 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700600 Class* c = NULL;
601 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800602 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800603 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700604 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800605 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700606 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700608 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700609
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700612 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700614 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 ScopedObjectAccess soa(env);
618 Field* field = soa.Decode<Field*>(java_field);
619 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700624 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700627
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 ScopedObjectAccess soa(env);
630 Field* field = soa.DecodeField(fid);
631 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughes37f7a402011-08-22 18:56:01 -0700634 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 ScopedObjectAccess soa(env);
636 Object* o = soa.Decode<Object*>(java_object);
637 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700638 }
639
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700640 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
642 Class* c = soa.Decode<Class*>(java_class);
643 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(env);
648 Class* c1 = soa.Decode<Class*>(java_class1);
649 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughese84278b2012-03-22 10:06:53 -0700653 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800655 if (java_class == NULL) {
656 JniAbortF("IsInstanceOf", "null class (second argument)");
657 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700659 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 return JNI_TRUE;
661 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 Object* obj = soa.Decode<Object*>(jobj);
663 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700664 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccess soa(env);
670 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 if (exception == NULL) {
672 return JNI_ERR;
673 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 return JNI_OK;
676 }
677
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700678 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700679 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 }
681
682 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700683 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 }
685
686 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700687 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 }
689
690 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700691 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700692
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700694 Throwable* original_exception = self->GetException();
695 self->ClearException();
696
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700698 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
699 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
700 if (mid == NULL) {
701 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700702 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 } else {
704 env->CallVoidMethod(exception.get(), mid);
705 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700706 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 << " thrown while calling printStackTrace";
708 self->ClearException();
709 }
710 }
711
712 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccess soa(env);
717 Object* exception = soa.Self()->GetException();
718 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
720
Ian Rogers25e8b912012-09-07 11:31:36 -0700721 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 LOG(FATAL) << "JNI FatalError called: " << msg;
723 }
724
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700725 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800726 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700727 return JNI_ERR;
728 }
Ian Rogersef28b142012-11-30 14:22:18 -0800729 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 return JNI_OK;
731 }
732
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700733 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 ScopedObjectAccess soa(env);
735 Object* survivor = soa.Decode<Object*>(java_survivor);
736 soa.Env()->PopFrame();
737 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
739
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700740 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800741 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 }
743
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 if (obj == NULL) {
746 return NULL;
747 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700748 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700752 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 return reinterpret_cast<jobject>(ref);
755 }
756
757 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 if (obj == NULL) {
759 return;
760 }
Ian Rogersef28b142012-11-30 14:22:18 -0800761 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800763 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
764 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765
766 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
767 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
768 << "failed to find entry";
769 }
770 }
771
772 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ScopedObjectAccess soa(env);
774 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 }
776
777 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 if (obj == NULL) {
779 return;
780 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700781 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700784 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785
786 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
787 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
788 << "failed to find entry";
789 }
790 }
791
792 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 if (obj == NULL) {
794 return NULL;
795 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700796 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700798
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700799 uint32_t cookie = soa.Env()->local_ref_cookie;
800 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 return reinterpret_cast<jobject>(ref);
802 }
803
804 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 if (obj == NULL) {
806 return;
807 }
Ian Rogersef28b142012-11-30 14:22:18 -0800808 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700809
Ian Rogersef28b142012-11-30 14:22:18 -0800810 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 if (!locals.Remove(cookie, obj)) {
812 // Attempting to delete a local reference that is not in the
813 // topmost local reference frame is a no-op. DeleteLocalRef returns
814 // void and doesn't throw any exceptions, but we should probably
815 // complain about it so the user will notice that things aren't
816 // going quite the way they expect.
817 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
818 << "failed to find entry";
819 }
820 }
821
822 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700824 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 }
826
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700827 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
829 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700830 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700831 return NULL;
832 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700833 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 }
835
Elliott Hughese84278b2012-03-22 10:06:53 -0700836 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700839 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 va_end(args);
841 return result;
842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700847 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700848 return NULL;
849 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700850 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700851 if (result == NULL) {
852 return NULL;
853 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700857 return local_result;
858 } else {
859 return NULL;
860 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(env);
865 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700866 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700867 return NULL;
868 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700869 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700870 if (result == NULL) {
871 return NULL;
872 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700876 return local_result;
877 } else {
878 return NULL;
879 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883 ScopedObjectAccess soa(env);
884 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
886
887 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 ScopedObjectAccess soa(env);
889 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 va_list ap;
894 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700895 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
903 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
904 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 ScopedObjectAccess soa(env);
909 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
910 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_list ap;
915 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700916 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700919 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
929 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 va_list ap;
935 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700938 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 ScopedObjectAccess soa(env);
943 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
948 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 va_list ap;
953 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700954 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700957 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 ScopedObjectAccess soa(env);
962 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(env);
967 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700973 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700976 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
981 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
986 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700995 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
1000 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_list ap;
1010 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001011 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001014 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
1019 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 ScopedObjectAccess soa(env);
1024 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001030 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001033 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
1043 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_list ap;
1048 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001049 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001052 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
1062 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_list ap;
1067 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001068 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001069 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 ScopedObjectAccess soa(env);
1075 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001083 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001086 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1088 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 va_end(ap);
1090 return local_result;
1091 }
1092
1093 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001094 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
1096 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1097 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
1100 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001101 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 ScopedObjectAccess soa(env);
1103 JValue result(InvokeWithJValues(soa, obj, mid, args));
1104 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
1107 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001108 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001111 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001114 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
1117 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001118 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
1123 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001124 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 ScopedObjectAccess soa(env);
1126 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001129 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001132 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001135 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
1138 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001139 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
1144 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001145 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 ScopedObjectAccess soa(env);
1147 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001150 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
1159 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001160 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
1165 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001166 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001174 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001187 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001195 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001198 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
1210 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001216 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
1222 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
1225 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
1231 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001237 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001240 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001244 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
1246 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
1252 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001258 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001261 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001265 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env);
1273 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001276 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001279 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_end(ap);
1282 }
1283
1284 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001291 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 ScopedObjectAccess soa(env);
1293 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001296 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 ScopedObjectAccess soa(env);
1298 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001300
1301
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001302 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedObjectAccess soa(env);
1304 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001306
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001307 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 ScopedObjectAccess soa(env);
1309 Object* o = soa.Decode<Object*>(obj);
1310 Field* f = soa.DecodeField(fid);
1311 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001313
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001314 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 ScopedObjectAccess soa(env);
1316 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001317 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001320 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 Object* o = soa.Decode<Object*>(java_object);
1323 Object* v = soa.Decode<Object*>(java_value);
1324 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001325 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001328 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 Object* v = soa.Decode<Object*>(java_value);
1331 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001332 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001335#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env); \
1337 Object* o = soa.Decode<Object*>(instance); \
1338 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001339 return f->fn(o)
1340
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001341#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1342 ScopedObjectAccess soa(env); \
1343 Field* f = soa.DecodeField(fid); \
1344 return f->fn(f->GetDeclaringClass())
1345
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env); \
1348 Object* o = soa.Decode<Object*>(instance); \
1349 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001350 f->fn(o, value)
1351
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001352#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1353 ScopedObjectAccess soa(env); \
1354 Field* f = soa.DecodeField(fid); \
1355 f->fn(f->GetDeclaringClass(), value)
1356
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1358 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1362 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1366 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1370 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1374 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1378 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1382 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1386 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001389 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001390 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001394 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001397 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001398 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001401 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001402 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001406 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001409 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001410 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 }
1412
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001413 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001414 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 }
1416
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001418 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 }
1420
1421 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1422 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1423 }
1424
1425 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1426 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1427 }
1428
1429 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1430 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1431 }
1432
1433 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1434 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1435 }
1436
1437 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1438 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1439 }
1440
1441 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1442 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1443 }
1444
1445 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1446 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1447 }
1448
1449 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1450 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1451 }
1452
1453 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001454 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 }
1456
1457 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001458 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 }
1460
1461 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001462 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 }
1464
1465 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001466 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 }
1468
1469 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001470 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 }
1472
1473 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001474 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 }
1476
1477 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001478 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 }
1480
1481 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001482 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001487 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001488 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1490 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 va_end(ap);
1492 return local_result;
1493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001496 ScopedObjectAccess soa(env);
1497 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1498 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001501 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502 ScopedObjectAccess soa(env);
1503 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1504 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001507 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001509 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001510 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001513 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
1518 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001521 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
1523 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001526 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001528 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001529 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001532 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 ScopedObjectAccess soa(env);
1537 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
1542 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001545 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001548 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001551 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
1556 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001560 ScopedObjectAccess soa(env);
1561 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001567 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001570 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001574 ScopedObjectAccess soa(env);
1575 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env);
1580 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001586 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001589 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env);
1594 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001598 ScopedObjectAccess soa(env);
1599 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001602 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001605 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001606 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001608 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001612 ScopedObjectAccess soa(env);
1613 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001617 ScopedObjectAccess soa(env);
1618 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001624 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001627 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 ScopedObjectAccess soa(env);
1632 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 ScopedObjectAccess soa(env);
1637 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001643 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001646 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 ScopedObjectAccess soa(env);
1651 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 ScopedObjectAccess soa(env);
1656 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001662 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_end(ap);
1665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
1669 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
1674 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes814e4032011-08-23 12:07:56 -07001677 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001679 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001680 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
1683 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 if (utf == NULL) {
1685 return NULL;
1686 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001688 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes814e4032011-08-23 12:07:56 -07001692 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 ScopedObjectAccess soa(env);
1694 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001695 }
1696
1697 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 ScopedObjectAccess soa(env);
1699 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001700 }
1701
Elliott Hughesb465ab02011-08-24 11:21:21 -07001702 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
1704 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001705 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001707 } else {
1708 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1709 memcpy(buf, chars + start, length * sizeof(jchar));
1710 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001711 }
1712
Elliott Hughesb465ab02011-08-24 11:21:21 -07001713 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 ScopedObjectAccess soa(env);
1715 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001716 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001718 } else {
1719 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1720 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1721 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001722 }
1723
Elliott Hughes75770752011-08-24 17:52:38 -07001724 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 ScopedObjectAccess soa(env);
1726 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001727 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001729 if (is_copy != NULL) {
1730 *is_copy = JNI_FALSE;
1731 }
1732 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
1737 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes75770752011-08-24 17:52:38 -07001740 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001741 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes75770752011-08-24 17:52:38 -07001744 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001746 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes75770752011-08-24 17:52:38 -07001749 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001750 if (java_string == NULL) {
1751 return NULL;
1752 }
1753 if (is_copy != NULL) {
1754 *is_copy = JNI_TRUE;
1755 }
Ian Rogersef28b142012-11-30 14:22:18 -08001756 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001758 size_t byte_count = s->GetUtfLength();
1759 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001760 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001761 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1762 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1763 bytes[byte_count] = '\0';
1764 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001765 }
1766
Elliott Hughes75770752011-08-24 17:52:38 -07001767 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001768 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001769 }
1770
Elliott Hughesbd935992011-08-22 11:59:34 -07001771 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 ScopedObjectAccess soa(env);
1773 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001774 if (!obj->IsArrayInstance()) {
1775 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1776 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001777 Array* array = obj->AsArray();
1778 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
1783 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1784 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
1787 static void SetObjectArrayElement(JNIEnv* env,
1788 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1791 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 array->Set(index, value);
1793 }
1794
1795 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
1797 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
1800 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
1802 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
1805 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
1807 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
1815 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 ScopedObjectAccess soa(env);
1817 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
1820 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
1822 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
1825 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001826 ScopedObjectAccess soa(env);
1827 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
1830 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001831 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001832 if (length < 0) {
1833 JniAbortF("NewObjectArray", "negative array length: %d", length);
1834 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001835
1836 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 std::string descriptor;
1839 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001840 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841
1842 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001843 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1844 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 return NULL;
1846 }
1847
Elliott Hughes75770752011-08-24 17:52:38 -07001848 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001850 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001851 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001853 for (jsize i = 0; i < length; ++i) {
1854 result->Set(i, initial_object);
1855 }
1856 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001857 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
1860 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 ScopedObjectAccess soa(env);
1862 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Ian Rogersa15e67d2012-02-28 13:51:55 -08001865 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 Array* array = soa.Decode<Array*>(java_array);
1868 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001869 if (is_copy != NULL) {
1870 *is_copy = JNI_FALSE;
1871 }
1872 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001876 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001877 }
1878
Elliott Hughes75770752011-08-24 17:52:38 -07001879 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
1881 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes75770752011-08-24 17:52:38 -07001884 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 ScopedObjectAccess soa(env);
1886 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Elliott Hughes75770752011-08-24 17:52:38 -07001889 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
1891 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes75770752011-08-24 17:52:38 -07001894 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
1896 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
Elliott Hughes75770752011-08-24 17:52:38 -07001899 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
1901 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
1906 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 ScopedObjectAccess soa(env);
1911 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 ScopedObjectAccess soa(env);
1916 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001919 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001920 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 }
1922
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001923 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001924 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001927 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001928 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001931 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001932 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001936 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001939 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001940 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001943 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001944 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001947 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001948 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes814e4032011-08-23 12:07:56 -07001951 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
1953 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes814e4032011-08-23 12:07:56 -07001956 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
1958 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes814e4032011-08-23 12:07:56 -07001961 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
1963 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
1968 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes814e4032011-08-23 12:07:56 -07001971 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ScopedObjectAccess soa(env);
1973 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 ScopedObjectAccess soa(env);
1978 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes814e4032011-08-23 12:07:56 -07001981 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982 ScopedObjectAccess soa(env);
1983 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes814e4032011-08-23 12:07:56 -07001986 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
1993 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
1998 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
2003 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes814e4032011-08-23 12:07:56 -07002006 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
2008 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes814e4032011-08-23 12:07:56 -07002011 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
2013 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes814e4032011-08-23 12:07:56 -07002016 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
2018 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes814e4032011-08-23 12:07:56 -07002021 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
2023 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
2028 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes5174fe62011-08-23 15:12:35 -07002031 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002032 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2033 }
2034
2035 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 -07002036 ScopedObjectAccess soa(env);
2037 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002038
Elliott Hughesc8fece32013-01-02 11:27:23 -08002039 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 const char* name = methods[i].name;
2041 const char* sig = methods[i].signature;
2042
2043 if (*sig == '!') {
2044 // TODO: fast jni. it's too noisy to log all these.
2045 ++sig;
2046 }
2047
Mathieu Chartier66f19252012-09-18 08:57:04 -07002048 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002049 if (m == NULL) {
2050 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002052 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002053 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2054 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002055 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002057 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002058 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2059 << PrettyDescriptor(c) << "." << name << sig
2060 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002061 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 return JNI_ERR;
2063 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002064
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002065 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002066
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069 return JNI_OK;
2070 }
2071
Elliott Hughes5174fe62011-08-23 15:12:35 -07002072 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 ScopedObjectAccess soa(env);
2074 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002075
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002076 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002077
2078 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002079 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002080 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002082 }
2083 }
2084 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002085 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002086 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002088 }
2089 }
2090
2091 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2095 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2096 ScopedObjectAccess soa(env);
2097 Object* o = soa.Decode<Object*>(java_object);
2098 o->MonitorEnter(soa.Self());
2099 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002100 return JNI_ERR;
2101 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002103 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 static jint MonitorExit(JNIEnv* env, jobject java_object)
2107 UNLOCK_FUNCTION(monitor_lock_) {
2108 ScopedObjectAccess soa(env);
2109 Object* o = soa.Decode<Object*>(java_object);
2110 o->MonitorExit(soa.Self());
2111 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002112 return JNI_ERR;
2113 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002114 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002115 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
2118 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 Runtime* runtime = Runtime::Current();
2120 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002121 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 } else {
2123 *vm = NULL;
2124 }
2125 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2126 }
2127
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002129 if (capacity < 0) {
2130 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2131 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002132 if (address == NULL && capacity != 0) {
2133 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002134 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002135
Elliott Hughes96a98872012-12-19 14:21:15 -08002136 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002137 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2138 CHECK_LE(capacity, 0xffffffff);
2139 jint address_arg = reinterpret_cast<jint>(address);
2140 jint capacity_arg = static_cast<jint>(capacity);
2141
Elliott Hugheseac76672012-05-24 21:56:51 -07002142 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2143 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2144 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002145 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughesb465ab02011-08-24 11:21:21 -07002148 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002149 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughesb465ab02011-08-24 11:21:21 -07002152 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002153 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155
Elliott Hughesb465ab02011-08-24 11:21:21 -07002156 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002157 if (java_object == NULL) {
2158 JniAbortF("GetObjectRefType", "null object");
2159 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002160
2161 // Do we definitely know what kind of reference this is?
2162 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2163 IndirectRefKind kind = GetIndirectRefKind(ref);
2164 switch (kind) {
2165 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002166 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002167 return JNILocalRefType;
2168 }
2169 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002170 case kGlobal:
2171 return JNIGlobalRefType;
2172 case kWeakGlobal:
2173 return JNIWeakGlobalRefType;
2174 case kSirtOrInvalid:
2175 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002176 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002177 return JNILocalRefType;
2178 }
2179
Ian Rogersef28b142012-11-30 14:22:18 -08002180 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002181 return JNIInvalidRefType;
2182 }
2183
Elliott Hughesb465ab02011-08-24 11:21:21 -07002184 // If we're handing out direct pointers, check whether it's a direct pointer
2185 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002186 {
2187 ScopedObjectAccess soa(env);
2188 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2189 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2190 return JNILocalRefType;
2191 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002192 }
2193 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002194 return JNIInvalidRefType;
2195 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002196 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2197 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002199
2200 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002201 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2202 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 // TODO: we should try to expand the table if necessary.
2204 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2205 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2206 return JNI_ERR;
2207 }
2208 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002209 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002210 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2211 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002212 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 soa.Self()->ThrowOutOfMemoryError(caller);
2214 }
2215 return okay ? JNI_OK : JNI_ERR;
2216 }
2217
2218 template<typename JniT, typename ArtT>
2219 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002221 if (length < 0) {
2222 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2223 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002224 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 return soa.AddLocalReference<JniT>(result);
2226 }
2227
2228 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2229 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2230 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2233 PinPrimitiveArray(soa, array);
2234 if (is_copy != NULL) {
2235 *is_copy = JNI_FALSE;
2236 }
2237 return array->GetData();
2238 }
2239
2240 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002241 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002243 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 Array* array = soa.Decode<Array*>(java_array);
2245 UnpinPrimitiveArray(soa, array);
2246 }
2247 }
2248
2249 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2250 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2251 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002253 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2254 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2255 ThrowAIOOBE(soa, array, start, length, "src");
2256 } else {
2257 JavaT* data = array->GetData();
2258 memcpy(buf, data + start, length * sizeof(JavaT));
2259 }
2260 }
2261
2262 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2263 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2264 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002265 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2267 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2268 ThrowAIOOBE(soa, array, start, length, "dst");
2269 } else {
2270 JavaT* data = array->GetData();
2271 memcpy(data + start, buf, length * sizeof(JavaT));
2272 }
2273 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002274};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002275
Elliott Hughes88c5c352012-03-15 18:49:48 -07002276const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002277 NULL, // reserved0.
2278 NULL, // reserved1.
2279 NULL, // reserved2.
2280 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 JNI::GetVersion,
2282 JNI::DefineClass,
2283 JNI::FindClass,
2284 JNI::FromReflectedMethod,
2285 JNI::FromReflectedField,
2286 JNI::ToReflectedMethod,
2287 JNI::GetSuperclass,
2288 JNI::IsAssignableFrom,
2289 JNI::ToReflectedField,
2290 JNI::Throw,
2291 JNI::ThrowNew,
2292 JNI::ExceptionOccurred,
2293 JNI::ExceptionDescribe,
2294 JNI::ExceptionClear,
2295 JNI::FatalError,
2296 JNI::PushLocalFrame,
2297 JNI::PopLocalFrame,
2298 JNI::NewGlobalRef,
2299 JNI::DeleteGlobalRef,
2300 JNI::DeleteLocalRef,
2301 JNI::IsSameObject,
2302 JNI::NewLocalRef,
2303 JNI::EnsureLocalCapacity,
2304 JNI::AllocObject,
2305 JNI::NewObject,
2306 JNI::NewObjectV,
2307 JNI::NewObjectA,
2308 JNI::GetObjectClass,
2309 JNI::IsInstanceOf,
2310 JNI::GetMethodID,
2311 JNI::CallObjectMethod,
2312 JNI::CallObjectMethodV,
2313 JNI::CallObjectMethodA,
2314 JNI::CallBooleanMethod,
2315 JNI::CallBooleanMethodV,
2316 JNI::CallBooleanMethodA,
2317 JNI::CallByteMethod,
2318 JNI::CallByteMethodV,
2319 JNI::CallByteMethodA,
2320 JNI::CallCharMethod,
2321 JNI::CallCharMethodV,
2322 JNI::CallCharMethodA,
2323 JNI::CallShortMethod,
2324 JNI::CallShortMethodV,
2325 JNI::CallShortMethodA,
2326 JNI::CallIntMethod,
2327 JNI::CallIntMethodV,
2328 JNI::CallIntMethodA,
2329 JNI::CallLongMethod,
2330 JNI::CallLongMethodV,
2331 JNI::CallLongMethodA,
2332 JNI::CallFloatMethod,
2333 JNI::CallFloatMethodV,
2334 JNI::CallFloatMethodA,
2335 JNI::CallDoubleMethod,
2336 JNI::CallDoubleMethodV,
2337 JNI::CallDoubleMethodA,
2338 JNI::CallVoidMethod,
2339 JNI::CallVoidMethodV,
2340 JNI::CallVoidMethodA,
2341 JNI::CallNonvirtualObjectMethod,
2342 JNI::CallNonvirtualObjectMethodV,
2343 JNI::CallNonvirtualObjectMethodA,
2344 JNI::CallNonvirtualBooleanMethod,
2345 JNI::CallNonvirtualBooleanMethodV,
2346 JNI::CallNonvirtualBooleanMethodA,
2347 JNI::CallNonvirtualByteMethod,
2348 JNI::CallNonvirtualByteMethodV,
2349 JNI::CallNonvirtualByteMethodA,
2350 JNI::CallNonvirtualCharMethod,
2351 JNI::CallNonvirtualCharMethodV,
2352 JNI::CallNonvirtualCharMethodA,
2353 JNI::CallNonvirtualShortMethod,
2354 JNI::CallNonvirtualShortMethodV,
2355 JNI::CallNonvirtualShortMethodA,
2356 JNI::CallNonvirtualIntMethod,
2357 JNI::CallNonvirtualIntMethodV,
2358 JNI::CallNonvirtualIntMethodA,
2359 JNI::CallNonvirtualLongMethod,
2360 JNI::CallNonvirtualLongMethodV,
2361 JNI::CallNonvirtualLongMethodA,
2362 JNI::CallNonvirtualFloatMethod,
2363 JNI::CallNonvirtualFloatMethodV,
2364 JNI::CallNonvirtualFloatMethodA,
2365 JNI::CallNonvirtualDoubleMethod,
2366 JNI::CallNonvirtualDoubleMethodV,
2367 JNI::CallNonvirtualDoubleMethodA,
2368 JNI::CallNonvirtualVoidMethod,
2369 JNI::CallNonvirtualVoidMethodV,
2370 JNI::CallNonvirtualVoidMethodA,
2371 JNI::GetFieldID,
2372 JNI::GetObjectField,
2373 JNI::GetBooleanField,
2374 JNI::GetByteField,
2375 JNI::GetCharField,
2376 JNI::GetShortField,
2377 JNI::GetIntField,
2378 JNI::GetLongField,
2379 JNI::GetFloatField,
2380 JNI::GetDoubleField,
2381 JNI::SetObjectField,
2382 JNI::SetBooleanField,
2383 JNI::SetByteField,
2384 JNI::SetCharField,
2385 JNI::SetShortField,
2386 JNI::SetIntField,
2387 JNI::SetLongField,
2388 JNI::SetFloatField,
2389 JNI::SetDoubleField,
2390 JNI::GetStaticMethodID,
2391 JNI::CallStaticObjectMethod,
2392 JNI::CallStaticObjectMethodV,
2393 JNI::CallStaticObjectMethodA,
2394 JNI::CallStaticBooleanMethod,
2395 JNI::CallStaticBooleanMethodV,
2396 JNI::CallStaticBooleanMethodA,
2397 JNI::CallStaticByteMethod,
2398 JNI::CallStaticByteMethodV,
2399 JNI::CallStaticByteMethodA,
2400 JNI::CallStaticCharMethod,
2401 JNI::CallStaticCharMethodV,
2402 JNI::CallStaticCharMethodA,
2403 JNI::CallStaticShortMethod,
2404 JNI::CallStaticShortMethodV,
2405 JNI::CallStaticShortMethodA,
2406 JNI::CallStaticIntMethod,
2407 JNI::CallStaticIntMethodV,
2408 JNI::CallStaticIntMethodA,
2409 JNI::CallStaticLongMethod,
2410 JNI::CallStaticLongMethodV,
2411 JNI::CallStaticLongMethodA,
2412 JNI::CallStaticFloatMethod,
2413 JNI::CallStaticFloatMethodV,
2414 JNI::CallStaticFloatMethodA,
2415 JNI::CallStaticDoubleMethod,
2416 JNI::CallStaticDoubleMethodV,
2417 JNI::CallStaticDoubleMethodA,
2418 JNI::CallStaticVoidMethod,
2419 JNI::CallStaticVoidMethodV,
2420 JNI::CallStaticVoidMethodA,
2421 JNI::GetStaticFieldID,
2422 JNI::GetStaticObjectField,
2423 JNI::GetStaticBooleanField,
2424 JNI::GetStaticByteField,
2425 JNI::GetStaticCharField,
2426 JNI::GetStaticShortField,
2427 JNI::GetStaticIntField,
2428 JNI::GetStaticLongField,
2429 JNI::GetStaticFloatField,
2430 JNI::GetStaticDoubleField,
2431 JNI::SetStaticObjectField,
2432 JNI::SetStaticBooleanField,
2433 JNI::SetStaticByteField,
2434 JNI::SetStaticCharField,
2435 JNI::SetStaticShortField,
2436 JNI::SetStaticIntField,
2437 JNI::SetStaticLongField,
2438 JNI::SetStaticFloatField,
2439 JNI::SetStaticDoubleField,
2440 JNI::NewString,
2441 JNI::GetStringLength,
2442 JNI::GetStringChars,
2443 JNI::ReleaseStringChars,
2444 JNI::NewStringUTF,
2445 JNI::GetStringUTFLength,
2446 JNI::GetStringUTFChars,
2447 JNI::ReleaseStringUTFChars,
2448 JNI::GetArrayLength,
2449 JNI::NewObjectArray,
2450 JNI::GetObjectArrayElement,
2451 JNI::SetObjectArrayElement,
2452 JNI::NewBooleanArray,
2453 JNI::NewByteArray,
2454 JNI::NewCharArray,
2455 JNI::NewShortArray,
2456 JNI::NewIntArray,
2457 JNI::NewLongArray,
2458 JNI::NewFloatArray,
2459 JNI::NewDoubleArray,
2460 JNI::GetBooleanArrayElements,
2461 JNI::GetByteArrayElements,
2462 JNI::GetCharArrayElements,
2463 JNI::GetShortArrayElements,
2464 JNI::GetIntArrayElements,
2465 JNI::GetLongArrayElements,
2466 JNI::GetFloatArrayElements,
2467 JNI::GetDoubleArrayElements,
2468 JNI::ReleaseBooleanArrayElements,
2469 JNI::ReleaseByteArrayElements,
2470 JNI::ReleaseCharArrayElements,
2471 JNI::ReleaseShortArrayElements,
2472 JNI::ReleaseIntArrayElements,
2473 JNI::ReleaseLongArrayElements,
2474 JNI::ReleaseFloatArrayElements,
2475 JNI::ReleaseDoubleArrayElements,
2476 JNI::GetBooleanArrayRegion,
2477 JNI::GetByteArrayRegion,
2478 JNI::GetCharArrayRegion,
2479 JNI::GetShortArrayRegion,
2480 JNI::GetIntArrayRegion,
2481 JNI::GetLongArrayRegion,
2482 JNI::GetFloatArrayRegion,
2483 JNI::GetDoubleArrayRegion,
2484 JNI::SetBooleanArrayRegion,
2485 JNI::SetByteArrayRegion,
2486 JNI::SetCharArrayRegion,
2487 JNI::SetShortArrayRegion,
2488 JNI::SetIntArrayRegion,
2489 JNI::SetLongArrayRegion,
2490 JNI::SetFloatArrayRegion,
2491 JNI::SetDoubleArrayRegion,
2492 JNI::RegisterNatives,
2493 JNI::UnregisterNatives,
2494 JNI::MonitorEnter,
2495 JNI::MonitorExit,
2496 JNI::GetJavaVM,
2497 JNI::GetStringRegion,
2498 JNI::GetStringUTFRegion,
2499 JNI::GetPrimitiveArrayCritical,
2500 JNI::ReleasePrimitiveArrayCritical,
2501 JNI::GetStringCritical,
2502 JNI::ReleaseStringCritical,
2503 JNI::NewWeakGlobalRef,
2504 JNI::DeleteWeakGlobalRef,
2505 JNI::ExceptionCheck,
2506 JNI::NewDirectByteBuffer,
2507 JNI::GetDirectBufferAddress,
2508 JNI::GetDirectBufferCapacity,
2509 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002510};
2511
Elliott Hughes75770752011-08-24 17:52:38 -07002512JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002513 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002514 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002515 local_ref_cookie(IRT_FIRST_SEGMENT),
2516 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002517 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002518 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002519 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002520 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002521 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002522 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002523 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002524 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2525 // errors will ensue.
2526 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2527 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002528}
2529
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002530JNIEnvExt::~JNIEnvExt() {
2531}
2532
Elliott Hughes88c5c352012-03-15 18:49:48 -07002533void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2534 check_jni = enabled;
2535 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002536}
2537
Elliott Hughes73e66f72012-05-09 09:34:45 -07002538void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2539 locals.Dump(os);
2540 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002541}
2542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002543void JNIEnvExt::PushFrame(int /*capacity*/) {
2544 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002545 stacked_local_ref_cookies.push_back(local_ref_cookie);
2546 local_ref_cookie = locals.GetSegmentState();
2547}
2548
2549void JNIEnvExt::PopFrame() {
2550 locals.SetSegmentState(local_ref_cookie);
2551 local_ref_cookie = stacked_local_ref_cookies.back();
2552 stacked_local_ref_cookies.pop_back();
2553}
2554
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002555Offset JNIEnvExt::SegmentStateOffset() {
2556 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2557 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2558}
2559
Carl Shapiroea4dca82011-08-01 13:45:38 -07002560// JNI Invocation interface.
2561
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002562extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2563 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002564 if (IsBadJniVersion(args->version)) {
2565 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566 return JNI_EVERSION;
2567 }
2568 Runtime::Options options;
2569 for (int i = 0; i < args->nOptions; ++i) {
2570 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002571 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002572 }
2573 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002574 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002577 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002578 bool started = runtime->Start();
2579 if (!started) {
2580 delete Thread::Current()->GetJniEnv();
2581 delete runtime->GetJavaVM();
2582 LOG(WARNING) << "CreateJavaVM failed";
2583 return JNI_ERR;
2584 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002585 *p_env = Thread::Current()->GetJniEnv();
2586 *p_vm = runtime->GetJavaVM();
2587 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002588}
2589
Elliott Hughesf2682d52011-08-15 16:37:04 -07002590extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002591 Runtime* runtime = Runtime::Current();
2592 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002593 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002595 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002596 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597 }
2598 return JNI_OK;
2599}
2600
2601// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002602extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002603 return JNI_ERR;
2604}
2605
Elliott Hughescdf53122011-08-19 15:46:09 -07002606class JII {
2607 public:
2608 static jint DestroyJavaVM(JavaVM* vm) {
2609 if (vm == NULL) {
2610 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002611 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002612 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2613 delete raw_vm->runtime;
2614 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002615 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616
Elliott Hughescdf53122011-08-19 15:46:09 -07002617 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002618 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002619 }
2620
2621 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002622 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002623 }
2624
2625 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002626 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002627 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002628 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002629 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2630 Runtime* runtime = raw_vm->runtime;
2631 runtime->DetachCurrentThread();
2632 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002633 }
2634
2635 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002636 if (IsBadJniVersion(version)) {
2637 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002638 return JNI_EVERSION;
2639 }
2640 if (vm == NULL || env == NULL) {
2641 return JNI_ERR;
2642 }
2643 Thread* thread = Thread::Current();
2644 if (thread == NULL) {
2645 *env = NULL;
2646 return JNI_EDETACHED;
2647 }
2648 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002649 return JNI_OK;
2650 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002651};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002652
Elliott Hughes88c5c352012-03-15 18:49:48 -07002653const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002654 NULL, // reserved0
2655 NULL, // reserved1
2656 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002657 JII::DestroyJavaVM,
2658 JII::AttachCurrentThread,
2659 JII::DetachCurrentThread,
2660 JII::GetEnv,
2661 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002662};
2663
Elliott Hughesa0957642011-09-02 14:27:33 -07002664JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002665 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002666 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002667 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002668 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002669 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002670 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002671 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002672 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002673 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002674 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002675 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002676 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002677 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002678 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002679 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002680 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002681 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002682 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002683 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002684}
2685
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002686JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002688}
2689
Elliott Hughes88c5c352012-03-15 18:49:48 -07002690void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2691 check_jni = enabled;
2692 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002693}
2694
Elliott Hughesae80b492012-04-24 10:43:17 -07002695void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2696 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2697 if (force_copy) {
2698 os << " (with forcecopy)";
2699 }
2700 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002701 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002702 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002703 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002704 os << "; pins=" << pin_table.Size();
2705 }
2706 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002707 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002708 os << "; globals=" << globals.Capacity();
2709 }
2710 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002711 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002712 if (weak_globals.Capacity() > 0) {
2713 os << " (plus " << weak_globals.Capacity() << " weak)";
2714 }
2715 }
2716 os << '\n';
2717
2718 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002719 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002720 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2721 }
2722}
2723
Elliott Hughes73e66f72012-05-09 09:34:45 -07002724void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002725 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002726 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002727 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002728 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002729 }
2730 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002731 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002732 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002733 }
2734 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002735 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002736 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002737 }
2738}
2739
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002740bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2741 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002742 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002743
2744 // See if we've already loaded this library. If we have, and the class loader
2745 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002746 // TODO: for better results we should canonicalize the pathname (or even compare
2747 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002748 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002749 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002750 {
2751 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002752 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002753 library = libraries->Get(path);
2754 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 if (library != NULL) {
2756 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 // The library will be associated with class_loader. The JNI
2758 // spec says we can't load the same library into more than one
2759 // class loader.
2760 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2761 "ClassLoader %p; can't open in ClassLoader %p",
2762 path.c_str(), library->GetClassLoader(), class_loader);
2763 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002764 return false;
2765 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002766 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2767 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002768 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002769 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2770 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 return false;
2772 }
2773 return true;
2774 }
2775
2776 // Open the shared library. Because we're using a full path, the system
2777 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2778 // resolve this library's dependencies though.)
2779
2780 // Failures here are expected when java.library.path has several entries
2781 // and we have to hunt for the lib.
2782
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002783 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2784 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2785 // dlopen) becomes zero from dlclose.
2786
Elliott Hughescdf53122011-08-19 15:46:09 -07002787 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002788 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002789 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2790 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2791 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002792
Elliott Hughes84b2f142012-09-27 09:16:28 -07002793 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002794
2795 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002796 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002797 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002798 return false;
2799 }
2800
2801 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002802 // TODO: move the locking (and more of this logic) into Libraries.
2803 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002805 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002806 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002807 if (library == NULL) { // We won race to get libraries_lock
2808 library = new SharedLibrary(path, handle, class_loader);
2809 libraries->Put(path, library);
2810 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002811 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002812 }
2813 if (!created_library) {
2814 LOG(INFO) << "WOW: we lost a race to add shared library: "
2815 << "\"" << path << "\" ClassLoader=" << class_loader;
2816 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002817 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002818
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002819 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002820
2821 bool result = true;
2822 void* sym = dlsym(handle, "JNI_OnLoad");
2823 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002824 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002825 } else {
2826 // Call JNI_OnLoad. We have to override the current class
2827 // loader, which will always be "null" since the stuff at the
2828 // top of the stack is around Runtime.loadLibrary(). (See
2829 // the comments in the JNI FindClass function.)
2830 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2831 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002832 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002833 self->SetClassLoaderOverride(class_loader);
2834
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002835 int version = 0;
2836 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002837 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002838 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002839 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002840 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002841
Brian Carlstromaded5f72011-10-07 17:15:04 -07002842 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002843
Elliott Hughes83a25322013-03-14 11:18:53 -07002844 if (IsBadJniVersion(version)) {
2845 LOG(ERROR) << "Bad JNI version returned from JNI_OnLoad in \"" << path << "\": " << version;
Elliott Hughes79082e32011-08-25 12:07:32 -07002846 // It's unwise to call dlclose() here, but we can mark it
2847 // as bad and ensure that future load attempts will fail.
2848 // We don't know how far JNI_OnLoad got, so there could
2849 // be some partially-initialized stuff accessible through
2850 // newly-registered native method calls. We could try to
2851 // unregister them, but that doesn't seem worthwhile.
2852 result = false;
2853 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002854 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2855 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002856 }
2857 }
2858
2859 library->SetResult(result);
2860 return result;
2861}
2862
Mathieu Chartier66f19252012-09-18 08:57:04 -07002863void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002864 CHECK(m->IsNative());
2865
2866 Class* c = m->GetDeclaringClass();
2867
2868 // If this is a static method, it could be called before the class
2869 // has been initialized.
2870 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002871 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002872 return NULL;
2873 }
2874 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002875 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002876 }
2877
Brian Carlstrom16192862011-09-12 17:50:06 -07002878 std::string detail;
2879 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002880 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002881 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002882 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002883 native_method = libraries->FindNativeMethod(m, detail);
2884 }
2885 // throwing can cause libraries_lock to be reacquired
2886 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002887 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002888 }
2889 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002890}
2891
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002892void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002893 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002894 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002895 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002896 globals.VisitRoots(visitor, arg);
2897 }
2898 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002899 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002900 pin_table.VisitRoots(visitor, arg);
2901 }
2902 // The weak_globals table is visited by the GC itself (because it mutates the table).
2903}
2904
Elliott Hughesc8fece32013-01-02 11:27:23 -08002905void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2906 size_t method_count) {
2907 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2908 if (c.get() == NULL) {
2909 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2910 }
2911 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2912}
2913
Ian Rogersdf20fe02011-07-20 20:34:16 -07002914} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002915
2916std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2917 switch (rhs) {
2918 case JNIInvalidRefType:
2919 os << "JNIInvalidRefType";
2920 return os;
2921 case JNILocalRefType:
2922 os << "JNILocalRefType";
2923 return os;
2924 case JNIGlobalRefType:
2925 os << "JNIGlobalRefType";
2926 return os;
2927 case JNIWeakGlobalRefType:
2928 os << "JNIWeakGlobalRefType";
2929 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002930 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002931 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002932 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002933 }
2934}