blob: 0845c0f4142f992eaed6e103f84353f35b60d4c0 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Carl Shapiro9b9ba282011-08-14 15:30:39 -07005#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -07006#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07007#include <sys/mman.h>
Elliott Hughes0af55432011-08-17 18:37:28 -07008#include <utility>
9#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070010
Elliott Hughes72025e52011-08-23 17:50:30 -070011#include "ScopedLocalRef.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070012#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070013#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070014#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070016#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017#include "runtime.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070018#include "scoped_ptr.h"
19#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070021
buzbeec143c552011-08-20 17:38:58 -070022extern bool oatCompileMethod(art::Method*, art::InstructionSet);
23
Ian Rogersdf20fe02011-07-20 20:34:16 -070024namespace art {
25
Elliott Hughescdf53122011-08-19 15:46:09 -070026// This is private API, but with two different implementations: ARM and x86.
27void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070028
Elliott Hughescdf53122011-08-19 15:46:09 -070029// TODO: this should be in our anonymous namespace, but is currently needed
30// for testing in "jni_internal_test.cc".
31bool 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 Hughesa0b8feb2011-08-20 09:50:55 -070044 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070045 }
46 MemoryRegion region(addr, length);
47 assembler.FinalizeInstructions(region);
48 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
49 return true;
50}
Elliott Hughes0af55432011-08-17 18:37:28 -070051
Elliott Hughescdf53122011-08-19 15:46:09 -070052// TODO: this can't be in our anonymous namespace because of the map in JavaVM.
53class SharedLibrary {
54public:
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 Hughes5174fe62011-08-23 15:12:35 -070061 pthread_cond_init(&jni_on_load_cond_, NULL);
Elliott Hughes18c07532011-08-18 15:50:51 -070062 }
63
64 ~SharedLibrary() {
Elliott Hughescdf53122011-08-19 15:46:09 -070065 delete jni_on_load_lock_;
Elliott Hughes0af55432011-08-17 18:37:28 -070066 }
67
Elliott Hughescdf53122011-08-19 15:46:09 -070068 Object* GetClassLoader() {
69 return class_loader_;
Elliott Hughes0af55432011-08-17 18:37:28 -070070 }
71
Elliott Hughescdf53122011-08-19 15:46:09 -070072 /*
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 Hughes0af55432011-08-17 18:37:28 -070084 }
85
Elliott Hughes5174fe62011-08-23 15:12:35 -070086 MutexLock mu(jni_on_load_lock_);
Elliott Hughescdf53122011-08-19 15:46:09 -070087 while (jni_on_load_result_ == kPending) {
88 if (vm->verbose_jni) {
89 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
90 << "JNI_OnLoad...]";
Elliott Hughes0af55432011-08-17 18:37:28 -070091 }
Elliott Hughescdf53122011-08-19 15:46:09 -070092 Thread::State old_state = self->GetState();
93 self->SetState(Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes5174fe62011-08-23 15:12:35 -070094 pthread_cond_wait(&jni_on_load_cond_, &(jni_on_load_lock_->lock_impl_));
Elliott Hughes0af55432011-08-17 18:37:28 -070095 self->SetState(old_state);
Elliott Hughes0af55432011-08-17 18:37:28 -070096 }
97
Elliott Hughescdf53122011-08-19 15:46:09 -070098 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 Hughes0af55432011-08-17 18:37:28 -0700109
110 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700111 MutexLock mu(jni_on_load_lock_);
112 pthread_cond_broadcast(&jni_on_load_cond_);
Elliott Hughes0af55432011-08-17 18:37:28 -0700113 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700114
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
141namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -0700142
Elliott Hughes22f40932011-08-12 13:06:37 -0700143// 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.
148class ScopedJniThreadState {
149 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700150 explicit ScopedJniThreadState(JNIEnv* env)
151 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700152 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -0700153 self_->SetState(Thread::kRunnable);
154 }
155
156 ~ScopedJniThreadState() {
157 self_->SetState(Thread::kNative);
158 }
159
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700160 JNIEnvExt* Env() {
161 return env_;
162 }
163
Elliott Hughesb20a5542011-08-12 18:03:12 -0700164 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -0700165 return self_;
166 }
167
Elliott Hughesb20a5542011-08-12 18:03:12 -0700168 private:
169 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -0700170 // 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 Hughes330304d2011-08-12 14:28:05 -0700175 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
176 << " used on " << *self;
177 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -0700178 }
179 return self;
180 }
181
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700182 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -0700183 Thread* self_;
184 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
185};
186
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700187/*
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 Hughes8a26c5c2011-08-15 18:35:43 -0700199template<typename T>
200T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700201 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 Hughese5b0dc82011-08-23 09:59:02 -0700221 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700222 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700223 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700224 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 Hughescdf53122011-08-19 15:46:09 -0700239jweak 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 Hughesc5f7c912011-08-18 14:00:42 -0700250template<typename T>
251T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700252 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700253}
254
Elliott Hughescdf53122011-08-19 15:46:09 -0700255Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
256 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
257}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700258
Elliott Hughescdf53122011-08-19 15:46:09 -0700259Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
260 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700261}
262
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700263byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700264 size_t num_bytes = method->NumArgArrayBytes();
265 scoped_array<byte> arg_array(new byte[num_bytes]);
266 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700267 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700268 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 Hughesc5f7c912011-08-18 14:00:42 -0700282 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700283 *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 Hughesc5f7c912011-08-18 14:00:42 -0700300byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700301 size_t num_bytes = method->NumArgArrayBytes();
302 scoped_array<byte> arg_array(new byte[num_bytes]);
303 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700304 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700305 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 Hughesc5f7c912011-08-18 14:00:42 -0700319 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700320 *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 Hughes72025e52011-08-23 17:50:30 -0700337JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
338 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700339 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 Shapiro9b9ba282011-08-14 15:30:39 -0700346 // Call the invoke stub associated with the method
347 // Pass everything as arguments
348 const Method::InvokeStub* stub = method->GetInvokeStub();
349 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700350
351#ifdef __arm__
352 // Compile...
353 // TODO: not here!
354 oatCompileMethod(method, kThumb2);
355#endif
356
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700357 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700358 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700359 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700360 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700361 LOG(WARNING) << "Not invoking method with no associated code: "
362 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700363 result.j = 0;
364 }
buzbeec143c552011-08-20 17:38:58 -0700365
Ian Rogers6de08602011-08-19 14:52:39 -0700366 // Pop transition
367 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700368 return result;
369}
370
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700371JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700372 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700373 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700374 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700375 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700376 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700377}
378
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700379JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700380 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700381 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700383 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700384 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
385}
386
387static Method* FindVirtualMethod(Object* receiver, Method* method) {
388 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
389}
390
391JValue 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
398JValue 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 Shapiroea4dca82011-08-01 13:45:38 -0700403}
404
Elliott Hughes6b436852011-08-12 10:16:44 -0700405// 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").
410std::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 Hughesa5b897e2011-08-16 11:33:06 -0700421 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 Hughes6b436852011-08-12 10:16:44 -0700425 }
426 return result;
427}
428
Elliott Hughescdf53122011-08-19 15:46:09 -0700429jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
430 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700431 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
432 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700433 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700434
435 Method* method = NULL;
436 if (is_static) {
437 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700438 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700439 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 Rogers4dd71f12011-08-16 14:16:02 -0700444 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700445 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700446
Elliott Hughescdf53122011-08-19 15:46:09 -0700447 if (method == NULL || method->IsStatic() != is_static) {
448 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700449 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700450 // TODO: try searching for the opposite kind of method from is_static
451 // for better diagnostics?
452 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700453 "no %s method %s", is_static ? "static" : "non-static",
454 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700455 return NULL;
456 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700457
Elliott Hughescdf53122011-08-19 15:46:09 -0700458 bool success = EnsureInvokeStub(method);
459 if (!success) {
460 // TODO: throw OutOfMemoryException
461 return NULL;
462 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700463
Elliott Hughescdf53122011-08-19 15:46:09 -0700464 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700465}
466
Elliott Hughescdf53122011-08-19 15:46:09 -0700467jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
468 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700469 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
470 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700471 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700472
473 Field* field = NULL;
474 if (is_static) {
475 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700476 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700477 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700478 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700479
Elliott Hughescdf53122011-08-19 15:46:09 -0700480 if (field == NULL) {
481 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700482 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700483 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
484 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700485 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700486 return NULL;
487 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700488
Elliott Hughescdf53122011-08-19 15:46:09 -0700489 jweak fid = AddWeakGlobalReference(ts, field);
490 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700491}
492
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700493template<typename JniT, typename ArtT>
494JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
495 CHECK_GE(length, 0); // TODO: ReportJniError
496 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700497 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700498}
499
Elliott Hughes814e4032011-08-23 12:07:56 -0700500void 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}
506
507template <typename JavaArrayT, typename JavaT, typename ArrayT>
508static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
509 ArrayT* array = Decode<ArrayT*>(ts, java_array);
510 if (start < 0 || length < 0 || start + length > array->GetLength()) {
511 ThrowAIOOBE(ts, array, start, length, "src");
512 } else {
513 JavaT* data = array->GetData();
514 memcpy(buf, data + start, length * sizeof(JavaT));
515 }
516}
517
518template <typename JavaArrayT, typename JavaT, typename ArrayT>
519static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
520 ArrayT* array = Decode<ArrayT*>(ts, java_array);
521 if (start < 0 || length < 0 || start + length > array->GetLength()) {
522 ThrowAIOOBE(ts, array, start, length, "dst");
523 } else {
524 JavaT* data = array->GetData();
525 memcpy(data + start, buf, length * sizeof(JavaT));
526 }
527}
528
Elliott Hughescdf53122011-08-19 15:46:09 -0700529} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700530
Elliott Hughescdf53122011-08-19 15:46:09 -0700531class JNI {
532 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700533
Elliott Hughescdf53122011-08-19 15:46:09 -0700534 static jint GetVersion(JNIEnv* env) {
535 ScopedJniThreadState ts(env);
536 return JNI_VERSION_1_6;
537 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700538
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
540 ScopedJniThreadState ts(env);
541 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700542 return NULL;
543 }
544
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 static jclass FindClass(JNIEnv* env, const char* name) {
546 ScopedJniThreadState ts(env);
547 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
548 std::string descriptor(NormalizeJniClassDescriptor(name));
549 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700550 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700551 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700552 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700553 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700554
Elliott Hughescdf53122011-08-19 15:46:09 -0700555 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
556 ScopedJniThreadState ts(env);
557 Method* method = Decode<Method*>(ts, java_method);
558 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700559 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700560
Elliott Hughescdf53122011-08-19 15:46:09 -0700561 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
562 ScopedJniThreadState ts(env);
563 Field* field = Decode<Field*>(ts, java_field);
564 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
565 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700566
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
568 ScopedJniThreadState ts(env);
569 Method* method = DecodeMethod(ts, mid);
570 return AddLocalReference<jobject>(ts, method);
571 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Elliott Hughescdf53122011-08-19 15:46:09 -0700573 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
574 ScopedJniThreadState ts(env);
575 Field* field = DecodeField(ts, fid);
576 return AddLocalReference<jobject>(ts, field);
577 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700578
Elliott Hughes37f7a402011-08-22 18:56:01 -0700579 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
580 ScopedJniThreadState ts(env);
581 Object* o = Decode<Object*>(ts, java_object);
582 return AddLocalReference<jclass>(ts, o->GetClass());
583 }
584
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700585 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700587 Class* c = Decode<Class*>(ts, java_class);
588 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700590
Elliott Hughes37f7a402011-08-22 18:56:01 -0700591 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700592 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700593 Class* c1 = Decode<Class*>(ts, java_class1);
594 Class* c2 = Decode<Class*>(ts, java_class2);
595 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700596 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700597
Elliott Hughes37f7a402011-08-22 18:56:01 -0700598 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700600 CHECK_NE(static_cast<jclass>(NULL), clazz);
601 if (jobj == NULL) {
602 // NB. JNI is different from regular Java instanceof in this respect
603 return JNI_TRUE;
604 } else {
605 Object* obj = Decode<Object*>(ts, jobj);
606 Class* klass = Decode<Class*>(ts, clazz);
607 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
608 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700610
Elliott Hughes37f7a402011-08-22 18:56:01 -0700611 static jint Throw(JNIEnv* env, jthrowable java_exception) {
612 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700613 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700614 if (exception == NULL) {
615 return JNI_ERR;
616 }
617 ts.Self()->SetException(exception);
618 return JNI_OK;
619 }
620
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700621 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700622 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700623 // TODO: check for a pending exception to decide what constructor to call.
624 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
625 if (mid == NULL) {
626 return JNI_ERR;
627 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700628 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
629 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700630 return JNI_ERR;
631 }
632
633 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700634 args[0].l = s.get();
635 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
636 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700637 return JNI_ERR;
638 }
639
Elliott Hughes72025e52011-08-23 17:50:30 -0700640 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700641 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700642 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700643
Elliott Hughes37f7a402011-08-22 18:56:01 -0700644 return JNI_OK;
645 }
646
647 static jboolean ExceptionCheck(JNIEnv* env) {
648 ScopedJniThreadState ts(env);
649 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
650 }
651
652 static void ExceptionClear(JNIEnv* env) {
653 ScopedJniThreadState ts(env);
654 ts.Self()->ClearException();
655 }
656
657 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700659
660 Thread* self = ts.Self();
661 Throwable* original_exception = self->GetException();
662 self->ClearException();
663
664 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
665 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
666 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
667 if (mid == NULL) {
668 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
669 << PrettyType(original_exception);
670 } else {
671 env->CallVoidMethod(exception.get(), mid);
672 if (self->IsExceptionPending()) {
673 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
674 << " thrown while calling printStackTrace";
675 self->ClearException();
676 }
677 }
678
679 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jthrowable ExceptionOccurred(JNIEnv* env) {
683 ScopedJniThreadState ts(env);
684 Object* exception = ts.Self()->GetException();
685 if (exception == NULL) {
686 return NULL;
687 } else {
688 // TODO: if adding a local reference failing causes the VM to abort
689 // then the following check will never occur.
690 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
691 if (localException == NULL) {
692 // We were unable to add a new local reference, and threw a new
693 // exception. We can't return "exception", because it's not a
694 // local reference. So we have to return NULL, indicating that
695 // there was no exception, even though it's pretty much raining
696 // exceptions in here.
697 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
698 }
699 return localException;
700 }
701 }
702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 static void FatalError(JNIEnv* env, const char* msg) {
704 ScopedJniThreadState ts(env);
705 LOG(FATAL) << "JNI FatalError called: " << msg;
706 }
707
708 static jint PushLocalFrame(JNIEnv* env, jint cap) {
709 ScopedJniThreadState ts(env);
710 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
711 return JNI_OK;
712 }
713
714 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
715 ScopedJniThreadState ts(env);
716 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
717 return res;
718 }
719
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
721 ScopedJniThreadState ts(env);
722 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
723 return 0;
724 }
725
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
727 ScopedJniThreadState ts(env);
728 if (obj == NULL) {
729 return NULL;
730 }
731
732 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
733 IndirectReferenceTable& globals = vm->globals;
734 MutexLock mu(vm->globals_lock);
735 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
736 return reinterpret_cast<jobject>(ref);
737 }
738
739 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
740 ScopedJniThreadState ts(env);
741 if (obj == NULL) {
742 return;
743 }
744
745 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
746 IndirectReferenceTable& globals = vm->globals;
747 MutexLock mu(vm->globals_lock);
748
749 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
750 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
751 << "failed to find entry";
752 }
753 }
754
755 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
756 ScopedJniThreadState ts(env);
757 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
758 }
759
760 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
761 ScopedJniThreadState ts(env);
762 if (obj == NULL) {
763 return;
764 }
765
766 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
767 IndirectReferenceTable& weak_globals = vm->weak_globals;
768 MutexLock mu(vm->weak_globals_lock);
769
770 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
771 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
772 << "failed to find entry";
773 }
774 }
775
776 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
777 ScopedJniThreadState ts(env);
778 if (obj == NULL) {
779 return NULL;
780 }
781
782 IndirectReferenceTable& locals = ts.Env()->locals;
783
784 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
785 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
786 return reinterpret_cast<jobject>(ref);
787 }
788
789 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
790 ScopedJniThreadState ts(env);
791 if (obj == NULL) {
792 return;
793 }
794
795 IndirectReferenceTable& locals = ts.Env()->locals;
796
797 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
798 if (!locals.Remove(cookie, obj)) {
799 // Attempting to delete a local reference that is not in the
800 // topmost local reference frame is a no-op. DeleteLocalRef returns
801 // void and doesn't throw any exceptions, but we should probably
802 // complain about it so the user will notice that things aren't
803 // going quite the way they expect.
804 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
805 << "failed to find entry";
806 }
807 }
808
809 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
810 ScopedJniThreadState ts(env);
811 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
812 ? JNI_TRUE : JNI_FALSE;
813 }
814
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700815 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700817 Class* c = Decode<Class*>(ts, java_class);
818 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
819 return NULL;
820 }
821 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 ScopedJniThreadState ts(env);
826 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 va_start(args, mid);
828 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 va_end(args);
830 return result;
831 }
832
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700835 Class* c = Decode<Class*>(ts, java_class);
836 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
837 return NULL;
838 }
839 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 return local_result;
843 }
844
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700847 Class* c = Decode<Class*>(ts, java_class);
848 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
849 return NULL;
850 }
851 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 return local_result;
855 }
856
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
858 ScopedJniThreadState ts(env);
859 return FindMethodID(ts, c, name, sig, false);
860 }
861
862 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
863 ScopedJniThreadState ts(env);
864 return FindMethodID(ts, c, name, sig, true);
865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700869 va_list ap;
870 va_start(ap, mid);
871 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
872 va_end(ap);
873 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
879 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
885 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 }
887
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 va_list ap;
891 va_start(ap, mid);
892 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
893 va_end(ap);
894 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 va_list ap;
910 va_start(ap, mid);
911 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
912 va_end(ap);
913 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 va_list ap;
929 va_start(ap, mid);
930 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
931 va_end(ap);
932 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 }
934
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 return InvokeVirtualWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 return InvokeVirtualWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 va_list ap;
948 va_start(ap, mid);
949 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
950 va_end(ap);
951 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 return InvokeVirtualWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 return InvokeVirtualWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_list ap;
967 va_start(ap, mid);
968 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
969 va_end(ap);
970 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 return InvokeVirtualWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 return InvokeVirtualWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_list ap;
986 va_start(ap, mid);
987 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
988 va_end(ap);
989 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 return InvokeVirtualWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 return InvokeVirtualWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_list ap;
1005 va_start(ap, mid);
1006 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1007 va_end(ap);
1008 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 return InvokeVirtualWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 return InvokeVirtualWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_list ap;
1024 va_start(ap, mid);
1025 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1026 va_end(ap);
1027 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 return InvokeVirtualWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 return InvokeVirtualWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_list ap;
1043 va_start(ap, mid);
1044 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1045 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
1058 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
1061 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_start(ap, mid);
1063 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1065 va_end(ap);
1066 return local_result;
1067 }
1068
1069 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 return AddLocalReference<jobject>(ts, result.l);
1074 }
1075
1076 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 return AddLocalReference<jobject>(ts, result.l);
1081 }
1082
1083 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
1086 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_start(ap, mid);
1088 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 va_end(ap);
1090 return result.z;
1091 }
1092
1093 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
1099 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
1105 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
1108 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_start(ap, mid);
1110 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 va_end(ap);
1112 return result.b;
1113 }
1114
1115 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
1121 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
1127 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
1130 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_start(ap, mid);
1132 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 va_end(ap);
1134 return result.c;
1135 }
1136
1137 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
1143 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
1149 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
1152 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_start(ap, mid);
1154 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 va_end(ap);
1156 return result.s;
1157 }
1158
1159 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
1165 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
1171 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
1174 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_start(ap, mid);
1176 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 va_end(ap);
1178 return result.i;
1179 }
1180
1181 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
1187 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
1196 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_start(ap, mid);
1198 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_end(ap);
1200 return result.j;
1201 }
1202
1203 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
1209 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
1215 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
1218 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 va_start(ap, mid);
1220 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 va_end(ap);
1222 return result.f;
1223 }
1224
1225 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
1231 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
1237 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
1240 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_start(ap, mid);
1242 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 va_end(ap);
1244 return result.d;
1245 }
1246
1247 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
1253 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
1262 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_start(ap, mid);
1264 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 va_end(ap);
1266 }
1267
1268 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jfieldID GetFieldID(JNIEnv* env,
1281 jclass c, const char* name, const char* sig) {
1282 ScopedJniThreadState ts(env);
1283 return FindFieldID(ts, c, name, sig, false);
1284 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001285
1286
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 static jfieldID GetStaticFieldID(JNIEnv* env,
1288 jclass c, const char* name, const char* sig) {
1289 ScopedJniThreadState ts(env);
1290 return FindFieldID(ts, c, name, sig, true);
1291 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001292
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001293 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001295 Object* o = Decode<Object*>(ts, obj);
1296 Field* f = DecodeField(ts, fid);
1297 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001299
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001300 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001302 Field* f = DecodeField(ts, fid);
1303 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001308 Object* o = Decode<Object*>(ts, java_object);
1309 Object* v = Decode<Object*>(ts, java_value);
1310 Field* f = DecodeField(ts, fid);
1311 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001314 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001316 Object* v = Decode<Object*>(ts, java_value);
1317 Field* f = DecodeField(ts, fid);
1318 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001321#define GET_PRIMITIVE_FIELD(fn, instance) \
1322 ScopedJniThreadState ts(env); \
1323 Object* o = Decode<Object*>(ts, instance); \
1324 Field* f = DecodeField(ts, fid); \
1325 return f->fn(o)
1326
1327#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1328 ScopedJniThreadState ts(env); \
1329 Object* o = Decode<Object*>(ts, instance); \
1330 Field* f = DecodeField(ts, fid); \
1331 f->fn(o, value)
1332
1333 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1334 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001337 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1338 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001341 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1342 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001345 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1346 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1350 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1354 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1358 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1362 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1366 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1370 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1374 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1378 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1382 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1386 GET_PRIMITIVE_FIELD(GetLong, NULL);
1387 }
1388
1389 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1390 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1391 }
1392
1393 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1394 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1395 }
1396
1397 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1398 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1399 }
1400
1401 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1402 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1403 }
1404
1405 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1406 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1407 }
1408
1409 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1410 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1411 }
1412
1413 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1414 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1415 }
1416
1417 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1418 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1419 }
1420
1421 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1422 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1423 }
1424
1425 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1426 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1427 }
1428
1429 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1430 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1431 }
1432
1433 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1434 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1435 }
1436
1437 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1438 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1439 }
1440
1441 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1442 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1443 }
1444
1445 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1446 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1447 }
1448
1449 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1450 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1451 }
1452
1453 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1454 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1455 }
1456
1457 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1458 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
1461 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
1464 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001465 va_start(ap, mid);
1466 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1468 va_end(ap);
1469 return local_result;
1470 }
1471
1472 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001473 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001475 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 return AddLocalReference<jobject>(ts, result.l);
1477 }
1478
1479 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001480 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001482 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 return AddLocalReference<jobject>(ts, result.l);
1484 }
1485
1486 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001487 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 ScopedJniThreadState ts(env);
1489 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 va_start(ap, mid);
1491 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 va_end(ap);
1493 return result.z;
1494 }
1495
1496 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001497 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001499 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
1502 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001503 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001505 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes72025e52011-08-23 17:50:30 -07001508 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 ScopedJniThreadState ts(env);
1510 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001511 va_start(ap, mid);
1512 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 va_end(ap);
1514 return result.b;
1515 }
1516
1517 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001518 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001520 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
1523 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001524 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001526 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes72025e52011-08-23 17:50:30 -07001529 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 ScopedJniThreadState ts(env);
1531 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001532 va_start(ap, mid);
1533 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 va_end(ap);
1535 return result.c;
1536 }
1537
1538 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001541 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
1544 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001545 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 ScopedJniThreadState ts(env);
1552 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001553 va_start(ap, mid);
1554 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_end(ap);
1556 return result.s;
1557 }
1558
1559 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001562 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
1565 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes72025e52011-08-23 17:50:30 -07001571 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 ScopedJniThreadState ts(env);
1573 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 va_start(ap, mid);
1575 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_end(ap);
1577 return result.i;
1578 }
1579
1580 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001581 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
1586 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 ScopedJniThreadState ts(env);
1594 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 va_start(ap, mid);
1596 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_end(ap);
1598 return result.j;
1599 }
1600
1601 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
1607 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
1615 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 va_start(ap, mid);
1617 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 va_end(ap);
1619 return result.f;
1620 }
1621
1622 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
1628 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
1636 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 va_start(ap, mid);
1638 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 va_end(ap);
1640 return result.d;
1641 }
1642
1643 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
1649 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
1657 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 va_start(ap, mid);
1659 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 va_end(ap);
1661 }
1662
1663 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
1669 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes814e4032011-08-23 12:07:56 -07001675 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001677 if (chars == NULL && char_count == 0) {
1678 return NULL;
1679 }
1680 String* result = String::AllocFromUtf16(char_count, chars);
1681 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
1684 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1685 ScopedJniThreadState ts(env);
1686 if (utf == NULL) {
1687 return NULL;
1688 }
1689 String* result = String::AllocFromModifiedUtf8(utf);
1690 return AddLocalReference<jstring>(ts, result);
1691 }
1692
Elliott Hughes814e4032011-08-23 12:07:56 -07001693 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1694 ScopedJniThreadState ts(env);
1695 return Decode<String*>(ts, java_string)->GetLength();
1696 }
1697
1698 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1699 ScopedJniThreadState ts(env);
1700 return Decode<String*>(ts, java_string)->GetUtfLength();
1701 }
1702
1703 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
1705 UNIMPLEMENTED(FATAL);
Elliott Hughes814e4032011-08-23 12:07:56 -07001706 }
1707
1708 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1709 ScopedJniThreadState ts(env);
1710 UNIMPLEMENTED(FATAL);
1711 }
1712
1713 static const jchar* GetStringChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1714 ScopedJniThreadState ts(env);
1715 UNIMPLEMENTED(FATAL);
1716 return NULL;
1717 }
1718
1719 static void ReleaseStringChars(JNIEnv* env, jstring str, const jchar* chars) {
1720 ScopedJniThreadState ts(env);
1721 UNIMPLEMENTED(FATAL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
1724 static const char* GetStringUTFChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1725 ScopedJniThreadState ts(env);
1726 UNIMPLEMENTED(FATAL);
1727 return NULL;
1728 }
1729
1730 static void ReleaseStringUTFChars(JNIEnv* env, jstring str, const char* chars) {
1731 ScopedJniThreadState ts(env);
1732 UNIMPLEMENTED(FATAL);
1733 }
1734
Elliott Hughesbd935992011-08-22 11:59:34 -07001735 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001737 Object* obj = Decode<Object*>(ts, java_array);
1738 CHECK(obj->IsArray()); // TODO: ReportJniError
1739 Array* array = obj->AsArray();
1740 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes814e4032011-08-23 12:07:56 -07001743 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001745 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1746 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
1749 static void SetObjectArrayElement(JNIEnv* env,
1750 jobjectArray java_array, jsize index, jobject java_value) {
1751 ScopedJniThreadState ts(env);
1752 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1753 Object* value = Decode<Object*>(ts, java_value);
1754 array->Set(index, value);
1755 }
1756
1757 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1758 ScopedJniThreadState ts(env);
1759 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1760 }
1761
1762 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1763 ScopedJniThreadState ts(env);
1764 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1765 }
1766
1767 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1768 ScopedJniThreadState ts(env);
1769 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1770 }
1771
1772 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1773 ScopedJniThreadState ts(env);
1774 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1775 }
1776
1777 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1778 ScopedJniThreadState ts(env);
1779 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1780 }
1781
1782 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1783 ScopedJniThreadState ts(env);
1784 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1785 }
1786
1787 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1788 ScopedJniThreadState ts(env);
1789 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1790 }
1791
1792 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1793 ScopedJniThreadState ts(env);
1794 CHECK_GE(length, 0); // TODO: ReportJniError
1795
1796 // Compute the array class corresponding to the given element class.
1797 Class* element_class = Decode<Class*>(ts, element_jclass);
1798 std::string descriptor;
1799 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001800 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801
1802 // Find the class.
1803 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1804 // TODO: need to get the appropriate ClassLoader.
1805 Class* array_class = class_linker->FindClass(descriptor, NULL);
1806 if (array_class == NULL) {
1807 return NULL;
1808 }
1809
1810 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1811 CHECK(initial_element == NULL); // TODO: support initial_element
1812 return AddLocalReference<jobjectArray>(ts, result);
1813 }
1814
1815 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1816 ScopedJniThreadState ts(env);
1817 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1818 }
1819
1820 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1821 jbooleanArray array, jboolean* isCopy) {
1822 ScopedJniThreadState ts(env);
1823 UNIMPLEMENTED(FATAL);
1824 return NULL;
1825 }
1826
1827 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1828 ScopedJniThreadState ts(env);
1829 UNIMPLEMENTED(FATAL);
1830 return NULL;
1831 }
1832
1833 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1834 ScopedJniThreadState ts(env);
1835 UNIMPLEMENTED(FATAL);
1836 return NULL;
1837 }
1838
1839 static jshort* GetShortArrayElements(JNIEnv* env,
1840 jshortArray array, jboolean* isCopy) {
1841 ScopedJniThreadState ts(env);
1842 UNIMPLEMENTED(FATAL);
1843 return NULL;
1844 }
1845
1846 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1847 ScopedJniThreadState ts(env);
1848 UNIMPLEMENTED(FATAL);
1849 return NULL;
1850 }
1851
1852 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1853 ScopedJniThreadState ts(env);
1854 UNIMPLEMENTED(FATAL);
1855 return NULL;
1856 }
1857
1858 static jfloat* GetFloatArrayElements(JNIEnv* env,
1859 jfloatArray array, jboolean* isCopy) {
1860 ScopedJniThreadState ts(env);
1861 UNIMPLEMENTED(FATAL);
1862 return NULL;
1863 }
1864
1865 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1866 jdoubleArray array, jboolean* isCopy) {
1867 ScopedJniThreadState ts(env);
1868 UNIMPLEMENTED(FATAL);
1869 return NULL;
1870 }
1871
1872 static void ReleaseBooleanArrayElements(JNIEnv* env,
1873 jbooleanArray array, jboolean* elems, jint mode) {
1874 ScopedJniThreadState ts(env);
1875 UNIMPLEMENTED(FATAL);
1876 }
1877
1878 static void ReleaseByteArrayElements(JNIEnv* env,
1879 jbyteArray array, jbyte* elems, jint mode) {
1880 ScopedJniThreadState ts(env);
1881 UNIMPLEMENTED(FATAL);
1882 }
1883
1884 static void ReleaseCharArrayElements(JNIEnv* env,
1885 jcharArray array, jchar* elems, jint mode) {
1886 ScopedJniThreadState ts(env);
1887 UNIMPLEMENTED(FATAL);
1888 }
1889
1890 static void ReleaseShortArrayElements(JNIEnv* env,
1891 jshortArray array, jshort* elems, jint mode) {
1892 ScopedJniThreadState ts(env);
1893 UNIMPLEMENTED(FATAL);
1894 }
1895
1896 static void ReleaseIntArrayElements(JNIEnv* env,
1897 jintArray array, jint* elems, jint mode) {
1898 ScopedJniThreadState ts(env);
1899 UNIMPLEMENTED(FATAL);
1900 }
1901
1902 static void ReleaseLongArrayElements(JNIEnv* env,
1903 jlongArray array, jlong* elems, jint mode) {
1904 ScopedJniThreadState ts(env);
1905 UNIMPLEMENTED(FATAL);
1906 }
1907
1908 static void ReleaseFloatArrayElements(JNIEnv* env,
1909 jfloatArray array, jfloat* elems, jint mode) {
1910 ScopedJniThreadState ts(env);
1911 UNIMPLEMENTED(FATAL);
1912 }
1913
1914 static void ReleaseDoubleArrayElements(JNIEnv* env,
1915 jdoubleArray array, jdouble* elems, jint mode) {
1916 ScopedJniThreadState ts(env);
1917 UNIMPLEMENTED(FATAL);
1918 }
1919
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001922 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes814e4032011-08-23 12:07:56 -07001925 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001927 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes814e4032011-08-23 12:07:56 -07001930 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001932 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes814e4032011-08-23 12:07:56 -07001935 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes814e4032011-08-23 12:07:56 -07001940 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes814e4032011-08-23 12:07:56 -07001945 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes814e4032011-08-23 12:07:56 -07001950 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001952 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001967 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001972 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001977 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001982 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001997 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes5174fe62011-08-23 15:12:35 -07002000 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002002 Class* c = Decode<Class*>(ts, java_class);
2003
2004 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2005
2006 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 const char* name = methods[i].name;
2008 const char* sig = methods[i].signature;
2009
2010 if (*sig == '!') {
2011 // TODO: fast jni. it's too noisy to log all these.
2012 ++sig;
2013 }
2014
Elliott Hughes5174fe62011-08-23 15:12:35 -07002015 Method* m = c->FindDirectMethod(name, sig);
2016 if (m == NULL) {
2017 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002019 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002021 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2023 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002024 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002026 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002028 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2030 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002031 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 return JNI_ERR;
2033 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002034
2035 if (vm->verbose_jni) {
2036 LOG(INFO) << "[Registering JNI native method "
2037 << PrettyMethod(m, true) << "]";
2038 }
2039
2040 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042 return JNI_OK;
2043 }
2044
Elliott Hughes5174fe62011-08-23 15:12:35 -07002045 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002047 Class* c = Decode<Class*>(ts, java_class);
2048
2049 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2050 if (vm->verbose_jni) {
2051 LOG(INFO) << "[Unregistering JNI native methods for "
2052 << PrettyDescriptor(c->GetDescriptor()) << "]";
2053 }
2054
2055 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2056 Method* m = c->GetDirectMethod(i);
2057 if (m->IsNative()) {
2058 m->UnregisterNative();
2059 }
2060 }
2061 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2062 Method* m = c->GetVirtualMethod(i);
2063 if (m->IsNative()) {
2064 m->UnregisterNative();
2065 }
2066 }
2067
2068 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes72025e52011-08-23 17:50:30 -07002071 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002073 Decode<Object*>(ts, java_object)->MonitorEnter();
2074 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes72025e52011-08-23 17:50:30 -07002077 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002079 Decode<Object*>(ts, java_object)->MonitorEnter();
2080 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
2083 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2084 ScopedJniThreadState ts(env);
2085 Runtime* runtime = Runtime::Current();
2086 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002087 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 } else {
2089 *vm = NULL;
2090 }
2091 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2092 }
2093
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 static void* GetPrimitiveArrayCritical(JNIEnv* env,
2095 jarray array, jboolean* isCopy) {
2096 ScopedJniThreadState ts(env);
2097 UNIMPLEMENTED(FATAL);
2098 return NULL;
2099 }
2100
2101 static void ReleasePrimitiveArrayCritical(JNIEnv* env,
2102 jarray array, void* carray, jint mode) {
2103 ScopedJniThreadState ts(env);
2104 UNIMPLEMENTED(FATAL);
2105 }
2106
2107 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
2108 ScopedJniThreadState ts(env);
2109 UNIMPLEMENTED(FATAL);
2110 return NULL;
2111 }
2112
2113 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
2114 ScopedJniThreadState ts(env);
2115 UNIMPLEMENTED(FATAL);
2116 }
2117
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2119 ScopedJniThreadState ts(env);
2120 UNIMPLEMENTED(FATAL);
2121 return NULL;
2122 }
2123
2124 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
2125 ScopedJniThreadState ts(env);
2126 UNIMPLEMENTED(FATAL);
2127 return NULL;
2128 }
2129
2130 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
2131 ScopedJniThreadState ts(env);
2132 UNIMPLEMENTED(FATAL);
2133 return 0;
2134 }
2135
2136 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2137 ScopedJniThreadState ts(env);
2138 UNIMPLEMENTED(FATAL);
2139 return JNIInvalidRefType;
2140 }
2141};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002142
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002143static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002144 NULL, // reserved0.
2145 NULL, // reserved1.
2146 NULL, // reserved2.
2147 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 JNI::GetVersion,
2149 JNI::DefineClass,
2150 JNI::FindClass,
2151 JNI::FromReflectedMethod,
2152 JNI::FromReflectedField,
2153 JNI::ToReflectedMethod,
2154 JNI::GetSuperclass,
2155 JNI::IsAssignableFrom,
2156 JNI::ToReflectedField,
2157 JNI::Throw,
2158 JNI::ThrowNew,
2159 JNI::ExceptionOccurred,
2160 JNI::ExceptionDescribe,
2161 JNI::ExceptionClear,
2162 JNI::FatalError,
2163 JNI::PushLocalFrame,
2164 JNI::PopLocalFrame,
2165 JNI::NewGlobalRef,
2166 JNI::DeleteGlobalRef,
2167 JNI::DeleteLocalRef,
2168 JNI::IsSameObject,
2169 JNI::NewLocalRef,
2170 JNI::EnsureLocalCapacity,
2171 JNI::AllocObject,
2172 JNI::NewObject,
2173 JNI::NewObjectV,
2174 JNI::NewObjectA,
2175 JNI::GetObjectClass,
2176 JNI::IsInstanceOf,
2177 JNI::GetMethodID,
2178 JNI::CallObjectMethod,
2179 JNI::CallObjectMethodV,
2180 JNI::CallObjectMethodA,
2181 JNI::CallBooleanMethod,
2182 JNI::CallBooleanMethodV,
2183 JNI::CallBooleanMethodA,
2184 JNI::CallByteMethod,
2185 JNI::CallByteMethodV,
2186 JNI::CallByteMethodA,
2187 JNI::CallCharMethod,
2188 JNI::CallCharMethodV,
2189 JNI::CallCharMethodA,
2190 JNI::CallShortMethod,
2191 JNI::CallShortMethodV,
2192 JNI::CallShortMethodA,
2193 JNI::CallIntMethod,
2194 JNI::CallIntMethodV,
2195 JNI::CallIntMethodA,
2196 JNI::CallLongMethod,
2197 JNI::CallLongMethodV,
2198 JNI::CallLongMethodA,
2199 JNI::CallFloatMethod,
2200 JNI::CallFloatMethodV,
2201 JNI::CallFloatMethodA,
2202 JNI::CallDoubleMethod,
2203 JNI::CallDoubleMethodV,
2204 JNI::CallDoubleMethodA,
2205 JNI::CallVoidMethod,
2206 JNI::CallVoidMethodV,
2207 JNI::CallVoidMethodA,
2208 JNI::CallNonvirtualObjectMethod,
2209 JNI::CallNonvirtualObjectMethodV,
2210 JNI::CallNonvirtualObjectMethodA,
2211 JNI::CallNonvirtualBooleanMethod,
2212 JNI::CallNonvirtualBooleanMethodV,
2213 JNI::CallNonvirtualBooleanMethodA,
2214 JNI::CallNonvirtualByteMethod,
2215 JNI::CallNonvirtualByteMethodV,
2216 JNI::CallNonvirtualByteMethodA,
2217 JNI::CallNonvirtualCharMethod,
2218 JNI::CallNonvirtualCharMethodV,
2219 JNI::CallNonvirtualCharMethodA,
2220 JNI::CallNonvirtualShortMethod,
2221 JNI::CallNonvirtualShortMethodV,
2222 JNI::CallNonvirtualShortMethodA,
2223 JNI::CallNonvirtualIntMethod,
2224 JNI::CallNonvirtualIntMethodV,
2225 JNI::CallNonvirtualIntMethodA,
2226 JNI::CallNonvirtualLongMethod,
2227 JNI::CallNonvirtualLongMethodV,
2228 JNI::CallNonvirtualLongMethodA,
2229 JNI::CallNonvirtualFloatMethod,
2230 JNI::CallNonvirtualFloatMethodV,
2231 JNI::CallNonvirtualFloatMethodA,
2232 JNI::CallNonvirtualDoubleMethod,
2233 JNI::CallNonvirtualDoubleMethodV,
2234 JNI::CallNonvirtualDoubleMethodA,
2235 JNI::CallNonvirtualVoidMethod,
2236 JNI::CallNonvirtualVoidMethodV,
2237 JNI::CallNonvirtualVoidMethodA,
2238 JNI::GetFieldID,
2239 JNI::GetObjectField,
2240 JNI::GetBooleanField,
2241 JNI::GetByteField,
2242 JNI::GetCharField,
2243 JNI::GetShortField,
2244 JNI::GetIntField,
2245 JNI::GetLongField,
2246 JNI::GetFloatField,
2247 JNI::GetDoubleField,
2248 JNI::SetObjectField,
2249 JNI::SetBooleanField,
2250 JNI::SetByteField,
2251 JNI::SetCharField,
2252 JNI::SetShortField,
2253 JNI::SetIntField,
2254 JNI::SetLongField,
2255 JNI::SetFloatField,
2256 JNI::SetDoubleField,
2257 JNI::GetStaticMethodID,
2258 JNI::CallStaticObjectMethod,
2259 JNI::CallStaticObjectMethodV,
2260 JNI::CallStaticObjectMethodA,
2261 JNI::CallStaticBooleanMethod,
2262 JNI::CallStaticBooleanMethodV,
2263 JNI::CallStaticBooleanMethodA,
2264 JNI::CallStaticByteMethod,
2265 JNI::CallStaticByteMethodV,
2266 JNI::CallStaticByteMethodA,
2267 JNI::CallStaticCharMethod,
2268 JNI::CallStaticCharMethodV,
2269 JNI::CallStaticCharMethodA,
2270 JNI::CallStaticShortMethod,
2271 JNI::CallStaticShortMethodV,
2272 JNI::CallStaticShortMethodA,
2273 JNI::CallStaticIntMethod,
2274 JNI::CallStaticIntMethodV,
2275 JNI::CallStaticIntMethodA,
2276 JNI::CallStaticLongMethod,
2277 JNI::CallStaticLongMethodV,
2278 JNI::CallStaticLongMethodA,
2279 JNI::CallStaticFloatMethod,
2280 JNI::CallStaticFloatMethodV,
2281 JNI::CallStaticFloatMethodA,
2282 JNI::CallStaticDoubleMethod,
2283 JNI::CallStaticDoubleMethodV,
2284 JNI::CallStaticDoubleMethodA,
2285 JNI::CallStaticVoidMethod,
2286 JNI::CallStaticVoidMethodV,
2287 JNI::CallStaticVoidMethodA,
2288 JNI::GetStaticFieldID,
2289 JNI::GetStaticObjectField,
2290 JNI::GetStaticBooleanField,
2291 JNI::GetStaticByteField,
2292 JNI::GetStaticCharField,
2293 JNI::GetStaticShortField,
2294 JNI::GetStaticIntField,
2295 JNI::GetStaticLongField,
2296 JNI::GetStaticFloatField,
2297 JNI::GetStaticDoubleField,
2298 JNI::SetStaticObjectField,
2299 JNI::SetStaticBooleanField,
2300 JNI::SetStaticByteField,
2301 JNI::SetStaticCharField,
2302 JNI::SetStaticShortField,
2303 JNI::SetStaticIntField,
2304 JNI::SetStaticLongField,
2305 JNI::SetStaticFloatField,
2306 JNI::SetStaticDoubleField,
2307 JNI::NewString,
2308 JNI::GetStringLength,
2309 JNI::GetStringChars,
2310 JNI::ReleaseStringChars,
2311 JNI::NewStringUTF,
2312 JNI::GetStringUTFLength,
2313 JNI::GetStringUTFChars,
2314 JNI::ReleaseStringUTFChars,
2315 JNI::GetArrayLength,
2316 JNI::NewObjectArray,
2317 JNI::GetObjectArrayElement,
2318 JNI::SetObjectArrayElement,
2319 JNI::NewBooleanArray,
2320 JNI::NewByteArray,
2321 JNI::NewCharArray,
2322 JNI::NewShortArray,
2323 JNI::NewIntArray,
2324 JNI::NewLongArray,
2325 JNI::NewFloatArray,
2326 JNI::NewDoubleArray,
2327 JNI::GetBooleanArrayElements,
2328 JNI::GetByteArrayElements,
2329 JNI::GetCharArrayElements,
2330 JNI::GetShortArrayElements,
2331 JNI::GetIntArrayElements,
2332 JNI::GetLongArrayElements,
2333 JNI::GetFloatArrayElements,
2334 JNI::GetDoubleArrayElements,
2335 JNI::ReleaseBooleanArrayElements,
2336 JNI::ReleaseByteArrayElements,
2337 JNI::ReleaseCharArrayElements,
2338 JNI::ReleaseShortArrayElements,
2339 JNI::ReleaseIntArrayElements,
2340 JNI::ReleaseLongArrayElements,
2341 JNI::ReleaseFloatArrayElements,
2342 JNI::ReleaseDoubleArrayElements,
2343 JNI::GetBooleanArrayRegion,
2344 JNI::GetByteArrayRegion,
2345 JNI::GetCharArrayRegion,
2346 JNI::GetShortArrayRegion,
2347 JNI::GetIntArrayRegion,
2348 JNI::GetLongArrayRegion,
2349 JNI::GetFloatArrayRegion,
2350 JNI::GetDoubleArrayRegion,
2351 JNI::SetBooleanArrayRegion,
2352 JNI::SetByteArrayRegion,
2353 JNI::SetCharArrayRegion,
2354 JNI::SetShortArrayRegion,
2355 JNI::SetIntArrayRegion,
2356 JNI::SetLongArrayRegion,
2357 JNI::SetFloatArrayRegion,
2358 JNI::SetDoubleArrayRegion,
2359 JNI::RegisterNatives,
2360 JNI::UnregisterNatives,
2361 JNI::MonitorEnter,
2362 JNI::MonitorExit,
2363 JNI::GetJavaVM,
2364 JNI::GetStringRegion,
2365 JNI::GetStringUTFRegion,
2366 JNI::GetPrimitiveArrayCritical,
2367 JNI::ReleasePrimitiveArrayCritical,
2368 JNI::GetStringCritical,
2369 JNI::ReleaseStringCritical,
2370 JNI::NewWeakGlobalRef,
2371 JNI::DeleteWeakGlobalRef,
2372 JNI::ExceptionCheck,
2373 JNI::NewDirectByteBuffer,
2374 JNI::GetDirectBufferAddress,
2375 JNI::GetDirectBufferCapacity,
2376 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002377};
2378
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002379static const size_t kMonitorsInitial = 32; // Arbitrary.
2380static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2381
2382static const size_t kLocalsInitial = 64; // Arbitrary.
2383static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002384
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002385JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002386 : self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002387 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002388 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002389 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2390 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002391 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002392}
2393
Carl Shapiroea4dca82011-08-01 13:45:38 -07002394// JNI Invocation interface.
2395
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002396extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2397 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2398 if (args->version < JNI_VERSION_1_2) {
2399 return JNI_EVERSION;
2400 }
2401 Runtime::Options options;
2402 for (int i = 0; i < args->nOptions; ++i) {
2403 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002404 options.push_back(std::make_pair(StringPiece(option->optionString),
2405 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002406 }
2407 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002408 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002409 if (runtime == NULL) {
2410 return JNI_ERR;
2411 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002412 *p_env = Thread::Current()->GetJniEnv();
2413 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002414 return JNI_OK;
2415 }
2416}
2417
Elliott Hughesf2682d52011-08-15 16:37:04 -07002418extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002419 Runtime* runtime = Runtime::Current();
2420 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002421 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002422 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002423 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002424 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002425 }
2426 return JNI_OK;
2427}
2428
2429// Historically unsupported.
2430extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2431 return JNI_ERR;
2432}
2433
Elliott Hughescdf53122011-08-19 15:46:09 -07002434class JII {
2435 public:
2436 static jint DestroyJavaVM(JavaVM* vm) {
2437 if (vm == NULL) {
2438 return JNI_ERR;
2439 } else {
2440 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2441 delete raw_vm->runtime;
2442 return JNI_OK;
2443 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002444 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002445
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2447 if (vm == NULL || p_env == NULL) {
2448 return JNI_ERR;
2449 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002450 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2451 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002452 const char* name = NULL;
2453 if (thr_args != NULL) {
2454 // TODO: check version
2455 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2456 // TODO: thread group
2457 }
2458 bool success = runtime->AttachCurrentThread(name, p_env);
2459 if (!success) {
2460 return JNI_ERR;
2461 } else {
2462 return JNI_OK;
2463 }
2464 }
2465
2466 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2467 if (vm == NULL || p_env == NULL) {
2468 return JNI_ERR;
2469 }
2470 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2471 Runtime* runtime = raw_vm->runtime;
2472 const char* name = NULL;
2473 if (thr_args != NULL) {
2474 // TODO: check version
2475 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2476 // TODO: thread group
2477 }
2478 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2479 if (!success) {
2480 return JNI_ERR;
2481 } else {
2482 return JNI_OK;
2483 }
2484 }
2485
2486 static jint DetachCurrentThread(JavaVM* vm) {
2487 if (vm == NULL) {
2488 return JNI_ERR;
2489 } else {
2490 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2491 Runtime* runtime = raw_vm->runtime;
2492 runtime->DetachCurrentThread();
2493 return JNI_OK;
2494 }
2495 }
2496
2497 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2498 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2499 return JNI_EVERSION;
2500 }
2501 if (vm == NULL || env == NULL) {
2502 return JNI_ERR;
2503 }
2504 Thread* thread = Thread::Current();
2505 if (thread == NULL) {
2506 *env = NULL;
2507 return JNI_EDETACHED;
2508 }
2509 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002510 return JNI_OK;
2511 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002512};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002513
Elliott Hughesf2682d52011-08-15 16:37:04 -07002514struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002515 NULL, // reserved0
2516 NULL, // reserved1
2517 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002518 JII::DestroyJavaVM,
2519 JII::AttachCurrentThread,
2520 JII::DetachCurrentThread,
2521 JII::GetEnv,
2522 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002523};
2524
Elliott Hughesbbd76712011-08-17 10:25:24 -07002525static const size_t kPinTableInitialSize = 16;
2526static const size_t kPinTableMaxSize = 1024;
2527
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002528static const size_t kGlobalsInitial = 512; // Arbitrary.
2529static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2530
2531static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2532static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2533
Elliott Hughes0af55432011-08-17 18:37:28 -07002534JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002535 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002536 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002537 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002538 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002539 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002540 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002541 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002542 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002543 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002544}
2545
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002546JavaVMExt::~JavaVMExt() {
2547 delete globals_lock;
2548 delete weak_globals_lock;
2549}
2550
Elliott Hughescdf53122011-08-19 15:46:09 -07002551/*
2552 * Load native code from the specified absolute pathname. Per the spec,
2553 * if we've already loaded a library with the specified pathname, we
2554 * return without doing anything.
2555 *
2556 * TODO? for better results we should absolutify the pathname. For fully
2557 * correct results we should stat to get the inode and compare that. The
2558 * existing implementation is fine so long as everybody is using
2559 * System.loadLibrary.
2560 *
2561 * The library will be associated with the specified class loader. The JNI
2562 * spec says we can't load the same library into more than one class loader.
2563 *
2564 * Returns "true" on success. On failure, sets *detail to a
2565 * human-readable description of the error or NULL if no detail is
2566 * available; ownership of the string is transferred to the caller.
2567 */
Elliott Hughes814e4032011-08-23 12:07:56 -07002568bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, char** detail) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002569 *detail = NULL;
2570
2571 // See if we've already loaded this library. If we have, and the class loader
2572 // matches, return successfully without doing anything.
2573 SharedLibrary* library = libraries[path];
2574 if (library != NULL) {
2575 if (library->GetClassLoader() != class_loader) {
2576 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2577 << "ClassLoader " << library->GetClassLoader() << "; "
2578 << "can't open in " << class_loader;
2579 *detail = strdup("already opened by different ClassLoader");
2580 return false;
2581 }
2582 if (verbose_jni) {
2583 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2584 << "ClassLoader " << class_loader << "]";
2585 }
2586 if (!library->CheckOnLoadResult(this)) {
2587 *detail = strdup("JNI_OnLoad failed before");
2588 return false;
2589 }
2590 return true;
2591 }
2592
2593 // Open the shared library. Because we're using a full path, the system
2594 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2595 // resolve this library's dependencies though.)
2596
2597 // Failures here are expected when java.library.path has several entries
2598 // and we have to hunt for the lib.
2599
2600 // The current version of the dynamic linker prints detailed information
2601 // about dlopen() failures. Some things to check if the message is
2602 // cryptic:
2603 // - make sure the library exists on the device
2604 // - verify that the right path is being opened (the debug log message
2605 // above can help with that)
2606 // - check to see if the library is valid (e.g. not zero bytes long)
2607 // - check config/prelink-linux-arm.map to ensure that the library
2608 // is listed and is not being overrun by the previous entry (if
2609 // loading suddenly stops working on a prelinked library, this is
2610 // a good one to check)
2611 // - write a trivial app that calls sleep() then dlopen(), attach
2612 // to it with "strace -p <pid>" while it sleeps, and watch for
2613 // attempts to open nonexistent dependent shared libs
2614
2615 // TODO: automate some of these checks!
2616
2617 // This can execute slowly for a large library on a busy system, so we
2618 // want to switch from RUNNING to VMWAIT while it executes. This allows
2619 // the GC to ignore us.
2620 Thread* self = Thread::Current();
2621 Thread::State old_state = self->GetState();
2622 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2623 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2624 self->SetState(old_state);
2625
2626 if (verbose_jni) {
2627 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2628 }
2629
2630 if (handle == NULL) {
2631 *detail = strdup(dlerror());
2632 return false;
2633 }
2634
2635 // Create a new entry.
2636 library = new SharedLibrary(path, handle, class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002637
2638 libraries[path] = library;
2639
2640 // if (pNewEntry != pActualEntry) {
2641 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2642 // freeSharedLibEntry(pNewEntry);
2643 // return CheckOnLoadResult(this, pActualEntry);
2644 // } else
2645 {
2646 if (verbose_jni) {
2647 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2648 }
2649
2650 bool result = true;
2651 void* sym = dlsym(handle, "JNI_OnLoad");
2652 if (sym == NULL) {
2653 if (verbose_jni) {
2654 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2655 }
2656 } else {
2657 // Call JNI_OnLoad. We have to override the current class
2658 // loader, which will always be "null" since the stuff at the
2659 // top of the stack is around Runtime.loadLibrary(). (See
2660 // the comments in the JNI FindClass function.)
Elliott Hughescdf53122011-08-19 15:46:09 -07002661 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2662 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Elliott Hughes814e4032011-08-23 12:07:56 -07002663 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2664 self->SetClassLoaderOverride(class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002665
2666 old_state = self->GetState();
2667 self->SetState(Thread::kNative);
2668 if (verbose_jni) {
2669 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2670 }
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002671 int version = (*jni_on_load)(this, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 self->SetState(old_state);
2673
Elliott Hughes814e4032011-08-23 12:07:56 -07002674 self->SetClassLoaderOverride(old_class_loader);;
Elliott Hughescdf53122011-08-19 15:46:09 -07002675
2676 if (version != JNI_VERSION_1_2 &&
2677 version != JNI_VERSION_1_4 &&
2678 version != JNI_VERSION_1_6) {
2679 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2680 << "bad version: " << version;
2681 // It's unwise to call dlclose() here, but we can mark it
2682 // as bad and ensure that future load attempts will fail.
2683 // We don't know how far JNI_OnLoad got, so there could
2684 // be some partially-initialized stuff accessible through
2685 // newly-registered native method calls. We could try to
2686 // unregister them, but that doesn't seem worthwhile.
2687 result = false;
2688 } else {
2689 if (verbose_jni) {
2690 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2691 << " from JNI_OnLoad in \"" << path << "\"]";
2692 }
2693 }
2694 }
2695
2696 library->SetResult(result);
2697 return result;
2698 }
2699}
2700
Ian Rogersdf20fe02011-07-20 20:34:16 -07002701} // namespace art