Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 19 | #include <dlfcn.h> |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 20 | |
| 21 | #include <cstdarg> |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 22 | #include <utility> |
| 23 | #include <vector> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 25 | #include "base/logging.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 26 | #include "base/mutex.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 28 | #include "base/stringpiece.h" |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 32 | #include "interpreter/interpreter.h" |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 33 | #include "invoke_arg_array_builder.h" |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 34 | #include "jni.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
| 36 | #include "mirror/class_loader.h" |
| 37 | #include "mirror/field-inl.h" |
| 38 | #include "mirror/abstract_method-inl.h" |
| 39 | #include "mirror/object-inl.h" |
| 40 | #include "mirror/object_array-inl.h" |
| 41 | #include "mirror/throwable.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 42 | #include "object_utils.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 43 | #include "runtime.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 44 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | #include "scoped_thread_state_change.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 46 | #include "ScopedLocalRef.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 47 | #include "thread.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "utf.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 49 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 50 | #include "well_known_classes.h" |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 52 | using ::art::mirror::AbstractMethod; |
| 53 | using ::art::mirror::Array; |
| 54 | using ::art::mirror::BooleanArray; |
| 55 | using ::art::mirror::ByteArray; |
| 56 | using ::art::mirror::CharArray; |
| 57 | using ::art::mirror::Class; |
| 58 | using ::art::mirror::ClassLoader; |
| 59 | using ::art::mirror::DoubleArray; |
| 60 | using ::art::mirror::Field; |
| 61 | using ::art::mirror::FloatArray; |
| 62 | using ::art::mirror::IntArray; |
| 63 | using ::art::mirror::LongArray; |
| 64 | using ::art::mirror::Object; |
| 65 | using ::art::mirror::ObjectArray; |
| 66 | using ::art::mirror::ShortArray; |
| 67 | using ::art::mirror::String; |
| 68 | using ::art::mirror::Throwable; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 70 | namespace art { |
| 71 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 72 | static const size_t kMonitorsInitial = 32; // Arbitrary. |
| 73 | static const size_t kMonitorsMax = 4096; // Arbitrary sanity check. |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 74 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 75 | static const size_t kLocalsInitial = 64; // Arbitrary. |
| 76 | static const size_t kLocalsMax = 512; // Arbitrary sanity check. |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 78 | static const size_t kPinTableInitial = 16; // Arbitrary. |
| 79 | static const size_t kPinTableMax = 1024; // Arbitrary sanity check. |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 80 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 81 | static size_t gGlobalsInitial = 512; // Arbitrary. |
| 82 | static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.) |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 83 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 84 | static const size_t kWeakGlobalsInitial = 16; // Arbitrary. |
| 85 | static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.) |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 86 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 89 | if (obj == NULL) { |
| 90 | return NULL; |
| 91 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 92 | JavaVMExt* vm = soa.Vm(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 93 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 94 | MutexLock mu(soa.Self(), vm->weak_globals_lock); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 95 | IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj); |
| 96 | return reinterpret_cast<jweak>(ref); |
| 97 | } |
| 98 | |
Jeff Hao | 19c5d37 | 2013-03-15 14:33:43 -0700 | [diff] [blame] | 99 | static bool IsBadJniVersion(int version) { |
| 100 | // We don't support JNI_VERSION_1_1. These are the only other valid versions. |
| 101 | return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6; |
| 102 | } |
| 103 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 104 | static void CheckMethodArguments(AbstractMethod* m, uint32_t* args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 105 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 106 | MethodHelper mh(m); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 107 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 108 | if (params == NULL) { |
| 109 | return; // No arguments so nothing to check. |
| 110 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 111 | uint32_t offset = 0; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 112 | uint32_t num_params = params->Size(); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 113 | size_t error_count = 0; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 114 | if (!m->IsStatic()) { |
| 115 | offset = 1; |
| 116 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 117 | for (uint32_t i = 0; i < num_params; i++) { |
| 118 | uint16_t type_idx = params->GetTypeItem(i).type_idx_; |
| 119 | Class* param_type = mh.GetClassFromTypeIdx(type_idx); |
| 120 | if (param_type == NULL) { |
| 121 | Thread* self = Thread::Current(); |
| 122 | CHECK(self->IsExceptionPending()); |
| 123 | LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: " |
| 124 | << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 125 | << self->GetException(NULL)->Dump(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 126 | self->ClearException(); |
| 127 | ++error_count; |
| 128 | } else if (!param_type->IsPrimitive()) { |
| 129 | // TODO: check primitives are in range. |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 130 | Object* argument = reinterpret_cast<Object*>(args[i + offset]); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 131 | if (argument != NULL && !argument->InstanceOf(param_type)) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 132 | LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of " |
| 133 | << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m); |
| 134 | ++error_count; |
| 135 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 136 | } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) { |
| 137 | offset++; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | if (error_count > 0) { |
| 141 | // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort |
| 142 | // with an argument. |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 143 | JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 146 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 147 | void InvokeWithArgArray(const ScopedObjectAccess& soa, AbstractMethod* method, |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 148 | ArgArray* arg_array, JValue* result, char result_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 149 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 150 | uint32_t* args = arg_array->GetArray(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 151 | if (UNLIKELY(soa.Env()->check_jni)) { |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 152 | CheckMethodArguments(method, args); |
Elliott Hughes | 4cacde8 | 2012-04-11 18:32:27 -0700 | [diff] [blame] | 153 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 154 | method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 157 | static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj, |
| 158 | jmethodID mid, va_list args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 159 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 160 | AbstractMethod* method = soa.DecodeMethod(mid); |
Jeff Hao | 7a54946 | 2013-03-18 19:04:24 -0700 | [diff] [blame] | 161 | Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 162 | MethodHelper mh(method); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 163 | JValue result; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 164 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 165 | arg_array.BuildArgArray(soa, receiver, args); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 166 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]); |
| 167 | return result; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 170 | static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 171 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 172 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 175 | static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa, |
| 176 | jobject obj, jmethodID mid, jvalue* args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 178 | Object* receiver = soa.Decode<Object*>(obj); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 179 | AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 180 | MethodHelper mh(method); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 181 | JValue result; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 182 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 183 | arg_array.BuildArgArray(soa, receiver, args); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 184 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]); |
| 185 | return result; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 188 | static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa, |
| 189 | jobject obj, jmethodID mid, va_list args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 190 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 191 | Object* receiver = soa.Decode<Object*>(obj); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 192 | AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 193 | MethodHelper mh(method); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 194 | JValue result; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 195 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 196 | arg_array.BuildArgArray(soa, receiver, args); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 197 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]); |
| 198 | return result; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Elliott Hughes | 6b43685 | 2011-08-12 10:16:44 -0700 | [diff] [blame] | 201 | // Section 12.3.2 of the JNI spec describes JNI class descriptors. They're |
| 202 | // separated with slashes but aren't wrapped with "L;" like regular descriptors |
| 203 | // (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an |
| 204 | // exception; there the "L;" must be present ("[La/b/C;"). Historically we've |
| 205 | // supported names with dots too (such as "a.b.C"). |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 206 | static std::string NormalizeJniClassDescriptor(const char* name) { |
Elliott Hughes | 6b43685 | 2011-08-12 10:16:44 -0700 | [diff] [blame] | 207 | std::string result; |
| 208 | // Add the missing "L;" if necessary. |
| 209 | if (name[0] == '[') { |
| 210 | result = name; |
| 211 | } else { |
| 212 | result += 'L'; |
| 213 | result += name; |
| 214 | result += ';'; |
| 215 | } |
| 216 | // Rewrite '.' as '/' for backwards compatibility. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 217 | if (result.find('.') != std::string::npos) { |
| 218 | LOG(WARNING) << "Call to JNI FindClass with dots in name: " |
| 219 | << "\"" << name << "\""; |
| 220 | std::replace(result.begin(), result.end(), '.', '/'); |
Elliott Hughes | 6b43685 | 2011-08-12 10:16:44 -0700 | [diff] [blame] | 221 | } |
| 222 | return result; |
| 223 | } |
| 224 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 225 | static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c, |
| 226 | const char* name, const char* sig, const char* kind) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 227 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 228 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 229 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;", |
| 230 | "no %s method \"%s.%s%s\"", |
| 231 | kind, ClassHelper(c).GetDescriptor(), name, sig); |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 234 | static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class, |
| 235 | const char* name, const char* sig, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 236 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 237 | Class* c = soa.Decode<Class*>(jni_class); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 238 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 239 | return NULL; |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 240 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 241 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 242 | AbstractMethod* method = NULL; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 243 | if (is_static) { |
| 244 | method = c->FindDirectMethod(name, sig); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 245 | } else { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 246 | method = c->FindVirtualMethod(name, sig); |
| 247 | if (method == NULL) { |
| 248 | // No virtual method matching the signature. Search declared |
| 249 | // private methods and constructors. |
| 250 | method = c->FindDeclaredDirectMethod(name, sig); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 251 | } |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 252 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 254 | if (method == NULL || method->IsStatic() != is_static) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 255 | ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 258 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 259 | return soa.EncodeMethod(method); |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 262 | static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 263 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 264 | AbstractMethod* method = soa.Self()->GetCurrentMethod(NULL); |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 265 | if (method == NULL || |
| 266 | method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) { |
| 267 | return soa.Self()->GetClassLoaderOverride(); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 268 | } |
| 269 | return method->GetDeclaringClass()->GetClassLoader(); |
| 270 | } |
| 271 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 272 | static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name, |
| 273 | const char* sig, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 274 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 275 | Class* c = soa.Decode<Class*>(jni_class); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 276 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 277 | return NULL; |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 278 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 279 | |
| 280 | Field* field = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 281 | Class* field_type; |
| 282 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 283 | if (sig[1] != '\0') { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 284 | ClassLoader* cl = GetClassLoader(soa); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 285 | field_type = class_linker->FindClass(sig, cl); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 286 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 287 | field_type = class_linker->FindPrimitiveClass(*sig); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 288 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 289 | if (field_type == NULL) { |
| 290 | // Failed to find type from the signature of the field. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 291 | DCHECK(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 292 | ThrowLocation throw_location; |
| 293 | SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 294 | soa.Self()->ClearException(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 295 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;", |
| 296 | "no type \"%s\" found and so no field \"%s\" could be found in class " |
| 297 | "\"%s\" or its superclasses", sig, name, |
| 298 | ClassHelper(c).GetDescriptor()); |
| 299 | soa.Self()->GetException(NULL)->SetCause(cause.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 300 | return NULL; |
| 301 | } |
| 302 | if (is_static) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 303 | field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 304 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 305 | field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 306 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 307 | if (field == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 308 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 309 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;", |
| 310 | "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", |
| 311 | sig, name, ClassHelper(c).GetDescriptor()); |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 314 | return soa.EncodeField(field); |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 317 | static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 318 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 319 | JavaVMExt* vm = soa.Vm(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 320 | MutexLock mu(soa.Self(), vm->pins_lock); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 321 | vm->pin_table.Add(array); |
| 322 | } |
| 323 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 324 | static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 325 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 326 | JavaVMExt* vm = soa.Vm(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 327 | MutexLock mu(soa.Self(), vm->pins_lock); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 328 | vm->pin_table.Remove(array); |
| 329 | } |
| 330 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 331 | static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start, |
| 332 | jsize length, const char* identifier) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 333 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 334 | std::string type(PrettyTypeOf(array)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 335 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 336 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 337 | "%s offset=%d length=%d %s.length=%d", |
| 338 | type.c_str(), start, length, identifier, array->GetLength()); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 339 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 340 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 341 | static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length, |
| 342 | jsize array_length) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 343 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 344 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 345 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;", |
| 346 | "offset=%d length=%d string.length()=%d", start, length, |
| 347 | array_length); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 348 | } |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 350 | int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 351 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 352 | // Turn the const char* into a java.lang.String. |
| 353 | ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg)); |
| 354 | if (msg != NULL && s.get() == NULL) { |
| 355 | return JNI_ERR; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 356 | } |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 357 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 358 | // Choose an appropriate constructor and set up the arguments. |
| 359 | jvalue args[2]; |
| 360 | const char* signature; |
| 361 | if (msg == NULL && cause == NULL) { |
| 362 | signature = "()V"; |
| 363 | } else if (msg != NULL && cause == NULL) { |
| 364 | signature = "(Ljava/lang/String;)V"; |
| 365 | args[0].l = s.get(); |
| 366 | } else if (msg == NULL && cause != NULL) { |
| 367 | signature = "(Ljava/lang/Throwable;)V"; |
| 368 | args[0].l = cause; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 369 | } else { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 370 | signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V"; |
| 371 | args[0].l = s.get(); |
| 372 | args[1].l = cause; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 373 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 374 | jmethodID mid = env->GetMethodID(exception_class, "<init>", signature); |
| 375 | if (mid == NULL) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 376 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | LOG(ERROR) << "No <init>" << signature << " in " |
| 378 | << PrettyClass(soa.Decode<Class*>(exception_class)); |
| 379 | return JNI_ERR; |
| 380 | } |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 381 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 382 | ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args))); |
| 383 | if (exception.get() == NULL) { |
| 384 | return JNI_ERR; |
| 385 | } |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 386 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 387 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 388 | soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 389 | return JNI_OK; |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 392 | static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 393 | if (vm == NULL || p_env == NULL) { |
| 394 | return JNI_ERR; |
| 395 | } |
| 396 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 397 | // Return immediately if we're already attached. |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 398 | Thread* self = Thread::Current(); |
| 399 | if (self != NULL) { |
| 400 | *p_env = self->GetJniEnv(); |
| 401 | return JNI_OK; |
| 402 | } |
| 403 | |
| 404 | Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime; |
| 405 | |
| 406 | // No threads allowed in zygote mode. |
| 407 | if (runtime->IsZygote()) { |
| 408 | LOG(ERROR) << "Attempt to attach a thread in the zygote"; |
| 409 | return JNI_ERR; |
| 410 | } |
| 411 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 412 | JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args); |
| 413 | const char* thread_name = NULL; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 414 | jobject thread_group = NULL; |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 415 | if (args != NULL) { |
Elliott Hughes | 83a2532 | 2013-03-14 11:18:53 -0700 | [diff] [blame] | 416 | if (IsBadJniVersion(args->version)) { |
| 417 | LOG(ERROR) << "Bad JNI version passed to " |
| 418 | << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": " |
| 419 | << args->version; |
| 420 | return JNI_EVERSION; |
Brian Carlstrom | 8692215 | 2013-03-12 00:59:36 -0700 | [diff] [blame] | 421 | } |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 422 | thread_name = args->name; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 423 | thread_group = args->group; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 424 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 425 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 426 | if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 427 | *p_env = NULL; |
| 428 | return JNI_ERR; |
| 429 | } else { |
| 430 | *p_env = Thread::Current()->GetJniEnv(); |
| 431 | return JNI_OK; |
| 432 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 435 | class SharedLibrary { |
| 436 | public: |
| 437 | SharedLibrary(const std::string& path, void* handle, Object* class_loader) |
| 438 | : path_(path), |
| 439 | handle_(handle), |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 440 | class_loader_(class_loader), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 441 | jni_on_load_lock_("JNI_OnLoad lock"), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 442 | jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 443 | jni_on_load_thread_id_(Thread::Current()->GetThinLockId()), |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 444 | jni_on_load_result_(kPending) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 447 | Object* GetClassLoader() { |
| 448 | return class_loader_; |
| 449 | } |
| 450 | |
| 451 | std::string GetPath() { |
| 452 | return path_; |
| 453 | } |
| 454 | |
| 455 | /* |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 456 | * Check the result of an earlier call to JNI_OnLoad on this library. |
| 457 | * If the call has not yet finished in another thread, wait for it. |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 458 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 459 | bool CheckOnLoadResult() |
| 460 | LOCKS_EXCLUDED(jni_on_load_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 461 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 462 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 463 | self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad); |
| 464 | bool okay; |
| 465 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 466 | MutexLock mu(self, jni_on_load_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 467 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | if (jni_on_load_thread_id_ == self->GetThinLockId()) { |
| 469 | // Check this so we don't end up waiting for ourselves. We need to return "true" so the |
| 470 | // caller can continue. |
| 471 | LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\""; |
| 472 | okay = true; |
| 473 | } else { |
| 474 | while (jni_on_load_result_ == kPending) { |
| 475 | VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]"; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 476 | jni_on_load_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 477 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 478 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 479 | okay = (jni_on_load_result_ == kOkay); |
| 480 | VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" " |
| 481 | << (okay ? "succeeded" : "failed") << "]"; |
| 482 | } |
| 483 | } |
| 484 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 485 | return okay; |
| 486 | } |
| 487 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 488 | void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 489 | Thread* self = Thread::Current(); |
| 490 | MutexLock mu(self, jni_on_load_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 491 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 492 | jni_on_load_result_ = result ? kOkay : kFailed; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 493 | jni_on_load_thread_id_ = 0; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 494 | |
| 495 | // Broadcast a wakeup to anybody sleeping on the condition variable. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 496 | jni_on_load_cond_.Broadcast(self); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void* FindSymbol(const std::string& symbol_name) { |
| 500 | return dlsym(handle_, symbol_name.c_str()); |
| 501 | } |
| 502 | |
| 503 | private: |
| 504 | enum JNI_OnLoadState { |
| 505 | kPending, |
| 506 | kFailed, |
| 507 | kOkay, |
| 508 | }; |
| 509 | |
| 510 | // Path to library "/system/lib/libjni.so". |
| 511 | std::string path_; |
| 512 | |
| 513 | // The void* returned by dlopen(3). |
| 514 | void* handle_; |
| 515 | |
| 516 | // The ClassLoader this library is associated with. |
| 517 | Object* class_loader_; |
| 518 | |
| 519 | // Guards remaining items. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 520 | Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 521 | // Wait for JNI_OnLoad in other thread. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 522 | ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 523 | // Recursive invocation guard. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 524 | uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 525 | // Result of earlier JNI_OnLoad call. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 526 | JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 527 | }; |
| 528 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 529 | // This exists mainly to keep implementation details out of the header file. |
| 530 | class Libraries { |
| 531 | public: |
| 532 | Libraries() { |
| 533 | } |
| 534 | |
| 535 | ~Libraries() { |
Elliott Hughes | c31664f | 2011-09-29 15:58:28 -0700 | [diff] [blame] | 536 | STLDeleteValues(&libraries_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 539 | void Dump(std::ostream& os) const { |
| 540 | bool first = true; |
| 541 | for (It it = libraries_.begin(); it != libraries_.end(); ++it) { |
| 542 | if (!first) { |
| 543 | os << ' '; |
| 544 | } |
| 545 | first = false; |
| 546 | os << it->first; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | size_t size() const { |
| 551 | return libraries_.size(); |
| 552 | } |
| 553 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 554 | SharedLibrary* Get(const std::string& path) { |
Ian Rogers | 712462a | 2012-04-12 16:32:29 -0700 | [diff] [blame] | 555 | It it = libraries_.find(path); |
| 556 | return (it == libraries_.end()) ? NULL : it->second; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void Put(const std::string& path, SharedLibrary* library) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 560 | libraries_.Put(path, library); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // See section 11.3 "Linking Native Methods" of the JNI spec. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 564 | void* FindNativeMethod(const AbstractMethod* m, std::string& detail) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 565 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 566 | std::string jni_short_name(JniShortName(m)); |
| 567 | std::string jni_long_name(JniLongName(m)); |
Elliott Hughes | 4b093bf | 2011-08-25 13:34:29 -0700 | [diff] [blame] | 568 | const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader(); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 569 | for (It it = libraries_.begin(); it != libraries_.end(); ++it) { |
| 570 | SharedLibrary* library = it->second; |
| 571 | if (library->GetClassLoader() != declaring_class_loader) { |
| 572 | // We only search libraries loaded by the appropriate ClassLoader. |
| 573 | continue; |
| 574 | } |
| 575 | // Try the short name then the long name... |
| 576 | void* fn = library->FindSymbol(jni_short_name); |
| 577 | if (fn == NULL) { |
| 578 | fn = library->FindSymbol(jni_long_name); |
| 579 | } |
| 580 | if (fn != NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 581 | VLOG(jni) << "[Found native code for " << PrettyMethod(m) |
| 582 | << " in \"" << library->GetPath() << "\"]"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 583 | return fn; |
| 584 | } |
| 585 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 586 | detail += "No implementation found for "; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 587 | detail += PrettyMethod(m); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 588 | detail += " (tried " + jni_short_name + " and " + jni_long_name + ")"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 589 | LOG(ERROR) << detail; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | private: |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 594 | typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 595 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 596 | SafeMap<std::string, SharedLibrary*> libraries_; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 597 | }; |
| 598 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 599 | JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid, |
| 600 | jvalue* args) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 601 | AbstractMethod* method = soa.DecodeMethod(mid); |
Jeff Hao | 7a54946 | 2013-03-18 19:04:24 -0700 | [diff] [blame] | 602 | Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 603 | MethodHelper mh(method); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 604 | JValue result; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 605 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 606 | arg_array.BuildArgArray(soa, receiver, args); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 607 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]); |
| 608 | return result; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 611 | class JNI { |
| 612 | public: |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 613 | static jint GetVersion(JNIEnv*) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 614 | return JNI_VERSION_1_6; |
| 615 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 616 | |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 617 | static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 618 | LOG(WARNING) << "JNI DefineClass is not supported"; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 622 | static jclass FindClass(JNIEnv* env, const char* name) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 623 | ScopedObjectAccess soa(env); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 624 | Runtime* runtime = Runtime::Current(); |
| 625 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 626 | std::string descriptor(NormalizeJniClassDescriptor(name)); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 627 | Class* c = NULL; |
| 628 | if (runtime->IsStarted()) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 629 | ClassLoader* cl = GetClassLoader(soa); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 630 | c = class_linker->FindClass(descriptor.c_str(), cl); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 631 | } else { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 632 | c = class_linker->FindSystemClass(descriptor.c_str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 633 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 634 | return soa.AddLocalReference<jclass>(c); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 635 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 636 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 637 | static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 638 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 639 | AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 640 | return soa.EncodeMethod(method); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 641 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 642 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 643 | static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 644 | ScopedObjectAccess soa(env); |
| 645 | Field* field = soa.Decode<Field*>(java_field); |
| 646 | return soa.EncodeField(field); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 647 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 648 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 649 | static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 650 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 651 | AbstractMethod* method = soa.DecodeMethod(mid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 652 | return soa.AddLocalReference<jobject>(method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 653 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 654 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 655 | static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 656 | ScopedObjectAccess soa(env); |
| 657 | Field* field = soa.DecodeField(fid); |
| 658 | return soa.AddLocalReference<jobject>(field); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 659 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 660 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 661 | static jclass GetObjectClass(JNIEnv* env, jobject java_object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 662 | ScopedObjectAccess soa(env); |
| 663 | Object* o = soa.Decode<Object*>(java_object); |
| 664 | return soa.AddLocalReference<jclass>(o->GetClass()); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 667 | static jclass GetSuperclass(JNIEnv* env, jclass java_class) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 668 | ScopedObjectAccess soa(env); |
| 669 | Class* c = soa.Decode<Class*>(java_class); |
| 670 | return soa.AddLocalReference<jclass>(c->GetSuperClass()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 671 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 672 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 673 | static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 674 | ScopedObjectAccess soa(env); |
| 675 | Class* c1 = soa.Decode<Class*>(java_class1); |
| 676 | Class* c2 = soa.Decode<Class*>(java_class2); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 677 | return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 678 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 679 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 680 | static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 681 | ScopedObjectAccess soa(env); |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 682 | if (java_class == NULL) { |
| 683 | JniAbortF("IsInstanceOf", "null class (second argument)"); |
| 684 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 685 | if (jobj == NULL) { |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 686 | // Note: JNI is different from regular Java instanceof in this respect |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 687 | return JNI_TRUE; |
| 688 | } else { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 689 | Object* obj = soa.Decode<Object*>(jobj); |
| 690 | Class* c = soa.Decode<Class*>(java_class); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 691 | return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 692 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 693 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 694 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 695 | static jint Throw(JNIEnv* env, jthrowable java_exception) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 696 | ScopedObjectAccess soa(env); |
| 697 | Throwable* exception = soa.Decode<Throwable*>(java_exception); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 698 | if (exception == NULL) { |
| 699 | return JNI_ERR; |
| 700 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 701 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 702 | soa.Self()->SetException(throw_location, exception); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 703 | return JNI_OK; |
| 704 | } |
| 705 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 706 | static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 707 | return ThrowNewException(env, c, msg, NULL); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static jboolean ExceptionCheck(JNIEnv* env) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 711 | return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void ExceptionClear(JNIEnv* env) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 715 | static_cast<JNIEnvExt*>(env)->self->ClearException(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static void ExceptionDescribe(JNIEnv* env) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 719 | ScopedObjectAccess soa(env); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 720 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 721 | SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL); |
| 722 | SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL); |
| 723 | SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL); |
| 724 | uint32_t old_throw_dex_pc; |
| 725 | { |
| 726 | ThrowLocation old_throw_location; |
| 727 | mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location); |
| 728 | old_throw_this_object.reset(old_throw_location.GetThis()); |
| 729 | old_throw_method.reset(old_throw_location.GetMethod()); |
| 730 | old_exception.reset(old_exception_obj); |
| 731 | old_throw_dex_pc = old_throw_location.GetDexPc(); |
| 732 | soa.Self()->ClearException(); |
| 733 | } |
| 734 | ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get())); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 735 | ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get())); |
| 736 | jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V"); |
| 737 | if (mid == NULL) { |
| 738 | LOG(WARNING) << "JNI WARNING: no printStackTrace()V in " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 739 | << PrettyTypeOf(old_exception.get()); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 740 | } else { |
| 741 | env->CallVoidMethod(exception.get(), mid); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 742 | if (soa.Self()->IsExceptionPending()) { |
| 743 | LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL)) |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 744 | << " thrown while calling printStackTrace"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 745 | soa.Self()->ClearException(); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 748 | ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(), |
| 749 | old_throw_dex_pc); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 750 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 751 | soa.Self()->SetException(gc_safe_throw_location, old_exception.get()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 752 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 753 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 754 | static jthrowable ExceptionOccurred(JNIEnv* env) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 755 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 756 | Object* exception = soa.Self()->GetException(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 757 | return soa.AddLocalReference<jthrowable>(exception); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 760 | static void FatalError(JNIEnv*, const char* msg) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 761 | LOG(FATAL) << "JNI FatalError called: " << msg; |
| 762 | } |
| 763 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 764 | static jint PushLocalFrame(JNIEnv* env, jint capacity) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 765 | if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) { |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 766 | return JNI_ERR; |
| 767 | } |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 768 | static_cast<JNIEnvExt*>(env)->PushFrame(capacity); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 769 | return JNI_OK; |
| 770 | } |
| 771 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 772 | static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 773 | ScopedObjectAccess soa(env); |
| 774 | Object* survivor = soa.Decode<Object*>(java_survivor); |
| 775 | soa.Env()->PopFrame(); |
| 776 | return soa.AddLocalReference<jobject>(survivor); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 779 | static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 780 | return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity"); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 783 | static jobject NewGlobalRef(JNIEnv* env, jobject obj) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 784 | if (obj == NULL) { |
| 785 | return NULL; |
| 786 | } |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 787 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 788 | JavaVMExt* vm = soa.Vm(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 789 | IndirectReferenceTable& globals = vm->globals; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 790 | Object* decoded_obj = soa.Decode<Object*>(obj); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 791 | MutexLock mu(soa.Self(), vm->globals_lock); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 792 | IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 793 | return reinterpret_cast<jobject>(ref); |
| 794 | } |
| 795 | |
| 796 | static void DeleteGlobalRef(JNIEnv* env, jobject obj) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 797 | if (obj == NULL) { |
| 798 | return; |
| 799 | } |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 800 | JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 801 | IndirectReferenceTable& globals = vm->globals; |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 802 | Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self; |
| 803 | MutexLock mu(self, vm->globals_lock); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 804 | |
| 805 | if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) { |
| 806 | LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") " |
| 807 | << "failed to find entry"; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 812 | ScopedObjectAccess soa(env); |
| 813 | return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 817 | if (obj == NULL) { |
| 818 | return; |
| 819 | } |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 820 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 821 | JavaVMExt* vm = soa.Vm(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 822 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 823 | MutexLock mu(soa.Self(), vm->weak_globals_lock); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 824 | |
| 825 | if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) { |
| 826 | LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") " |
| 827 | << "failed to find entry"; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static jobject NewLocalRef(JNIEnv* env, jobject obj) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 832 | if (obj == NULL) { |
| 833 | return NULL; |
| 834 | } |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 835 | ScopedObjectAccess soa(env); |
Elliott Hughes | 9dcd45c | 2013-07-29 14:40:52 -0700 | [diff] [blame] | 836 | return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void DeleteLocalRef(JNIEnv* env, jobject obj) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 840 | if (obj == NULL) { |
| 841 | return; |
| 842 | } |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 843 | IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 844 | |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 845 | uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 846 | if (!locals.Remove(cookie, obj)) { |
| 847 | // Attempting to delete a local reference that is not in the |
| 848 | // topmost local reference frame is a no-op. DeleteLocalRef returns |
| 849 | // void and doesn't throw any exceptions, but we should probably |
| 850 | // complain about it so the user will notice that things aren't |
| 851 | // going quite the way they expect. |
| 852 | LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") " |
| 853 | << "failed to find entry"; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 858 | ScopedObjectAccess soa(env); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 859 | return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 862 | static jobject AllocObject(JNIEnv* env, jclass java_class) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 863 | ScopedObjectAccess soa(env); |
| 864 | Class* c = soa.Decode<Class*>(java_class); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 865 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 866 | return NULL; |
| 867 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 868 | return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self())); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 871 | static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 872 | va_list args; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 873 | va_start(args, mid); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 874 | jobject result = NewObjectV(env, c, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 875 | va_end(args); |
| 876 | return result; |
| 877 | } |
| 878 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 879 | static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 880 | ScopedObjectAccess soa(env); |
| 881 | Class* c = soa.Decode<Class*>(java_class); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 882 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 883 | return NULL; |
| 884 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 885 | Object* result = c->AllocObject(soa.Self()); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 886 | if (result == NULL) { |
| 887 | return NULL; |
| 888 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 889 | jobject local_result = soa.AddLocalReference<jobject>(result); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 890 | CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 891 | if (!soa.Self()->IsExceptionPending()) { |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 892 | return local_result; |
| 893 | } else { |
| 894 | return NULL; |
| 895 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 898 | static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 899 | ScopedObjectAccess soa(env); |
| 900 | Class* c = soa.Decode<Class*>(java_class); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 901 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 902 | return NULL; |
| 903 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 904 | Object* result = c->AllocObject(soa.Self()); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 905 | if (result == NULL) { |
| 906 | return NULL; |
| 907 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 908 | jobject local_result = soa.AddLocalReference<jobjectArray>(result); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 909 | CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 910 | if (!soa.Self()->IsExceptionPending()) { |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 911 | return local_result; |
| 912 | } else { |
| 913 | return NULL; |
| 914 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 917 | static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 918 | ScopedObjectAccess soa(env); |
| 919 | return FindMethodID(soa, c, name, sig, false); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 923 | ScopedObjectAccess soa(env); |
| 924 | return FindMethodID(soa, c, name, sig, true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 927 | static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 928 | va_list ap; |
| 929 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 930 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 931 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 932 | va_end(ap); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 933 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 936 | static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 937 | ScopedObjectAccess soa(env); |
| 938 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args)); |
| 939 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 942 | static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 943 | ScopedObjectAccess soa(env); |
| 944 | JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args)); |
| 945 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 948 | static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 949 | va_list ap; |
| 950 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 951 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 952 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 953 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 954 | return result.GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 957 | static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 958 | ScopedObjectAccess soa(env); |
| 959 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 962 | static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 963 | ScopedObjectAccess soa(env); |
| 964 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 967 | static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 968 | ScopedObjectAccess soa(env); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 969 | va_list ap; |
| 970 | va_start(ap, mid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 971 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 972 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 973 | return result.GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 976 | static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 977 | ScopedObjectAccess soa(env); |
| 978 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 981 | static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 982 | ScopedObjectAccess soa(env); |
| 983 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 986 | static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 987 | va_list ap; |
| 988 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 989 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 990 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 991 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 992 | return result.GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 995 | static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 996 | ScopedObjectAccess soa(env); |
| 997 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1000 | static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1001 | ScopedObjectAccess soa(env); |
| 1002 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1005 | static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1006 | va_list ap; |
| 1007 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1008 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1009 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1010 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1011 | return result.GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1014 | static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1015 | ScopedObjectAccess soa(env); |
| 1016 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1019 | static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1020 | ScopedObjectAccess soa(env); |
| 1021 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1024 | static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1025 | ScopedObjectAccess soa(env); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1026 | va_list ap; |
| 1027 | va_start(ap, mid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1028 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1029 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1030 | return result.GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1033 | static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1034 | ScopedObjectAccess soa(env); |
| 1035 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1038 | static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1039 | ScopedObjectAccess soa(env); |
| 1040 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1043 | static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1044 | va_list ap; |
| 1045 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1046 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1047 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1048 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1049 | return result.GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1052 | static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1053 | ScopedObjectAccess soa(env); |
| 1054 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1057 | static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1058 | ScopedObjectAccess soa(env); |
| 1059 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1062 | static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1063 | va_list ap; |
| 1064 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1065 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1066 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1067 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1068 | return result.GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1071 | static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1072 | ScopedObjectAccess soa(env); |
| 1073 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1076 | static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1077 | ScopedObjectAccess soa(env); |
| 1078 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1081 | static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1082 | va_list ap; |
| 1083 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1084 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1085 | JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1086 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1087 | return result.GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1090 | static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1091 | ScopedObjectAccess soa(env); |
| 1092 | return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1095 | static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1096 | ScopedObjectAccess soa(env); |
| 1097 | return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1100 | static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1101 | va_list ap; |
| 1102 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1103 | ScopedObjectAccess soa(env); |
Ian Rogers | 1b09b09 | 2012-08-20 15:35:52 -0700 | [diff] [blame] | 1104 | InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1105 | va_end(ap); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1108 | static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1109 | ScopedObjectAccess soa(env); |
| 1110 | InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1113 | static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1114 | ScopedObjectAccess soa(env); |
| 1115 | InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1118 | static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1119 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1120 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1121 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1122 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
| 1123 | jobject local_result = soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1124 | va_end(ap); |
| 1125 | return local_result; |
| 1126 | } |
| 1127 | |
| 1128 | static jobject CallNonvirtualObjectMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1129 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1130 | ScopedObjectAccess soa(env); |
| 1131 | JValue result(InvokeWithVarArgs(soa, obj, mid, args)); |
| 1132 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static jobject CallNonvirtualObjectMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1136 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1137 | ScopedObjectAccess soa(env); |
| 1138 | JValue result(InvokeWithJValues(soa, obj, mid, args)); |
| 1139 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1143 | jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1144 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1145 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1146 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1147 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1148 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1149 | return result.GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1153 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1154 | ScopedObjectAccess soa(env); |
| 1155 | return InvokeWithVarArgs(soa, obj, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1159 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1160 | ScopedObjectAccess soa(env); |
| 1161 | return InvokeWithJValues(soa, obj, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1164 | static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1165 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1166 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1167 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1168 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1169 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1170 | return result.GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static jbyte CallNonvirtualByteMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1174 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1175 | ScopedObjectAccess soa(env); |
| 1176 | return InvokeWithVarArgs(soa, obj, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static jbyte CallNonvirtualByteMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1180 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1181 | ScopedObjectAccess soa(env); |
| 1182 | return InvokeWithJValues(soa, obj, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1185 | static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1186 | ScopedObjectAccess soa(env); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1187 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1188 | va_start(ap, mid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1189 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1190 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1191 | return result.GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static jchar CallNonvirtualCharMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1195 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1196 | ScopedObjectAccess soa(env); |
| 1197 | return InvokeWithVarArgs(soa, obj, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static jchar CallNonvirtualCharMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1201 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1202 | ScopedObjectAccess soa(env); |
| 1203 | return InvokeWithJValues(soa, obj, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1206 | static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1207 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1208 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1209 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1210 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1211 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1212 | return result.GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static jshort CallNonvirtualShortMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1216 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1217 | ScopedObjectAccess soa(env); |
| 1218 | return InvokeWithVarArgs(soa, obj, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static jshort CallNonvirtualShortMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1222 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1223 | ScopedObjectAccess soa(env); |
| 1224 | return InvokeWithJValues(soa, obj, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1227 | static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1228 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1229 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1230 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1231 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1232 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1233 | return result.GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | static jint CallNonvirtualIntMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1237 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1238 | ScopedObjectAccess soa(env); |
| 1239 | return InvokeWithVarArgs(soa, obj, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | static jint CallNonvirtualIntMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1243 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1244 | ScopedObjectAccess soa(env); |
| 1245 | return InvokeWithJValues(soa, obj, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1248 | static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1249 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1250 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1251 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1252 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1253 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1254 | return result.GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | static jlong CallNonvirtualLongMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1258 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1259 | ScopedObjectAccess soa(env); |
| 1260 | return InvokeWithVarArgs(soa, obj, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static jlong CallNonvirtualLongMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1264 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1265 | ScopedObjectAccess soa(env); |
| 1266 | return InvokeWithJValues(soa, obj, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1269 | static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1270 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1271 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1272 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1273 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1274 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1275 | return result.GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1279 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1280 | ScopedObjectAccess soa(env); |
| 1281 | return InvokeWithVarArgs(soa, obj, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1285 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1286 | ScopedObjectAccess soa(env); |
| 1287 | return InvokeWithJValues(soa, obj, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1290 | static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1291 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1292 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1293 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1294 | JValue result(InvokeWithVarArgs(soa, obj, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1295 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1296 | return result.GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1300 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1301 | ScopedObjectAccess soa(env); |
| 1302 | return InvokeWithVarArgs(soa, obj, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1306 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1307 | ScopedObjectAccess soa(env); |
| 1308 | return InvokeWithJValues(soa, obj, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1311 | static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1312 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1313 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1314 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1315 | InvokeWithVarArgs(soa, obj, mid, ap); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1316 | va_end(ap); |
| 1317 | } |
| 1318 | |
| 1319 | static void CallNonvirtualVoidMethodV(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1320 | jobject obj, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1321 | ScopedObjectAccess soa(env); |
| 1322 | InvokeWithVarArgs(soa, obj, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static void CallNonvirtualVoidMethodA(JNIEnv* env, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1326 | jobject obj, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1327 | ScopedObjectAccess soa(env); |
| 1328 | InvokeWithJValues(soa, obj, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 1331 | static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1332 | ScopedObjectAccess soa(env); |
| 1333 | return FindFieldID(soa, c, name, sig, false); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1334 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 1335 | |
| 1336 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 1337 | static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1338 | ScopedObjectAccess soa(env); |
| 1339 | return FindFieldID(soa, c, name, sig, true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1340 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 1341 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1342 | static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1343 | ScopedObjectAccess soa(env); |
| 1344 | Object* o = soa.Decode<Object*>(obj); |
| 1345 | Field* f = soa.DecodeField(fid); |
| 1346 | return soa.AddLocalReference<jobject>(f->GetObject(o)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1347 | } |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 1348 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1349 | static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1350 | ScopedObjectAccess soa(env); |
| 1351 | Field* f = soa.DecodeField(fid); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1352 | return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass())); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1355 | static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1356 | ScopedObjectAccess soa(env); |
| 1357 | Object* o = soa.Decode<Object*>(java_object); |
| 1358 | Object* v = soa.Decode<Object*>(java_value); |
| 1359 | Field* f = soa.DecodeField(fid); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1360 | f->SetObject(o, v); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1363 | static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1364 | ScopedObjectAccess soa(env); |
| 1365 | Object* v = soa.Decode<Object*>(java_value); |
| 1366 | Field* f = soa.DecodeField(fid); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1367 | f->SetObject(f->GetDeclaringClass(), v); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1370 | #define GET_PRIMITIVE_FIELD(fn, instance) \ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1371 | ScopedObjectAccess soa(env); \ |
| 1372 | Object* o = soa.Decode<Object*>(instance); \ |
| 1373 | Field* f = soa.DecodeField(fid); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1374 | return f->fn(o) |
| 1375 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1376 | #define GET_STATIC_PRIMITIVE_FIELD(fn) \ |
| 1377 | ScopedObjectAccess soa(env); \ |
| 1378 | Field* f = soa.DecodeField(fid); \ |
| 1379 | return f->fn(f->GetDeclaringClass()) |
| 1380 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1381 | #define SET_PRIMITIVE_FIELD(fn, instance, value) \ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1382 | ScopedObjectAccess soa(env); \ |
| 1383 | Object* o = soa.Decode<Object*>(instance); \ |
| 1384 | Field* f = soa.DecodeField(fid); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1385 | f->fn(o, value) |
| 1386 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1387 | #define SET_STATIC_PRIMITIVE_FIELD(fn, value) \ |
| 1388 | ScopedObjectAccess soa(env); \ |
| 1389 | Field* f = soa.DecodeField(fid); \ |
| 1390 | f->fn(f->GetDeclaringClass(), value) |
| 1391 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1392 | static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1393 | GET_PRIMITIVE_FIELD(GetBoolean, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1396 | static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1397 | GET_PRIMITIVE_FIELD(GetByte, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1400 | static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1401 | GET_PRIMITIVE_FIELD(GetChar, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1404 | static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1405 | GET_PRIMITIVE_FIELD(GetShort, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1408 | static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1409 | GET_PRIMITIVE_FIELD(GetInt, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1412 | static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1413 | GET_PRIMITIVE_FIELD(GetLong, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1416 | static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1417 | GET_PRIMITIVE_FIELD(GetFloat, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1420 | static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) { |
| 1421 | GET_PRIMITIVE_FIELD(GetDouble, obj); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1424 | static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1425 | GET_STATIC_PRIMITIVE_FIELD(GetBoolean); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1428 | static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1429 | GET_STATIC_PRIMITIVE_FIELD(GetByte); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1432 | static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1433 | GET_STATIC_PRIMITIVE_FIELD(GetChar); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1436 | static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1437 | GET_STATIC_PRIMITIVE_FIELD(GetShort); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1440 | static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1441 | GET_STATIC_PRIMITIVE_FIELD(GetInt); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1444 | static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1445 | GET_STATIC_PRIMITIVE_FIELD(GetLong); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1448 | static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1449 | GET_STATIC_PRIMITIVE_FIELD(GetFloat); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1452 | static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1453 | GET_STATIC_PRIMITIVE_FIELD(GetDouble); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) { |
| 1457 | SET_PRIMITIVE_FIELD(SetBoolean, obj, v); |
| 1458 | } |
| 1459 | |
| 1460 | static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) { |
| 1461 | SET_PRIMITIVE_FIELD(SetByte, obj, v); |
| 1462 | } |
| 1463 | |
| 1464 | static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) { |
| 1465 | SET_PRIMITIVE_FIELD(SetChar, obj, v); |
| 1466 | } |
| 1467 | |
| 1468 | static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) { |
| 1469 | SET_PRIMITIVE_FIELD(SetFloat, obj, v); |
| 1470 | } |
| 1471 | |
| 1472 | static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) { |
| 1473 | SET_PRIMITIVE_FIELD(SetDouble, obj, v); |
| 1474 | } |
| 1475 | |
| 1476 | static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) { |
| 1477 | SET_PRIMITIVE_FIELD(SetInt, obj, v); |
| 1478 | } |
| 1479 | |
| 1480 | static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) { |
| 1481 | SET_PRIMITIVE_FIELD(SetLong, obj, v); |
| 1482 | } |
| 1483 | |
| 1484 | static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) { |
| 1485 | SET_PRIMITIVE_FIELD(SetShort, obj, v); |
| 1486 | } |
| 1487 | |
| 1488 | static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1489 | SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1493 | SET_STATIC_PRIMITIVE_FIELD(SetByte, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1497 | SET_STATIC_PRIMITIVE_FIELD(SetChar, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1501 | SET_STATIC_PRIMITIVE_FIELD(SetFloat, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1505 | SET_STATIC_PRIMITIVE_FIELD(SetDouble, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1509 | SET_STATIC_PRIMITIVE_FIELD(SetInt, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1513 | SET_STATIC_PRIMITIVE_FIELD(SetLong, v); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1517 | SET_STATIC_PRIMITIVE_FIELD(SetShort, v); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1520 | static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1521 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1522 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1523 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1524 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
| 1525 | jobject local_result = soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1526 | va_end(ap); |
| 1527 | return local_result; |
| 1528 | } |
| 1529 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1530 | static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1531 | ScopedObjectAccess soa(env); |
| 1532 | JValue result(InvokeWithVarArgs(soa, NULL, mid, args)); |
| 1533 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1536 | static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1537 | ScopedObjectAccess soa(env); |
| 1538 | JValue result(InvokeWithJValues(soa, NULL, mid, args)); |
| 1539 | return soa.AddLocalReference<jobject>(result.GetL()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1542 | static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1543 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1544 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1545 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1546 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1547 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1548 | return result.GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1551 | static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1552 | ScopedObjectAccess soa(env); |
| 1553 | return InvokeWithVarArgs(soa, NULL, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1556 | static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1557 | ScopedObjectAccess soa(env); |
| 1558 | return InvokeWithJValues(soa, NULL, mid, args).GetZ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1561 | static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1562 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1563 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1564 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1565 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1566 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1567 | return result.GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1570 | static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1571 | ScopedObjectAccess soa(env); |
| 1572 | return InvokeWithVarArgs(soa, NULL, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1575 | static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1576 | ScopedObjectAccess soa(env); |
| 1577 | return InvokeWithJValues(soa, NULL, mid, args).GetB(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1580 | static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1581 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1582 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1583 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1584 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1585 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1586 | return result.GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1589 | static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1590 | ScopedObjectAccess soa(env); |
| 1591 | return InvokeWithVarArgs(soa, NULL, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1594 | static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1595 | ScopedObjectAccess soa(env); |
| 1596 | return InvokeWithJValues(soa, NULL, mid, args).GetC(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1599 | static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1600 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1601 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1602 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1603 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1604 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1605 | return result.GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1608 | static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1609 | ScopedObjectAccess soa(env); |
| 1610 | return InvokeWithVarArgs(soa, NULL, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1613 | static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1614 | ScopedObjectAccess soa(env); |
| 1615 | return InvokeWithJValues(soa, NULL, mid, args).GetS(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1618 | static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1619 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1620 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1621 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1622 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1623 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1624 | return result.GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1627 | static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1628 | ScopedObjectAccess soa(env); |
| 1629 | return InvokeWithVarArgs(soa, NULL, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1632 | static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1633 | ScopedObjectAccess soa(env); |
| 1634 | return InvokeWithJValues(soa, NULL, mid, args).GetI(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1637 | static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1638 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1639 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1640 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1641 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1642 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1643 | return result.GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1646 | static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1647 | ScopedObjectAccess soa(env); |
| 1648 | return InvokeWithVarArgs(soa, NULL, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1651 | static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1652 | ScopedObjectAccess soa(env); |
| 1653 | return InvokeWithJValues(soa, NULL, mid, args).GetJ(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1656 | static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1657 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1658 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1659 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1660 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1661 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1662 | return result.GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1665 | static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1666 | ScopedObjectAccess soa(env); |
| 1667 | return InvokeWithVarArgs(soa, NULL, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1670 | static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1671 | ScopedObjectAccess soa(env); |
| 1672 | return InvokeWithJValues(soa, NULL, mid, args).GetF(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1675 | static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1676 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1677 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1678 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1679 | JValue result(InvokeWithVarArgs(soa, NULL, mid, ap)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1680 | va_end(ap); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1681 | return result.GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1684 | static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1685 | ScopedObjectAccess soa(env); |
| 1686 | return InvokeWithVarArgs(soa, NULL, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1689 | static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1690 | ScopedObjectAccess soa(env); |
| 1691 | return InvokeWithJValues(soa, NULL, mid, args).GetD(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1694 | static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1695 | va_list ap; |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1696 | va_start(ap, mid); |
Ian Rogers | 25e8b91 | 2012-09-07 11:31:36 -0700 | [diff] [blame] | 1697 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1698 | InvokeWithVarArgs(soa, NULL, mid, ap); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1699 | va_end(ap); |
| 1700 | } |
| 1701 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1702 | static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1703 | ScopedObjectAccess soa(env); |
| 1704 | InvokeWithVarArgs(soa, NULL, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1707 | static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1708 | ScopedObjectAccess soa(env); |
| 1709 | InvokeWithJValues(soa, NULL, mid, args); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1712 | static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1713 | ScopedObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1714 | String* result = String::AllocFromUtf16(soa.Self(), char_count, chars); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1715 | return soa.AddLocalReference<jstring>(result); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static jstring NewStringUTF(JNIEnv* env, const char* utf) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1719 | if (utf == NULL) { |
| 1720 | return NULL; |
| 1721 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1722 | ScopedObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1723 | String* result = String::AllocFromModifiedUtf8(soa.Self(), utf); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1724 | return soa.AddLocalReference<jstring>(result); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1727 | static jsize GetStringLength(JNIEnv* env, jstring java_string) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1728 | ScopedObjectAccess soa(env); |
| 1729 | return soa.Decode<String*>(java_string)->GetLength(); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1733 | ScopedObjectAccess soa(env); |
| 1734 | return soa.Decode<String*>(java_string)->GetUtfLength(); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1737 | static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1738 | ScopedObjectAccess soa(env); |
| 1739 | String* s = soa.Decode<String*>(java_string); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1740 | if (start < 0 || length < 0 || start + length > s->GetLength()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1741 | ThrowSIOOBE(soa, start, length, s->GetLength()); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1742 | } else { |
| 1743 | const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset(); |
| 1744 | memcpy(buf, chars + start, length * sizeof(jchar)); |
| 1745 | } |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1748 | static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1749 | ScopedObjectAccess soa(env); |
| 1750 | String* s = soa.Decode<String*>(java_string); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1751 | if (start < 0 || length < 0 || start + length > s->GetLength()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1752 | ThrowSIOOBE(soa, start, length, s->GetLength()); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1753 | } else { |
| 1754 | const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset(); |
| 1755 | ConvertUtf16ToModifiedUtf8(buf, chars + start, length); |
| 1756 | } |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1759 | static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1760 | ScopedObjectAccess soa(env); |
| 1761 | String* s = soa.Decode<String*>(java_string); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1762 | const CharArray* chars = s->GetCharArray(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1763 | PinPrimitiveArray(soa, chars); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1764 | if (is_copy != NULL) { |
| 1765 | *is_copy = JNI_FALSE; |
| 1766 | } |
| 1767 | return chars->GetData() + s->GetOffset(); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1770 | static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1771 | ScopedObjectAccess soa(env); |
| 1772 | UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1775 | static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1776 | return GetStringChars(env, java_string, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1779 | static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1780 | ScopedObjectAccess soa(env); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1781 | return ReleaseStringChars(env, java_string, chars); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1784 | static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1785 | if (java_string == NULL) { |
| 1786 | return NULL; |
| 1787 | } |
| 1788 | if (is_copy != NULL) { |
| 1789 | *is_copy = JNI_TRUE; |
| 1790 | } |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1791 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1792 | String* s = soa.Decode<String*>(java_string); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1793 | size_t byte_count = s->GetUtfLength(); |
| 1794 | char* bytes = new char[byte_count + 1]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1795 | CHECK(bytes != NULL); // bionic aborts anyway. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1796 | const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset(); |
| 1797 | ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength()); |
| 1798 | bytes[byte_count] = '\0'; |
| 1799 | return bytes; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1802 | static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1803 | delete[] chars; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1806 | static jsize GetArrayLength(JNIEnv* env, jarray java_array) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1807 | ScopedObjectAccess soa(env); |
| 1808 | Object* obj = soa.Decode<Object*>(java_array); |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 1809 | if (!obj->IsArrayInstance()) { |
| 1810 | JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str()); |
| 1811 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1812 | Array* array = obj->AsArray(); |
| 1813 | return array->GetLength(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1816 | static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1817 | ScopedObjectAccess soa(env); |
| 1818 | ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array); |
| 1819 | return soa.AddLocalReference<jobject>(array->Get(index)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static void SetObjectArrayElement(JNIEnv* env, |
| 1823 | jobjectArray java_array, jsize index, jobject java_value) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1824 | ScopedObjectAccess soa(env); |
| 1825 | ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array); |
| 1826 | Object* value = soa.Decode<Object*>(java_value); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1827 | array->Set(index, value); |
| 1828 | } |
| 1829 | |
| 1830 | static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1831 | ScopedObjectAccess soa(env); |
| 1832 | return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | static jbyteArray NewByteArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1836 | ScopedObjectAccess soa(env); |
| 1837 | return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | static jcharArray NewCharArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1841 | ScopedObjectAccess soa(env); |
| 1842 | return NewPrimitiveArray<jcharArray, CharArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1846 | ScopedObjectAccess soa(env); |
| 1847 | return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | static jfloatArray NewFloatArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1851 | ScopedObjectAccess soa(env); |
| 1852 | return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | static jintArray NewIntArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1856 | ScopedObjectAccess soa(env); |
| 1857 | return NewPrimitiveArray<jintArray, IntArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static jlongArray NewLongArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1861 | ScopedObjectAccess soa(env); |
| 1862 | return NewPrimitiveArray<jlongArray, LongArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1866 | ScopedObjectAccess soa(env); |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 1867 | if (length < 0) { |
| 1868 | JniAbortF("NewObjectArray", "negative array length: %d", length); |
| 1869 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1870 | |
| 1871 | // Compute the array class corresponding to the given element class. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1872 | Class* element_class = soa.Decode<Class*>(element_jclass); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1873 | std::string descriptor; |
| 1874 | descriptor += "["; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1875 | descriptor += ClassHelper(element_class).GetDescriptor(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1876 | |
| 1877 | // Find the class. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1878 | ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str())); |
| 1879 | if (java_array_class.get() == NULL) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1880 | return NULL; |
| 1881 | } |
| 1882 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1883 | // Allocate and initialize if necessary. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1884 | Class* array_class = soa.Decode<Class*>(java_array_class.get()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1885 | ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1886 | if (initial_element != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1887 | Object* initial_object = soa.Decode<Object*>(initial_element); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1888 | for (jsize i = 0; i < length; ++i) { |
| 1889 | result->Set(i, initial_object); |
| 1890 | } |
| 1891 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1892 | return soa.AddLocalReference<jobjectArray>(result); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | static jshortArray NewShortArray(JNIEnv* env, jsize length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1896 | ScopedObjectAccess soa(env); |
| 1897 | return NewPrimitiveArray<jshortArray, ShortArray>(soa, length); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1900 | static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1901 | ScopedObjectAccess soa(env); |
| 1902 | Array* array = soa.Decode<Array*>(java_array); |
| 1903 | PinPrimitiveArray(soa, array); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1904 | if (is_copy != NULL) { |
| 1905 | *is_copy = JNI_FALSE; |
| 1906 | } |
| 1907 | return array->GetRawData(array->GetClass()->GetComponentSize()); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1910 | static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1911 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1914 | static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1915 | ScopedObjectAccess soa(env); |
| 1916 | return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1919 | static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1920 | ScopedObjectAccess soa(env); |
| 1921 | return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1924 | static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1925 | ScopedObjectAccess soa(env); |
| 1926 | return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1929 | static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1930 | ScopedObjectAccess soa(env); |
| 1931 | return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1934 | static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1935 | ScopedObjectAccess soa(env); |
| 1936 | return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1939 | static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1940 | ScopedObjectAccess soa(env); |
| 1941 | return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1944 | static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1945 | ScopedObjectAccess soa(env); |
| 1946 | return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1949 | static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1950 | ScopedObjectAccess soa(env); |
| 1951 | return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1954 | static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1955 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1958 | static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1959 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1962 | static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1963 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1966 | static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1967 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1970 | static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1971 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1974 | static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1975 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1978 | static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1979 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1982 | static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 1983 | ReleasePrimitiveArray(env, array, mode); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1986 | static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1987 | ScopedObjectAccess soa(env); |
| 1988 | GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1991 | static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1992 | ScopedObjectAccess soa(env); |
| 1993 | GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1996 | static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1997 | ScopedObjectAccess soa(env); |
| 1998 | GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2001 | static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2002 | ScopedObjectAccess soa(env); |
| 2003 | GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2006 | static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2007 | ScopedObjectAccess soa(env); |
| 2008 | GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2011 | static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2012 | ScopedObjectAccess soa(env); |
| 2013 | GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2016 | static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2017 | ScopedObjectAccess soa(env); |
| 2018 | GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2021 | static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2022 | ScopedObjectAccess soa(env); |
| 2023 | GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2026 | static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2027 | ScopedObjectAccess soa(env); |
| 2028 | SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2031 | static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2032 | ScopedObjectAccess soa(env); |
| 2033 | SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2036 | static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2037 | ScopedObjectAccess soa(env); |
| 2038 | SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2041 | static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2042 | ScopedObjectAccess soa(env); |
| 2043 | SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2046 | static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2047 | ScopedObjectAccess soa(env); |
| 2048 | SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2051 | static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2052 | ScopedObjectAccess soa(env); |
| 2053 | SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2056 | static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2057 | ScopedObjectAccess soa(env); |
| 2058 | SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2061 | static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2062 | ScopedObjectAccess soa(env); |
| 2063 | SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2066 | static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) { |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 2067 | return RegisterNativeMethods(env, java_class, methods, method_count, true); |
| 2068 | } |
| 2069 | |
| 2070 | static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, size_t method_count, bool return_errors) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2071 | ScopedObjectAccess soa(env); |
| 2072 | Class* c = soa.Decode<Class*>(java_class); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2073 | |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 2074 | for (size_t i = 0; i < method_count; ++i) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2075 | const char* name = methods[i].name; |
| 2076 | const char* sig = methods[i].signature; |
| 2077 | |
| 2078 | if (*sig == '!') { |
| 2079 | // TODO: fast jni. it's too noisy to log all these. |
| 2080 | ++sig; |
| 2081 | } |
| 2082 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2083 | AbstractMethod* m = c->FindDirectMethod(name, sig); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2084 | if (m == NULL) { |
| 2085 | m = c->FindVirtualMethod(name, sig); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2086 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2087 | if (m == NULL) { |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 2088 | LOG(return_errors ? ERROR : FATAL) << "Failed to register native method " |
| 2089 | << PrettyDescriptor(c) << "." << name << sig; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2090 | ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2091 | return JNI_ERR; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2092 | } else if (!m->IsNative()) { |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 2093 | LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method " |
| 2094 | << PrettyDescriptor(c) << "." << name << sig |
| 2095 | << " as native"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2096 | ThrowNoSuchMethodError(soa, c, name, sig, "native"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2097 | return JNI_ERR; |
| 2098 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2099 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2100 | VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]"; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2101 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2102 | m->RegisterNative(soa.Self(), methods[i].fnPtr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2103 | } |
| 2104 | return JNI_OK; |
| 2105 | } |
| 2106 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2107 | static jint UnregisterNatives(JNIEnv* env, jclass java_class) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2108 | ScopedObjectAccess soa(env); |
| 2109 | Class* c = soa.Decode<Class*>(java_class); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2110 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2111 | VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]"; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2112 | |
| 2113 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2114 | AbstractMethod* m = c->GetDirectMethod(i); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2115 | if (m->IsNative()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2116 | m->UnregisterNative(soa.Self()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2120 | AbstractMethod* m = c->GetVirtualMethod(i); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2121 | if (m->IsNative()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2122 | m->UnregisterNative(soa.Self()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | return JNI_OK; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2129 | static jint MonitorEnter(JNIEnv* env, jobject java_object) |
| 2130 | EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) { |
| 2131 | ScopedObjectAccess soa(env); |
| 2132 | Object* o = soa.Decode<Object*>(java_object); |
| 2133 | o->MonitorEnter(soa.Self()); |
| 2134 | if (soa.Self()->IsExceptionPending()) { |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 2135 | return JNI_ERR; |
| 2136 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2137 | soa.Env()->monitors.Add(o); |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 2138 | return JNI_OK; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2141 | static jint MonitorExit(JNIEnv* env, jobject java_object) |
| 2142 | UNLOCK_FUNCTION(monitor_lock_) { |
| 2143 | ScopedObjectAccess soa(env); |
| 2144 | Object* o = soa.Decode<Object*>(java_object); |
| 2145 | o->MonitorExit(soa.Self()); |
| 2146 | if (soa.Self()->IsExceptionPending()) { |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 2147 | return JNI_ERR; |
| 2148 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2149 | soa.Env()->monitors.Remove(o); |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 2150 | return JNI_OK; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | static jint GetJavaVM(JNIEnv* env, JavaVM** vm) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2154 | Runtime* runtime = Runtime::Current(); |
| 2155 | if (runtime != NULL) { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 2156 | *vm = runtime->GetJavaVM(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2157 | } else { |
| 2158 | *vm = NULL; |
| 2159 | } |
| 2160 | return (*vm != NULL) ? JNI_OK : JNI_ERR; |
| 2161 | } |
| 2162 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2163 | static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) { |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2164 | if (capacity < 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2165 | JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity); |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2166 | } |
Elliott Hughes | 11a796e | 2012-12-19 14:42:57 -0800 | [diff] [blame] | 2167 | if (address == NULL && capacity != 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2168 | JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity); |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2169 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2170 | |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2171 | // At the moment, the Java side is limited to 32 bits. |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2172 | CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff); |
| 2173 | CHECK_LE(capacity, 0xffffffff); |
Elliott Hughes | b568121 | 2013-03-29 17:29:22 -0700 | [diff] [blame] | 2174 | jlong address_arg = reinterpret_cast<jlong>(address); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2175 | jint capacity_arg = static_cast<jint>(capacity); |
| 2176 | |
Elliott Hughes | aecb5f3 | 2013-03-28 08:27:38 -0700 | [diff] [blame] | 2177 | jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer, |
| 2178 | WellKnownClasses::java_nio_DirectByteBuffer_init, |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2179 | address_arg, capacity_arg); |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2180 | return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2183 | static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) { |
Jeff Hao | 534f2b6 | 2013-07-10 15:29:36 -0700 | [diff] [blame] | 2184 | return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2187 | static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) { |
Elliott Hughes | aecb5f3 | 2013-03-28 08:27:38 -0700 | [diff] [blame] | 2188 | return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2191 | static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) { |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2192 | if (java_object == NULL) { |
| 2193 | JniAbortF("GetObjectRefType", "null object"); |
| 2194 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2195 | |
| 2196 | // Do we definitely know what kind of reference this is? |
| 2197 | IndirectRef ref = reinterpret_cast<IndirectRef>(java_object); |
| 2198 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 2199 | switch (kind) { |
| 2200 | case kLocal: |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2201 | if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) { |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 2202 | return JNILocalRefType; |
| 2203 | } |
| 2204 | return JNIInvalidRefType; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2205 | case kGlobal: |
| 2206 | return JNIGlobalRefType; |
| 2207 | case kWeakGlobal: |
| 2208 | return JNIWeakGlobalRefType; |
| 2209 | case kSirtOrInvalid: |
| 2210 | // Is it in a stack IRT? |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2211 | if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) { |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2212 | return JNILocalRefType; |
| 2213 | } |
| 2214 | |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2215 | if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) { |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 2216 | return JNIInvalidRefType; |
| 2217 | } |
| 2218 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2219 | // If we're handing out direct pointers, check whether it's a direct pointer |
| 2220 | // to a local reference. |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2221 | { |
| 2222 | ScopedObjectAccess soa(env); |
| 2223 | if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) { |
| 2224 | if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) { |
| 2225 | return JNILocalRefType; |
| 2226 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2229 | return JNIInvalidRefType; |
| 2230 | } |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 2231 | LOG(FATAL) << "IndirectRefKind[" << kind << "]"; |
| 2232 | return JNIInvalidRefType; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2233 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2234 | |
| 2235 | private: |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2236 | static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity, |
| 2237 | const char* caller) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2238 | // TODO: we should try to expand the table if necessary. |
| 2239 | if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) { |
| 2240 | LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity; |
| 2241 | return JNI_ERR; |
| 2242 | } |
| 2243 | // TODO: this isn't quite right, since "capacity" includes holes. |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2244 | size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2245 | bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity); |
| 2246 | if (!okay) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2247 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2248 | soa.Self()->ThrowOutOfMemoryError(caller); |
| 2249 | } |
| 2250 | return okay ? JNI_OK : JNI_ERR; |
| 2251 | } |
| 2252 | |
| 2253 | template<typename JniT, typename ArtT> |
| 2254 | static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2255 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 96a9887 | 2012-12-19 14:21:15 -0800 | [diff] [blame] | 2256 | if (length < 0) { |
| 2257 | JniAbortF("NewPrimitiveArray", "negative array length: %d", length); |
| 2258 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2259 | ArtT* result = ArtT::Alloc(soa.Self(), length); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2260 | return soa.AddLocalReference<JniT>(result); |
| 2261 | } |
| 2262 | |
| 2263 | template <typename ArrayT, typename CArrayT, typename ArtArrayT> |
| 2264 | static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array, |
| 2265 | jboolean* is_copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2266 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2267 | ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array); |
| 2268 | PinPrimitiveArray(soa, array); |
| 2269 | if (is_copy != NULL) { |
| 2270 | *is_copy = JNI_FALSE; |
| 2271 | } |
| 2272 | return array->GetData(); |
| 2273 | } |
| 2274 | |
| 2275 | template <typename ArrayT> |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2276 | static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2277 | if (mode != JNI_COMMIT) { |
Ian Rogers | ef28b14 | 2012-11-30 14:22:18 -0800 | [diff] [blame] | 2278 | ScopedObjectAccess soa(env); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2279 | Array* array = soa.Decode<Array*>(java_array); |
| 2280 | UnpinPrimitiveArray(soa, array); |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | template <typename JavaArrayT, typename JavaT, typename ArrayT> |
| 2285 | static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array, |
| 2286 | jsize start, jsize length, JavaT* buf) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2287 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2288 | ArrayT* array = soa.Decode<ArrayT*>(java_array); |
| 2289 | if (start < 0 || length < 0 || start + length > array->GetLength()) { |
| 2290 | ThrowAIOOBE(soa, array, start, length, "src"); |
| 2291 | } else { |
| 2292 | JavaT* data = array->GetData(); |
| 2293 | memcpy(buf, data + start, length * sizeof(JavaT)); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | template <typename JavaArrayT, typename JavaT, typename ArrayT> |
| 2298 | static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array, |
| 2299 | jsize start, jsize length, const JavaT* buf) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2300 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2301 | ArrayT* array = soa.Decode<ArrayT*>(java_array); |
| 2302 | if (start < 0 || length < 0 || start + length > array->GetLength()) { |
| 2303 | ThrowAIOOBE(soa, array, start, length, "dst"); |
| 2304 | } else { |
| 2305 | JavaT* data = array->GetData(); |
| 2306 | memcpy(data + start, buf, length * sizeof(JavaT)); |
| 2307 | } |
| 2308 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2309 | }; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2310 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2311 | const JNINativeInterface gJniNativeInterface = { |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2312 | NULL, // reserved0. |
| 2313 | NULL, // reserved1. |
| 2314 | NULL, // reserved2. |
| 2315 | NULL, // reserved3. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2316 | JNI::GetVersion, |
| 2317 | JNI::DefineClass, |
| 2318 | JNI::FindClass, |
| 2319 | JNI::FromReflectedMethod, |
| 2320 | JNI::FromReflectedField, |
| 2321 | JNI::ToReflectedMethod, |
| 2322 | JNI::GetSuperclass, |
| 2323 | JNI::IsAssignableFrom, |
| 2324 | JNI::ToReflectedField, |
| 2325 | JNI::Throw, |
| 2326 | JNI::ThrowNew, |
| 2327 | JNI::ExceptionOccurred, |
| 2328 | JNI::ExceptionDescribe, |
| 2329 | JNI::ExceptionClear, |
| 2330 | JNI::FatalError, |
| 2331 | JNI::PushLocalFrame, |
| 2332 | JNI::PopLocalFrame, |
| 2333 | JNI::NewGlobalRef, |
| 2334 | JNI::DeleteGlobalRef, |
| 2335 | JNI::DeleteLocalRef, |
| 2336 | JNI::IsSameObject, |
| 2337 | JNI::NewLocalRef, |
| 2338 | JNI::EnsureLocalCapacity, |
| 2339 | JNI::AllocObject, |
| 2340 | JNI::NewObject, |
| 2341 | JNI::NewObjectV, |
| 2342 | JNI::NewObjectA, |
| 2343 | JNI::GetObjectClass, |
| 2344 | JNI::IsInstanceOf, |
| 2345 | JNI::GetMethodID, |
| 2346 | JNI::CallObjectMethod, |
| 2347 | JNI::CallObjectMethodV, |
| 2348 | JNI::CallObjectMethodA, |
| 2349 | JNI::CallBooleanMethod, |
| 2350 | JNI::CallBooleanMethodV, |
| 2351 | JNI::CallBooleanMethodA, |
| 2352 | JNI::CallByteMethod, |
| 2353 | JNI::CallByteMethodV, |
| 2354 | JNI::CallByteMethodA, |
| 2355 | JNI::CallCharMethod, |
| 2356 | JNI::CallCharMethodV, |
| 2357 | JNI::CallCharMethodA, |
| 2358 | JNI::CallShortMethod, |
| 2359 | JNI::CallShortMethodV, |
| 2360 | JNI::CallShortMethodA, |
| 2361 | JNI::CallIntMethod, |
| 2362 | JNI::CallIntMethodV, |
| 2363 | JNI::CallIntMethodA, |
| 2364 | JNI::CallLongMethod, |
| 2365 | JNI::CallLongMethodV, |
| 2366 | JNI::CallLongMethodA, |
| 2367 | JNI::CallFloatMethod, |
| 2368 | JNI::CallFloatMethodV, |
| 2369 | JNI::CallFloatMethodA, |
| 2370 | JNI::CallDoubleMethod, |
| 2371 | JNI::CallDoubleMethodV, |
| 2372 | JNI::CallDoubleMethodA, |
| 2373 | JNI::CallVoidMethod, |
| 2374 | JNI::CallVoidMethodV, |
| 2375 | JNI::CallVoidMethodA, |
| 2376 | JNI::CallNonvirtualObjectMethod, |
| 2377 | JNI::CallNonvirtualObjectMethodV, |
| 2378 | JNI::CallNonvirtualObjectMethodA, |
| 2379 | JNI::CallNonvirtualBooleanMethod, |
| 2380 | JNI::CallNonvirtualBooleanMethodV, |
| 2381 | JNI::CallNonvirtualBooleanMethodA, |
| 2382 | JNI::CallNonvirtualByteMethod, |
| 2383 | JNI::CallNonvirtualByteMethodV, |
| 2384 | JNI::CallNonvirtualByteMethodA, |
| 2385 | JNI::CallNonvirtualCharMethod, |
| 2386 | JNI::CallNonvirtualCharMethodV, |
| 2387 | JNI::CallNonvirtualCharMethodA, |
| 2388 | JNI::CallNonvirtualShortMethod, |
| 2389 | JNI::CallNonvirtualShortMethodV, |
| 2390 | JNI::CallNonvirtualShortMethodA, |
| 2391 | JNI::CallNonvirtualIntMethod, |
| 2392 | JNI::CallNonvirtualIntMethodV, |
| 2393 | JNI::CallNonvirtualIntMethodA, |
| 2394 | JNI::CallNonvirtualLongMethod, |
| 2395 | JNI::CallNonvirtualLongMethodV, |
| 2396 | JNI::CallNonvirtualLongMethodA, |
| 2397 | JNI::CallNonvirtualFloatMethod, |
| 2398 | JNI::CallNonvirtualFloatMethodV, |
| 2399 | JNI::CallNonvirtualFloatMethodA, |
| 2400 | JNI::CallNonvirtualDoubleMethod, |
| 2401 | JNI::CallNonvirtualDoubleMethodV, |
| 2402 | JNI::CallNonvirtualDoubleMethodA, |
| 2403 | JNI::CallNonvirtualVoidMethod, |
| 2404 | JNI::CallNonvirtualVoidMethodV, |
| 2405 | JNI::CallNonvirtualVoidMethodA, |
| 2406 | JNI::GetFieldID, |
| 2407 | JNI::GetObjectField, |
| 2408 | JNI::GetBooleanField, |
| 2409 | JNI::GetByteField, |
| 2410 | JNI::GetCharField, |
| 2411 | JNI::GetShortField, |
| 2412 | JNI::GetIntField, |
| 2413 | JNI::GetLongField, |
| 2414 | JNI::GetFloatField, |
| 2415 | JNI::GetDoubleField, |
| 2416 | JNI::SetObjectField, |
| 2417 | JNI::SetBooleanField, |
| 2418 | JNI::SetByteField, |
| 2419 | JNI::SetCharField, |
| 2420 | JNI::SetShortField, |
| 2421 | JNI::SetIntField, |
| 2422 | JNI::SetLongField, |
| 2423 | JNI::SetFloatField, |
| 2424 | JNI::SetDoubleField, |
| 2425 | JNI::GetStaticMethodID, |
| 2426 | JNI::CallStaticObjectMethod, |
| 2427 | JNI::CallStaticObjectMethodV, |
| 2428 | JNI::CallStaticObjectMethodA, |
| 2429 | JNI::CallStaticBooleanMethod, |
| 2430 | JNI::CallStaticBooleanMethodV, |
| 2431 | JNI::CallStaticBooleanMethodA, |
| 2432 | JNI::CallStaticByteMethod, |
| 2433 | JNI::CallStaticByteMethodV, |
| 2434 | JNI::CallStaticByteMethodA, |
| 2435 | JNI::CallStaticCharMethod, |
| 2436 | JNI::CallStaticCharMethodV, |
| 2437 | JNI::CallStaticCharMethodA, |
| 2438 | JNI::CallStaticShortMethod, |
| 2439 | JNI::CallStaticShortMethodV, |
| 2440 | JNI::CallStaticShortMethodA, |
| 2441 | JNI::CallStaticIntMethod, |
| 2442 | JNI::CallStaticIntMethodV, |
| 2443 | JNI::CallStaticIntMethodA, |
| 2444 | JNI::CallStaticLongMethod, |
| 2445 | JNI::CallStaticLongMethodV, |
| 2446 | JNI::CallStaticLongMethodA, |
| 2447 | JNI::CallStaticFloatMethod, |
| 2448 | JNI::CallStaticFloatMethodV, |
| 2449 | JNI::CallStaticFloatMethodA, |
| 2450 | JNI::CallStaticDoubleMethod, |
| 2451 | JNI::CallStaticDoubleMethodV, |
| 2452 | JNI::CallStaticDoubleMethodA, |
| 2453 | JNI::CallStaticVoidMethod, |
| 2454 | JNI::CallStaticVoidMethodV, |
| 2455 | JNI::CallStaticVoidMethodA, |
| 2456 | JNI::GetStaticFieldID, |
| 2457 | JNI::GetStaticObjectField, |
| 2458 | JNI::GetStaticBooleanField, |
| 2459 | JNI::GetStaticByteField, |
| 2460 | JNI::GetStaticCharField, |
| 2461 | JNI::GetStaticShortField, |
| 2462 | JNI::GetStaticIntField, |
| 2463 | JNI::GetStaticLongField, |
| 2464 | JNI::GetStaticFloatField, |
| 2465 | JNI::GetStaticDoubleField, |
| 2466 | JNI::SetStaticObjectField, |
| 2467 | JNI::SetStaticBooleanField, |
| 2468 | JNI::SetStaticByteField, |
| 2469 | JNI::SetStaticCharField, |
| 2470 | JNI::SetStaticShortField, |
| 2471 | JNI::SetStaticIntField, |
| 2472 | JNI::SetStaticLongField, |
| 2473 | JNI::SetStaticFloatField, |
| 2474 | JNI::SetStaticDoubleField, |
| 2475 | JNI::NewString, |
| 2476 | JNI::GetStringLength, |
| 2477 | JNI::GetStringChars, |
| 2478 | JNI::ReleaseStringChars, |
| 2479 | JNI::NewStringUTF, |
| 2480 | JNI::GetStringUTFLength, |
| 2481 | JNI::GetStringUTFChars, |
| 2482 | JNI::ReleaseStringUTFChars, |
| 2483 | JNI::GetArrayLength, |
| 2484 | JNI::NewObjectArray, |
| 2485 | JNI::GetObjectArrayElement, |
| 2486 | JNI::SetObjectArrayElement, |
| 2487 | JNI::NewBooleanArray, |
| 2488 | JNI::NewByteArray, |
| 2489 | JNI::NewCharArray, |
| 2490 | JNI::NewShortArray, |
| 2491 | JNI::NewIntArray, |
| 2492 | JNI::NewLongArray, |
| 2493 | JNI::NewFloatArray, |
| 2494 | JNI::NewDoubleArray, |
| 2495 | JNI::GetBooleanArrayElements, |
| 2496 | JNI::GetByteArrayElements, |
| 2497 | JNI::GetCharArrayElements, |
| 2498 | JNI::GetShortArrayElements, |
| 2499 | JNI::GetIntArrayElements, |
| 2500 | JNI::GetLongArrayElements, |
| 2501 | JNI::GetFloatArrayElements, |
| 2502 | JNI::GetDoubleArrayElements, |
| 2503 | JNI::ReleaseBooleanArrayElements, |
| 2504 | JNI::ReleaseByteArrayElements, |
| 2505 | JNI::ReleaseCharArrayElements, |
| 2506 | JNI::ReleaseShortArrayElements, |
| 2507 | JNI::ReleaseIntArrayElements, |
| 2508 | JNI::ReleaseLongArrayElements, |
| 2509 | JNI::ReleaseFloatArrayElements, |
| 2510 | JNI::ReleaseDoubleArrayElements, |
| 2511 | JNI::GetBooleanArrayRegion, |
| 2512 | JNI::GetByteArrayRegion, |
| 2513 | JNI::GetCharArrayRegion, |
| 2514 | JNI::GetShortArrayRegion, |
| 2515 | JNI::GetIntArrayRegion, |
| 2516 | JNI::GetLongArrayRegion, |
| 2517 | JNI::GetFloatArrayRegion, |
| 2518 | JNI::GetDoubleArrayRegion, |
| 2519 | JNI::SetBooleanArrayRegion, |
| 2520 | JNI::SetByteArrayRegion, |
| 2521 | JNI::SetCharArrayRegion, |
| 2522 | JNI::SetShortArrayRegion, |
| 2523 | JNI::SetIntArrayRegion, |
| 2524 | JNI::SetLongArrayRegion, |
| 2525 | JNI::SetFloatArrayRegion, |
| 2526 | JNI::SetDoubleArrayRegion, |
| 2527 | JNI::RegisterNatives, |
| 2528 | JNI::UnregisterNatives, |
| 2529 | JNI::MonitorEnter, |
| 2530 | JNI::MonitorExit, |
| 2531 | JNI::GetJavaVM, |
| 2532 | JNI::GetStringRegion, |
| 2533 | JNI::GetStringUTFRegion, |
| 2534 | JNI::GetPrimitiveArrayCritical, |
| 2535 | JNI::ReleasePrimitiveArrayCritical, |
| 2536 | JNI::GetStringCritical, |
| 2537 | JNI::ReleaseStringCritical, |
| 2538 | JNI::NewWeakGlobalRef, |
| 2539 | JNI::DeleteWeakGlobalRef, |
| 2540 | JNI::ExceptionCheck, |
| 2541 | JNI::NewDirectByteBuffer, |
| 2542 | JNI::GetDirectBufferAddress, |
| 2543 | JNI::GetDirectBufferCapacity, |
| 2544 | JNI::GetObjectRefType, |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2545 | }; |
| 2546 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2547 | JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm) |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 2548 | : self(self), |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2549 | vm(vm), |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 2550 | local_ref_cookie(IRT_FIRST_SEGMENT), |
| 2551 | locals(kLocalsInitial, kLocalsMax, kLocal), |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2552 | check_jni(false), |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 2553 | critical(false), |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 2554 | monitors("monitors", kMonitorsInitial, kMonitorsMax) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2555 | functions = unchecked_functions = &gJniNativeInterface; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2556 | if (vm->check_jni) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2557 | SetCheckJniEnabled(true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2558 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 2559 | // The JniEnv local reference values must be at a consistent offset or else cross-compilation |
| 2560 | // errors will ensue. |
| 2561 | CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12); |
| 2562 | CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16); |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 2565 | JNIEnvExt::~JNIEnvExt() { |
| 2566 | } |
| 2567 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2568 | void JNIEnvExt::SetCheckJniEnabled(bool enabled) { |
| 2569 | check_jni = enabled; |
| 2570 | functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 2573 | void JNIEnvExt::DumpReferenceTables(std::ostream& os) { |
| 2574 | locals.Dump(os); |
| 2575 | monitors.Dump(os); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 2576 | } |
| 2577 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2578 | void JNIEnvExt::PushFrame(int /*capacity*/) { |
| 2579 | // TODO: take 'capacity' into account. |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 2580 | stacked_local_ref_cookies.push_back(local_ref_cookie); |
| 2581 | local_ref_cookie = locals.GetSegmentState(); |
| 2582 | } |
| 2583 | |
| 2584 | void JNIEnvExt::PopFrame() { |
| 2585 | locals.SetSegmentState(local_ref_cookie); |
| 2586 | local_ref_cookie = stacked_local_ref_cookies.back(); |
| 2587 | stacked_local_ref_cookies.pop_back(); |
| 2588 | } |
| 2589 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2590 | Offset JNIEnvExt::SegmentStateOffset() { |
| 2591 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) + |
| 2592 | IndirectReferenceTable::SegmentStateOffset().Int32Value()); |
| 2593 | } |
| 2594 | |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2595 | // JNI Invocation interface. |
| 2596 | |
Brian Carlstrom | bddf976 | 2013-05-14 11:35:37 -0700 | [diff] [blame] | 2597 | extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2598 | const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args); |
Elliott Hughes | 83a2532 | 2013-03-14 11:18:53 -0700 | [diff] [blame] | 2599 | if (IsBadJniVersion(args->version)) { |
| 2600 | LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2601 | return JNI_EVERSION; |
| 2602 | } |
| 2603 | Runtime::Options options; |
| 2604 | for (int i = 0; i < args->nOptions; ++i) { |
| 2605 | JavaVMOption* option = &args->options[i]; |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 2606 | options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2607 | } |
| 2608 | bool ignore_unrecognized = args->ignoreUnrecognized; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2609 | if (!Runtime::Create(options, ignore_unrecognized)) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2610 | return JNI_ERR; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2611 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2612 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 2613 | bool started = runtime->Start(); |
| 2614 | if (!started) { |
| 2615 | delete Thread::Current()->GetJniEnv(); |
| 2616 | delete runtime->GetJavaVM(); |
| 2617 | LOG(WARNING) << "CreateJavaVM failed"; |
| 2618 | return JNI_ERR; |
| 2619 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 2620 | *p_env = Thread::Current()->GetJniEnv(); |
| 2621 | *p_vm = runtime->GetJavaVM(); |
| 2622 | return JNI_OK; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 2625 | extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2626 | Runtime* runtime = Runtime::Current(); |
| 2627 | if (runtime == NULL) { |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 2628 | *vm_count = 0; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2629 | } else { |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 2630 | *vm_count = 1; |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 2631 | vms[0] = runtime->GetJavaVM(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2632 | } |
| 2633 | return JNI_OK; |
| 2634 | } |
| 2635 | |
| 2636 | // Historically unsupported. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2637 | extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2638 | return JNI_ERR; |
| 2639 | } |
| 2640 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2641 | class JII { |
| 2642 | public: |
| 2643 | static jint DestroyJavaVM(JavaVM* vm) { |
| 2644 | if (vm == NULL) { |
| 2645 | return JNI_ERR; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2646 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 2647 | JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm); |
| 2648 | delete raw_vm->runtime; |
| 2649 | return JNI_OK; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2650 | } |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2651 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2652 | static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2653 | return JII_AttachCurrentThread(vm, p_env, thr_args, false); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2657 | return JII_AttachCurrentThread(vm, p_env, thr_args, true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | static jint DetachCurrentThread(JavaVM* vm) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 2661 | if (vm == NULL || Thread::Current() == NULL) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2662 | return JNI_ERR; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2663 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 2664 | JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm); |
| 2665 | Runtime* runtime = raw_vm->runtime; |
| 2666 | runtime->DetachCurrentThread(); |
| 2667 | return JNI_OK; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | static jint GetEnv(JavaVM* vm, void** env, jint version) { |
Elliott Hughes | 83a2532 | 2013-03-14 11:18:53 -0700 | [diff] [blame] | 2671 | if (IsBadJniVersion(version)) { |
| 2672 | LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2673 | return JNI_EVERSION; |
| 2674 | } |
| 2675 | if (vm == NULL || env == NULL) { |
| 2676 | return JNI_ERR; |
| 2677 | } |
| 2678 | Thread* thread = Thread::Current(); |
| 2679 | if (thread == NULL) { |
| 2680 | *env = NULL; |
| 2681 | return JNI_EDETACHED; |
| 2682 | } |
| 2683 | *env = thread->GetJniEnv(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2684 | return JNI_OK; |
| 2685 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2686 | }; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 2687 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2688 | const JNIInvokeInterface gJniInvokeInterface = { |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2689 | NULL, // reserved0 |
| 2690 | NULL, // reserved1 |
| 2691 | NULL, // reserved2 |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2692 | JII::DestroyJavaVM, |
| 2693 | JII::AttachCurrentThread, |
| 2694 | JII::DetachCurrentThread, |
| 2695 | JII::GetEnv, |
| 2696 | JII::AttachCurrentThreadAsDaemon |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 2697 | }; |
| 2698 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2699 | JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options) |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 2700 | : runtime(runtime), |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2701 | check_jni_abort_hook(NULL), |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2702 | check_jni_abort_hook_data(NULL), |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2703 | check_jni(false), |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2704 | force_copy(false), // TODO: add a way to enable this |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2705 | trace(options->jni_trace_), |
Elliott Hughes | c2dc62d | 2012-01-17 20:06:12 -0800 | [diff] [blame] | 2706 | work_around_app_jni_bugs(false), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2707 | pins_lock("JNI pin table lock", kPinTableLock), |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 2708 | pin_table("pin table", kPinTableInitial, kPinTableMax), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 2709 | globals_lock("JNI global reference table lock"), |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 2710 | globals(gGlobalsInitial, gGlobalsMax, kGlobal), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 2711 | weak_globals_lock("JNI weak global reference table lock"), |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2712 | weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2713 | libraries_lock("JNI shared libraries map lock", kLoadLibraryLock), |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2714 | libraries(new Libraries) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2715 | functions = unchecked_functions = &gJniInvokeInterface; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2716 | if (options->check_jni_) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2717 | SetCheckJniEnabled(true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2718 | } |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 2719 | } |
| 2720 | |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 2721 | JavaVMExt::~JavaVMExt() { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2722 | delete libraries; |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 2725 | void JavaVMExt::SetCheckJniEnabled(bool enabled) { |
| 2726 | check_jni = enabled; |
| 2727 | functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2730 | void JavaVMExt::DumpForSigQuit(std::ostream& os) { |
| 2731 | os << "JNI: CheckJNI is " << (check_jni ? "on" : "off"); |
| 2732 | if (force_copy) { |
| 2733 | os << " (with forcecopy)"; |
| 2734 | } |
| 2735 | os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off"); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2736 | Thread* self = Thread::Current(); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2737 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2738 | MutexLock mu(self, pins_lock); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2739 | os << "; pins=" << pin_table.Size(); |
| 2740 | } |
| 2741 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2742 | MutexLock mu(self, globals_lock); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2743 | os << "; globals=" << globals.Capacity(); |
| 2744 | } |
| 2745 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2746 | MutexLock mu(self, weak_globals_lock); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2747 | if (weak_globals.Capacity() > 0) { |
| 2748 | os << " (plus " << weak_globals.Capacity() << " weak)"; |
| 2749 | } |
| 2750 | } |
| 2751 | os << '\n'; |
| 2752 | |
| 2753 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2754 | MutexLock mu(self, libraries_lock); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 2755 | os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n"; |
| 2756 | } |
| 2757 | } |
| 2758 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 2759 | void JavaVMExt::DumpReferenceTables(std::ostream& os) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2760 | Thread* self = Thread::Current(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 2761 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2762 | MutexLock mu(self, globals_lock); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 2763 | globals.Dump(os); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 2764 | } |
| 2765 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2766 | MutexLock mu(self, weak_globals_lock); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 2767 | weak_globals.Dump(os); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 2768 | } |
| 2769 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2770 | MutexLock mu(self, pins_lock); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 2771 | pin_table.Dump(os); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2775 | bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, |
| 2776 | std::string& detail) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2777 | detail.clear(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2778 | |
| 2779 | // See if we've already loaded this library. If we have, and the class loader |
| 2780 | // matches, return successfully without doing anything. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2781 | // TODO: for better results we should canonicalize the pathname (or even compare |
| 2782 | // inodes). This implementation is fine if everybody is using System.loadLibrary. |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2783 | SharedLibrary* library; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2784 | Thread* self = Thread::Current(); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2785 | { |
| 2786 | // TODO: move the locking (and more of this logic) into Libraries. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2787 | MutexLock mu(self, libraries_lock); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2788 | library = libraries->Get(path); |
| 2789 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2790 | if (library != NULL) { |
| 2791 | if (library->GetClassLoader() != class_loader) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2792 | // The library will be associated with class_loader. The JNI |
| 2793 | // spec says we can't load the same library into more than one |
| 2794 | // class loader. |
| 2795 | StringAppendF(&detail, "Shared library \"%s\" already opened by " |
| 2796 | "ClassLoader %p; can't open in ClassLoader %p", |
| 2797 | path.c_str(), library->GetClassLoader(), class_loader); |
| 2798 | LOG(WARNING) << detail; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2799 | return false; |
| 2800 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2801 | VLOG(jni) << "[Shared library \"" << path << "\" already loaded in " |
| 2802 | << "ClassLoader " << class_loader << "]"; |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2803 | if (!library->CheckOnLoadResult()) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2804 | StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt " |
| 2805 | "to load \"%s\"", path.c_str()); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2806 | return false; |
| 2807 | } |
| 2808 | return true; |
| 2809 | } |
| 2810 | |
| 2811 | // Open the shared library. Because we're using a full path, the system |
| 2812 | // doesn't have to search through LD_LIBRARY_PATH. (It may do so to |
| 2813 | // resolve this library's dependencies though.) |
| 2814 | |
| 2815 | // Failures here are expected when java.library.path has several entries |
| 2816 | // and we have to hunt for the lib. |
| 2817 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2818 | // Below we dlopen but there is no paired dlclose, this would be necessary if we supported |
| 2819 | // class unloading. Libraries will only be unloaded when the reference count (incremented by |
| 2820 | // dlopen) becomes zero from dlclose. |
| 2821 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2822 | // This can execute slowly for a large library on a busy system, so we |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2823 | // want to switch from kRunnable while it executes. This allows the GC to ignore us. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2824 | self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad); |
| 2825 | void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY); |
| 2826 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2827 | |
Elliott Hughes | 84b2f14 | 2012-09-27 09:16:28 -0700 | [diff] [blame] | 2828 | VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]"; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2829 | |
| 2830 | if (handle == NULL) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 2831 | detail = dlerror(); |
Elliott Hughes | 84b2f14 | 2012-09-27 09:16:28 -0700 | [diff] [blame] | 2832 | LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2833 | return false; |
| 2834 | } |
| 2835 | |
| 2836 | // Create a new entry. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2837 | // TODO: move the locking (and more of this logic) into Libraries. |
| 2838 | bool created_library = false; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2839 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2840 | MutexLock mu(self, libraries_lock); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2841 | library = libraries->Get(path); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2842 | if (library == NULL) { // We won race to get libraries_lock |
| 2843 | library = new SharedLibrary(path, handle, class_loader); |
| 2844 | libraries->Put(path, library); |
| 2845 | created_library = true; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2846 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2847 | } |
| 2848 | if (!created_library) { |
| 2849 | LOG(INFO) << "WOW: we lost a race to add shared library: " |
| 2850 | << "\"" << path << "\" ClassLoader=" << class_loader; |
| 2851 | return library->CheckOnLoadResult(); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2852 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2853 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2854 | VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2855 | |
Elliott Hughes | 7935372 | 2013-08-02 16:52:18 -0700 | [diff] [blame] | 2856 | bool was_successful = false; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2857 | void* sym = dlsym(handle, "JNI_OnLoad"); |
| 2858 | if (sym == NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2859 | VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]"; |
Elliott Hughes | 85affca | 2013-08-02 17:48:52 -0700 | [diff] [blame^] | 2860 | was_successful = true; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2861 | } else { |
| 2862 | // Call JNI_OnLoad. We have to override the current class |
| 2863 | // loader, which will always be "null" since the stuff at the |
| 2864 | // top of the stack is around Runtime.loadLibrary(). (See |
| 2865 | // the comments in the JNI FindClass function.) |
| 2866 | typedef int (*JNI_OnLoadFn)(JavaVM*, void*); |
| 2867 | JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2868 | ClassLoader* old_class_loader = self->GetClassLoaderOverride(); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2869 | self->SetClassLoaderOverride(class_loader); |
| 2870 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 2871 | int version = 0; |
| 2872 | { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 2873 | ScopedThreadStateChange tsc(self, kNative); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2874 | VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]"; |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 2875 | version = (*jni_on_load)(this, NULL); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2876 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2877 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2878 | self->SetClassLoaderOverride(old_class_loader); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2879 | |
Elliott Hughes | 7935372 | 2013-08-02 16:52:18 -0700 | [diff] [blame] | 2880 | if (version == JNI_ERR) { |
| 2881 | StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str()); |
| 2882 | } else if (IsBadJniVersion(version)) { |
Brian Carlstrom | 75fe90c | 2013-06-26 22:26:16 -0700 | [diff] [blame] | 2883 | StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d", |
| 2884 | path.c_str(), version); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2885 | // It's unwise to call dlclose() here, but we can mark it |
| 2886 | // as bad and ensure that future load attempts will fail. |
| 2887 | // We don't know how far JNI_OnLoad got, so there could |
| 2888 | // be some partially-initialized stuff accessible through |
| 2889 | // newly-registered native method calls. We could try to |
| 2890 | // unregister them, but that doesn't seem worthwhile. |
Elliott Hughes | 7935372 | 2013-08-02 16:52:18 -0700 | [diff] [blame] | 2891 | } else { |
| 2892 | was_successful = true; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2893 | } |
Elliott Hughes | 7935372 | 2013-08-02 16:52:18 -0700 | [diff] [blame] | 2894 | VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure") |
Brian Carlstrom | 75fe90c | 2013-06-26 22:26:16 -0700 | [diff] [blame] | 2895 | << " from JNI_OnLoad in \"" << path << "\"]"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Elliott Hughes | 7935372 | 2013-08-02 16:52:18 -0700 | [diff] [blame] | 2898 | library->SetResult(was_successful); |
| 2899 | return was_successful; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2900 | } |
| 2901 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2902 | void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2903 | CHECK(m->IsNative()); |
| 2904 | |
| 2905 | Class* c = m->GetDeclaringClass(); |
| 2906 | |
| 2907 | // If this is a static method, it could be called before the class |
| 2908 | // has been initialized. |
| 2909 | if (m->IsStatic()) { |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 2910 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2911 | return NULL; |
| 2912 | } |
| 2913 | } else { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 2914 | CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 2917 | std::string detail; |
| 2918 | void* native_method; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2919 | Thread* self = Thread::Current(); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 2920 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2921 | MutexLock mu(self, libraries_lock); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 2922 | native_method = libraries->FindNativeMethod(m, detail); |
| 2923 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2924 | // Throwing can cause libraries_lock to be reacquired. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 2925 | if (native_method == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2926 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 2927 | self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 2928 | } |
| 2929 | return native_method; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2932 | void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2933 | Thread* self = Thread::Current(); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 2934 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2935 | MutexLock mu(self, globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 2936 | globals.VisitRoots(visitor, arg); |
| 2937 | } |
| 2938 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2939 | MutexLock mu(self, pins_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 2940 | pin_table.VisitRoots(visitor, arg); |
| 2941 | } |
| 2942 | // The weak_globals table is visited by the GC itself (because it mutates the table). |
| 2943 | } |
| 2944 | |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 2945 | void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, |
| 2946 | size_t method_count) { |
| 2947 | ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name)); |
| 2948 | if (c.get() == NULL) { |
| 2949 | LOG(FATAL) << "Couldn't find class: " << jni_class_name; |
| 2950 | } |
| 2951 | JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false); |
| 2952 | } |
| 2953 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 2954 | } // namespace art |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2955 | |
| 2956 | std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) { |
| 2957 | switch (rhs) { |
| 2958 | case JNIInvalidRefType: |
| 2959 | os << "JNIInvalidRefType"; |
| 2960 | return os; |
| 2961 | case JNILocalRefType: |
| 2962 | os << "JNILocalRefType"; |
| 2963 | return os; |
| 2964 | case JNIGlobalRefType: |
| 2965 | os << "JNIGlobalRefType"; |
| 2966 | return os; |
| 2967 | case JNIWeakGlobalRefType: |
| 2968 | os << "JNIWeakGlobalRefType"; |
| 2969 | return os; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 2970 | default: |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 2971 | LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]"; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 2972 | return os; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2973 | } |
| 2974 | } |