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