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