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