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