blob: ca40e598c7d11a16e7a0d925967f9f975bb31f7c [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) {
252 if (obj == NULL) {
253 return NULL;
254 }
255
256 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
257 IndirectRefKind kind = GetIndirectRefKind(ref);
258 Object* result;
259 switch (kind) {
260 case kLocal:
261 {
262 IndirectReferenceTable& locals = ts.Env()->locals;
263 result = locals.Get(ref);
264 break;
265 }
266 case kGlobal:
267 {
268 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
269 IndirectReferenceTable& globals = vm->globals;
Elliott Hughes18c07532011-08-18 15:50:51 -0700270 MutexLock mu(vm->globals_lock);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700271 result = globals.Get(ref);
272 break;
273 }
274 case kWeakGlobal:
275 {
276 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
277 IndirectReferenceTable& weak_globals = vm->weak_globals;
Elliott Hughes18c07532011-08-18 15:50:51 -0700278 MutexLock mu(vm->weak_globals_lock);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700279 result = weak_globals.Get(ref);
280 if (result == kClearedJniWeakGlobal) {
281 // This is a special case where it's okay to return NULL.
282 return NULL;
283 }
284 break;
285 }
286 case kInvalid:
287 default:
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700288 // TODO: make stack handle blocks more efficient
289 // Check if this is a local reference in a stack handle block
290 if (ts.Self()->ShbContains(obj)) {
291 return *reinterpret_cast<T*>(obj); // Read from stack handle block
292 }
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700293 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
294 // Assume an invalid local reference is actually a direct pointer.
295 return reinterpret_cast<T>(obj);
296 }
297 LOG(FATAL) << "Invalid indirect reference " << obj;
298 return reinterpret_cast<T>(kInvalidIndirectRefObject);
299 }
300
301 if (result == NULL) {
302 LOG(FATAL) << "JNI ERROR (app bug): use of deleted " << kind << ": "
303 << obj;
304 }
305 return reinterpret_cast<T>(result);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700306}
307
Elliott Hughescdf53122011-08-19 15:46:09 -0700308Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
309 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
310}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700311
Elliott Hughescdf53122011-08-19 15:46:09 -0700312Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
313 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700314}
315
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700316byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700317 size_t num_bytes = method->NumArgArrayBytes();
318 scoped_array<byte> arg_array(new byte[num_bytes]);
319 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700320 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700321 switch (shorty[i]) {
322 case 'Z':
323 case 'B':
324 case 'C':
325 case 'S':
326 case 'I':
327 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
328 offset += 4;
329 break;
330 case 'F':
331 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
332 offset += 4;
333 break;
334 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700335 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
337 offset += sizeof(Object*);
338 break;
339 }
340 case 'D':
341 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
342 offset += 8;
343 break;
344 case 'J':
345 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
346 offset += 8;
347 break;
348 }
349 }
350 return arg_array.release();
351}
352
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700353byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700354 size_t num_bytes = method->NumArgArrayBytes();
355 scoped_array<byte> arg_array(new byte[num_bytes]);
356 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700357 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700358 switch (shorty[i]) {
359 case 'Z':
360 case 'B':
361 case 'C':
362 case 'S':
363 case 'I':
364 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
365 offset += 4;
366 break;
367 case 'F':
368 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
369 offset += 4;
370 break;
371 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700372 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700373 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
374 offset += sizeof(Object*);
375 break;
376 }
377 case 'D':
378 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
379 offset += 8;
380 break;
381 case 'J':
382 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
383 offset += 8;
384 break;
385 }
386 }
387 return arg_array.release();
388}
389
Elliott Hughes72025e52011-08-23 17:50:30 -0700390JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
391 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700392 Thread* self = ts.Self();
393
394 // Push a transition back into managed code onto the linked list in thread
395 CHECK_EQ(Thread::kRunnable, self->GetState());
396 NativeToManagedRecord record;
397 self->PushNativeToManagedRecord(&record);
398
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700399 // Call the invoke stub associated with the method
400 // Pass everything as arguments
401 const Method::InvokeStub* stub = method->GetInvokeStub();
402 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700403
404#ifdef __arm__
405 // Compile...
406 // TODO: not here!
407 oatCompileMethod(method, kThumb2);
408#endif
409
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700410 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700411 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700412 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700413 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700414 LOG(WARNING) << "Not invoking method with no associated code: "
415 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700416 result.j = 0;
417 }
buzbeec143c552011-08-20 17:38:58 -0700418
Ian Rogers6de08602011-08-19 14:52:39 -0700419 // Pop transition
420 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700421 return result;
422}
423
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700424JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700425 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700426 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700427 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700428 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700429 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700430}
431
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700432JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700433 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700434 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700435 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700436 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700437 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
438}
439
440static Method* FindVirtualMethod(Object* receiver, Method* method) {
441 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
442}
443
444JValue InvokeVirtualWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
445 Object* receiver = Decode<Object*>(ts, obj);
446 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
447 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
448 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
449}
450
451JValue InvokeVirtualWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
452 Object* receiver = Decode<Object*>(ts, obj);
453 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
454 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
455 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700456}
457
Elliott Hughes6b436852011-08-12 10:16:44 -0700458// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
459// separated with slashes but aren't wrapped with "L;" like regular descriptors
460// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
461// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
462// supported names with dots too (such as "a.b.C").
463std::string NormalizeJniClassDescriptor(const char* name) {
464 std::string result;
465 // Add the missing "L;" if necessary.
466 if (name[0] == '[') {
467 result = name;
468 } else {
469 result += 'L';
470 result += name;
471 result += ';';
472 }
473 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700474 if (result.find('.') != std::string::npos) {
475 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
476 << "\"" << name << "\"";
477 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700478 }
479 return result;
480}
481
Elliott Hughescdf53122011-08-19 15:46:09 -0700482jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
483 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700484 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
485 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700486 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700487
488 Method* method = NULL;
489 if (is_static) {
490 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700491 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700492 method = c->FindVirtualMethod(name, sig);
493 if (method == NULL) {
494 // No virtual method matching the signature. Search declared
495 // private methods and constructors.
496 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700497 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700498 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700499
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 if (method == NULL || method->IsStatic() != is_static) {
501 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700502 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700503 // TODO: try searching for the opposite kind of method from is_static
504 // for better diagnostics?
505 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700506 "no %s method %s", is_static ? "static" : "non-static",
507 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700508 return NULL;
509 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700510
Elliott Hughescdf53122011-08-19 15:46:09 -0700511 bool success = EnsureInvokeStub(method);
512 if (!success) {
513 // TODO: throw OutOfMemoryException
514 return NULL;
515 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700516
Elliott Hughescdf53122011-08-19 15:46:09 -0700517 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700518}
519
Elliott Hughescdf53122011-08-19 15:46:09 -0700520jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
521 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700522 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
523 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700524 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700525
526 Field* field = NULL;
527 if (is_static) {
528 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700529 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700530 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700531 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700532
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 if (field == NULL) {
534 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700535 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700536 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
537 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700538 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700539 return NULL;
540 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700541
Elliott Hughescdf53122011-08-19 15:46:09 -0700542 jweak fid = AddWeakGlobalReference(ts, field);
543 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700544}
545
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700546template<typename JniT, typename ArtT>
547JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
548 CHECK_GE(length, 0); // TODO: ReportJniError
549 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700550 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700551}
552
Elliott Hughes814e4032011-08-23 12:07:56 -0700553void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
554 std::string type(PrettyType(array));
555 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
556 "%s offset=%d length=%d %s.length=%d",
557 type.c_str(), start, length, identifier, array->GetLength());
558}
559
560template <typename JavaArrayT, typename JavaT, typename ArrayT>
561static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
562 ArrayT* array = Decode<ArrayT*>(ts, java_array);
563 if (start < 0 || length < 0 || start + length > array->GetLength()) {
564 ThrowAIOOBE(ts, array, start, length, "src");
565 } else {
566 JavaT* data = array->GetData();
567 memcpy(buf, data + start, length * sizeof(JavaT));
568 }
569}
570
571template <typename JavaArrayT, typename JavaT, typename ArrayT>
572static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
573 ArrayT* array = Decode<ArrayT*>(ts, java_array);
574 if (start < 0 || length < 0 || start + length > array->GetLength()) {
575 ThrowAIOOBE(ts, array, start, length, "dst");
576 } else {
577 JavaT* data = array->GetData();
578 memcpy(data + start, buf, length * sizeof(JavaT));
579 }
580}
581
Elliott Hughescdf53122011-08-19 15:46:09 -0700582} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700583
Elliott Hughescdf53122011-08-19 15:46:09 -0700584class JNI {
585 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700586
Elliott Hughescdf53122011-08-19 15:46:09 -0700587 static jint GetVersion(JNIEnv* env) {
588 ScopedJniThreadState ts(env);
589 return JNI_VERSION_1_6;
590 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700591
Elliott Hughescdf53122011-08-19 15:46:09 -0700592 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
593 ScopedJniThreadState ts(env);
594 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700595 return NULL;
596 }
597
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 static jclass FindClass(JNIEnv* env, const char* name) {
599 ScopedJniThreadState ts(env);
600 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
601 std::string descriptor(NormalizeJniClassDescriptor(name));
602 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700603 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700604 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700605 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700606 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
609 ScopedJniThreadState ts(env);
610 Method* method = Decode<Method*>(ts, java_method);
611 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700612 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
615 ScopedJniThreadState ts(env);
616 Field* field = Decode<Field*>(ts, java_field);
617 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
621 ScopedJniThreadState ts(env);
622 Method* method = DecodeMethod(ts, mid);
623 return AddLocalReference<jobject>(ts, method);
624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
627 ScopedJniThreadState ts(env);
628 Field* field = DecodeField(ts, fid);
629 return AddLocalReference<jobject>(ts, field);
630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughes37f7a402011-08-22 18:56:01 -0700632 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
633 ScopedJniThreadState ts(env);
634 Object* o = Decode<Object*>(ts, java_object);
635 return AddLocalReference<jclass>(ts, o->GetClass());
636 }
637
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700638 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700640 Class* c = Decode<Class*>(ts, java_class);
641 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughes37f7a402011-08-22 18:56:01 -0700644 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700646 Class* c1 = Decode<Class*>(ts, java_class1);
647 Class* c2 = Decode<Class*>(ts, java_class2);
648 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughes37f7a402011-08-22 18:56:01 -0700651 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 CHECK_NE(static_cast<jclass>(NULL), clazz);
654 if (jobj == NULL) {
655 // NB. JNI is different from regular Java instanceof in this respect
656 return JNI_TRUE;
657 } else {
658 Object* obj = Decode<Object*>(ts, jobj);
659 Class* klass = Decode<Class*>(ts, clazz);
660 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
661 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 static jint Throw(JNIEnv* env, jthrowable java_exception) {
665 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700666 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 if (exception == NULL) {
668 return JNI_ERR;
669 }
670 ts.Self()->SetException(exception);
671 return JNI_OK;
672 }
673
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700674 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700676 // TODO: check for a pending exception to decide what constructor to call.
677 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
678 if (mid == NULL) {
679 return JNI_ERR;
680 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
682 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700683 return JNI_ERR;
684 }
685
686 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 args[0].l = s.get();
688 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
689 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700690 return JNI_ERR;
691 }
692
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700694 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700695 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700696
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 return JNI_OK;
698 }
699
700 static jboolean ExceptionCheck(JNIEnv* env) {
701 ScopedJniThreadState ts(env);
702 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
703 }
704
705 static void ExceptionClear(JNIEnv* env) {
706 ScopedJniThreadState ts(env);
707 ts.Self()->ClearException();
708 }
709
710 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700712
713 Thread* self = ts.Self();
714 Throwable* original_exception = self->GetException();
715 self->ClearException();
716
717 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
718 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
719 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
720 if (mid == NULL) {
721 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
722 << PrettyType(original_exception);
723 } else {
724 env->CallVoidMethod(exception.get(), mid);
725 if (self->IsExceptionPending()) {
726 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
727 << " thrown while calling printStackTrace";
728 self->ClearException();
729 }
730 }
731
732 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700734
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 static jthrowable ExceptionOccurred(JNIEnv* env) {
736 ScopedJniThreadState ts(env);
737 Object* exception = ts.Self()->GetException();
738 if (exception == NULL) {
739 return NULL;
740 } else {
741 // TODO: if adding a local reference failing causes the VM to abort
742 // then the following check will never occur.
743 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
744 if (localException == NULL) {
745 // We were unable to add a new local reference, and threw a new
746 // exception. We can't return "exception", because it's not a
747 // local reference. So we have to return NULL, indicating that
748 // there was no exception, even though it's pretty much raining
749 // exceptions in here.
750 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
751 }
752 return localException;
753 }
754 }
755
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 static void FatalError(JNIEnv* env, const char* msg) {
757 ScopedJniThreadState ts(env);
758 LOG(FATAL) << "JNI FatalError called: " << msg;
759 }
760
761 static jint PushLocalFrame(JNIEnv* env, jint cap) {
762 ScopedJniThreadState ts(env);
763 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
764 return JNI_OK;
765 }
766
767 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
768 ScopedJniThreadState ts(env);
769 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
770 return res;
771 }
772
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
774 ScopedJniThreadState ts(env);
775 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
776 return 0;
777 }
778
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
780 ScopedJniThreadState ts(env);
781 if (obj == NULL) {
782 return NULL;
783 }
784
785 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
786 IndirectReferenceTable& globals = vm->globals;
787 MutexLock mu(vm->globals_lock);
788 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
789 return reinterpret_cast<jobject>(ref);
790 }
791
792 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
793 ScopedJniThreadState ts(env);
794 if (obj == NULL) {
795 return;
796 }
797
798 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
799 IndirectReferenceTable& globals = vm->globals;
800 MutexLock mu(vm->globals_lock);
801
802 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
803 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
804 << "failed to find entry";
805 }
806 }
807
808 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
809 ScopedJniThreadState ts(env);
810 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
811 }
812
813 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
814 ScopedJniThreadState ts(env);
815 if (obj == NULL) {
816 return;
817 }
818
819 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
820 IndirectReferenceTable& weak_globals = vm->weak_globals;
821 MutexLock mu(vm->weak_globals_lock);
822
823 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
824 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
825 << "failed to find entry";
826 }
827 }
828
829 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
830 ScopedJniThreadState ts(env);
831 if (obj == NULL) {
832 return NULL;
833 }
834
835 IndirectReferenceTable& locals = ts.Env()->locals;
836
837 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
838 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
839 return reinterpret_cast<jobject>(ref);
840 }
841
842 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
843 ScopedJniThreadState ts(env);
844 if (obj == NULL) {
845 return;
846 }
847
848 IndirectReferenceTable& locals = ts.Env()->locals;
849
850 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
851 if (!locals.Remove(cookie, obj)) {
852 // Attempting to delete a local reference that is not in the
853 // topmost local reference frame is a no-op. DeleteLocalRef returns
854 // void and doesn't throw any exceptions, but we should probably
855 // complain about it so the user will notice that things aren't
856 // going quite the way they expect.
857 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
858 << "failed to find entry";
859 }
860 }
861
862 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
863 ScopedJniThreadState ts(env);
864 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
865 ? JNI_TRUE : JNI_FALSE;
866 }
867
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700868 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700870 Class* c = Decode<Class*>(ts, java_class);
871 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
872 return NULL;
873 }
874 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 ScopedJniThreadState ts(env);
879 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700880 va_start(args, mid);
881 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 va_end(args);
883 return result;
884 }
885
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700888 Class* c = Decode<Class*>(ts, java_class);
889 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
890 return NULL;
891 }
892 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 return local_result;
896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700900 Class* c = Decode<Class*>(ts, java_class);
901 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
902 return NULL;
903 }
904 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 return local_result;
908 }
909
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
911 ScopedJniThreadState ts(env);
912 return FindMethodID(ts, c, name, sig, false);
913 }
914
915 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
916 ScopedJniThreadState ts(env);
917 return FindMethodID(ts, c, name, sig, true);
918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 va_list ap;
923 va_start(ap, mid);
924 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
925 va_end(ap);
926 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
932 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 }
934
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
938 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 va_list ap;
944 va_start(ap, mid);
945 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
946 va_end(ap);
947 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 va_list ap;
963 va_start(ap, mid);
964 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
965 va_end(ap);
966 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
983 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
984 va_end(ap);
985 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 return InvokeVirtualWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 return InvokeVirtualWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 va_list ap;
1001 va_start(ap, mid);
1002 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1003 va_end(ap);
1004 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 return InvokeVirtualWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 return InvokeVirtualWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
1021 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1022 va_end(ap);
1023 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 return InvokeVirtualWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 return InvokeVirtualWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
1040 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1041 va_end(ap);
1042 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 return InvokeVirtualWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 return InvokeVirtualWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_list ap;
1058 va_start(ap, mid);
1059 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1060 va_end(ap);
1061 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 return InvokeVirtualWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 return InvokeVirtualWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_list ap;
1077 va_start(ap, mid);
1078 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1079 va_end(ap);
1080 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 return InvokeVirtualWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 return InvokeVirtualWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_list ap;
1096 va_start(ap, mid);
1097 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1098 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
1111 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
1114 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_start(ap, mid);
1116 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1118 va_end(ap);
1119 return local_result;
1120 }
1121
1122 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 return AddLocalReference<jobject>(ts, result.l);
1127 }
1128
1129 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 return AddLocalReference<jobject>(ts, result.l);
1134 }
1135
1136 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
1139 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_start(ap, mid);
1141 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 va_end(ap);
1143 return result.z;
1144 }
1145
1146 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
1152 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
1158 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
1161 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 va_start(ap, mid);
1163 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 va_end(ap);
1165 return result.b;
1166 }
1167
1168 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
1174 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
1183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
1185 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 va_end(ap);
1187 return result.c;
1188 }
1189
1190 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
1196 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
1205 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_start(ap, mid);
1207 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_end(ap);
1209 return result.s;
1210 }
1211
1212 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
1227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
1229 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
1231 return result.i;
1232 }
1233
1234 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
1249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
1251 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_end(ap);
1253 return result.j;
1254 }
1255
1256 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
1273 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return result.f;
1276 }
1277
1278 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
1295 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.d;
1298 }
1299
1300 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
1315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
1317 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 }
1320
1321 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jfieldID GetFieldID(JNIEnv* env,
1334 jclass c, const char* name, const char* sig) {
1335 ScopedJniThreadState ts(env);
1336 return FindFieldID(ts, c, name, sig, false);
1337 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001338
1339
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 static jfieldID GetStaticFieldID(JNIEnv* env,
1341 jclass c, const char* name, const char* sig) {
1342 ScopedJniThreadState ts(env);
1343 return FindFieldID(ts, c, name, sig, true);
1344 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001345
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001348 Object* o = Decode<Object*>(ts, obj);
1349 Field* f = DecodeField(ts, fid);
1350 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 Field* f = DecodeField(ts, fid);
1356 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 Object* o = Decode<Object*>(ts, java_object);
1362 Object* v = Decode<Object*>(ts, java_value);
1363 Field* f = DecodeField(ts, fid);
1364 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 Object* v = Decode<Object*>(ts, java_value);
1370 Field* f = DecodeField(ts, fid);
1371 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374#define GET_PRIMITIVE_FIELD(fn, instance) \
1375 ScopedJniThreadState ts(env); \
1376 Object* o = Decode<Object*>(ts, instance); \
1377 Field* f = DecodeField(ts, fid); \
1378 return f->fn(o)
1379
1380#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1381 ScopedJniThreadState ts(env); \
1382 Object* o = Decode<Object*>(ts, instance); \
1383 Field* f = DecodeField(ts, fid); \
1384 f->fn(o, value)
1385
1386 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1387 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1391 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1395 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1399 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1403 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1407 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1411 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1415 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetLong, NULL);
1440 }
1441
1442 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1444 }
1445
1446 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1448 }
1449
1450 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1451 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1452 }
1453
1454 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1455 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1456 }
1457
1458 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1459 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1460 }
1461
1462 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1463 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1464 }
1465
1466 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1467 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1468 }
1469
1470 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1471 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1472 }
1473
1474 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1475 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1476 }
1477
1478 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1479 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1480 }
1481
1482 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1483 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1484 }
1485
1486 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1487 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1488 }
1489
1490 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1491 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1492 }
1493
1494 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1495 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1496 }
1497
1498 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1499 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1500 }
1501
1502 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1503 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1504 }
1505
1506 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1507 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1508 }
1509
1510 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1511 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
1514 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001515 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 ScopedJniThreadState ts(env);
1517 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001518 va_start(ap, mid);
1519 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1521 va_end(ap);
1522 return local_result;
1523 }
1524
1525 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001526 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001528 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 return AddLocalReference<jobject>(ts, result.l);
1530 }
1531
1532 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001533 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001535 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 return AddLocalReference<jobject>(ts, result.l);
1537 }
1538
1539 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001540 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 ScopedJniThreadState ts(env);
1542 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001543 va_start(ap, mid);
1544 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 va_end(ap);
1546 return result.z;
1547 }
1548
1549 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001552 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
1555 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 ScopedJniThreadState ts(env);
1563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
1565 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_end(ap);
1567 return result.b;
1568 }
1569
1570 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001571 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
1576 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
1584 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 va_start(ap, mid);
1586 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 va_end(ap);
1588 return result.c;
1589 }
1590
1591 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
1597 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
1605 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 va_start(ap, mid);
1607 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 va_end(ap);
1609 return result.s;
1610 }
1611
1612 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
1618 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
1626 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 va_start(ap, mid);
1628 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 va_end(ap);
1630 return result.i;
1631 }
1632
1633 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
1639 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
1647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
1649 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_end(ap);
1651 return result.j;
1652 }
1653
1654 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
1660 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
1668 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 va_start(ap, mid);
1670 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 return result.f;
1673 }
1674
1675 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
1681 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
1689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
1691 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_end(ap);
1693 return result.d;
1694 }
1695
1696 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
1702 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
1710 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 va_start(ap, mid);
1712 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_end(ap);
1714 }
1715
1716 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
1722 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes814e4032011-08-23 12:07:56 -07001728 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001730 if (chars == NULL && char_count == 0) {
1731 return NULL;
1732 }
1733 String* result = String::AllocFromUtf16(char_count, chars);
1734 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
1737 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1738 ScopedJniThreadState ts(env);
1739 if (utf == NULL) {
1740 return NULL;
1741 }
1742 String* result = String::AllocFromModifiedUtf8(utf);
1743 return AddLocalReference<jstring>(ts, result);
1744 }
1745
Elliott Hughes814e4032011-08-23 12:07:56 -07001746 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1747 ScopedJniThreadState ts(env);
1748 return Decode<String*>(ts, java_string)->GetLength();
1749 }
1750
1751 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1752 ScopedJniThreadState ts(env);
1753 return Decode<String*>(ts, java_string)->GetUtfLength();
1754 }
1755
1756 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
1758 UNIMPLEMENTED(FATAL);
Elliott Hughes814e4032011-08-23 12:07:56 -07001759 }
1760
1761 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1762 ScopedJniThreadState ts(env);
1763 UNIMPLEMENTED(FATAL);
1764 }
1765
1766 static const jchar* GetStringChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1767 ScopedJniThreadState ts(env);
1768 UNIMPLEMENTED(FATAL);
1769 return NULL;
1770 }
1771
1772 static void ReleaseStringChars(JNIEnv* env, jstring str, const jchar* chars) {
1773 ScopedJniThreadState ts(env);
1774 UNIMPLEMENTED(FATAL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
1777 static const char* GetStringUTFChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1778 ScopedJniThreadState ts(env);
1779 UNIMPLEMENTED(FATAL);
1780 return NULL;
1781 }
1782
1783 static void ReleaseStringUTFChars(JNIEnv* env, jstring str, const char* chars) {
1784 ScopedJniThreadState ts(env);
1785 UNIMPLEMENTED(FATAL);
1786 }
1787
Elliott Hughesbd935992011-08-22 11:59:34 -07001788 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001790 Object* obj = Decode<Object*>(ts, java_array);
1791 CHECK(obj->IsArray()); // TODO: ReportJniError
1792 Array* array = obj->AsArray();
1793 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1799 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static void SetObjectArrayElement(JNIEnv* env,
1803 jobjectArray java_array, jsize index, jobject java_value) {
1804 ScopedJniThreadState ts(env);
1805 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1806 Object* value = Decode<Object*>(ts, java_value);
1807 array->Set(index, value);
1808 }
1809
1810 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1811 ScopedJniThreadState ts(env);
1812 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1813 }
1814
1815 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1816 ScopedJniThreadState ts(env);
1817 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1818 }
1819
1820 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1821 ScopedJniThreadState ts(env);
1822 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1823 }
1824
1825 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1826 ScopedJniThreadState ts(env);
1827 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1828 }
1829
1830 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1831 ScopedJniThreadState ts(env);
1832 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1833 }
1834
1835 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1836 ScopedJniThreadState ts(env);
1837 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1838 }
1839
1840 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1841 ScopedJniThreadState ts(env);
1842 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1843 }
1844
1845 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1846 ScopedJniThreadState ts(env);
1847 CHECK_GE(length, 0); // TODO: ReportJniError
1848
1849 // Compute the array class corresponding to the given element class.
1850 Class* element_class = Decode<Class*>(ts, element_jclass);
1851 std::string descriptor;
1852 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001853 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854
1855 // Find the class.
1856 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1857 // TODO: need to get the appropriate ClassLoader.
1858 Class* array_class = class_linker->FindClass(descriptor, NULL);
1859 if (array_class == NULL) {
1860 return NULL;
1861 }
1862
1863 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1864 CHECK(initial_element == NULL); // TODO: support initial_element
1865 return AddLocalReference<jobjectArray>(ts, result);
1866 }
1867
1868 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1869 ScopedJniThreadState ts(env);
1870 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1871 }
1872
1873 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1874 jbooleanArray array, jboolean* isCopy) {
1875 ScopedJniThreadState ts(env);
1876 UNIMPLEMENTED(FATAL);
1877 return NULL;
1878 }
1879
1880 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1881 ScopedJniThreadState ts(env);
1882 UNIMPLEMENTED(FATAL);
1883 return NULL;
1884 }
1885
1886 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1887 ScopedJniThreadState ts(env);
1888 UNIMPLEMENTED(FATAL);
1889 return NULL;
1890 }
1891
1892 static jshort* GetShortArrayElements(JNIEnv* env,
1893 jshortArray array, jboolean* isCopy) {
1894 ScopedJniThreadState ts(env);
1895 UNIMPLEMENTED(FATAL);
1896 return NULL;
1897 }
1898
1899 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1900 ScopedJniThreadState ts(env);
1901 UNIMPLEMENTED(FATAL);
1902 return NULL;
1903 }
1904
1905 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1906 ScopedJniThreadState ts(env);
1907 UNIMPLEMENTED(FATAL);
1908 return NULL;
1909 }
1910
1911 static jfloat* GetFloatArrayElements(JNIEnv* env,
1912 jfloatArray array, jboolean* isCopy) {
1913 ScopedJniThreadState ts(env);
1914 UNIMPLEMENTED(FATAL);
1915 return NULL;
1916 }
1917
1918 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1919 jdoubleArray array, jboolean* isCopy) {
1920 ScopedJniThreadState ts(env);
1921 UNIMPLEMENTED(FATAL);
1922 return NULL;
1923 }
1924
1925 static void ReleaseBooleanArrayElements(JNIEnv* env,
1926 jbooleanArray array, jboolean* elems, jint mode) {
1927 ScopedJniThreadState ts(env);
1928 UNIMPLEMENTED(FATAL);
1929 }
1930
1931 static void ReleaseByteArrayElements(JNIEnv* env,
1932 jbyteArray array, jbyte* elems, jint mode) {
1933 ScopedJniThreadState ts(env);
1934 UNIMPLEMENTED(FATAL);
1935 }
1936
1937 static void ReleaseCharArrayElements(JNIEnv* env,
1938 jcharArray array, jchar* elems, jint mode) {
1939 ScopedJniThreadState ts(env);
1940 UNIMPLEMENTED(FATAL);
1941 }
1942
1943 static void ReleaseShortArrayElements(JNIEnv* env,
1944 jshortArray array, jshort* elems, jint mode) {
1945 ScopedJniThreadState ts(env);
1946 UNIMPLEMENTED(FATAL);
1947 }
1948
1949 static void ReleaseIntArrayElements(JNIEnv* env,
1950 jintArray array, jint* elems, jint mode) {
1951 ScopedJniThreadState ts(env);
1952 UNIMPLEMENTED(FATAL);
1953 }
1954
1955 static void ReleaseLongArrayElements(JNIEnv* env,
1956 jlongArray array, jlong* elems, jint mode) {
1957 ScopedJniThreadState ts(env);
1958 UNIMPLEMENTED(FATAL);
1959 }
1960
1961 static void ReleaseFloatArrayElements(JNIEnv* env,
1962 jfloatArray array, jfloat* elems, jint mode) {
1963 ScopedJniThreadState ts(env);
1964 UNIMPLEMENTED(FATAL);
1965 }
1966
1967 static void ReleaseDoubleArrayElements(JNIEnv* env,
1968 jdoubleArray array, jdouble* elems, jint mode) {
1969 ScopedJniThreadState ts(env);
1970 UNIMPLEMENTED(FATAL);
1971 }
1972
Elliott Hughes814e4032011-08-23 12:07:56 -07001973 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes814e4032011-08-23 12:07:56 -07001978 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes814e4032011-08-23 12:07:56 -07001983 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes814e4032011-08-23 12:07:56 -07001988 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes814e4032011-08-23 12:07:56 -07001993 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes814e4032011-08-23 12:07:56 -07001998 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002000 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002005 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes814e4032011-08-23 12:07:56 -07002013 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes814e4032011-08-23 12:07:56 -07002018 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002030 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes5174fe62011-08-23 15:12:35 -07002053 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002055 Class* c = Decode<Class*>(ts, java_class);
2056
2057 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2058
2059 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 const char* name = methods[i].name;
2061 const char* sig = methods[i].signature;
2062
2063 if (*sig == '!') {
2064 // TODO: fast jni. it's too noisy to log all these.
2065 ++sig;
2066 }
2067
Elliott Hughes5174fe62011-08-23 15:12:35 -07002068 Method* m = c->FindDirectMethod(name, sig);
2069 if (m == NULL) {
2070 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002072 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002074 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2076 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002077 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002079 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002081 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2083 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002084 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 return JNI_ERR;
2086 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087
2088 if (vm->verbose_jni) {
2089 LOG(INFO) << "[Registering JNI native method "
2090 << PrettyMethod(m, true) << "]";
2091 }
2092
2093 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095 return JNI_OK;
2096 }
2097
Elliott Hughes5174fe62011-08-23 15:12:35 -07002098 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002100 Class* c = Decode<Class*>(ts, java_class);
2101
2102 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2103 if (vm->verbose_jni) {
2104 LOG(INFO) << "[Unregistering JNI native methods for "
2105 << PrettyDescriptor(c->GetDescriptor()) << "]";
2106 }
2107
2108 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2109 Method* m = c->GetDirectMethod(i);
2110 if (m->IsNative()) {
2111 m->UnregisterNative();
2112 }
2113 }
2114 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2115 Method* m = c->GetVirtualMethod(i);
2116 if (m->IsNative()) {
2117 m->UnregisterNative();
2118 }
2119 }
2120
2121 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes72025e52011-08-23 17:50:30 -07002124 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002126 Decode<Object*>(ts, java_object)->MonitorEnter();
2127 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes72025e52011-08-23 17:50:30 -07002130 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002132 Decode<Object*>(ts, java_object)->MonitorEnter();
2133 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
2136 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2137 ScopedJniThreadState ts(env);
2138 Runtime* runtime = Runtime::Current();
2139 if (runtime != NULL) {
2140 *vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
2141 } else {
2142 *vm = NULL;
2143 }
2144 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2145 }
2146
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 static void* GetPrimitiveArrayCritical(JNIEnv* env,
2148 jarray array, jboolean* isCopy) {
2149 ScopedJniThreadState ts(env);
2150 UNIMPLEMENTED(FATAL);
2151 return NULL;
2152 }
2153
2154 static void ReleasePrimitiveArrayCritical(JNIEnv* env,
2155 jarray array, void* carray, jint mode) {
2156 ScopedJniThreadState ts(env);
2157 UNIMPLEMENTED(FATAL);
2158 }
2159
2160 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
2161 ScopedJniThreadState ts(env);
2162 UNIMPLEMENTED(FATAL);
2163 return NULL;
2164 }
2165
2166 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
2167 ScopedJniThreadState ts(env);
2168 UNIMPLEMENTED(FATAL);
2169 }
2170
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2172 ScopedJniThreadState ts(env);
2173 UNIMPLEMENTED(FATAL);
2174 return NULL;
2175 }
2176
2177 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
2178 ScopedJniThreadState ts(env);
2179 UNIMPLEMENTED(FATAL);
2180 return NULL;
2181 }
2182
2183 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
2184 ScopedJniThreadState ts(env);
2185 UNIMPLEMENTED(FATAL);
2186 return 0;
2187 }
2188
2189 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2190 ScopedJniThreadState ts(env);
2191 UNIMPLEMENTED(FATAL);
2192 return JNIInvalidRefType;
2193 }
2194};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002195
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002196static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002197 NULL, // reserved0.
2198 NULL, // reserved1.
2199 NULL, // reserved2.
2200 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 JNI::GetVersion,
2202 JNI::DefineClass,
2203 JNI::FindClass,
2204 JNI::FromReflectedMethod,
2205 JNI::FromReflectedField,
2206 JNI::ToReflectedMethod,
2207 JNI::GetSuperclass,
2208 JNI::IsAssignableFrom,
2209 JNI::ToReflectedField,
2210 JNI::Throw,
2211 JNI::ThrowNew,
2212 JNI::ExceptionOccurred,
2213 JNI::ExceptionDescribe,
2214 JNI::ExceptionClear,
2215 JNI::FatalError,
2216 JNI::PushLocalFrame,
2217 JNI::PopLocalFrame,
2218 JNI::NewGlobalRef,
2219 JNI::DeleteGlobalRef,
2220 JNI::DeleteLocalRef,
2221 JNI::IsSameObject,
2222 JNI::NewLocalRef,
2223 JNI::EnsureLocalCapacity,
2224 JNI::AllocObject,
2225 JNI::NewObject,
2226 JNI::NewObjectV,
2227 JNI::NewObjectA,
2228 JNI::GetObjectClass,
2229 JNI::IsInstanceOf,
2230 JNI::GetMethodID,
2231 JNI::CallObjectMethod,
2232 JNI::CallObjectMethodV,
2233 JNI::CallObjectMethodA,
2234 JNI::CallBooleanMethod,
2235 JNI::CallBooleanMethodV,
2236 JNI::CallBooleanMethodA,
2237 JNI::CallByteMethod,
2238 JNI::CallByteMethodV,
2239 JNI::CallByteMethodA,
2240 JNI::CallCharMethod,
2241 JNI::CallCharMethodV,
2242 JNI::CallCharMethodA,
2243 JNI::CallShortMethod,
2244 JNI::CallShortMethodV,
2245 JNI::CallShortMethodA,
2246 JNI::CallIntMethod,
2247 JNI::CallIntMethodV,
2248 JNI::CallIntMethodA,
2249 JNI::CallLongMethod,
2250 JNI::CallLongMethodV,
2251 JNI::CallLongMethodA,
2252 JNI::CallFloatMethod,
2253 JNI::CallFloatMethodV,
2254 JNI::CallFloatMethodA,
2255 JNI::CallDoubleMethod,
2256 JNI::CallDoubleMethodV,
2257 JNI::CallDoubleMethodA,
2258 JNI::CallVoidMethod,
2259 JNI::CallVoidMethodV,
2260 JNI::CallVoidMethodA,
2261 JNI::CallNonvirtualObjectMethod,
2262 JNI::CallNonvirtualObjectMethodV,
2263 JNI::CallNonvirtualObjectMethodA,
2264 JNI::CallNonvirtualBooleanMethod,
2265 JNI::CallNonvirtualBooleanMethodV,
2266 JNI::CallNonvirtualBooleanMethodA,
2267 JNI::CallNonvirtualByteMethod,
2268 JNI::CallNonvirtualByteMethodV,
2269 JNI::CallNonvirtualByteMethodA,
2270 JNI::CallNonvirtualCharMethod,
2271 JNI::CallNonvirtualCharMethodV,
2272 JNI::CallNonvirtualCharMethodA,
2273 JNI::CallNonvirtualShortMethod,
2274 JNI::CallNonvirtualShortMethodV,
2275 JNI::CallNonvirtualShortMethodA,
2276 JNI::CallNonvirtualIntMethod,
2277 JNI::CallNonvirtualIntMethodV,
2278 JNI::CallNonvirtualIntMethodA,
2279 JNI::CallNonvirtualLongMethod,
2280 JNI::CallNonvirtualLongMethodV,
2281 JNI::CallNonvirtualLongMethodA,
2282 JNI::CallNonvirtualFloatMethod,
2283 JNI::CallNonvirtualFloatMethodV,
2284 JNI::CallNonvirtualFloatMethodA,
2285 JNI::CallNonvirtualDoubleMethod,
2286 JNI::CallNonvirtualDoubleMethodV,
2287 JNI::CallNonvirtualDoubleMethodA,
2288 JNI::CallNonvirtualVoidMethod,
2289 JNI::CallNonvirtualVoidMethodV,
2290 JNI::CallNonvirtualVoidMethodA,
2291 JNI::GetFieldID,
2292 JNI::GetObjectField,
2293 JNI::GetBooleanField,
2294 JNI::GetByteField,
2295 JNI::GetCharField,
2296 JNI::GetShortField,
2297 JNI::GetIntField,
2298 JNI::GetLongField,
2299 JNI::GetFloatField,
2300 JNI::GetDoubleField,
2301 JNI::SetObjectField,
2302 JNI::SetBooleanField,
2303 JNI::SetByteField,
2304 JNI::SetCharField,
2305 JNI::SetShortField,
2306 JNI::SetIntField,
2307 JNI::SetLongField,
2308 JNI::SetFloatField,
2309 JNI::SetDoubleField,
2310 JNI::GetStaticMethodID,
2311 JNI::CallStaticObjectMethod,
2312 JNI::CallStaticObjectMethodV,
2313 JNI::CallStaticObjectMethodA,
2314 JNI::CallStaticBooleanMethod,
2315 JNI::CallStaticBooleanMethodV,
2316 JNI::CallStaticBooleanMethodA,
2317 JNI::CallStaticByteMethod,
2318 JNI::CallStaticByteMethodV,
2319 JNI::CallStaticByteMethodA,
2320 JNI::CallStaticCharMethod,
2321 JNI::CallStaticCharMethodV,
2322 JNI::CallStaticCharMethodA,
2323 JNI::CallStaticShortMethod,
2324 JNI::CallStaticShortMethodV,
2325 JNI::CallStaticShortMethodA,
2326 JNI::CallStaticIntMethod,
2327 JNI::CallStaticIntMethodV,
2328 JNI::CallStaticIntMethodA,
2329 JNI::CallStaticLongMethod,
2330 JNI::CallStaticLongMethodV,
2331 JNI::CallStaticLongMethodA,
2332 JNI::CallStaticFloatMethod,
2333 JNI::CallStaticFloatMethodV,
2334 JNI::CallStaticFloatMethodA,
2335 JNI::CallStaticDoubleMethod,
2336 JNI::CallStaticDoubleMethodV,
2337 JNI::CallStaticDoubleMethodA,
2338 JNI::CallStaticVoidMethod,
2339 JNI::CallStaticVoidMethodV,
2340 JNI::CallStaticVoidMethodA,
2341 JNI::GetStaticFieldID,
2342 JNI::GetStaticObjectField,
2343 JNI::GetStaticBooleanField,
2344 JNI::GetStaticByteField,
2345 JNI::GetStaticCharField,
2346 JNI::GetStaticShortField,
2347 JNI::GetStaticIntField,
2348 JNI::GetStaticLongField,
2349 JNI::GetStaticFloatField,
2350 JNI::GetStaticDoubleField,
2351 JNI::SetStaticObjectField,
2352 JNI::SetStaticBooleanField,
2353 JNI::SetStaticByteField,
2354 JNI::SetStaticCharField,
2355 JNI::SetStaticShortField,
2356 JNI::SetStaticIntField,
2357 JNI::SetStaticLongField,
2358 JNI::SetStaticFloatField,
2359 JNI::SetStaticDoubleField,
2360 JNI::NewString,
2361 JNI::GetStringLength,
2362 JNI::GetStringChars,
2363 JNI::ReleaseStringChars,
2364 JNI::NewStringUTF,
2365 JNI::GetStringUTFLength,
2366 JNI::GetStringUTFChars,
2367 JNI::ReleaseStringUTFChars,
2368 JNI::GetArrayLength,
2369 JNI::NewObjectArray,
2370 JNI::GetObjectArrayElement,
2371 JNI::SetObjectArrayElement,
2372 JNI::NewBooleanArray,
2373 JNI::NewByteArray,
2374 JNI::NewCharArray,
2375 JNI::NewShortArray,
2376 JNI::NewIntArray,
2377 JNI::NewLongArray,
2378 JNI::NewFloatArray,
2379 JNI::NewDoubleArray,
2380 JNI::GetBooleanArrayElements,
2381 JNI::GetByteArrayElements,
2382 JNI::GetCharArrayElements,
2383 JNI::GetShortArrayElements,
2384 JNI::GetIntArrayElements,
2385 JNI::GetLongArrayElements,
2386 JNI::GetFloatArrayElements,
2387 JNI::GetDoubleArrayElements,
2388 JNI::ReleaseBooleanArrayElements,
2389 JNI::ReleaseByteArrayElements,
2390 JNI::ReleaseCharArrayElements,
2391 JNI::ReleaseShortArrayElements,
2392 JNI::ReleaseIntArrayElements,
2393 JNI::ReleaseLongArrayElements,
2394 JNI::ReleaseFloatArrayElements,
2395 JNI::ReleaseDoubleArrayElements,
2396 JNI::GetBooleanArrayRegion,
2397 JNI::GetByteArrayRegion,
2398 JNI::GetCharArrayRegion,
2399 JNI::GetShortArrayRegion,
2400 JNI::GetIntArrayRegion,
2401 JNI::GetLongArrayRegion,
2402 JNI::GetFloatArrayRegion,
2403 JNI::GetDoubleArrayRegion,
2404 JNI::SetBooleanArrayRegion,
2405 JNI::SetByteArrayRegion,
2406 JNI::SetCharArrayRegion,
2407 JNI::SetShortArrayRegion,
2408 JNI::SetIntArrayRegion,
2409 JNI::SetLongArrayRegion,
2410 JNI::SetFloatArrayRegion,
2411 JNI::SetDoubleArrayRegion,
2412 JNI::RegisterNatives,
2413 JNI::UnregisterNatives,
2414 JNI::MonitorEnter,
2415 JNI::MonitorExit,
2416 JNI::GetJavaVM,
2417 JNI::GetStringRegion,
2418 JNI::GetStringUTFRegion,
2419 JNI::GetPrimitiveArrayCritical,
2420 JNI::ReleasePrimitiveArrayCritical,
2421 JNI::GetStringCritical,
2422 JNI::ReleaseStringCritical,
2423 JNI::NewWeakGlobalRef,
2424 JNI::DeleteWeakGlobalRef,
2425 JNI::ExceptionCheck,
2426 JNI::NewDirectByteBuffer,
2427 JNI::GetDirectBufferAddress,
2428 JNI::GetDirectBufferCapacity,
2429 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002430};
2431
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002432static const size_t kMonitorsInitial = 32; // Arbitrary.
2433static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2434
2435static const size_t kLocalsInitial = 64; // Arbitrary.
2436static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002437
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002438JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002439 : fns(&gNativeInterface),
2440 self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002441 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002442 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002443 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2444 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002445}
2446
Carl Shapiroea4dca82011-08-01 13:45:38 -07002447// JNI Invocation interface.
2448
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002449extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2450 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2451 if (args->version < JNI_VERSION_1_2) {
2452 return JNI_EVERSION;
2453 }
2454 Runtime::Options options;
2455 for (int i = 0; i < args->nOptions; ++i) {
2456 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002457 options.push_back(std::make_pair(StringPiece(option->optionString),
2458 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002459 }
2460 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002461 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002462 if (runtime == NULL) {
2463 return JNI_ERR;
2464 } else {
2465 *p_env = reinterpret_cast<JNIEnv*>(Thread::Current()->GetJniEnv());
Elliott Hughes0af55432011-08-17 18:37:28 -07002466 *p_vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002467 return JNI_OK;
2468 }
2469}
2470
Elliott Hughesf2682d52011-08-15 16:37:04 -07002471extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002472 Runtime* runtime = Runtime::Current();
2473 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002474 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002475 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002476 *vm_count = 1;
Elliott Hughes0af55432011-08-17 18:37:28 -07002477 vms[0] = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002478 }
2479 return JNI_OK;
2480}
2481
2482// Historically unsupported.
2483extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2484 return JNI_ERR;
2485}
2486
Elliott Hughescdf53122011-08-19 15:46:09 -07002487class JII {
2488 public:
2489 static jint DestroyJavaVM(JavaVM* vm) {
2490 if (vm == NULL) {
2491 return JNI_ERR;
2492 } else {
2493 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2494 delete raw_vm->runtime;
2495 return JNI_OK;
2496 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002497 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002498
Elliott Hughescdf53122011-08-19 15:46:09 -07002499 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2500 if (vm == NULL || p_env == NULL) {
2501 return JNI_ERR;
2502 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002503 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2504 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002505 const char* name = NULL;
2506 if (thr_args != NULL) {
2507 // TODO: check version
2508 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2509 // TODO: thread group
2510 }
2511 bool success = runtime->AttachCurrentThread(name, p_env);
2512 if (!success) {
2513 return JNI_ERR;
2514 } else {
2515 return JNI_OK;
2516 }
2517 }
2518
2519 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2520 if (vm == NULL || p_env == NULL) {
2521 return JNI_ERR;
2522 }
2523 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2524 Runtime* runtime = raw_vm->runtime;
2525 const char* name = NULL;
2526 if (thr_args != NULL) {
2527 // TODO: check version
2528 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2529 // TODO: thread group
2530 }
2531 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2532 if (!success) {
2533 return JNI_ERR;
2534 } else {
2535 return JNI_OK;
2536 }
2537 }
2538
2539 static jint DetachCurrentThread(JavaVM* vm) {
2540 if (vm == NULL) {
2541 return JNI_ERR;
2542 } else {
2543 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2544 Runtime* runtime = raw_vm->runtime;
2545 runtime->DetachCurrentThread();
2546 return JNI_OK;
2547 }
2548 }
2549
2550 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2551 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2552 return JNI_EVERSION;
2553 }
2554 if (vm == NULL || env == NULL) {
2555 return JNI_ERR;
2556 }
2557 Thread* thread = Thread::Current();
2558 if (thread == NULL) {
2559 *env = NULL;
2560 return JNI_EDETACHED;
2561 }
2562 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 return JNI_OK;
2564 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002565};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566
Elliott Hughesf2682d52011-08-15 16:37:04 -07002567struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002568 NULL, // reserved0
2569 NULL, // reserved1
2570 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002571 JII::DestroyJavaVM,
2572 JII::AttachCurrentThread,
2573 JII::DetachCurrentThread,
2574 JII::GetEnv,
2575 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002576};
2577
Elliott Hughesbbd76712011-08-17 10:25:24 -07002578static const size_t kPinTableInitialSize = 16;
2579static const size_t kPinTableMaxSize = 1024;
2580
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002581static const size_t kGlobalsInitial = 512; // Arbitrary.
2582static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2583
2584static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2585static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2586
Elliott Hughes0af55432011-08-17 18:37:28 -07002587JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002588 : fns(&gInvokeInterface),
2589 runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002590 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002591 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002592 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002593 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002594 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002595 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002596 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002597}
2598
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002599JavaVMExt::~JavaVMExt() {
2600 delete globals_lock;
2601 delete weak_globals_lock;
2602}
2603
Elliott Hughescdf53122011-08-19 15:46:09 -07002604/*
2605 * Load native code from the specified absolute pathname. Per the spec,
2606 * if we've already loaded a library with the specified pathname, we
2607 * return without doing anything.
2608 *
2609 * TODO? for better results we should absolutify the pathname. For fully
2610 * correct results we should stat to get the inode and compare that. The
2611 * existing implementation is fine so long as everybody is using
2612 * System.loadLibrary.
2613 *
2614 * The library will be associated with the specified class loader. The JNI
2615 * spec says we can't load the same library into more than one class loader.
2616 *
2617 * Returns "true" on success. On failure, sets *detail to a
2618 * human-readable description of the error or NULL if no detail is
2619 * available; ownership of the string is transferred to the caller.
2620 */
Elliott Hughes814e4032011-08-23 12:07:56 -07002621bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, char** detail) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002622 *detail = NULL;
2623
2624 // See if we've already loaded this library. If we have, and the class loader
2625 // matches, return successfully without doing anything.
2626 SharedLibrary* library = libraries[path];
2627 if (library != NULL) {
2628 if (library->GetClassLoader() != class_loader) {
2629 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2630 << "ClassLoader " << library->GetClassLoader() << "; "
2631 << "can't open in " << class_loader;
2632 *detail = strdup("already opened by different ClassLoader");
2633 return false;
2634 }
2635 if (verbose_jni) {
2636 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2637 << "ClassLoader " << class_loader << "]";
2638 }
2639 if (!library->CheckOnLoadResult(this)) {
2640 *detail = strdup("JNI_OnLoad failed before");
2641 return false;
2642 }
2643 return true;
2644 }
2645
2646 // Open the shared library. Because we're using a full path, the system
2647 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2648 // resolve this library's dependencies though.)
2649
2650 // Failures here are expected when java.library.path has several entries
2651 // and we have to hunt for the lib.
2652
2653 // The current version of the dynamic linker prints detailed information
2654 // about dlopen() failures. Some things to check if the message is
2655 // cryptic:
2656 // - make sure the library exists on the device
2657 // - verify that the right path is being opened (the debug log message
2658 // above can help with that)
2659 // - check to see if the library is valid (e.g. not zero bytes long)
2660 // - check config/prelink-linux-arm.map to ensure that the library
2661 // is listed and is not being overrun by the previous entry (if
2662 // loading suddenly stops working on a prelinked library, this is
2663 // a good one to check)
2664 // - write a trivial app that calls sleep() then dlopen(), attach
2665 // to it with "strace -p <pid>" while it sleeps, and watch for
2666 // attempts to open nonexistent dependent shared libs
2667
2668 // TODO: automate some of these checks!
2669
2670 // This can execute slowly for a large library on a busy system, so we
2671 // want to switch from RUNNING to VMWAIT while it executes. This allows
2672 // the GC to ignore us.
2673 Thread* self = Thread::Current();
2674 Thread::State old_state = self->GetState();
2675 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2676 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2677 self->SetState(old_state);
2678
2679 if (verbose_jni) {
2680 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2681 }
2682
2683 if (handle == NULL) {
2684 *detail = strdup(dlerror());
2685 return false;
2686 }
2687
2688 // Create a new entry.
2689 library = new SharedLibrary(path, handle, class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002690
2691 libraries[path] = library;
2692
2693 // if (pNewEntry != pActualEntry) {
2694 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2695 // freeSharedLibEntry(pNewEntry);
2696 // return CheckOnLoadResult(this, pActualEntry);
2697 // } else
2698 {
2699 if (verbose_jni) {
2700 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2701 }
2702
2703 bool result = true;
2704 void* sym = dlsym(handle, "JNI_OnLoad");
2705 if (sym == NULL) {
2706 if (verbose_jni) {
2707 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2708 }
2709 } else {
2710 // Call JNI_OnLoad. We have to override the current class
2711 // loader, which will always be "null" since the stuff at the
2712 // top of the stack is around Runtime.loadLibrary(). (See
2713 // the comments in the JNI FindClass function.)
Elliott Hughescdf53122011-08-19 15:46:09 -07002714 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2715 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Elliott Hughes814e4032011-08-23 12:07:56 -07002716 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2717 self->SetClassLoaderOverride(class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002718
2719 old_state = self->GetState();
2720 self->SetState(Thread::kNative);
2721 if (verbose_jni) {
2722 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2723 }
2724 int version = (*jni_on_load)(reinterpret_cast<JavaVM*>(this), NULL);
2725 self->SetState(old_state);
2726
Elliott Hughes814e4032011-08-23 12:07:56 -07002727 self->SetClassLoaderOverride(old_class_loader);;
Elliott Hughescdf53122011-08-19 15:46:09 -07002728
2729 if (version != JNI_VERSION_1_2 &&
2730 version != JNI_VERSION_1_4 &&
2731 version != JNI_VERSION_1_6) {
2732 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2733 << "bad version: " << version;
2734 // It's unwise to call dlclose() here, but we can mark it
2735 // as bad and ensure that future load attempts will fail.
2736 // We don't know how far JNI_OnLoad got, so there could
2737 // be some partially-initialized stuff accessible through
2738 // newly-registered native method calls. We could try to
2739 // unregister them, but that doesn't seem worthwhile.
2740 result = false;
2741 } else {
2742 if (verbose_jni) {
2743 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2744 << " from JNI_OnLoad in \"" << path << "\"]";
2745 }
2746 }
2747 }
2748
2749 library->SetResult(result);
2750 return result;
2751 }
2752}
2753
Ian Rogersdf20fe02011-07-20 20:34:16 -07002754} // namespace art