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