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