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