blob: 6df03e999dca9699fefe537152eab9448b6523d7 [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 Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "gc/card_table-inl.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070032#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070033#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/class_loader.h"
36#include "mirror/field-inl.h"
37#include "mirror/abstract_method-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
40#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080041#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070042#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070043#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070048#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051using namespace art::mirror;
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053namespace art {
54
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055static const size_t kMonitorsInitial = 32; // Arbitrary.
56static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
57
58static const size_t kLocalsInitial = 64; // Arbitrary.
59static const size_t kLocalsMax = 512; // Arbitrary sanity check.
60
61static const size_t kPinTableInitial = 16; // Arbitrary.
62static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
63
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070064static size_t gGlobalsInitial = 512; // Arbitrary.
65static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
67static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
68static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
69
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070void SetJniGlobalsMax(size_t max) {
71 if (max != 0) {
72 gGlobalsMax = max;
73 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
74 }
75}
Ian Rogersdf20fe02011-07-20 20:34:16 -070076
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070079 if (obj == NULL) {
80 return NULL;
81 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070083 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070084 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070085 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
86 return reinterpret_cast<jweak>(ref);
87}
88
Jeff Hao19c5d372013-03-15 14:33:43 -070089static bool IsBadJniVersion(int version) {
90 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
91 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
92}
93
Jeff Hao5d917302013-02-27 17:57:33 -080094static void CheckMethodArguments(AbstractMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070096 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070097 const DexFile::TypeList* params = mh.GetParameterTypeList();
98 if (params == NULL) {
99 return; // No arguments so nothing to check.
100 }
Jeff Hao5d917302013-02-27 17:57:33 -0800101 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700103 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800104 if (!m->IsStatic()) {
105 offset = 1;
106 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 for (uint32_t i = 0; i < num_params; i++) {
108 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
109 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
110 if (param_type == NULL) {
111 Thread* self = Thread::Current();
112 CHECK(self->IsExceptionPending());
113 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
114 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
115 << self->GetException()->Dump();
116 self->ClearException();
117 ++error_count;
118 } else if (!param_type->IsPrimitive()) {
119 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800120 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700121 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700122 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
123 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
124 ++error_count;
125 }
Jeff Hao5d917302013-02-27 17:57:33 -0800126 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
127 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700128 }
129 }
130 if (error_count > 0) {
131 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
132 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700133 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700134 }
135}
Elliott Hughesb264f082012-04-06 17:10:10 -0700136
Jeff Hao5d917302013-02-27 17:57:33 -0800137void InvokeWithArgArray(const ScopedObjectAccess& soa, AbstractMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700138 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao5d917302013-02-27 17:57:33 -0800141 CheckMethodArguments(method, arg_array->GetArray());
Elliott Hughes4cacde82012-04-11 18:32:27 -0700142 }
Jeff Hao6474d192013-03-26 14:08:09 -0700143 method->Invoke(soa.Self(), arg_array->GetArray(), arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700144}
145
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
147 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700149 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700150 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700151 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800152 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700153 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800154 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700155 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
156 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700157}
158
Mathieu Chartier66f19252012-09-18 08:57:04 -0700159static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700161 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700162}
163
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
165 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700168 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700169 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800170 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700171 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800172 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700173 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
174 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700175}
176
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
178 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700181 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800183 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700184 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800185 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700186 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
187 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700188}
189
Elliott Hughes6b436852011-08-12 10:16:44 -0700190// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
191// separated with slashes but aren't wrapped with "L;" like regular descriptors
192// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
193// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
194// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700195static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700196 std::string result;
197 // Add the missing "L;" if necessary.
198 if (name[0] == '[') {
199 result = name;
200 } else {
201 result += 'L';
202 result += name;
203 result += ';';
204 }
205 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700206 if (result.find('.') != std::string::npos) {
207 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
208 << "\"" << name << "\"";
209 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700210 }
211 return result;
212}
213
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
215 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800218 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700219}
220
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
222 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700225 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700226 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700227 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700228
Mathieu Chartier66f19252012-09-18 08:57:04 -0700229 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700230 if (is_static) {
231 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700232 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700233 method = c->FindVirtualMethod(name, sig);
234 if (method == NULL) {
235 // No virtual method matching the signature. Search declared
236 // private methods and constructors.
237 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700238 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700239 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700240
Elliott Hughescdf53122011-08-19 15:46:09 -0700241 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 return NULL;
244 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700247}
248
Ian Rogersef28b142012-11-30 14:22:18 -0800249static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800251 AbstractMethod* method = soa.Self()->GetCurrentMethod();
252 if (method == NULL ||
253 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
254 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700255 }
256 return method->GetDeclaringClass()->GetClassLoader();
257}
258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
260 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700263 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700264 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700265 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700266
267 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 Class* field_type;
269 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
270 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800271 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700273 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700274 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700275 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 if (field_type == NULL) {
277 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 DCHECK(soa.Self()->IsExceptionPending());
279 soa.Self()->ClearException();
280 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700281 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800282 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return NULL;
284 }
285 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700292 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800293 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700294 return NULL;
295 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700297}
298
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700302 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700303 vm->pin_table.Add(array);
304}
305
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700309 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700310 vm->pin_table.Remove(array);
311}
312
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
314 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700316 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700318 "%s offset=%d length=%d %s.length=%d",
319 type.c_str(), start, length, identifier, array->GetLength());
320}
Ian Rogers0571d352011-11-03 19:51:38 -0700321
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
323 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700326 "offset=%d length=%d string.length()=%d", start, length, array_length);
327}
Elliott Hughes814e4032011-08-23 12:07:56 -0700328
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700330 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 // Turn the const char* into a java.lang.String.
332 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
333 if (msg != NULL && s.get() == NULL) {
334 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700335 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700336
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 // Choose an appropriate constructor and set up the arguments.
338 jvalue args[2];
339 const char* signature;
340 if (msg == NULL && cause == NULL) {
341 signature = "()V";
342 } else if (msg != NULL && cause == NULL) {
343 signature = "(Ljava/lang/String;)V";
344 args[0].l = s.get();
345 } else if (msg == NULL && cause != NULL) {
346 signature = "(Ljava/lang/Throwable;)V";
347 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700348 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
350 args[0].l = s.get();
351 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700352 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
354 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800355 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 LOG(ERROR) << "No <init>" << signature << " in "
357 << PrettyClass(soa.Decode<Class*>(exception_class));
358 return JNI_ERR;
359 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700360
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
362 if (exception.get() == NULL) {
363 return JNI_ERR;
364 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700365
Ian Rogersef28b142012-11-30 14:22:18 -0800366 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700368
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700370}
371
Elliott Hughes462c9442012-03-23 18:47:50 -0700372static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700373 if (vm == NULL || p_env == NULL) {
374 return JNI_ERR;
375 }
376
Elliott Hughes462c9442012-03-23 18:47:50 -0700377 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700378 Thread* self = Thread::Current();
379 if (self != NULL) {
380 *p_env = self->GetJniEnv();
381 return JNI_OK;
382 }
383
384 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
385
386 // No threads allowed in zygote mode.
387 if (runtime->IsZygote()) {
388 LOG(ERROR) << "Attempt to attach a thread in the zygote";
389 return JNI_ERR;
390 }
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
393 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700394 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700395 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700396 if (IsBadJniVersion(args->version)) {
397 LOG(ERROR) << "Bad JNI version passed to "
398 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
399 << args->version;
400 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700401 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700402 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700403 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700404 }
Elliott Hughes75770752011-08-24 17:52:38 -0700405
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800406 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700407 *p_env = NULL;
408 return JNI_ERR;
409 } else {
410 *p_env = Thread::Current()->GetJniEnv();
411 return JNI_OK;
412 }
Elliott Hughes75770752011-08-24 17:52:38 -0700413}
414
Elliott Hughes79082e32011-08-25 12:07:32 -0700415class SharedLibrary {
416 public:
417 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
418 : path_(path),
419 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700420 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700421 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700422 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700423 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700424 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700425 }
426
Elliott Hughes79082e32011-08-25 12:07:32 -0700427 Object* GetClassLoader() {
428 return class_loader_;
429 }
430
431 std::string GetPath() {
432 return path_;
433 }
434
435 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700436 * Check the result of an earlier call to JNI_OnLoad on this library.
437 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 bool CheckOnLoadResult()
440 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700441 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700443 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
444 bool okay;
445 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700446 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
449 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
450 // caller can continue.
451 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
452 okay = true;
453 } else {
454 while (jni_on_load_result_ == kPending) {
455 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700456 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700458
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 okay = (jni_on_load_result_ == kOkay);
460 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
461 << (okay ? "succeeded" : "failed") << "]";
462 }
463 }
464 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700465 return okay;
466 }
467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700469 Thread* self = Thread::Current();
470 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700471
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700473 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700474
475 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700477 }
478
479 void* FindSymbol(const std::string& symbol_name) {
480 return dlsym(handle_, symbol_name.c_str());
481 }
482
483 private:
484 enum JNI_OnLoadState {
485 kPending,
486 kFailed,
487 kOkay,
488 };
489
490 // Path to library "/system/lib/libjni.so".
491 std::string path_;
492
493 // The void* returned by dlopen(3).
494 void* handle_;
495
496 // The ClassLoader this library is associated with.
497 Object* class_loader_;
498
499 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700502 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700504 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700506 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700507};
508
Elliott Hughes79082e32011-08-25 12:07:32 -0700509// This exists mainly to keep implementation details out of the header file.
510class Libraries {
511 public:
512 Libraries() {
513 }
514
515 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700516 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
Elliott Hughesae80b492012-04-24 10:43:17 -0700519 void Dump(std::ostream& os) const {
520 bool first = true;
521 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
522 if (!first) {
523 os << ' ';
524 }
525 first = false;
526 os << it->first;
527 }
528 }
529
530 size_t size() const {
531 return libraries_.size();
532 }
533
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700535 It it = libraries_.find(path);
536 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
539 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700540 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 }
542
543 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700544 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 std::string jni_short_name(JniShortName(m));
547 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700548 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
550 SharedLibrary* library = it->second;
551 if (library->GetClassLoader() != declaring_class_loader) {
552 // We only search libraries loaded by the appropriate ClassLoader.
553 continue;
554 }
555 // Try the short name then the long name...
556 void* fn = library->FindSymbol(jni_short_name);
557 if (fn == NULL) {
558 fn = library->FindSymbol(jni_long_name);
559 }
560 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800561 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
562 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700563 return fn;
564 }
565 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700567 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700568 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 return NULL;
571 }
572
573 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700574 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700575
Elliott Hughesa0e18062012-04-13 15:59:59 -0700576 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700577};
578
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
580 jvalue* args) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700581 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700582 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700583 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800584 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700585 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800586 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700587 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
588 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800589}
590
Elliott Hughescdf53122011-08-19 15:46:09 -0700591class JNI {
592 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700593 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700594 return JNI_VERSION_1_6;
595 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700596
Ian Rogers25e8b912012-09-07 11:31:36 -0700597 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700599 return NULL;
600 }
601
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700604 Runtime* runtime = Runtime::Current();
605 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700607 Class* c = NULL;
608 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800609 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800610 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700611 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800612 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700613 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700619 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700621 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700622
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 ScopedObjectAccess soa(env);
625 Field* field = soa.Decode<Field*>(java_field);
626 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700631 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(env);
637 Field* field = soa.DecodeField(fid);
638 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughes37f7a402011-08-22 18:56:01 -0700641 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 ScopedObjectAccess soa(env);
643 Object* o = soa.Decode<Object*>(java_object);
644 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700645 }
646
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700647 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 ScopedObjectAccess soa(env);
649 Class* c = soa.Decode<Class*>(java_class);
650 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
655 Class* c1 = soa.Decode<Class*>(java_class1);
656 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughese84278b2012-03-22 10:06:53 -0700660 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800662 if (java_class == NULL) {
663 JniAbortF("IsInstanceOf", "null class (second argument)");
664 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700666 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 return JNI_TRUE;
668 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 Object* obj = soa.Decode<Object*>(jobj);
670 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700671 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccess soa(env);
677 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 if (exception == NULL) {
679 return JNI_ERR;
680 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 return JNI_OK;
683 }
684
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700685 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700686 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 }
688
689 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700690 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 }
692
693 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700694 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 }
696
697 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700699
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 Throwable* original_exception = self->GetException();
702 self->ClearException();
703
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
706 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
707 if (mid == NULL) {
708 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700709 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700710 } else {
711 env->CallVoidMethod(exception.get(), mid);
712 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700713 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 << " thrown while calling printStackTrace";
715 self->ClearException();
716 }
717 }
718
719 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 ScopedObjectAccess soa(env);
724 Object* exception = soa.Self()->GetException();
725 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
727
Ian Rogers25e8b912012-09-07 11:31:36 -0700728 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 LOG(FATAL) << "JNI FatalError called: " << msg;
730 }
731
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700732 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800733 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700734 return JNI_ERR;
735 }
Ian Rogersef28b142012-11-30 14:22:18 -0800736 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 return JNI_OK;
738 }
739
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700740 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
742 Object* survivor = soa.Decode<Object*>(java_survivor);
743 soa.Env()->PopFrame();
744 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
746
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700747 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800748 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 }
750
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 if (obj == NULL) {
753 return NULL;
754 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700755 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700756 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700759 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 return reinterpret_cast<jobject>(ref);
762 }
763
764 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 if (obj == NULL) {
766 return;
767 }
Ian Rogersef28b142012-11-30 14:22:18 -0800768 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800770 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
771 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700772
773 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
774 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
775 << "failed to find entry";
776 }
777 }
778
779 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 ScopedObjectAccess soa(env);
781 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
783
784 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 if (obj == NULL) {
786 return;
787 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700788 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700792
793 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
794 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
795 << "failed to find entry";
796 }
797 }
798
799 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 if (obj == NULL) {
801 return NULL;
802 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700803 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700805
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 uint32_t cookie = soa.Env()->local_ref_cookie;
807 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 return reinterpret_cast<jobject>(ref);
809 }
810
811 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 if (obj == NULL) {
813 return;
814 }
Ian Rogersef28b142012-11-30 14:22:18 -0800815 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700816
Ian Rogersef28b142012-11-30 14:22:18 -0800817 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 if (!locals.Remove(cookie, obj)) {
819 // Attempting to delete a local reference that is not in the
820 // topmost local reference frame is a no-op. DeleteLocalRef returns
821 // void and doesn't throw any exceptions, but we should probably
822 // complain about it so the user will notice that things aren't
823 // going quite the way they expect.
824 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
825 << "failed to find entry";
826 }
827 }
828
829 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700830 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700831 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 }
833
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700834 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(env);
836 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700837 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700838 return NULL;
839 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Elliott Hughese84278b2012-03-22 10:06:53 -0700843 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700846 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 va_end(args);
848 return result;
849 }
850
Elliott Hughes72025e52011-08-23 17:50:30 -0700851 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
853 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700854 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700855 return NULL;
856 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700857 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700858 if (result == NULL) {
859 return NULL;
860 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700862 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700864 return local_result;
865 } else {
866 return NULL;
867 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
869
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871 ScopedObjectAccess soa(env);
872 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700873 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700874 return NULL;
875 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700876 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700877 if (result == NULL) {
878 return NULL;
879 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700883 return local_result;
884 } else {
885 return NULL;
886 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 }
888
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 ScopedObjectAccess soa(env);
891 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
894 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(env);
896 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700900 va_list ap;
901 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700902 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 ScopedObjectAccess soa(env);
910 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
911 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
916 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
917 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_list ap;
922 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700923 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700926 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 ScopedObjectAccess soa(env);
931 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 }
933
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
936 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 va_list ap;
942 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700945 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 ScopedObjectAccess soa(env);
950 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(env);
955 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_list ap;
960 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700961 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700964 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
969 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 ScopedObjectAccess soa(env);
974 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_list ap;
979 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700980 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700983 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 ScopedObjectAccess soa(env);
988 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 ScopedObjectAccess soa(env);
993 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001002 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
1007 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_list ap;
1017 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001018 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001021 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001037 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001040 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
1045 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 ScopedObjectAccess soa(env);
1050 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001056 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001059 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
1064 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ScopedObjectAccess soa(env);
1069 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_list ap;
1074 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001075 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001076 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001090 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001093 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1095 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 va_end(ap);
1097 return local_result;
1098 }
1099
1100 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001101 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 ScopedObjectAccess soa(env);
1103 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1104 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
1107 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001108 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 JValue result(InvokeWithJValues(soa, obj, mid, args));
1111 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
1114 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001115 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001118 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001121 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
1124 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001125 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
1127 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
1130 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
1133 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001136 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001139 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001142 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
1145 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001146 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 ScopedObjectAccess soa(env);
1148 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
1151 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001157 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001163 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
1166 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001167 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 ScopedObjectAccess soa(env);
1169 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
1172 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001173 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
1175 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001178 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001181 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001184 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
1187 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001188 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 ScopedObjectAccess soa(env);
1190 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
1196 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001199 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001202 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001205 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001209 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
1211 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001215 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 ScopedObjectAccess soa(env);
1217 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001220 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001223 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001226 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
1229 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001230 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 ScopedObjectAccess soa(env);
1232 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001236 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001244 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001247 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
1250 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001251 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
1253 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001257 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 ScopedObjectAccess soa(env);
1259 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001262 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001265 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001268 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001272 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
1274 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
1277 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 ScopedObjectAccess soa(env);
1280 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001286 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 va_end(ap);
1289 }
1290
1291 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001292 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
1297 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001298 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
1300 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001303 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 ScopedObjectAccess soa(env);
1305 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001307
1308
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001309 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 ScopedObjectAccess soa(env);
1311 return FindFieldID(soa, c, name, sig, true);
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 GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 ScopedObjectAccess soa(env);
1316 Object* o = soa.Decode<Object*>(obj);
1317 Field* f = soa.DecodeField(fid);
1318 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001320
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001321 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
1323 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001324 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001327 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001328 ScopedObjectAccess soa(env);
1329 Object* o = soa.Decode<Object*>(java_object);
1330 Object* v = soa.Decode<Object*>(java_value);
1331 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001332 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001335 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env);
1337 Object* v = soa.Decode<Object*>(java_value);
1338 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001339 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 ScopedObjectAccess soa(env); \
1344 Object* o = soa.Decode<Object*>(instance); \
1345 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 return f->fn(o)
1347
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001348#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1349 ScopedObjectAccess soa(env); \
1350 Field* f = soa.DecodeField(fid); \
1351 return f->fn(f->GetDeclaringClass())
1352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env); \
1355 Object* o = soa.Decode<Object*>(instance); \
1356 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 f->fn(o, value)
1358
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001359#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1360 ScopedObjectAccess soa(env); \
1361 Field* f = soa.DecodeField(fid); \
1362 f->fn(f->GetDeclaringClass(), value)
1363
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1365 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1369 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1373 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1377 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1381 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1385 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1389 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1393 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001396 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001397 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001400 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001401 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001405 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001408 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001409 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001412 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001413 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001417 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 }
1419
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001421 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 }
1423
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001425 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 }
1427
1428 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1429 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1430 }
1431
1432 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1433 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1434 }
1435
1436 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1437 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1438 }
1439
1440 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1441 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1442 }
1443
1444 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1445 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1446 }
1447
1448 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1449 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1450 }
1451
1452 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1453 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1454 }
1455
1456 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1457 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1458 }
1459
1460 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001461 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 }
1463
1464 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001465 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 }
1467
1468 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001469 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 }
1471
1472 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 }
1475
1476 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001477 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 }
1479
1480 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001481 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 }
1483
1484 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001485 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 }
1487
1488 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001489 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001492 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001494 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001495 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001496 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1497 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 va_end(ap);
1499 return local_result;
1500 }
1501
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001502 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001503 ScopedObjectAccess soa(env);
1504 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1505 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
1510 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1511 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001514 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001516 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001517 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001520 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001523 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 ScopedObjectAccess soa(env);
1525 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001529 ScopedObjectAccess soa(env);
1530 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001533 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001535 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001536 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001539 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 ScopedObjectAccess soa(env);
1544 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 ScopedObjectAccess soa(env);
1549 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001555 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001558 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 ScopedObjectAccess soa(env);
1563 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 ScopedObjectAccess soa(env);
1568 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001571 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001574 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001575 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001577 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env);
1582 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001586 ScopedObjectAccess soa(env);
1587 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001590 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001593 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001594 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001596 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600 ScopedObjectAccess soa(env);
1601 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001605 ScopedObjectAccess soa(env);
1606 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001612 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001613 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001615 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001619 ScopedObjectAccess soa(env);
1620 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001624 ScopedObjectAccess soa(env);
1625 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001631 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001634 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 ScopedObjectAccess soa(env);
1639 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 ScopedObjectAccess soa(env);
1644 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001647 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001650 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001653 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
1658 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 ScopedObjectAccess soa(env);
1663 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001669 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
1676 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001680 ScopedObjectAccess soa(env);
1681 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes814e4032011-08-23 12:07:56 -07001684 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001686 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
1690 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 if (utf == NULL) {
1692 return NULL;
1693 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001695 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes814e4032011-08-23 12:07:56 -07001699 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 ScopedObjectAccess soa(env);
1701 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001702 }
1703
1704 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 ScopedObjectAccess soa(env);
1706 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001707 }
1708
Elliott Hughesb465ab02011-08-24 11:21:21 -07001709 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 ScopedObjectAccess soa(env);
1711 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001712 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001714 } else {
1715 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1716 memcpy(buf, chars + start, length * sizeof(jchar));
1717 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001718 }
1719
Elliott Hughesb465ab02011-08-24 11:21:21 -07001720 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
1722 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001723 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001725 } else {
1726 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1727 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1728 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001729 }
1730
Elliott Hughes75770752011-08-24 17:52:38 -07001731 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
1733 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001734 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001736 if (is_copy != NULL) {
1737 *is_copy = JNI_FALSE;
1738 }
1739 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 ScopedObjectAccess soa(env);
1744 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes75770752011-08-24 17:52:38 -07001747 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001748 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes75770752011-08-24 17:52:38 -07001751 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001753 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes75770752011-08-24 17:52:38 -07001756 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001757 if (java_string == NULL) {
1758 return NULL;
1759 }
1760 if (is_copy != NULL) {
1761 *is_copy = JNI_TRUE;
1762 }
Ian Rogersef28b142012-11-30 14:22:18 -08001763 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001765 size_t byte_count = s->GetUtfLength();
1766 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001767 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001768 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1769 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1770 bytes[byte_count] = '\0';
1771 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001772 }
1773
Elliott Hughes75770752011-08-24 17:52:38 -07001774 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001775 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001776 }
1777
Elliott Hughesbd935992011-08-22 11:59:34 -07001778 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
1780 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001781 if (!obj->IsArrayInstance()) {
1782 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1783 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001784 Array* array = obj->AsArray();
1785 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes814e4032011-08-23 12:07:56 -07001788 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1791 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
1794 static void SetObjectArrayElement(JNIEnv* env,
1795 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
1797 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1798 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 array->Set(index, value);
1800 }
1801
1802 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
1804 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
1807 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
1809 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
1812 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
1814 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
1819 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
1824 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
1827 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
1829 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
1832 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
1834 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
1837 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001839 if (length < 0) {
1840 JniAbortF("NewObjectArray", "negative array length: %d", length);
1841 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001842
1843 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 std::string descriptor;
1846 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001847 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848
1849 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001850 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1851 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 return NULL;
1853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001857 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001858 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 for (jsize i = 0; i < length; ++i) {
1861 result->Set(i, initial_object);
1862 }
1863 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
1867 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
1869 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Ian Rogersa15e67d2012-02-28 13:51:55 -08001872 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
1874 Array* array = soa.Decode<Array*>(java_array);
1875 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001876 if (is_copy != NULL) {
1877 *is_copy = JNI_FALSE;
1878 }
1879 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001880 }
1881
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001882 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001883 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 }
1885
Elliott Hughes75770752011-08-24 17:52:38 -07001886 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes75770752011-08-24 17:52:38 -07001891 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
1893 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes75770752011-08-24 17:52:38 -07001896 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
1898 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 }
1900
Elliott Hughes75770752011-08-24 17:52:38 -07001901 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 ScopedObjectAccess soa(env);
1903 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes75770752011-08-24 17:52:38 -07001906 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ScopedObjectAccess soa(env);
1908 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 }
1910
Elliott Hughes75770752011-08-24 17:52:38 -07001911 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
1913 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes75770752011-08-24 17:52:38 -07001916 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
1918 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
1923 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001926 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001927 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001930 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001931 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001934 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001935 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001938 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001939 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001942 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001943 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001946 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001947 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001950 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001951 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001954 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001955 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes814e4032011-08-23 12:07:56 -07001958 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001959 ScopedObjectAccess soa(env);
1960 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes814e4032011-08-23 12:07:56 -07001963 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
1965 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes814e4032011-08-23 12:07:56 -07001968 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ScopedObjectAccess soa(env);
1970 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes814e4032011-08-23 12:07:56 -07001973 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
1975 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes814e4032011-08-23 12:07:56 -07001978 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 ScopedObjectAccess soa(env);
1980 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes814e4032011-08-23 12:07:56 -07001983 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ScopedObjectAccess soa(env);
1985 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes814e4032011-08-23 12:07:56 -07001988 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
1990 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes814e4032011-08-23 12:07:56 -07001993 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
1995 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes814e4032011-08-23 12:07:56 -07001998 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
2000 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 ScopedObjectAccess soa(env);
2005 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ScopedObjectAccess soa(env);
2010 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes814e4032011-08-23 12:07:56 -07002013 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002014 ScopedObjectAccess soa(env);
2015 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes814e4032011-08-23 12:07:56 -07002018 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 ScopedObjectAccess soa(env);
2020 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002024 ScopedObjectAccess soa(env);
2025 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002029 ScopedObjectAccess soa(env);
2030 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002034 ScopedObjectAccess soa(env);
2035 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes5174fe62011-08-23 15:12:35 -07002038 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002039 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2040 }
2041
2042 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 -07002043 ScopedObjectAccess soa(env);
2044 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002045
Elliott Hughesc8fece32013-01-02 11:27:23 -08002046 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 const char* name = methods[i].name;
2048 const char* sig = methods[i].signature;
2049
2050 if (*sig == '!') {
2051 // TODO: fast jni. it's too noisy to log all these.
2052 ++sig;
2053 }
2054
Mathieu Chartier66f19252012-09-18 08:57:04 -07002055 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002056 if (m == NULL) {
2057 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002059 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002060 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2061 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002064 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002065 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2066 << PrettyDescriptor(c) << "." << name << sig
2067 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 return JNI_ERR;
2070 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002071
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002072 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002073
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076 return JNI_OK;
2077 }
2078
Elliott Hughes5174fe62011-08-23 15:12:35 -07002079 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 ScopedObjectAccess soa(env);
2081 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002082
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002083 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002084
2085 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002086 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002089 }
2090 }
2091 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002092 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002093 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002095 }
2096 }
2097
2098 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2102 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2103 ScopedObjectAccess soa(env);
2104 Object* o = soa.Decode<Object*>(java_object);
2105 o->MonitorEnter(soa.Self());
2106 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002107 return JNI_ERR;
2108 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002109 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002110 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002113 static jint MonitorExit(JNIEnv* env, jobject java_object)
2114 UNLOCK_FUNCTION(monitor_lock_) {
2115 ScopedObjectAccess soa(env);
2116 Object* o = soa.Decode<Object*>(java_object);
2117 o->MonitorExit(soa.Self());
2118 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002119 return JNI_ERR;
2120 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002121 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002122 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
2125 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 Runtime* runtime = Runtime::Current();
2127 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002128 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 } else {
2130 *vm = NULL;
2131 }
2132 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2133 }
2134
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002136 if (capacity < 0) {
2137 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2138 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002139 if (address == NULL && capacity != 0) {
2140 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002141 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002142
Elliott Hughes96a98872012-12-19 14:21:15 -08002143 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002144 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2145 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002146 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002147 jint capacity_arg = static_cast<jint>(capacity);
2148
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002149 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2150 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002151 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002152 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughesb465ab02011-08-24 11:21:21 -07002155 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002156 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughesb465ab02011-08-24 11:21:21 -07002159 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002160 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughesb465ab02011-08-24 11:21:21 -07002163 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002164 if (java_object == NULL) {
2165 JniAbortF("GetObjectRefType", "null object");
2166 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002167
2168 // Do we definitely know what kind of reference this is?
2169 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2170 IndirectRefKind kind = GetIndirectRefKind(ref);
2171 switch (kind) {
2172 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002173 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002174 return JNILocalRefType;
2175 }
2176 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002177 case kGlobal:
2178 return JNIGlobalRefType;
2179 case kWeakGlobal:
2180 return JNIWeakGlobalRefType;
2181 case kSirtOrInvalid:
2182 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002183 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002184 return JNILocalRefType;
2185 }
2186
Ian Rogersef28b142012-11-30 14:22:18 -08002187 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002188 return JNIInvalidRefType;
2189 }
2190
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 // If we're handing out direct pointers, check whether it's a direct pointer
2192 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002193 {
2194 ScopedObjectAccess soa(env);
2195 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2196 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2197 return JNILocalRefType;
2198 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002199 }
2200 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002201 return JNIInvalidRefType;
2202 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002203 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2204 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206
2207 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002208 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2209 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002210 // TODO: we should try to expand the table if necessary.
2211 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2212 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2213 return JNI_ERR;
2214 }
2215 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002216 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002217 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2218 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002219 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002220 soa.Self()->ThrowOutOfMemoryError(caller);
2221 }
2222 return okay ? JNI_OK : JNI_ERR;
2223 }
2224
2225 template<typename JniT, typename ArtT>
2226 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002228 if (length < 0) {
2229 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2230 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002231 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 return soa.AddLocalReference<JniT>(result);
2233 }
2234
2235 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2236 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2237 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002239 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2240 PinPrimitiveArray(soa, array);
2241 if (is_copy != NULL) {
2242 *is_copy = JNI_FALSE;
2243 }
2244 return array->GetData();
2245 }
2246
2247 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002248 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002250 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002251 Array* array = soa.Decode<Array*>(java_array);
2252 UnpinPrimitiveArray(soa, array);
2253 }
2254 }
2255
2256 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2257 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2258 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2261 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2262 ThrowAIOOBE(soa, array, start, length, "src");
2263 } else {
2264 JavaT* data = array->GetData();
2265 memcpy(buf, data + start, length * sizeof(JavaT));
2266 }
2267 }
2268
2269 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2270 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2271 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2274 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2275 ThrowAIOOBE(soa, array, start, length, "dst");
2276 } else {
2277 JavaT* data = array->GetData();
2278 memcpy(data + start, buf, length * sizeof(JavaT));
2279 }
2280 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002281};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002282
Elliott Hughes88c5c352012-03-15 18:49:48 -07002283const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002284 NULL, // reserved0.
2285 NULL, // reserved1.
2286 NULL, // reserved2.
2287 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 JNI::GetVersion,
2289 JNI::DefineClass,
2290 JNI::FindClass,
2291 JNI::FromReflectedMethod,
2292 JNI::FromReflectedField,
2293 JNI::ToReflectedMethod,
2294 JNI::GetSuperclass,
2295 JNI::IsAssignableFrom,
2296 JNI::ToReflectedField,
2297 JNI::Throw,
2298 JNI::ThrowNew,
2299 JNI::ExceptionOccurred,
2300 JNI::ExceptionDescribe,
2301 JNI::ExceptionClear,
2302 JNI::FatalError,
2303 JNI::PushLocalFrame,
2304 JNI::PopLocalFrame,
2305 JNI::NewGlobalRef,
2306 JNI::DeleteGlobalRef,
2307 JNI::DeleteLocalRef,
2308 JNI::IsSameObject,
2309 JNI::NewLocalRef,
2310 JNI::EnsureLocalCapacity,
2311 JNI::AllocObject,
2312 JNI::NewObject,
2313 JNI::NewObjectV,
2314 JNI::NewObjectA,
2315 JNI::GetObjectClass,
2316 JNI::IsInstanceOf,
2317 JNI::GetMethodID,
2318 JNI::CallObjectMethod,
2319 JNI::CallObjectMethodV,
2320 JNI::CallObjectMethodA,
2321 JNI::CallBooleanMethod,
2322 JNI::CallBooleanMethodV,
2323 JNI::CallBooleanMethodA,
2324 JNI::CallByteMethod,
2325 JNI::CallByteMethodV,
2326 JNI::CallByteMethodA,
2327 JNI::CallCharMethod,
2328 JNI::CallCharMethodV,
2329 JNI::CallCharMethodA,
2330 JNI::CallShortMethod,
2331 JNI::CallShortMethodV,
2332 JNI::CallShortMethodA,
2333 JNI::CallIntMethod,
2334 JNI::CallIntMethodV,
2335 JNI::CallIntMethodA,
2336 JNI::CallLongMethod,
2337 JNI::CallLongMethodV,
2338 JNI::CallLongMethodA,
2339 JNI::CallFloatMethod,
2340 JNI::CallFloatMethodV,
2341 JNI::CallFloatMethodA,
2342 JNI::CallDoubleMethod,
2343 JNI::CallDoubleMethodV,
2344 JNI::CallDoubleMethodA,
2345 JNI::CallVoidMethod,
2346 JNI::CallVoidMethodV,
2347 JNI::CallVoidMethodA,
2348 JNI::CallNonvirtualObjectMethod,
2349 JNI::CallNonvirtualObjectMethodV,
2350 JNI::CallNonvirtualObjectMethodA,
2351 JNI::CallNonvirtualBooleanMethod,
2352 JNI::CallNonvirtualBooleanMethodV,
2353 JNI::CallNonvirtualBooleanMethodA,
2354 JNI::CallNonvirtualByteMethod,
2355 JNI::CallNonvirtualByteMethodV,
2356 JNI::CallNonvirtualByteMethodA,
2357 JNI::CallNonvirtualCharMethod,
2358 JNI::CallNonvirtualCharMethodV,
2359 JNI::CallNonvirtualCharMethodA,
2360 JNI::CallNonvirtualShortMethod,
2361 JNI::CallNonvirtualShortMethodV,
2362 JNI::CallNonvirtualShortMethodA,
2363 JNI::CallNonvirtualIntMethod,
2364 JNI::CallNonvirtualIntMethodV,
2365 JNI::CallNonvirtualIntMethodA,
2366 JNI::CallNonvirtualLongMethod,
2367 JNI::CallNonvirtualLongMethodV,
2368 JNI::CallNonvirtualLongMethodA,
2369 JNI::CallNonvirtualFloatMethod,
2370 JNI::CallNonvirtualFloatMethodV,
2371 JNI::CallNonvirtualFloatMethodA,
2372 JNI::CallNonvirtualDoubleMethod,
2373 JNI::CallNonvirtualDoubleMethodV,
2374 JNI::CallNonvirtualDoubleMethodA,
2375 JNI::CallNonvirtualVoidMethod,
2376 JNI::CallNonvirtualVoidMethodV,
2377 JNI::CallNonvirtualVoidMethodA,
2378 JNI::GetFieldID,
2379 JNI::GetObjectField,
2380 JNI::GetBooleanField,
2381 JNI::GetByteField,
2382 JNI::GetCharField,
2383 JNI::GetShortField,
2384 JNI::GetIntField,
2385 JNI::GetLongField,
2386 JNI::GetFloatField,
2387 JNI::GetDoubleField,
2388 JNI::SetObjectField,
2389 JNI::SetBooleanField,
2390 JNI::SetByteField,
2391 JNI::SetCharField,
2392 JNI::SetShortField,
2393 JNI::SetIntField,
2394 JNI::SetLongField,
2395 JNI::SetFloatField,
2396 JNI::SetDoubleField,
2397 JNI::GetStaticMethodID,
2398 JNI::CallStaticObjectMethod,
2399 JNI::CallStaticObjectMethodV,
2400 JNI::CallStaticObjectMethodA,
2401 JNI::CallStaticBooleanMethod,
2402 JNI::CallStaticBooleanMethodV,
2403 JNI::CallStaticBooleanMethodA,
2404 JNI::CallStaticByteMethod,
2405 JNI::CallStaticByteMethodV,
2406 JNI::CallStaticByteMethodA,
2407 JNI::CallStaticCharMethod,
2408 JNI::CallStaticCharMethodV,
2409 JNI::CallStaticCharMethodA,
2410 JNI::CallStaticShortMethod,
2411 JNI::CallStaticShortMethodV,
2412 JNI::CallStaticShortMethodA,
2413 JNI::CallStaticIntMethod,
2414 JNI::CallStaticIntMethodV,
2415 JNI::CallStaticIntMethodA,
2416 JNI::CallStaticLongMethod,
2417 JNI::CallStaticLongMethodV,
2418 JNI::CallStaticLongMethodA,
2419 JNI::CallStaticFloatMethod,
2420 JNI::CallStaticFloatMethodV,
2421 JNI::CallStaticFloatMethodA,
2422 JNI::CallStaticDoubleMethod,
2423 JNI::CallStaticDoubleMethodV,
2424 JNI::CallStaticDoubleMethodA,
2425 JNI::CallStaticVoidMethod,
2426 JNI::CallStaticVoidMethodV,
2427 JNI::CallStaticVoidMethodA,
2428 JNI::GetStaticFieldID,
2429 JNI::GetStaticObjectField,
2430 JNI::GetStaticBooleanField,
2431 JNI::GetStaticByteField,
2432 JNI::GetStaticCharField,
2433 JNI::GetStaticShortField,
2434 JNI::GetStaticIntField,
2435 JNI::GetStaticLongField,
2436 JNI::GetStaticFloatField,
2437 JNI::GetStaticDoubleField,
2438 JNI::SetStaticObjectField,
2439 JNI::SetStaticBooleanField,
2440 JNI::SetStaticByteField,
2441 JNI::SetStaticCharField,
2442 JNI::SetStaticShortField,
2443 JNI::SetStaticIntField,
2444 JNI::SetStaticLongField,
2445 JNI::SetStaticFloatField,
2446 JNI::SetStaticDoubleField,
2447 JNI::NewString,
2448 JNI::GetStringLength,
2449 JNI::GetStringChars,
2450 JNI::ReleaseStringChars,
2451 JNI::NewStringUTF,
2452 JNI::GetStringUTFLength,
2453 JNI::GetStringUTFChars,
2454 JNI::ReleaseStringUTFChars,
2455 JNI::GetArrayLength,
2456 JNI::NewObjectArray,
2457 JNI::GetObjectArrayElement,
2458 JNI::SetObjectArrayElement,
2459 JNI::NewBooleanArray,
2460 JNI::NewByteArray,
2461 JNI::NewCharArray,
2462 JNI::NewShortArray,
2463 JNI::NewIntArray,
2464 JNI::NewLongArray,
2465 JNI::NewFloatArray,
2466 JNI::NewDoubleArray,
2467 JNI::GetBooleanArrayElements,
2468 JNI::GetByteArrayElements,
2469 JNI::GetCharArrayElements,
2470 JNI::GetShortArrayElements,
2471 JNI::GetIntArrayElements,
2472 JNI::GetLongArrayElements,
2473 JNI::GetFloatArrayElements,
2474 JNI::GetDoubleArrayElements,
2475 JNI::ReleaseBooleanArrayElements,
2476 JNI::ReleaseByteArrayElements,
2477 JNI::ReleaseCharArrayElements,
2478 JNI::ReleaseShortArrayElements,
2479 JNI::ReleaseIntArrayElements,
2480 JNI::ReleaseLongArrayElements,
2481 JNI::ReleaseFloatArrayElements,
2482 JNI::ReleaseDoubleArrayElements,
2483 JNI::GetBooleanArrayRegion,
2484 JNI::GetByteArrayRegion,
2485 JNI::GetCharArrayRegion,
2486 JNI::GetShortArrayRegion,
2487 JNI::GetIntArrayRegion,
2488 JNI::GetLongArrayRegion,
2489 JNI::GetFloatArrayRegion,
2490 JNI::GetDoubleArrayRegion,
2491 JNI::SetBooleanArrayRegion,
2492 JNI::SetByteArrayRegion,
2493 JNI::SetCharArrayRegion,
2494 JNI::SetShortArrayRegion,
2495 JNI::SetIntArrayRegion,
2496 JNI::SetLongArrayRegion,
2497 JNI::SetFloatArrayRegion,
2498 JNI::SetDoubleArrayRegion,
2499 JNI::RegisterNatives,
2500 JNI::UnregisterNatives,
2501 JNI::MonitorEnter,
2502 JNI::MonitorExit,
2503 JNI::GetJavaVM,
2504 JNI::GetStringRegion,
2505 JNI::GetStringUTFRegion,
2506 JNI::GetPrimitiveArrayCritical,
2507 JNI::ReleasePrimitiveArrayCritical,
2508 JNI::GetStringCritical,
2509 JNI::ReleaseStringCritical,
2510 JNI::NewWeakGlobalRef,
2511 JNI::DeleteWeakGlobalRef,
2512 JNI::ExceptionCheck,
2513 JNI::NewDirectByteBuffer,
2514 JNI::GetDirectBufferAddress,
2515 JNI::GetDirectBufferCapacity,
2516 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002517};
2518
Elliott Hughes75770752011-08-24 17:52:38 -07002519JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002520 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002521 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002522 local_ref_cookie(IRT_FIRST_SEGMENT),
2523 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002524 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002525 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002526 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002527 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002528 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002529 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002530 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002531 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2532 // errors will ensue.
2533 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2534 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002535}
2536
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002537JNIEnvExt::~JNIEnvExt() {
2538}
2539
Elliott Hughes88c5c352012-03-15 18:49:48 -07002540void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2541 check_jni = enabled;
2542 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002543}
2544
Elliott Hughes73e66f72012-05-09 09:34:45 -07002545void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2546 locals.Dump(os);
2547 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002548}
2549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002550void JNIEnvExt::PushFrame(int /*capacity*/) {
2551 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002552 stacked_local_ref_cookies.push_back(local_ref_cookie);
2553 local_ref_cookie = locals.GetSegmentState();
2554}
2555
2556void JNIEnvExt::PopFrame() {
2557 locals.SetSegmentState(local_ref_cookie);
2558 local_ref_cookie = stacked_local_ref_cookies.back();
2559 stacked_local_ref_cookies.pop_back();
2560}
2561
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002562Offset JNIEnvExt::SegmentStateOffset() {
2563 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2564 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2565}
2566
Carl Shapiroea4dca82011-08-01 13:45:38 -07002567// JNI Invocation interface.
2568
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2570 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002571 if (IsBadJniVersion(args->version)) {
2572 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 return JNI_EVERSION;
2574 }
2575 Runtime::Options options;
2576 for (int i = 0; i < args->nOptions; ++i) {
2577 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002578 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002579 }
2580 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002581 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002584 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002585 bool started = runtime->Start();
2586 if (!started) {
2587 delete Thread::Current()->GetJniEnv();
2588 delete runtime->GetJavaVM();
2589 LOG(WARNING) << "CreateJavaVM failed";
2590 return JNI_ERR;
2591 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002592 *p_env = Thread::Current()->GetJniEnv();
2593 *p_vm = runtime->GetJavaVM();
2594 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595}
2596
Elliott Hughesf2682d52011-08-15 16:37:04 -07002597extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598 Runtime* runtime = Runtime::Current();
2599 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002600 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002601 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002602 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002603 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604 }
2605 return JNI_OK;
2606}
2607
2608// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002609extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002610 return JNI_ERR;
2611}
2612
Elliott Hughescdf53122011-08-19 15:46:09 -07002613class JII {
2614 public:
2615 static jint DestroyJavaVM(JavaVM* vm) {
2616 if (vm == NULL) {
2617 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002618 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002619 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2620 delete raw_vm->runtime;
2621 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002622 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002623
Elliott Hughescdf53122011-08-19 15:46:09 -07002624 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002625 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002626 }
2627
2628 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002629 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002630 }
2631
2632 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002633 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002634 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002635 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002636 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2637 Runtime* runtime = raw_vm->runtime;
2638 runtime->DetachCurrentThread();
2639 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002640 }
2641
2642 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002643 if (IsBadJniVersion(version)) {
2644 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002645 return JNI_EVERSION;
2646 }
2647 if (vm == NULL || env == NULL) {
2648 return JNI_ERR;
2649 }
2650 Thread* thread = Thread::Current();
2651 if (thread == NULL) {
2652 *env = NULL;
2653 return JNI_EDETACHED;
2654 }
2655 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002656 return JNI_OK;
2657 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002658};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659
Elliott Hughes88c5c352012-03-15 18:49:48 -07002660const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002661 NULL, // reserved0
2662 NULL, // reserved1
2663 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002664 JII::DestroyJavaVM,
2665 JII::AttachCurrentThread,
2666 JII::DetachCurrentThread,
2667 JII::GetEnv,
2668 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002669};
2670
Elliott Hughesa0957642011-09-02 14:27:33 -07002671JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002672 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002673 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002674 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002675 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002676 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002677 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002678 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002679 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002680 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002681 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002682 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002683 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002684 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002685 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002686 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002687 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002688 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002689 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002690 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002691}
2692
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002693JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002694 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002695}
2696
Elliott Hughes88c5c352012-03-15 18:49:48 -07002697void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2698 check_jni = enabled;
2699 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002700}
2701
Elliott Hughesae80b492012-04-24 10:43:17 -07002702void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2703 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2704 if (force_copy) {
2705 os << " (with forcecopy)";
2706 }
2707 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002708 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002709 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002710 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002711 os << "; pins=" << pin_table.Size();
2712 }
2713 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002714 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002715 os << "; globals=" << globals.Capacity();
2716 }
2717 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002718 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002719 if (weak_globals.Capacity() > 0) {
2720 os << " (plus " << weak_globals.Capacity() << " weak)";
2721 }
2722 }
2723 os << '\n';
2724
2725 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002726 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002727 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2728 }
2729}
2730
Elliott Hughes73e66f72012-05-09 09:34:45 -07002731void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002732 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002733 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002734 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002735 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002736 }
2737 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002738 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002739 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002740 }
2741 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002742 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002743 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002744 }
2745}
2746
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002747bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2748 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002749 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002750
2751 // See if we've already loaded this library. If we have, and the class loader
2752 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002753 // TODO: for better results we should canonicalize the pathname (or even compare
2754 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002755 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002756 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002757 {
2758 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002759 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002760 library = libraries->Get(path);
2761 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002762 if (library != NULL) {
2763 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002764 // The library will be associated with class_loader. The JNI
2765 // spec says we can't load the same library into more than one
2766 // class loader.
2767 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2768 "ClassLoader %p; can't open in ClassLoader %p",
2769 path.c_str(), library->GetClassLoader(), class_loader);
2770 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 return false;
2772 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002773 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2774 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002775 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002776 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2777 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002778 return false;
2779 }
2780 return true;
2781 }
2782
2783 // Open the shared library. Because we're using a full path, the system
2784 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2785 // resolve this library's dependencies though.)
2786
2787 // Failures here are expected when java.library.path has several entries
2788 // and we have to hunt for the lib.
2789
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002790 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2791 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2792 // dlopen) becomes zero from dlclose.
2793
Elliott Hughescdf53122011-08-19 15:46:09 -07002794 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002795 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002796 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2797 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2798 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002799
Elliott Hughes84b2f142012-09-27 09:16:28 -07002800 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002801
2802 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002803 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002804 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002805 return false;
2806 }
2807
2808 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002809 // TODO: move the locking (and more of this logic) into Libraries.
2810 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002811 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002812 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002814 if (library == NULL) { // We won race to get libraries_lock
2815 library = new SharedLibrary(path, handle, class_loader);
2816 libraries->Put(path, library);
2817 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002818 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002819 }
2820 if (!created_library) {
2821 LOG(INFO) << "WOW: we lost a race to add shared library: "
2822 << "\"" << path << "\" ClassLoader=" << class_loader;
2823 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002824 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002825
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002826 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002827
2828 bool result = true;
2829 void* sym = dlsym(handle, "JNI_OnLoad");
2830 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002831 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002832 } else {
2833 // Call JNI_OnLoad. We have to override the current class
2834 // loader, which will always be "null" since the stuff at the
2835 // top of the stack is around Runtime.loadLibrary(). (See
2836 // the comments in the JNI FindClass function.)
2837 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2838 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002839 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002840 self->SetClassLoaderOverride(class_loader);
2841
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002842 int version = 0;
2843 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002844 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002845 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002846 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002847 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002848
Brian Carlstromaded5f72011-10-07 17:15:04 -07002849 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002850
Elliott Hughes83a25322013-03-14 11:18:53 -07002851 if (IsBadJniVersion(version)) {
2852 LOG(ERROR) << "Bad JNI version returned from JNI_OnLoad in \"" << path << "\": " << version;
Elliott Hughes79082e32011-08-25 12:07:32 -07002853 // It's unwise to call dlclose() here, but we can mark it
2854 // as bad and ensure that future load attempts will fail.
2855 // We don't know how far JNI_OnLoad got, so there could
2856 // be some partially-initialized stuff accessible through
2857 // newly-registered native method calls. We could try to
2858 // unregister them, but that doesn't seem worthwhile.
2859 result = false;
2860 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002861 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2862 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 }
2864 }
2865
2866 library->SetResult(result);
2867 return result;
2868}
2869
Mathieu Chartier66f19252012-09-18 08:57:04 -07002870void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002871 CHECK(m->IsNative());
2872
2873 Class* c = m->GetDeclaringClass();
2874
2875 // If this is a static method, it could be called before the class
2876 // has been initialized.
2877 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002878 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002879 return NULL;
2880 }
2881 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002882 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002883 }
2884
Brian Carlstrom16192862011-09-12 17:50:06 -07002885 std::string detail;
2886 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002887 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002888 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002889 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002890 native_method = libraries->FindNativeMethod(m, detail);
2891 }
2892 // throwing can cause libraries_lock to be reacquired
2893 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002894 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002895 }
2896 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002897}
2898
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002899void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002900 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002901 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002902 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002903 globals.VisitRoots(visitor, arg);
2904 }
2905 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002906 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002907 pin_table.VisitRoots(visitor, arg);
2908 }
2909 // The weak_globals table is visited by the GC itself (because it mutates the table).
2910}
2911
Elliott Hughesc8fece32013-01-02 11:27:23 -08002912void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2913 size_t method_count) {
2914 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2915 if (c.get() == NULL) {
2916 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2917 }
2918 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2919}
2920
Ian Rogersdf20fe02011-07-20 20:34:16 -07002921} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002922
2923std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2924 switch (rhs) {
2925 case JNIInvalidRefType:
2926 os << "JNIInvalidRefType";
2927 return os;
2928 case JNILocalRefType:
2929 os << "JNILocalRefType";
2930 return os;
2931 case JNIGlobalRefType:
2932 os << "JNIGlobalRefType";
2933 return os;
2934 case JNIWeakGlobalRefType:
2935 os << "JNIWeakGlobalRefType";
2936 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002937 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002938 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002939 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002940 }
2941}