blob: d11f2a7acbc9237fa3e020e2140de5accea4774f [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Carl Shapiro9b9ba282011-08-14 15:30:39 -07005#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -07006#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07007#include <sys/mman.h>
Elliott Hughes0af55432011-08-17 18:37:28 -07008#include <utility>
9#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070010
Elliott Hughes72025e52011-08-23 17:50:30 -070011#include "ScopedLocalRef.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070012#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070013#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070014#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070016#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017#include "runtime.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070018#include "scoped_ptr.h"
19#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070021
buzbeec143c552011-08-20 17:38:58 -070022extern bool oatCompileMethod(art::Method*, art::InstructionSet);
23
Ian Rogersdf20fe02011-07-20 20:34:16 -070024namespace art {
25
Elliott Hughescdf53122011-08-19 15:46:09 -070026// This is private API, but with two different implementations: ARM and x86.
27void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070028
Elliott Hughescdf53122011-08-19 15:46:09 -070029// TODO: this should be in our anonymous namespace, but is currently needed
30// for testing in "jni_internal_test.cc".
31bool EnsureInvokeStub(Method* method) {
32 if (method->GetInvokeStub() != NULL) {
33 return true;
34 }
35 // TODO: use signature to find a matching stub
36 // TODO: failed, acquire a lock on the stub table
37 Assembler assembler;
38 CreateInvokeStub(&assembler, method);
39 // TODO: store native_entry in the stub table
40 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
41 size_t length = assembler.CodeSize();
42 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
43 if (addr == MAP_FAILED) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070044 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070045 }
46 MemoryRegion region(addr, length);
47 assembler.FinalizeInstructions(region);
48 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
49 return true;
50}
Elliott Hughes0af55432011-08-17 18:37:28 -070051
Elliott Hughescdf53122011-08-19 15:46:09 -070052// TODO: this can't be in our anonymous namespace because of the map in JavaVM.
53class SharedLibrary {
54public:
55 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
56 : path_(path),
57 handle_(handle),
58 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
59 jni_on_load_tid_(Thread::Current()->GetId()),
60 jni_on_load_result_(kPending) {
Elliott Hughes5174fe62011-08-23 15:12:35 -070061 pthread_cond_init(&jni_on_load_cond_, NULL);
Elliott Hughes18c07532011-08-18 15:50:51 -070062 }
63
64 ~SharedLibrary() {
Elliott Hughescdf53122011-08-19 15:46:09 -070065 delete jni_on_load_lock_;
Elliott Hughes0af55432011-08-17 18:37:28 -070066 }
67
Elliott Hughescdf53122011-08-19 15:46:09 -070068 Object* GetClassLoader() {
69 return class_loader_;
Elliott Hughes0af55432011-08-17 18:37:28 -070070 }
71
Elliott Hughescdf53122011-08-19 15:46:09 -070072 /*
73 * Check the result of an earlier call to JNI_OnLoad on this library. If
74 * the call has not yet finished in another thread, wait for it.
75 */
76 bool CheckOnLoadResult(JavaVMExt* vm) {
77 Thread* self = Thread::Current();
78 if (jni_on_load_tid_ == self->GetId()) {
79 // Check this so we don't end up waiting for ourselves. We need
80 // to return "true" so the caller can continue.
81 LOG(INFO) << *self << " recursive attempt to load library "
82 << "\"" << path_ << "\"";
83 return true;
Elliott Hughes0af55432011-08-17 18:37:28 -070084 }
85
Elliott Hughes5174fe62011-08-23 15:12:35 -070086 MutexLock mu(jni_on_load_lock_);
Elliott Hughescdf53122011-08-19 15:46:09 -070087 while (jni_on_load_result_ == kPending) {
88 if (vm->verbose_jni) {
89 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
90 << "JNI_OnLoad...]";
Elliott Hughes0af55432011-08-17 18:37:28 -070091 }
Elliott Hughescdf53122011-08-19 15:46:09 -070092 Thread::State old_state = self->GetState();
93 self->SetState(Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes5174fe62011-08-23 15:12:35 -070094 pthread_cond_wait(&jni_on_load_cond_, &(jni_on_load_lock_->lock_impl_));
Elliott Hughes0af55432011-08-17 18:37:28 -070095 self->SetState(old_state);
Elliott Hughes0af55432011-08-17 18:37:28 -070096 }
97
Elliott Hughescdf53122011-08-19 15:46:09 -070098 bool okay = (jni_on_load_result_ == kOkay);
99 if (vm->verbose_jni) {
100 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
101 << (okay ? "succeeded" : "failed") << "]";
102 }
103 return okay;
104 }
105
106 void SetResult(bool result) {
107 jni_on_load_result_ = result ? kOkay : kFailed;
108 jni_on_load_tid_ = 0;
Elliott Hughes0af55432011-08-17 18:37:28 -0700109
110 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700111 MutexLock mu(jni_on_load_lock_);
112 pthread_cond_broadcast(&jni_on_load_cond_);
Elliott Hughes0af55432011-08-17 18:37:28 -0700113 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700114
115 private:
116 enum JNI_OnLoadState {
117 kPending,
118 kFailed,
119 kOkay,
120 };
121
122 // Path to library "/system/lib/libjni.so".
123 std::string path_;
124
125 // The void* returned by dlopen(3).
126 void* handle_;
127
128 // The ClassLoader this library is associated with.
129 Object* class_loader_;
130
131 // Guards remaining items.
132 Mutex* jni_on_load_lock_;
133 // Wait for JNI_OnLoad in other thread.
134 pthread_cond_t jni_on_load_cond_;
135 // Recursive invocation guard.
136 uint32_t jni_on_load_tid_;
137 // Result of earlier JNI_OnLoad call.
138 JNI_OnLoadState jni_on_load_result_;
139};
140
141namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -0700142
Elliott Hughes22f40932011-08-12 13:06:37 -0700143// Entry/exit processing for all JNI calls.
144//
145// This performs the necessary thread state switching, lets us amortize the
146// cost of working out the current thread, and lets us check (and repair) apps
147// that are using a JNIEnv on the wrong thread.
148class ScopedJniThreadState {
149 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700150 explicit ScopedJniThreadState(JNIEnv* env)
151 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700152 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -0700153 self_->SetState(Thread::kRunnable);
154 }
155
156 ~ScopedJniThreadState() {
157 self_->SetState(Thread::kNative);
158 }
159
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700160 JNIEnvExt* Env() {
161 return env_;
162 }
163
Elliott Hughesb20a5542011-08-12 18:03:12 -0700164 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -0700165 return self_;
166 }
167
Elliott Hughesb20a5542011-08-12 18:03:12 -0700168 private:
169 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -0700170 // TODO: need replacement for gDvmJni.
171 bool workAroundAppJniBugs = true;
172 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
173 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
174 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -0700175 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
176 << " used on " << *self;
177 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -0700178 }
179 return self;
180 }
181
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700182 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -0700183 Thread* self_;
184 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
185};
186
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700187/*
188 * Add a local reference for an object to the current stack frame. When
189 * the native function returns, the reference will be discarded.
190 *
191 * We need to allow the same reference to be added multiple times.
192 *
193 * This will be called on otherwise unreferenced objects. We cannot do
194 * GC allocations here, and it's best if we don't grab a mutex.
195 *
196 * Returns the local reference (currently just the same pointer that was
197 * passed in), or NULL on failure.
198 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700199template<typename T>
200T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700201 if (obj == NULL) {
202 return NULL;
203 }
204
205 IndirectReferenceTable& locals = ts.Env()->locals;
206
207 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
208 IndirectRef ref = locals.Add(cookie, obj);
209 if (ref == NULL) {
210 // TODO: just change Add's DCHECK to CHECK and lose this?
211 locals.Dump();
212 LOG(FATAL) << "Failed adding to JNI local reference table "
213 << "(has " << locals.Capacity() << " entries)";
214 // TODO: dvmDumpThread(dvmThreadSelf(), false);
215 }
216
217#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
218 if (ts.Env()->check_jni) {
219 size_t entry_count = locals.Capacity();
220 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700221 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700222 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700223 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700224 locals.Dump();
225 // TODO: dvmDumpThread(dvmThreadSelf(), false);
226 // dvmAbort();
227 }
228 }
229#endif
230
231 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
232 // Hand out direct pointers to support broken old apps.
233 return reinterpret_cast<T>(obj);
234 }
235
236 return reinterpret_cast<T>(ref);
237}
238
Elliott Hughescdf53122011-08-19 15:46:09 -0700239jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
240 if (obj == NULL) {
241 return NULL;
242 }
243 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
244 IndirectReferenceTable& weak_globals = vm->weak_globals;
245 MutexLock mu(vm->weak_globals_lock);
246 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
247 return reinterpret_cast<jweak>(ref);
248}
249
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700250template<typename T>
251T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700252 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700253}
254
Elliott Hughescdf53122011-08-19 15:46:09 -0700255Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
256 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
257}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700258
Elliott Hughescdf53122011-08-19 15:46:09 -0700259Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
260 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700261}
262
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700263byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700264 size_t num_bytes = method->NumArgArrayBytes();
265 scoped_array<byte> arg_array(new byte[num_bytes]);
266 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700267 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700268 switch (shorty[i]) {
269 case 'Z':
270 case 'B':
271 case 'C':
272 case 'S':
273 case 'I':
274 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
275 offset += 4;
276 break;
277 case 'F':
278 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
279 offset += 4;
280 break;
281 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700282 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700283 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
284 offset += sizeof(Object*);
285 break;
286 }
287 case 'D':
288 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
289 offset += 8;
290 break;
291 case 'J':
292 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
293 offset += 8;
294 break;
295 }
296 }
297 return arg_array.release();
298}
299
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700300byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700301 size_t num_bytes = method->NumArgArrayBytes();
302 scoped_array<byte> arg_array(new byte[num_bytes]);
303 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700304 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700305 switch (shorty[i]) {
306 case 'Z':
307 case 'B':
308 case 'C':
309 case 'S':
310 case 'I':
311 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
312 offset += 4;
313 break;
314 case 'F':
315 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
316 offset += 4;
317 break;
318 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700319 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700320 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
321 offset += sizeof(Object*);
322 break;
323 }
324 case 'D':
325 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
326 offset += 8;
327 break;
328 case 'J':
329 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
330 offset += 8;
331 break;
332 }
333 }
334 return arg_array.release();
335}
336
Elliott Hughes72025e52011-08-23 17:50:30 -0700337JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
338 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700339 Thread* self = ts.Self();
340
341 // Push a transition back into managed code onto the linked list in thread
342 CHECK_EQ(Thread::kRunnable, self->GetState());
343 NativeToManagedRecord record;
344 self->PushNativeToManagedRecord(&record);
345
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700346 // Call the invoke stub associated with the method
347 // Pass everything as arguments
348 const Method::InvokeStub* stub = method->GetInvokeStub();
349 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700350
351#ifdef __arm__
352 // Compile...
353 // TODO: not here!
354 oatCompileMethod(method, kThumb2);
355#endif
356
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700357 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700358 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700359 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700360 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700361 LOG(WARNING) << "Not invoking method with no associated code: "
362 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700363 result.j = 0;
364 }
buzbeec143c552011-08-20 17:38:58 -0700365
Ian Rogers6de08602011-08-19 14:52:39 -0700366 // Pop transition
367 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700368 return result;
369}
370
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700371JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700372 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700373 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700374 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700375 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700376 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700377}
378
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700379JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700380 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700381 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700383 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700384 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
385}
386
387static Method* FindVirtualMethod(Object* receiver, Method* method) {
388 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
389}
390
391JValue InvokeVirtualWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
392 Object* receiver = Decode<Object*>(ts, obj);
393 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
394 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
395 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
396}
397
398JValue InvokeVirtualWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
399 Object* receiver = Decode<Object*>(ts, obj);
400 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
401 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
402 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403}
404
Elliott Hughes6b436852011-08-12 10:16:44 -0700405// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
406// separated with slashes but aren't wrapped with "L;" like regular descriptors
407// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
408// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
409// supported names with dots too (such as "a.b.C").
410std::string NormalizeJniClassDescriptor(const char* name) {
411 std::string result;
412 // Add the missing "L;" if necessary.
413 if (name[0] == '[') {
414 result = name;
415 } else {
416 result += 'L';
417 result += name;
418 result += ';';
419 }
420 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700421 if (result.find('.') != std::string::npos) {
422 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
423 << "\"" << name << "\"";
424 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700425 }
426 return result;
427}
428
Elliott Hughescdf53122011-08-19 15:46:09 -0700429jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
430 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700431 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
432 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700433 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700434
435 Method* method = NULL;
436 if (is_static) {
437 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700438 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700439 method = c->FindVirtualMethod(name, sig);
440 if (method == NULL) {
441 // No virtual method matching the signature. Search declared
442 // private methods and constructors.
443 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700444 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700445 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700446
Elliott Hughescdf53122011-08-19 15:46:09 -0700447 if (method == NULL || method->IsStatic() != is_static) {
448 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700449 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700450 // TODO: try searching for the opposite kind of method from is_static
451 // for better diagnostics?
452 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700453 "no %s method %s", is_static ? "static" : "non-static",
454 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700455 return NULL;
456 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700457
Elliott Hughescdf53122011-08-19 15:46:09 -0700458 bool success = EnsureInvokeStub(method);
459 if (!success) {
460 // TODO: throw OutOfMemoryException
461 return NULL;
462 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700463
Elliott Hughescdf53122011-08-19 15:46:09 -0700464 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700465}
466
Elliott Hughescdf53122011-08-19 15:46:09 -0700467jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
468 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700469 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
470 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700471 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700472
473 Field* field = NULL;
474 if (is_static) {
475 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700476 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700477 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700478 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700479
Elliott Hughescdf53122011-08-19 15:46:09 -0700480 if (field == NULL) {
481 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700482 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700483 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
484 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700485 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700486 return NULL;
487 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700488
Elliott Hughescdf53122011-08-19 15:46:09 -0700489 jweak fid = AddWeakGlobalReference(ts, field);
490 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700491}
492
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700493template<typename JniT, typename ArtT>
494JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
495 CHECK_GE(length, 0); // TODO: ReportJniError
496 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700497 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700498}
499
Elliott Hughes814e4032011-08-23 12:07:56 -0700500void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
501 std::string type(PrettyType(array));
502 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
503 "%s offset=%d length=%d %s.length=%d",
504 type.c_str(), start, length, identifier, array->GetLength());
505}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700506void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
507 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
508 "offset=%d length=%d string.length()=%d", start, length, array_length);
509}
Elliott Hughes814e4032011-08-23 12:07:56 -0700510
511template <typename JavaArrayT, typename JavaT, typename ArrayT>
512static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
513 ArrayT* array = Decode<ArrayT*>(ts, java_array);
514 if (start < 0 || length < 0 || start + length > array->GetLength()) {
515 ThrowAIOOBE(ts, array, start, length, "src");
516 } else {
517 JavaT* data = array->GetData();
518 memcpy(buf, data + start, length * sizeof(JavaT));
519 }
520}
521
522template <typename JavaArrayT, typename JavaT, typename ArrayT>
523static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
524 ArrayT* array = Decode<ArrayT*>(ts, java_array);
525 if (start < 0 || length < 0 || start + length > array->GetLength()) {
526 ThrowAIOOBE(ts, array, start, length, "dst");
527 } else {
528 JavaT* data = array->GetData();
529 memcpy(data + start, buf, length * sizeof(JavaT));
530 }
531}
532
Elliott Hughesb465ab02011-08-24 11:21:21 -0700533static jclass InitDirectByteBufferClass(JNIEnv* env) {
534 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
535 CHECK(buffer_class.get() != NULL);
536 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
537}
538
539static jclass GetDirectByteBufferClass(JNIEnv* env) {
540 static jclass buffer_class = InitDirectByteBufferClass(env);
541 return buffer_class;
542}
543
Elliott Hughescdf53122011-08-19 15:46:09 -0700544} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700545
Elliott Hughescdf53122011-08-19 15:46:09 -0700546class JNI {
547 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700548
Elliott Hughescdf53122011-08-19 15:46:09 -0700549 static jint GetVersion(JNIEnv* env) {
550 ScopedJniThreadState ts(env);
551 return JNI_VERSION_1_6;
552 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700553
Elliott Hughescdf53122011-08-19 15:46:09 -0700554 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
555 ScopedJniThreadState ts(env);
556 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700557 return NULL;
558 }
559
Elliott Hughescdf53122011-08-19 15:46:09 -0700560 static jclass FindClass(JNIEnv* env, const char* name) {
561 ScopedJniThreadState ts(env);
562 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
563 std::string descriptor(NormalizeJniClassDescriptor(name));
564 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700565 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700566 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700568 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700569
Elliott Hughescdf53122011-08-19 15:46:09 -0700570 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
571 ScopedJniThreadState ts(env);
572 Method* method = Decode<Method*>(ts, java_method);
573 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700574 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700575
Elliott Hughescdf53122011-08-19 15:46:09 -0700576 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
577 ScopedJniThreadState ts(env);
578 Field* field = Decode<Field*>(ts, java_field);
579 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
580 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700581
Elliott Hughescdf53122011-08-19 15:46:09 -0700582 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
583 ScopedJniThreadState ts(env);
584 Method* method = DecodeMethod(ts, mid);
585 return AddLocalReference<jobject>(ts, method);
586 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700587
Elliott Hughescdf53122011-08-19 15:46:09 -0700588 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
589 ScopedJniThreadState ts(env);
590 Field* field = DecodeField(ts, fid);
591 return AddLocalReference<jobject>(ts, field);
592 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700593
Elliott Hughes37f7a402011-08-22 18:56:01 -0700594 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
595 ScopedJniThreadState ts(env);
596 Object* o = Decode<Object*>(ts, java_object);
597 return AddLocalReference<jclass>(ts, o->GetClass());
598 }
599
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700600 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700602 Class* c = Decode<Class*>(ts, java_class);
603 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700605
Elliott Hughes37f7a402011-08-22 18:56:01 -0700606 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700608 Class* c1 = Decode<Class*>(ts, java_class1);
609 Class* c2 = Decode<Class*>(ts, java_class2);
610 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700611 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700612
Elliott Hughes37f7a402011-08-22 18:56:01 -0700613 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700615 CHECK_NE(static_cast<jclass>(NULL), clazz);
616 if (jobj == NULL) {
617 // NB. JNI is different from regular Java instanceof in this respect
618 return JNI_TRUE;
619 } else {
620 Object* obj = Decode<Object*>(ts, jobj);
621 Class* klass = Decode<Class*>(ts, clazz);
622 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
623 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughes37f7a402011-08-22 18:56:01 -0700626 static jint Throw(JNIEnv* env, jthrowable java_exception) {
627 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700628 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700629 if (exception == NULL) {
630 return JNI_ERR;
631 }
632 ts.Self()->SetException(exception);
633 return JNI_OK;
634 }
635
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700636 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700637 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700638 // TODO: check for a pending exception to decide what constructor to call.
639 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
640 if (mid == NULL) {
641 return JNI_ERR;
642 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700643 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
644 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700645 return JNI_ERR;
646 }
647
648 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700649 args[0].l = s.get();
650 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
651 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700652 return JNI_ERR;
653 }
654
Elliott Hughes72025e52011-08-23 17:50:30 -0700655 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700656 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700657 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700658
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 return JNI_OK;
660 }
661
662 static jboolean ExceptionCheck(JNIEnv* env) {
663 ScopedJniThreadState ts(env);
664 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
665 }
666
667 static void ExceptionClear(JNIEnv* env) {
668 ScopedJniThreadState ts(env);
669 ts.Self()->ClearException();
670 }
671
672 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700674
675 Thread* self = ts.Self();
676 Throwable* original_exception = self->GetException();
677 self->ClearException();
678
679 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
680 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
681 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
682 if (mid == NULL) {
683 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
684 << PrettyType(original_exception);
685 } else {
686 env->CallVoidMethod(exception.get(), mid);
687 if (self->IsExceptionPending()) {
688 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
689 << " thrown while calling printStackTrace";
690 self->ClearException();
691 }
692 }
693
694 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 static jthrowable ExceptionOccurred(JNIEnv* env) {
698 ScopedJniThreadState ts(env);
699 Object* exception = ts.Self()->GetException();
700 if (exception == NULL) {
701 return NULL;
702 } else {
703 // TODO: if adding a local reference failing causes the VM to abort
704 // then the following check will never occur.
705 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
706 if (localException == NULL) {
707 // We were unable to add a new local reference, and threw a new
708 // exception. We can't return "exception", because it's not a
709 // local reference. So we have to return NULL, indicating that
710 // there was no exception, even though it's pretty much raining
711 // exceptions in here.
712 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
713 }
714 return localException;
715 }
716 }
717
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 static void FatalError(JNIEnv* env, const char* msg) {
719 ScopedJniThreadState ts(env);
720 LOG(FATAL) << "JNI FatalError called: " << msg;
721 }
722
723 static jint PushLocalFrame(JNIEnv* env, jint cap) {
724 ScopedJniThreadState ts(env);
725 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
726 return JNI_OK;
727 }
728
729 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
730 ScopedJniThreadState ts(env);
731 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
732 return res;
733 }
734
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
736 ScopedJniThreadState ts(env);
737 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
738 return 0;
739 }
740
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
742 ScopedJniThreadState ts(env);
743 if (obj == NULL) {
744 return NULL;
745 }
746
747 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
748 IndirectReferenceTable& globals = vm->globals;
749 MutexLock mu(vm->globals_lock);
750 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
751 return reinterpret_cast<jobject>(ref);
752 }
753
754 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
755 ScopedJniThreadState ts(env);
756 if (obj == NULL) {
757 return;
758 }
759
760 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
761 IndirectReferenceTable& globals = vm->globals;
762 MutexLock mu(vm->globals_lock);
763
764 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
765 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
766 << "failed to find entry";
767 }
768 }
769
770 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
771 ScopedJniThreadState ts(env);
772 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
773 }
774
775 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
776 ScopedJniThreadState ts(env);
777 if (obj == NULL) {
778 return;
779 }
780
781 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
782 IndirectReferenceTable& weak_globals = vm->weak_globals;
783 MutexLock mu(vm->weak_globals_lock);
784
785 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
786 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
787 << "failed to find entry";
788 }
789 }
790
791 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
792 ScopedJniThreadState ts(env);
793 if (obj == NULL) {
794 return NULL;
795 }
796
797 IndirectReferenceTable& locals = ts.Env()->locals;
798
799 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
800 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
801 return reinterpret_cast<jobject>(ref);
802 }
803
804 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
805 ScopedJniThreadState ts(env);
806 if (obj == NULL) {
807 return;
808 }
809
810 IndirectReferenceTable& locals = ts.Env()->locals;
811
812 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
813 if (!locals.Remove(cookie, obj)) {
814 // Attempting to delete a local reference that is not in the
815 // topmost local reference frame is a no-op. DeleteLocalRef returns
816 // void and doesn't throw any exceptions, but we should probably
817 // complain about it so the user will notice that things aren't
818 // going quite the way they expect.
819 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
820 << "failed to find entry";
821 }
822 }
823
824 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
825 ScopedJniThreadState ts(env);
826 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
827 ? JNI_TRUE : JNI_FALSE;
828 }
829
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700830 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700832 Class* c = Decode<Class*>(ts, java_class);
833 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
834 return NULL;
835 }
836 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 }
838
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 ScopedJniThreadState ts(env);
841 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 va_start(args, mid);
843 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 va_end(args);
845 return result;
846 }
847
Elliott Hughes72025e52011-08-23 17:50:30 -0700848 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700850 Class* c = Decode<Class*>(ts, java_class);
851 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
852 return NULL;
853 }
854 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 return local_result;
858 }
859
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700862 Class* c = Decode<Class*>(ts, java_class);
863 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
864 return NULL;
865 }
866 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 return local_result;
870 }
871
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
873 ScopedJniThreadState ts(env);
874 return FindMethodID(ts, c, name, sig, false);
875 }
876
877 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
878 ScopedJniThreadState ts(env);
879 return FindMethodID(ts, c, name, sig, true);
880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 va_list ap;
885 va_start(ap, mid);
886 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
887 va_end(ap);
888 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
894 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
900 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_list ap;
906 va_start(ap, mid);
907 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
908 va_end(ap);
909 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_list ap;
925 va_start(ap, mid);
926 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
927 va_end(ap);
928 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jchar CallCharMethod(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.c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jchar CallCharMethodV(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jchar CallCharMethodA(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jdouble CallDoubleMethod(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.d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jdouble CallDoubleMethodV(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jdouble CallDoubleMethodA(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jfloat CallFloatMethod(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.f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jfloat CallFloatMethodV(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jfloat CallFloatMethodA(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jint CallIntMethod(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.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jint CallIntMethodV(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jint CallIntMethodA(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jlong CallLongMethod(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.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jlong CallLongMethodV(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jlong CallLongMethodA(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jshort CallShortMethod(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.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jshort CallShortMethodV(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jshort CallShortMethodA(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static void CallVoidMethod(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);
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
1073 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
1076 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_start(ap, mid);
1078 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1080 va_end(ap);
1081 return local_result;
1082 }
1083
1084 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 return AddLocalReference<jobject>(ts, result.l);
1089 }
1090
1091 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 return AddLocalReference<jobject>(ts, result.l);
1096 }
1097
1098 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
1101 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_start(ap, mid);
1103 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 va_end(ap);
1105 return result.z;
1106 }
1107
1108 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
1114 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
1120 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
1123 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_start(ap, mid);
1125 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 va_end(ap);
1127 return result.b;
1128 }
1129
1130 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
1136 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
1142 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
1145 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_start(ap, mid);
1147 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 va_end(ap);
1149 return result.c;
1150 }
1151
1152 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
1158 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
1164 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
1167 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_start(ap, mid);
1169 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_end(ap);
1171 return result.s;
1172 }
1173
1174 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
1189 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_start(ap, mid);
1191 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 va_end(ap);
1193 return result.i;
1194 }
1195
1196 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
1211 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_start(ap, mid);
1213 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
1215 return result.j;
1216 }
1217
1218 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
1233 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_start(ap, mid);
1235 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
1237 return result.f;
1238 }
1239
1240 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
1252 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
1255 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 va_start(ap, mid);
1257 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_end(ap);
1259 return result.d;
1260 }
1261
1262 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
1277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
1279 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_end(ap);
1281 }
1282
1283 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jfieldID GetFieldID(JNIEnv* env,
1296 jclass c, const char* name, const char* sig) {
1297 ScopedJniThreadState ts(env);
1298 return FindFieldID(ts, c, name, sig, false);
1299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001300
1301
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 static jfieldID GetStaticFieldID(JNIEnv* env,
1303 jclass c, const char* name, const char* sig) {
1304 ScopedJniThreadState ts(env);
1305 return FindFieldID(ts, c, name, sig, true);
1306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001307
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001308 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 Object* o = Decode<Object*>(ts, obj);
1311 Field* f = DecodeField(ts, fid);
1312 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001314
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001315 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001317 Field* f = DecodeField(ts, fid);
1318 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001321 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001323 Object* o = Decode<Object*>(ts, java_object);
1324 Object* v = Decode<Object*>(ts, java_value);
1325 Field* f = DecodeField(ts, fid);
1326 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001329 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001331 Object* v = Decode<Object*>(ts, java_value);
1332 Field* f = DecodeField(ts, fid);
1333 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001336#define GET_PRIMITIVE_FIELD(fn, instance) \
1337 ScopedJniThreadState ts(env); \
1338 Object* o = Decode<Object*>(ts, instance); \
1339 Field* f = DecodeField(ts, fid); \
1340 return f->fn(o)
1341
1342#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1343 ScopedJniThreadState ts(env); \
1344 Object* o = Decode<Object*>(ts, instance); \
1345 Field* f = DecodeField(ts, fid); \
1346 f->fn(o, value)
1347
1348 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1349 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001352 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1353 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1357 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1361 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1365 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1369 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1373 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1377 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1381 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1385 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1389 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1393 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1397 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1401 GET_PRIMITIVE_FIELD(GetLong, NULL);
1402 }
1403
1404 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1405 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1406 }
1407
1408 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1410 }
1411
1412 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1413 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1414 }
1415
1416 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1417 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1418 }
1419
1420 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1421 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1422 }
1423
1424 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1425 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1426 }
1427
1428 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1429 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1430 }
1431
1432 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1433 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1434 }
1435
1436 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1437 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1438 }
1439
1440 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1441 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1442 }
1443
1444 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1445 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1446 }
1447
1448 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1449 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1450 }
1451
1452 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1453 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1454 }
1455
1456 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1457 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1458 }
1459
1460 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1461 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1462 }
1463
1464 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1465 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1466 }
1467
1468 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1469 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1470 }
1471
1472 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1473 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
1476 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001477 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 ScopedJniThreadState ts(env);
1479 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001480 va_start(ap, mid);
1481 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1483 va_end(ap);
1484 return local_result;
1485 }
1486
1487 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001488 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 return AddLocalReference<jobject>(ts, result.l);
1492 }
1493
1494 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001495 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001497 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 return AddLocalReference<jobject>(ts, result.l);
1499 }
1500
1501 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001502 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 ScopedJniThreadState ts(env);
1504 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001505 va_start(ap, mid);
1506 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 va_end(ap);
1508 return result.z;
1509 }
1510
1511 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001512 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001514 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
1517 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001518 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001520 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes72025e52011-08-23 17:50:30 -07001523 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 ScopedJniThreadState ts(env);
1525 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001526 va_start(ap, mid);
1527 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 va_end(ap);
1529 return result.b;
1530 }
1531
1532 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001533 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001535 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
1538 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001541 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes72025e52011-08-23 17:50:30 -07001544 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 ScopedJniThreadState ts(env);
1546 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 va_start(ap, mid);
1548 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_end(ap);
1550 return result.c;
1551 }
1552
1553 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
1559 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001562 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 ScopedJniThreadState ts(env);
1567 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 va_start(ap, mid);
1569 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 va_end(ap);
1571 return result.s;
1572 }
1573
1574 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
1580 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001581 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 ScopedJniThreadState ts(env);
1588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
1590 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 va_end(ap);
1592 return result.i;
1593 }
1594
1595 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
1601 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
1609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
1611 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_end(ap);
1613 return result.j;
1614 }
1615
1616 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
1622 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
1630 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 va_start(ap, mid);
1632 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return result.f;
1635 }
1636
1637 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
1643 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 ScopedJniThreadState ts(env);
1651 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 va_start(ap, mid);
1653 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_end(ap);
1655 return result.d;
1656 }
1657
1658 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
1664 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
1672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
1674 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
1676 }
1677
1678 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
1684 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
Elliott Hughes814e4032011-08-23 12:07:56 -07001690 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001692 if (chars == NULL && char_count == 0) {
1693 return NULL;
1694 }
1695 String* result = String::AllocFromUtf16(char_count, chars);
1696 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
1699 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1700 ScopedJniThreadState ts(env);
1701 if (utf == NULL) {
1702 return NULL;
1703 }
1704 String* result = String::AllocFromModifiedUtf8(utf);
1705 return AddLocalReference<jstring>(ts, result);
1706 }
1707
Elliott Hughes814e4032011-08-23 12:07:56 -07001708 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1709 ScopedJniThreadState ts(env);
1710 return Decode<String*>(ts, java_string)->GetLength();
1711 }
1712
1713 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1714 ScopedJniThreadState ts(env);
1715 return Decode<String*>(ts, java_string)->GetUtfLength();
1716 }
1717
Elliott Hughesb465ab02011-08-24 11:21:21 -07001718 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001720 String* s = Decode<String*>(ts, java_string);
1721 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1722 ThrowSIOOBE(ts, start, length, s->GetLength());
1723 } else {
1724 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1725 memcpy(buf, chars + start, length * sizeof(jchar));
1726 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001727 }
1728
Elliott Hughesb465ab02011-08-24 11:21:21 -07001729 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001730 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001731 String* s = Decode<String*>(ts, java_string);
1732 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1733 ThrowSIOOBE(ts, start, length, s->GetLength());
1734 } else {
1735 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1736 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1737 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001738 }
1739
1740 static const jchar* GetStringChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1741 ScopedJniThreadState ts(env);
1742 UNIMPLEMENTED(FATAL);
1743 return NULL;
1744 }
1745
1746 static void ReleaseStringChars(JNIEnv* env, jstring str, const jchar* chars) {
1747 ScopedJniThreadState ts(env);
1748 UNIMPLEMENTED(FATAL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
1751 static const char* GetStringUTFChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1752 ScopedJniThreadState ts(env);
1753 UNIMPLEMENTED(FATAL);
1754 return NULL;
1755 }
1756
1757 static void ReleaseStringUTFChars(JNIEnv* env, jstring str, const char* chars) {
1758 ScopedJniThreadState ts(env);
1759 UNIMPLEMENTED(FATAL);
1760 }
1761
Elliott Hughesb465ab02011-08-24 11:21:21 -07001762 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
1763 ScopedJniThreadState ts(env);
1764 UNIMPLEMENTED(FATAL);
1765 return NULL;
1766 }
1767
1768 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
1769 ScopedJniThreadState ts(env);
1770 UNIMPLEMENTED(FATAL);
1771 }
1772
Elliott Hughesbd935992011-08-22 11:59:34 -07001773 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001775 Object* obj = Decode<Object*>(ts, java_array);
1776 CHECK(obj->IsArray()); // TODO: ReportJniError
1777 Array* array = obj->AsArray();
1778 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001783 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1784 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
1787 static void SetObjectArrayElement(JNIEnv* env,
1788 jobjectArray java_array, jsize index, jobject java_value) {
1789 ScopedJniThreadState ts(env);
1790 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1791 Object* value = Decode<Object*>(ts, java_value);
1792 array->Set(index, value);
1793 }
1794
1795 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1796 ScopedJniThreadState ts(env);
1797 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1798 }
1799
1800 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1801 ScopedJniThreadState ts(env);
1802 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1803 }
1804
1805 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1806 ScopedJniThreadState ts(env);
1807 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1808 }
1809
1810 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1811 ScopedJniThreadState ts(env);
1812 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1813 }
1814
1815 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1816 ScopedJniThreadState ts(env);
1817 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1818 }
1819
1820 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1821 ScopedJniThreadState ts(env);
1822 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1823 }
1824
1825 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1826 ScopedJniThreadState ts(env);
1827 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1828 }
1829
1830 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1831 ScopedJniThreadState ts(env);
1832 CHECK_GE(length, 0); // TODO: ReportJniError
1833
1834 // Compute the array class corresponding to the given element class.
1835 Class* element_class = Decode<Class*>(ts, element_jclass);
1836 std::string descriptor;
1837 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001838 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001839
1840 // Find the class.
1841 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1842 // TODO: need to get the appropriate ClassLoader.
1843 Class* array_class = class_linker->FindClass(descriptor, NULL);
1844 if (array_class == NULL) {
1845 return NULL;
1846 }
1847
1848 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1849 CHECK(initial_element == NULL); // TODO: support initial_element
1850 return AddLocalReference<jobjectArray>(ts, result);
1851 }
1852
1853 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1854 ScopedJniThreadState ts(env);
1855 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1856 }
1857
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1859 ScopedJniThreadState ts(env);
1860 UNIMPLEMENTED(FATAL);
1861 return NULL;
1862 }
1863
1864 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1865 ScopedJniThreadState ts(env);
1866 UNIMPLEMENTED(FATAL);
1867 }
1868
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1870 jbooleanArray array, jboolean* isCopy) {
1871 ScopedJniThreadState ts(env);
1872 UNIMPLEMENTED(FATAL);
1873 return NULL;
1874 }
1875
1876 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1877 ScopedJniThreadState ts(env);
1878 UNIMPLEMENTED(FATAL);
1879 return NULL;
1880 }
1881
1882 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1883 ScopedJniThreadState ts(env);
1884 UNIMPLEMENTED(FATAL);
1885 return NULL;
1886 }
1887
1888 static jshort* GetShortArrayElements(JNIEnv* env,
1889 jshortArray array, jboolean* isCopy) {
1890 ScopedJniThreadState ts(env);
1891 UNIMPLEMENTED(FATAL);
1892 return NULL;
1893 }
1894
1895 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1896 ScopedJniThreadState ts(env);
1897 UNIMPLEMENTED(FATAL);
1898 return NULL;
1899 }
1900
1901 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1902 ScopedJniThreadState ts(env);
1903 UNIMPLEMENTED(FATAL);
1904 return NULL;
1905 }
1906
1907 static jfloat* GetFloatArrayElements(JNIEnv* env,
1908 jfloatArray array, jboolean* isCopy) {
1909 ScopedJniThreadState ts(env);
1910 UNIMPLEMENTED(FATAL);
1911 return NULL;
1912 }
1913
1914 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1915 jdoubleArray array, jboolean* isCopy) {
1916 ScopedJniThreadState ts(env);
1917 UNIMPLEMENTED(FATAL);
1918 return NULL;
1919 }
1920
1921 static void ReleaseBooleanArrayElements(JNIEnv* env,
1922 jbooleanArray array, jboolean* elems, jint mode) {
1923 ScopedJniThreadState ts(env);
1924 UNIMPLEMENTED(FATAL);
1925 }
1926
1927 static void ReleaseByteArrayElements(JNIEnv* env,
1928 jbyteArray array, jbyte* elems, jint mode) {
1929 ScopedJniThreadState ts(env);
1930 UNIMPLEMENTED(FATAL);
1931 }
1932
1933 static void ReleaseCharArrayElements(JNIEnv* env,
1934 jcharArray array, jchar* elems, jint mode) {
1935 ScopedJniThreadState ts(env);
1936 UNIMPLEMENTED(FATAL);
1937 }
1938
1939 static void ReleaseShortArrayElements(JNIEnv* env,
1940 jshortArray array, jshort* elems, jint mode) {
1941 ScopedJniThreadState ts(env);
1942 UNIMPLEMENTED(FATAL);
1943 }
1944
1945 static void ReleaseIntArrayElements(JNIEnv* env,
1946 jintArray array, jint* elems, jint mode) {
1947 ScopedJniThreadState ts(env);
1948 UNIMPLEMENTED(FATAL);
1949 }
1950
1951 static void ReleaseLongArrayElements(JNIEnv* env,
1952 jlongArray array, jlong* elems, jint mode) {
1953 ScopedJniThreadState ts(env);
1954 UNIMPLEMENTED(FATAL);
1955 }
1956
1957 static void ReleaseFloatArrayElements(JNIEnv* env,
1958 jfloatArray array, jfloat* elems, jint mode) {
1959 ScopedJniThreadState ts(env);
1960 UNIMPLEMENTED(FATAL);
1961 }
1962
1963 static void ReleaseDoubleArrayElements(JNIEnv* env,
1964 jdoubleArray array, jdouble* elems, jint mode) {
1965 ScopedJniThreadState ts(env);
1966 UNIMPLEMENTED(FATAL);
1967 }
1968
Elliott Hughes814e4032011-08-23 12:07:56 -07001969 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001971 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes814e4032011-08-23 12:07:56 -07001974 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes814e4032011-08-23 12:07:56 -07001979 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001981 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes814e4032011-08-23 12:07:56 -07001984 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001986 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes814e4032011-08-23 12:07:56 -07001989 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes814e4032011-08-23 12:07:56 -07001994 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes814e4032011-08-23 12:07:56 -07001999 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes814e4032011-08-23 12:07:56 -07002004 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002006 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes814e4032011-08-23 12:07:56 -07002009 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002011 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes814e4032011-08-23 12:07:56 -07002014 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002016 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes814e4032011-08-23 12:07:56 -07002019 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002021 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes5174fe62011-08-23 15:12:35 -07002049 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002051 Class* c = Decode<Class*>(ts, java_class);
2052
2053 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2054
2055 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 const char* name = methods[i].name;
2057 const char* sig = methods[i].signature;
2058
2059 if (*sig == '!') {
2060 // TODO: fast jni. it's too noisy to log all these.
2061 ++sig;
2062 }
2063
Elliott Hughes5174fe62011-08-23 15:12:35 -07002064 Method* m = c->FindDirectMethod(name, sig);
2065 if (m == NULL) {
2066 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002068 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002070 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2072 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002073 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002075 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002077 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2079 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002080 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 return JNI_ERR;
2082 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002083
2084 if (vm->verbose_jni) {
2085 LOG(INFO) << "[Registering JNI native method "
2086 << PrettyMethod(m, true) << "]";
2087 }
2088
2089 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091 return JNI_OK;
2092 }
2093
Elliott Hughes5174fe62011-08-23 15:12:35 -07002094 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002096 Class* c = Decode<Class*>(ts, java_class);
2097
2098 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2099 if (vm->verbose_jni) {
2100 LOG(INFO) << "[Unregistering JNI native methods for "
2101 << PrettyDescriptor(c->GetDescriptor()) << "]";
2102 }
2103
2104 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2105 Method* m = c->GetDirectMethod(i);
2106 if (m->IsNative()) {
2107 m->UnregisterNative();
2108 }
2109 }
2110 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2111 Method* m = c->GetVirtualMethod(i);
2112 if (m->IsNative()) {
2113 m->UnregisterNative();
2114 }
2115 }
2116
2117 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes72025e52011-08-23 17:50:30 -07002120 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002122 Decode<Object*>(ts, java_object)->MonitorEnter();
2123 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes72025e52011-08-23 17:50:30 -07002126 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002128 Decode<Object*>(ts, java_object)->MonitorEnter();
2129 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
2132 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2133 ScopedJniThreadState ts(env);
2134 Runtime* runtime = Runtime::Current();
2135 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002136 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 } else {
2138 *vm = NULL;
2139 }
2140 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2141 }
2142
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2144 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002145
2146 // The address may not be NULL, and the capacity must be > 0.
2147 CHECK(address != NULL);
2148 CHECK_GT(capacity, 0);
2149
2150 jclass buffer_class = GetDirectByteBufferClass(env);
2151 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2152 if (mid == NULL) {
2153 return NULL;
2154 }
2155
2156 // At the moment, the Java side is limited to 32 bits.
2157 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2158 CHECK_LE(capacity, 0xffffffff);
2159 jint address_arg = reinterpret_cast<jint>(address);
2160 jint capacity_arg = static_cast<jint>(capacity);
2161
2162 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2163 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughesb465ab02011-08-24 11:21:21 -07002166 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002168 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2169 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughesb465ab02011-08-24 11:21:21 -07002172 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2175 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughesb465ab02011-08-24 11:21:21 -07002178 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002180
2181 CHECK(java_object != NULL);
2182
2183 // Do we definitely know what kind of reference this is?
2184 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2185 IndirectRefKind kind = GetIndirectRefKind(ref);
2186 switch (kind) {
2187 case kLocal:
2188 return JNILocalRefType;
2189 case kGlobal:
2190 return JNIGlobalRefType;
2191 case kWeakGlobal:
2192 return JNIWeakGlobalRefType;
2193 case kSirtOrInvalid:
2194 // Is it in a stack IRT?
2195 if (ts.Self()->SirtContains(java_object)) {
2196 return JNILocalRefType;
2197 }
2198
2199 // If we're handing out direct pointers, check whether it's a direct pointer
2200 // to a local reference.
2201 // TODO: replace 'false' with the replacement for gDvmJni.workAroundAppJniBugs
2202 if (false && Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
2203 if (ts.Env()->locals.Contains(java_object)) {
2204 return JNILocalRefType;
2205 }
2206 }
2207
2208 return JNIInvalidRefType;
2209 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002212
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002213static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002214 NULL, // reserved0.
2215 NULL, // reserved1.
2216 NULL, // reserved2.
2217 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 JNI::GetVersion,
2219 JNI::DefineClass,
2220 JNI::FindClass,
2221 JNI::FromReflectedMethod,
2222 JNI::FromReflectedField,
2223 JNI::ToReflectedMethod,
2224 JNI::GetSuperclass,
2225 JNI::IsAssignableFrom,
2226 JNI::ToReflectedField,
2227 JNI::Throw,
2228 JNI::ThrowNew,
2229 JNI::ExceptionOccurred,
2230 JNI::ExceptionDescribe,
2231 JNI::ExceptionClear,
2232 JNI::FatalError,
2233 JNI::PushLocalFrame,
2234 JNI::PopLocalFrame,
2235 JNI::NewGlobalRef,
2236 JNI::DeleteGlobalRef,
2237 JNI::DeleteLocalRef,
2238 JNI::IsSameObject,
2239 JNI::NewLocalRef,
2240 JNI::EnsureLocalCapacity,
2241 JNI::AllocObject,
2242 JNI::NewObject,
2243 JNI::NewObjectV,
2244 JNI::NewObjectA,
2245 JNI::GetObjectClass,
2246 JNI::IsInstanceOf,
2247 JNI::GetMethodID,
2248 JNI::CallObjectMethod,
2249 JNI::CallObjectMethodV,
2250 JNI::CallObjectMethodA,
2251 JNI::CallBooleanMethod,
2252 JNI::CallBooleanMethodV,
2253 JNI::CallBooleanMethodA,
2254 JNI::CallByteMethod,
2255 JNI::CallByteMethodV,
2256 JNI::CallByteMethodA,
2257 JNI::CallCharMethod,
2258 JNI::CallCharMethodV,
2259 JNI::CallCharMethodA,
2260 JNI::CallShortMethod,
2261 JNI::CallShortMethodV,
2262 JNI::CallShortMethodA,
2263 JNI::CallIntMethod,
2264 JNI::CallIntMethodV,
2265 JNI::CallIntMethodA,
2266 JNI::CallLongMethod,
2267 JNI::CallLongMethodV,
2268 JNI::CallLongMethodA,
2269 JNI::CallFloatMethod,
2270 JNI::CallFloatMethodV,
2271 JNI::CallFloatMethodA,
2272 JNI::CallDoubleMethod,
2273 JNI::CallDoubleMethodV,
2274 JNI::CallDoubleMethodA,
2275 JNI::CallVoidMethod,
2276 JNI::CallVoidMethodV,
2277 JNI::CallVoidMethodA,
2278 JNI::CallNonvirtualObjectMethod,
2279 JNI::CallNonvirtualObjectMethodV,
2280 JNI::CallNonvirtualObjectMethodA,
2281 JNI::CallNonvirtualBooleanMethod,
2282 JNI::CallNonvirtualBooleanMethodV,
2283 JNI::CallNonvirtualBooleanMethodA,
2284 JNI::CallNonvirtualByteMethod,
2285 JNI::CallNonvirtualByteMethodV,
2286 JNI::CallNonvirtualByteMethodA,
2287 JNI::CallNonvirtualCharMethod,
2288 JNI::CallNonvirtualCharMethodV,
2289 JNI::CallNonvirtualCharMethodA,
2290 JNI::CallNonvirtualShortMethod,
2291 JNI::CallNonvirtualShortMethodV,
2292 JNI::CallNonvirtualShortMethodA,
2293 JNI::CallNonvirtualIntMethod,
2294 JNI::CallNonvirtualIntMethodV,
2295 JNI::CallNonvirtualIntMethodA,
2296 JNI::CallNonvirtualLongMethod,
2297 JNI::CallNonvirtualLongMethodV,
2298 JNI::CallNonvirtualLongMethodA,
2299 JNI::CallNonvirtualFloatMethod,
2300 JNI::CallNonvirtualFloatMethodV,
2301 JNI::CallNonvirtualFloatMethodA,
2302 JNI::CallNonvirtualDoubleMethod,
2303 JNI::CallNonvirtualDoubleMethodV,
2304 JNI::CallNonvirtualDoubleMethodA,
2305 JNI::CallNonvirtualVoidMethod,
2306 JNI::CallNonvirtualVoidMethodV,
2307 JNI::CallNonvirtualVoidMethodA,
2308 JNI::GetFieldID,
2309 JNI::GetObjectField,
2310 JNI::GetBooleanField,
2311 JNI::GetByteField,
2312 JNI::GetCharField,
2313 JNI::GetShortField,
2314 JNI::GetIntField,
2315 JNI::GetLongField,
2316 JNI::GetFloatField,
2317 JNI::GetDoubleField,
2318 JNI::SetObjectField,
2319 JNI::SetBooleanField,
2320 JNI::SetByteField,
2321 JNI::SetCharField,
2322 JNI::SetShortField,
2323 JNI::SetIntField,
2324 JNI::SetLongField,
2325 JNI::SetFloatField,
2326 JNI::SetDoubleField,
2327 JNI::GetStaticMethodID,
2328 JNI::CallStaticObjectMethod,
2329 JNI::CallStaticObjectMethodV,
2330 JNI::CallStaticObjectMethodA,
2331 JNI::CallStaticBooleanMethod,
2332 JNI::CallStaticBooleanMethodV,
2333 JNI::CallStaticBooleanMethodA,
2334 JNI::CallStaticByteMethod,
2335 JNI::CallStaticByteMethodV,
2336 JNI::CallStaticByteMethodA,
2337 JNI::CallStaticCharMethod,
2338 JNI::CallStaticCharMethodV,
2339 JNI::CallStaticCharMethodA,
2340 JNI::CallStaticShortMethod,
2341 JNI::CallStaticShortMethodV,
2342 JNI::CallStaticShortMethodA,
2343 JNI::CallStaticIntMethod,
2344 JNI::CallStaticIntMethodV,
2345 JNI::CallStaticIntMethodA,
2346 JNI::CallStaticLongMethod,
2347 JNI::CallStaticLongMethodV,
2348 JNI::CallStaticLongMethodA,
2349 JNI::CallStaticFloatMethod,
2350 JNI::CallStaticFloatMethodV,
2351 JNI::CallStaticFloatMethodA,
2352 JNI::CallStaticDoubleMethod,
2353 JNI::CallStaticDoubleMethodV,
2354 JNI::CallStaticDoubleMethodA,
2355 JNI::CallStaticVoidMethod,
2356 JNI::CallStaticVoidMethodV,
2357 JNI::CallStaticVoidMethodA,
2358 JNI::GetStaticFieldID,
2359 JNI::GetStaticObjectField,
2360 JNI::GetStaticBooleanField,
2361 JNI::GetStaticByteField,
2362 JNI::GetStaticCharField,
2363 JNI::GetStaticShortField,
2364 JNI::GetStaticIntField,
2365 JNI::GetStaticLongField,
2366 JNI::GetStaticFloatField,
2367 JNI::GetStaticDoubleField,
2368 JNI::SetStaticObjectField,
2369 JNI::SetStaticBooleanField,
2370 JNI::SetStaticByteField,
2371 JNI::SetStaticCharField,
2372 JNI::SetStaticShortField,
2373 JNI::SetStaticIntField,
2374 JNI::SetStaticLongField,
2375 JNI::SetStaticFloatField,
2376 JNI::SetStaticDoubleField,
2377 JNI::NewString,
2378 JNI::GetStringLength,
2379 JNI::GetStringChars,
2380 JNI::ReleaseStringChars,
2381 JNI::NewStringUTF,
2382 JNI::GetStringUTFLength,
2383 JNI::GetStringUTFChars,
2384 JNI::ReleaseStringUTFChars,
2385 JNI::GetArrayLength,
2386 JNI::NewObjectArray,
2387 JNI::GetObjectArrayElement,
2388 JNI::SetObjectArrayElement,
2389 JNI::NewBooleanArray,
2390 JNI::NewByteArray,
2391 JNI::NewCharArray,
2392 JNI::NewShortArray,
2393 JNI::NewIntArray,
2394 JNI::NewLongArray,
2395 JNI::NewFloatArray,
2396 JNI::NewDoubleArray,
2397 JNI::GetBooleanArrayElements,
2398 JNI::GetByteArrayElements,
2399 JNI::GetCharArrayElements,
2400 JNI::GetShortArrayElements,
2401 JNI::GetIntArrayElements,
2402 JNI::GetLongArrayElements,
2403 JNI::GetFloatArrayElements,
2404 JNI::GetDoubleArrayElements,
2405 JNI::ReleaseBooleanArrayElements,
2406 JNI::ReleaseByteArrayElements,
2407 JNI::ReleaseCharArrayElements,
2408 JNI::ReleaseShortArrayElements,
2409 JNI::ReleaseIntArrayElements,
2410 JNI::ReleaseLongArrayElements,
2411 JNI::ReleaseFloatArrayElements,
2412 JNI::ReleaseDoubleArrayElements,
2413 JNI::GetBooleanArrayRegion,
2414 JNI::GetByteArrayRegion,
2415 JNI::GetCharArrayRegion,
2416 JNI::GetShortArrayRegion,
2417 JNI::GetIntArrayRegion,
2418 JNI::GetLongArrayRegion,
2419 JNI::GetFloatArrayRegion,
2420 JNI::GetDoubleArrayRegion,
2421 JNI::SetBooleanArrayRegion,
2422 JNI::SetByteArrayRegion,
2423 JNI::SetCharArrayRegion,
2424 JNI::SetShortArrayRegion,
2425 JNI::SetIntArrayRegion,
2426 JNI::SetLongArrayRegion,
2427 JNI::SetFloatArrayRegion,
2428 JNI::SetDoubleArrayRegion,
2429 JNI::RegisterNatives,
2430 JNI::UnregisterNatives,
2431 JNI::MonitorEnter,
2432 JNI::MonitorExit,
2433 JNI::GetJavaVM,
2434 JNI::GetStringRegion,
2435 JNI::GetStringUTFRegion,
2436 JNI::GetPrimitiveArrayCritical,
2437 JNI::ReleasePrimitiveArrayCritical,
2438 JNI::GetStringCritical,
2439 JNI::ReleaseStringCritical,
2440 JNI::NewWeakGlobalRef,
2441 JNI::DeleteWeakGlobalRef,
2442 JNI::ExceptionCheck,
2443 JNI::NewDirectByteBuffer,
2444 JNI::GetDirectBufferAddress,
2445 JNI::GetDirectBufferCapacity,
2446 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002447};
2448
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002449static const size_t kMonitorsInitial = 32; // Arbitrary.
2450static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2451
2452static const size_t kLocalsInitial = 64; // Arbitrary.
2453static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002454
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002455JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002456 : self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002457 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002458 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002459 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2460 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002461 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002462}
2463
Carl Shapiroea4dca82011-08-01 13:45:38 -07002464// JNI Invocation interface.
2465
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002466extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2467 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2468 if (args->version < JNI_VERSION_1_2) {
2469 return JNI_EVERSION;
2470 }
2471 Runtime::Options options;
2472 for (int i = 0; i < args->nOptions; ++i) {
2473 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002474 options.push_back(std::make_pair(StringPiece(option->optionString),
2475 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002476 }
2477 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002478 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002479 if (runtime == NULL) {
2480 return JNI_ERR;
2481 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002482 *p_env = Thread::Current()->GetJniEnv();
2483 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002484 return JNI_OK;
2485 }
2486}
2487
Elliott Hughesf2682d52011-08-15 16:37:04 -07002488extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002489 Runtime* runtime = Runtime::Current();
2490 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002491 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002492 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002493 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002494 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002495 }
2496 return JNI_OK;
2497}
2498
2499// Historically unsupported.
2500extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2501 return JNI_ERR;
2502}
2503
Elliott Hughescdf53122011-08-19 15:46:09 -07002504class JII {
2505 public:
2506 static jint DestroyJavaVM(JavaVM* vm) {
2507 if (vm == NULL) {
2508 return JNI_ERR;
2509 } else {
2510 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2511 delete raw_vm->runtime;
2512 return JNI_OK;
2513 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002514 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002515
Elliott Hughescdf53122011-08-19 15:46:09 -07002516 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2517 if (vm == NULL || p_env == NULL) {
2518 return JNI_ERR;
2519 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002520 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2521 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002522 const char* name = NULL;
2523 if (thr_args != NULL) {
2524 // TODO: check version
2525 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2526 // TODO: thread group
2527 }
2528 bool success = runtime->AttachCurrentThread(name, p_env);
2529 if (!success) {
2530 return JNI_ERR;
2531 } else {
2532 return JNI_OK;
2533 }
2534 }
2535
2536 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2537 if (vm == NULL || p_env == NULL) {
2538 return JNI_ERR;
2539 }
2540 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2541 Runtime* runtime = raw_vm->runtime;
2542 const char* name = NULL;
2543 if (thr_args != NULL) {
2544 // TODO: check version
2545 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2546 // TODO: thread group
2547 }
2548 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2549 if (!success) {
2550 return JNI_ERR;
2551 } else {
2552 return JNI_OK;
2553 }
2554 }
2555
2556 static jint DetachCurrentThread(JavaVM* vm) {
2557 if (vm == NULL) {
2558 return JNI_ERR;
2559 } else {
2560 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2561 Runtime* runtime = raw_vm->runtime;
2562 runtime->DetachCurrentThread();
2563 return JNI_OK;
2564 }
2565 }
2566
2567 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2568 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2569 return JNI_EVERSION;
2570 }
2571 if (vm == NULL || env == NULL) {
2572 return JNI_ERR;
2573 }
2574 Thread* thread = Thread::Current();
2575 if (thread == NULL) {
2576 *env = NULL;
2577 return JNI_EDETACHED;
2578 }
2579 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580 return JNI_OK;
2581 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002582};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583
Elliott Hughesf2682d52011-08-15 16:37:04 -07002584struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002585 NULL, // reserved0
2586 NULL, // reserved1
2587 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 JII::DestroyJavaVM,
2589 JII::AttachCurrentThread,
2590 JII::DetachCurrentThread,
2591 JII::GetEnv,
2592 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002593};
2594
Elliott Hughesbbd76712011-08-17 10:25:24 -07002595static const size_t kPinTableInitialSize = 16;
2596static const size_t kPinTableMaxSize = 1024;
2597
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002598static const size_t kGlobalsInitial = 512; // Arbitrary.
2599static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2600
2601static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2602static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2603
Elliott Hughes0af55432011-08-17 18:37:28 -07002604JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002605 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002606 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002607 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002608 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002609 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002610 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002611 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002612 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002613 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002614}
2615
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002616JavaVMExt::~JavaVMExt() {
2617 delete globals_lock;
2618 delete weak_globals_lock;
2619}
2620
Elliott Hughescdf53122011-08-19 15:46:09 -07002621/*
2622 * Load native code from the specified absolute pathname. Per the spec,
2623 * if we've already loaded a library with the specified pathname, we
2624 * return without doing anything.
2625 *
2626 * TODO? for better results we should absolutify the pathname. For fully
2627 * correct results we should stat to get the inode and compare that. The
2628 * existing implementation is fine so long as everybody is using
2629 * System.loadLibrary.
2630 *
2631 * The library will be associated with the specified class loader. The JNI
2632 * spec says we can't load the same library into more than one class loader.
2633 *
2634 * Returns "true" on success. On failure, sets *detail to a
2635 * human-readable description of the error or NULL if no detail is
2636 * available; ownership of the string is transferred to the caller.
2637 */
Elliott Hughes814e4032011-08-23 12:07:56 -07002638bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, char** detail) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002639 *detail = NULL;
2640
2641 // See if we've already loaded this library. If we have, and the class loader
2642 // matches, return successfully without doing anything.
2643 SharedLibrary* library = libraries[path];
2644 if (library != NULL) {
2645 if (library->GetClassLoader() != class_loader) {
2646 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2647 << "ClassLoader " << library->GetClassLoader() << "; "
2648 << "can't open in " << class_loader;
2649 *detail = strdup("already opened by different ClassLoader");
2650 return false;
2651 }
2652 if (verbose_jni) {
2653 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2654 << "ClassLoader " << class_loader << "]";
2655 }
2656 if (!library->CheckOnLoadResult(this)) {
2657 *detail = strdup("JNI_OnLoad failed before");
2658 return false;
2659 }
2660 return true;
2661 }
2662
2663 // Open the shared library. Because we're using a full path, the system
2664 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2665 // resolve this library's dependencies though.)
2666
2667 // Failures here are expected when java.library.path has several entries
2668 // and we have to hunt for the lib.
2669
2670 // The current version of the dynamic linker prints detailed information
2671 // about dlopen() failures. Some things to check if the message is
2672 // cryptic:
2673 // - make sure the library exists on the device
2674 // - verify that the right path is being opened (the debug log message
2675 // above can help with that)
2676 // - check to see if the library is valid (e.g. not zero bytes long)
2677 // - check config/prelink-linux-arm.map to ensure that the library
2678 // is listed and is not being overrun by the previous entry (if
2679 // loading suddenly stops working on a prelinked library, this is
2680 // a good one to check)
2681 // - write a trivial app that calls sleep() then dlopen(), attach
2682 // to it with "strace -p <pid>" while it sleeps, and watch for
2683 // attempts to open nonexistent dependent shared libs
2684
2685 // TODO: automate some of these checks!
2686
2687 // This can execute slowly for a large library on a busy system, so we
2688 // want to switch from RUNNING to VMWAIT while it executes. This allows
2689 // the GC to ignore us.
2690 Thread* self = Thread::Current();
2691 Thread::State old_state = self->GetState();
2692 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2693 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2694 self->SetState(old_state);
2695
2696 if (verbose_jni) {
2697 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2698 }
2699
2700 if (handle == NULL) {
2701 *detail = strdup(dlerror());
2702 return false;
2703 }
2704
2705 // Create a new entry.
2706 library = new SharedLibrary(path, handle, class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002707
2708 libraries[path] = library;
2709
2710 // if (pNewEntry != pActualEntry) {
2711 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2712 // freeSharedLibEntry(pNewEntry);
2713 // return CheckOnLoadResult(this, pActualEntry);
2714 // } else
2715 {
2716 if (verbose_jni) {
2717 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2718 }
2719
2720 bool result = true;
2721 void* sym = dlsym(handle, "JNI_OnLoad");
2722 if (sym == NULL) {
2723 if (verbose_jni) {
2724 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2725 }
2726 } else {
2727 // Call JNI_OnLoad. We have to override the current class
2728 // loader, which will always be "null" since the stuff at the
2729 // top of the stack is around Runtime.loadLibrary(). (See
2730 // the comments in the JNI FindClass function.)
Elliott Hughescdf53122011-08-19 15:46:09 -07002731 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2732 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Elliott Hughes814e4032011-08-23 12:07:56 -07002733 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2734 self->SetClassLoaderOverride(class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002735
2736 old_state = self->GetState();
2737 self->SetState(Thread::kNative);
2738 if (verbose_jni) {
2739 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2740 }
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002741 int version = (*jni_on_load)(this, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 self->SetState(old_state);
2743
Elliott Hughes814e4032011-08-23 12:07:56 -07002744 self->SetClassLoaderOverride(old_class_loader);;
Elliott Hughescdf53122011-08-19 15:46:09 -07002745
2746 if (version != JNI_VERSION_1_2 &&
2747 version != JNI_VERSION_1_4 &&
2748 version != JNI_VERSION_1_6) {
2749 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2750 << "bad version: " << version;
2751 // It's unwise to call dlclose() here, but we can mark it
2752 // as bad and ensure that future load attempts will fail.
2753 // We don't know how far JNI_OnLoad got, so there could
2754 // be some partially-initialized stuff accessible through
2755 // newly-registered native method calls. We could try to
2756 // unregister them, but that doesn't seem worthwhile.
2757 result = false;
2758 } else {
2759 if (verbose_jni) {
2760 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2761 << " from JNI_OnLoad in \"" << path << "\"]";
2762 }
2763 }
2764 }
2765
2766 library->SetResult(result);
2767 return result;
2768 }
2769}
2770
Ian Rogersdf20fe02011-07-20 20:34:16 -07002771} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002772
2773std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2774 switch (rhs) {
2775 case JNIInvalidRefType:
2776 os << "JNIInvalidRefType";
2777 return os;
2778 case JNILocalRefType:
2779 os << "JNILocalRefType";
2780 return os;
2781 case JNIGlobalRefType:
2782 os << "JNIGlobalRefType";
2783 return os;
2784 case JNIWeakGlobalRefType:
2785 os << "JNIWeakGlobalRefType";
2786 return os;
2787 }
2788}