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