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