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