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