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