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