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