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