blob: a95ff81f222add2c4cad0d396cfd9b32ed64bca2 [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 Hughes75770752011-08-24 17:52:38 -0700168 JavaVMExt* Vm() {
169 return env_->vm;
170 }
171
Elliott Hughesb20a5542011-08-12 18:03:12 -0700172 private:
173 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -0700174 // TODO: need replacement for gDvmJni.
175 bool workAroundAppJniBugs = true;
176 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
177 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
178 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -0700179 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
180 << " used on " << *self;
181 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -0700182 }
183 return self;
184 }
185
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700186 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -0700187 Thread* self_;
188 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
189};
190
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700191/*
192 * Add a local reference for an object to the current stack frame. When
193 * the native function returns, the reference will be discarded.
194 *
195 * We need to allow the same reference to be added multiple times.
196 *
197 * This will be called on otherwise unreferenced objects. We cannot do
198 * GC allocations here, and it's best if we don't grab a mutex.
199 *
200 * Returns the local reference (currently just the same pointer that was
201 * passed in), or NULL on failure.
202 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700203template<typename T>
204T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700205 if (obj == NULL) {
206 return NULL;
207 }
208
209 IndirectReferenceTable& locals = ts.Env()->locals;
210
211 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
212 IndirectRef ref = locals.Add(cookie, obj);
213 if (ref == NULL) {
214 // TODO: just change Add's DCHECK to CHECK and lose this?
215 locals.Dump();
216 LOG(FATAL) << "Failed adding to JNI local reference table "
217 << "(has " << locals.Capacity() << " entries)";
218 // TODO: dvmDumpThread(dvmThreadSelf(), false);
219 }
220
221#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
222 if (ts.Env()->check_jni) {
223 size_t entry_count = locals.Capacity();
224 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700225 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700226 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700227 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700228 locals.Dump();
229 // TODO: dvmDumpThread(dvmThreadSelf(), false);
230 // dvmAbort();
231 }
232 }
233#endif
234
235 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
236 // Hand out direct pointers to support broken old apps.
237 return reinterpret_cast<T>(obj);
238 }
239
240 return reinterpret_cast<T>(ref);
241}
242
Elliott Hughescdf53122011-08-19 15:46:09 -0700243jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
244 if (obj == NULL) {
245 return NULL;
246 }
Elliott Hughes75770752011-08-24 17:52:38 -0700247 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 IndirectReferenceTable& weak_globals = vm->weak_globals;
249 MutexLock mu(vm->weak_globals_lock);
250 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
251 return reinterpret_cast<jweak>(ref);
252}
253
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700254template<typename T>
255T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700256 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700257}
258
Elliott Hughescdf53122011-08-19 15:46:09 -0700259Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
260 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
261}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700262
Elliott Hughescdf53122011-08-19 15:46:09 -0700263Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
264 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700265}
266
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700267byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700268 size_t num_bytes = method->NumArgArrayBytes();
269 scoped_array<byte> arg_array(new byte[num_bytes]);
270 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700271 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700272 switch (shorty[i]) {
273 case 'Z':
274 case 'B':
275 case 'C':
276 case 'S':
277 case 'I':
278 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
279 offset += 4;
280 break;
281 case 'F':
282 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
283 offset += 4;
284 break;
285 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700286 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700287 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
288 offset += sizeof(Object*);
289 break;
290 }
291 case 'D':
292 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
293 offset += 8;
294 break;
295 case 'J':
296 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
297 offset += 8;
298 break;
299 }
300 }
301 return arg_array.release();
302}
303
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700304byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700305 size_t num_bytes = method->NumArgArrayBytes();
306 scoped_array<byte> arg_array(new byte[num_bytes]);
307 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700308 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700309 switch (shorty[i]) {
310 case 'Z':
311 case 'B':
312 case 'C':
313 case 'S':
314 case 'I':
315 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
316 offset += 4;
317 break;
318 case 'F':
319 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
320 offset += 4;
321 break;
322 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700323 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700324 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
325 offset += sizeof(Object*);
326 break;
327 }
328 case 'D':
329 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
330 offset += 8;
331 break;
332 case 'J':
333 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
334 offset += 8;
335 break;
336 }
337 }
338 return arg_array.release();
339}
340
Elliott Hughes72025e52011-08-23 17:50:30 -0700341JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
342 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700343 Thread* self = ts.Self();
344
345 // Push a transition back into managed code onto the linked list in thread
346 CHECK_EQ(Thread::kRunnable, self->GetState());
347 NativeToManagedRecord record;
348 self->PushNativeToManagedRecord(&record);
349
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700350 // Call the invoke stub associated with the method
351 // Pass everything as arguments
352 const Method::InvokeStub* stub = method->GetInvokeStub();
353 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700354
355#ifdef __arm__
356 // Compile...
357 // TODO: not here!
358 oatCompileMethod(method, kThumb2);
359#endif
360
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700361 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700362 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700363 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700364 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700365 LOG(WARNING) << "Not invoking method with no associated code: "
366 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700367 result.j = 0;
368 }
buzbeec143c552011-08-20 17:38:58 -0700369
Ian Rogers6de08602011-08-19 14:52:39 -0700370 // Pop transition
371 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700372 return result;
373}
374
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700375JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700377 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700378 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700379 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700380 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700381}
382
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700383JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700385 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700386 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700387 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700388 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
389}
390
Elliott Hughes75770752011-08-24 17:52:38 -0700391Method* FindVirtualMethod(Object* receiver, Method* method) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700392 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
393}
394
395JValue InvokeVirtualWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
396 Object* receiver = Decode<Object*>(ts, obj);
397 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
398 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
399 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
400}
401
402JValue InvokeVirtualWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
403 Object* receiver = Decode<Object*>(ts, obj);
404 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
405 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
406 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700407}
408
Elliott Hughes6b436852011-08-12 10:16:44 -0700409// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
410// separated with slashes but aren't wrapped with "L;" like regular descriptors
411// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
412// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
413// supported names with dots too (such as "a.b.C").
414std::string NormalizeJniClassDescriptor(const char* name) {
415 std::string result;
416 // Add the missing "L;" if necessary.
417 if (name[0] == '[') {
418 result = name;
419 } else {
420 result += 'L';
421 result += name;
422 result += ';';
423 }
424 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700425 if (result.find('.') != std::string::npos) {
426 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
427 << "\"" << name << "\"";
428 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700429 }
430 return result;
431}
432
Elliott Hughescdf53122011-08-19 15:46:09 -0700433jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
434 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700435 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
436 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700437 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700438
439 Method* method = NULL;
440 if (is_static) {
441 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700442 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700443 method = c->FindVirtualMethod(name, sig);
444 if (method == NULL) {
445 // No virtual method matching the signature. Search declared
446 // private methods and constructors.
447 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700448 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700449 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700450
Elliott Hughescdf53122011-08-19 15:46:09 -0700451 if (method == NULL || method->IsStatic() != is_static) {
452 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700453 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700454 // TODO: try searching for the opposite kind of method from is_static
455 // for better diagnostics?
456 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700457 "no %s method %s", is_static ? "static" : "non-static",
458 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700459 return NULL;
460 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700461
Elliott Hughescdf53122011-08-19 15:46:09 -0700462 bool success = EnsureInvokeStub(method);
463 if (!success) {
464 // TODO: throw OutOfMemoryException
465 return NULL;
466 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700467
Elliott Hughescdf53122011-08-19 15:46:09 -0700468 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700469}
470
Elliott Hughescdf53122011-08-19 15:46:09 -0700471jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
472 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700473 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
474 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700475 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700476
477 Field* field = NULL;
478 if (is_static) {
479 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700480 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700481 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700482 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700483
Elliott Hughescdf53122011-08-19 15:46:09 -0700484 if (field == NULL) {
485 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700486 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700487 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
488 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700489 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700490 return NULL;
491 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700492
Elliott Hughescdf53122011-08-19 15:46:09 -0700493 jweak fid = AddWeakGlobalReference(ts, field);
494 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700495}
496
Elliott Hughes75770752011-08-24 17:52:38 -0700497void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
498 JavaVMExt* vm = ts.Vm();
499 MutexLock mu(vm->pins_lock);
500 vm->pin_table.Add(array);
501}
502
503void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
504 JavaVMExt* vm = ts.Vm();
505 MutexLock mu(vm->pins_lock);
506 vm->pin_table.Remove(array);
507}
508
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700509template<typename JniT, typename ArtT>
510JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
511 CHECK_GE(length, 0); // TODO: ReportJniError
512 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700513 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700514}
515
Elliott Hughes75770752011-08-24 17:52:38 -0700516template <typename ArrayT, typename CArrayT, typename ArtArrayT>
517CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
518 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
519 PinPrimitiveArray(ts, array);
520 if (is_copy != NULL) {
521 *is_copy = JNI_FALSE;
522 }
523 return array->GetData();
524}
525
526template <typename ArrayT>
527void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
528 if (mode != JNI_COMMIT) {
529 Array* array = Decode<Array*>(ts, java_array);
530 UnpinPrimitiveArray(ts, array);
531 }
532}
533
Elliott Hughes814e4032011-08-23 12:07:56 -0700534void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
535 std::string type(PrettyType(array));
536 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
537 "%s offset=%d length=%d %s.length=%d",
538 type.c_str(), start, length, identifier, array->GetLength());
539}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700540void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
541 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
542 "offset=%d length=%d string.length()=%d", start, length, array_length);
543}
Elliott Hughes814e4032011-08-23 12:07:56 -0700544
545template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700546void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700547 ArrayT* array = Decode<ArrayT*>(ts, java_array);
548 if (start < 0 || length < 0 || start + length > array->GetLength()) {
549 ThrowAIOOBE(ts, array, start, length, "src");
550 } else {
551 JavaT* data = array->GetData();
552 memcpy(buf, data + start, length * sizeof(JavaT));
553 }
554}
555
556template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700557void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700558 ArrayT* array = Decode<ArrayT*>(ts, java_array);
559 if (start < 0 || length < 0 || start + length > array->GetLength()) {
560 ThrowAIOOBE(ts, array, start, length, "dst");
561 } else {
562 JavaT* data = array->GetData();
563 memcpy(data + start, buf, length * sizeof(JavaT));
564 }
565}
566
Elliott Hughes75770752011-08-24 17:52:38 -0700567jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700568 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
569 CHECK(buffer_class.get() != NULL);
570 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
571}
572
Elliott Hughes75770752011-08-24 17:52:38 -0700573jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700574 static jclass buffer_class = InitDirectByteBufferClass(env);
575 return buffer_class;
576}
577
Elliott Hughes75770752011-08-24 17:52:38 -0700578jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
579 if (vm == NULL || p_env == NULL) {
580 return JNI_ERR;
581 }
582
583 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
584 JavaVMAttachArgs args;
585 if (thr_args == NULL) {
586 // Allow the v1.1 calling convention.
587 args.version = JNI_VERSION_1_2;
588 args.name = NULL;
589 args.group = NULL; // TODO: get "main" thread group
590 } else {
591 args.version = in_args->version;
592 args.name = in_args->name;
593 if (in_args->group != NULL) {
594 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
595 args.group = NULL; // TODO: decode in_args->group
596 } else {
597 args.group = NULL; // TODO: get "main" thread group
598 }
599 }
600 CHECK_GE(args.version, JNI_VERSION_1_2);
601
602 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
603 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
604}
605
Elliott Hughescdf53122011-08-19 15:46:09 -0700606} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608class JNI {
609 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700610
Elliott Hughescdf53122011-08-19 15:46:09 -0700611 static jint GetVersion(JNIEnv* env) {
612 ScopedJniThreadState ts(env);
613 return JNI_VERSION_1_6;
614 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
617 ScopedJniThreadState ts(env);
618 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700619 return NULL;
620 }
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jclass FindClass(JNIEnv* env, const char* name) {
623 ScopedJniThreadState ts(env);
624 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
625 std::string descriptor(NormalizeJniClassDescriptor(name));
626 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700627 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700628 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
633 ScopedJniThreadState ts(env);
634 Method* method = Decode<Method*>(ts, java_method);
635 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
639 ScopedJniThreadState ts(env);
640 Field* field = Decode<Field*>(ts, java_field);
641 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
645 ScopedJniThreadState ts(env);
646 Method* method = DecodeMethod(ts, mid);
647 return AddLocalReference<jobject>(ts, method);
648 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700649
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
651 ScopedJniThreadState ts(env);
652 Field* field = DecodeField(ts, fid);
653 return AddLocalReference<jobject>(ts, field);
654 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughes37f7a402011-08-22 18:56:01 -0700656 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
657 ScopedJniThreadState ts(env);
658 Object* o = Decode<Object*>(ts, java_object);
659 return AddLocalReference<jclass>(ts, o->GetClass());
660 }
661
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700662 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700664 Class* c = Decode<Class*>(ts, java_class);
665 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 Class* c1 = Decode<Class*>(ts, java_class1);
671 Class* c2 = Decode<Class*>(ts, java_class2);
672 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700677 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 if (jobj == NULL) {
679 // NB. JNI is different from regular Java instanceof in this respect
680 return JNI_TRUE;
681 } else {
682 Object* obj = Decode<Object*>(ts, jobj);
683 Class* klass = Decode<Class*>(ts, clazz);
684 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
685 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 static jint Throw(JNIEnv* env, jthrowable java_exception) {
689 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700690 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 if (exception == NULL) {
692 return JNI_ERR;
693 }
694 ts.Self()->SetException(exception);
695 return JNI_OK;
696 }
697
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700698 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700700 // TODO: check for a pending exception to decide what constructor to call.
701 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
702 if (mid == NULL) {
703 return JNI_ERR;
704 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
706 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 return JNI_ERR;
708 }
709
710 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 args[0].l = s.get();
712 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
713 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714 return JNI_ERR;
715 }
716
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700720
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 return JNI_OK;
722 }
723
724 static jboolean ExceptionCheck(JNIEnv* env) {
725 ScopedJniThreadState ts(env);
726 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
727 }
728
729 static void ExceptionClear(JNIEnv* env) {
730 ScopedJniThreadState ts(env);
731 ts.Self()->ClearException();
732 }
733
734 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700736
737 Thread* self = ts.Self();
738 Throwable* original_exception = self->GetException();
739 self->ClearException();
740
741 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
742 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
743 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
744 if (mid == NULL) {
745 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
746 << PrettyType(original_exception);
747 } else {
748 env->CallVoidMethod(exception.get(), mid);
749 if (self->IsExceptionPending()) {
750 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
751 << " thrown while calling printStackTrace";
752 self->ClearException();
753 }
754 }
755
756 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700758
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 static jthrowable ExceptionOccurred(JNIEnv* env) {
760 ScopedJniThreadState ts(env);
761 Object* exception = ts.Self()->GetException();
762 if (exception == NULL) {
763 return NULL;
764 } else {
765 // TODO: if adding a local reference failing causes the VM to abort
766 // then the following check will never occur.
767 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
768 if (localException == NULL) {
769 // We were unable to add a new local reference, and threw a new
770 // exception. We can't return "exception", because it's not a
771 // local reference. So we have to return NULL, indicating that
772 // there was no exception, even though it's pretty much raining
773 // exceptions in here.
774 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
775 }
776 return localException;
777 }
778 }
779
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 static void FatalError(JNIEnv* env, const char* msg) {
781 ScopedJniThreadState ts(env);
782 LOG(FATAL) << "JNI FatalError called: " << msg;
783 }
784
785 static jint PushLocalFrame(JNIEnv* env, jint cap) {
786 ScopedJniThreadState ts(env);
787 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
788 return JNI_OK;
789 }
790
791 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
792 ScopedJniThreadState ts(env);
793 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
794 return res;
795 }
796
Elliott Hughes72025e52011-08-23 17:50:30 -0700797 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
798 ScopedJniThreadState ts(env);
799 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
800 return 0;
801 }
802
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
804 ScopedJniThreadState ts(env);
805 if (obj == NULL) {
806 return NULL;
807 }
808
Elliott Hughes75770752011-08-24 17:52:38 -0700809 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 IndirectReferenceTable& globals = vm->globals;
811 MutexLock mu(vm->globals_lock);
812 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
813 return reinterpret_cast<jobject>(ref);
814 }
815
816 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
817 ScopedJniThreadState ts(env);
818 if (obj == NULL) {
819 return;
820 }
821
Elliott Hughes75770752011-08-24 17:52:38 -0700822 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 IndirectReferenceTable& globals = vm->globals;
824 MutexLock mu(vm->globals_lock);
825
826 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
827 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
828 << "failed to find entry";
829 }
830 }
831
832 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
833 ScopedJniThreadState ts(env);
834 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
835 }
836
837 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
838 ScopedJniThreadState ts(env);
839 if (obj == NULL) {
840 return;
841 }
842
Elliott Hughes75770752011-08-24 17:52:38 -0700843 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 IndirectReferenceTable& weak_globals = vm->weak_globals;
845 MutexLock mu(vm->weak_globals_lock);
846
847 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
848 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
849 << "failed to find entry";
850 }
851 }
852
853 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
854 ScopedJniThreadState ts(env);
855 if (obj == NULL) {
856 return NULL;
857 }
858
859 IndirectReferenceTable& locals = ts.Env()->locals;
860
861 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
862 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
863 return reinterpret_cast<jobject>(ref);
864 }
865
866 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
867 ScopedJniThreadState ts(env);
868 if (obj == NULL) {
869 return;
870 }
871
872 IndirectReferenceTable& locals = ts.Env()->locals;
873
874 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
875 if (!locals.Remove(cookie, obj)) {
876 // Attempting to delete a local reference that is not in the
877 // topmost local reference frame is a no-op. DeleteLocalRef returns
878 // void and doesn't throw any exceptions, but we should probably
879 // complain about it so the user will notice that things aren't
880 // going quite the way they expect.
881 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
882 << "failed to find entry";
883 }
884 }
885
886 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
887 ScopedJniThreadState ts(env);
888 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
889 ? JNI_TRUE : JNI_FALSE;
890 }
891
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700892 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700894 Class* c = Decode<Class*>(ts, java_class);
895 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
896 return NULL;
897 }
898 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 ScopedJniThreadState ts(env);
903 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_start(args, mid);
905 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 va_end(args);
907 return result;
908 }
909
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700912 Class* c = Decode<Class*>(ts, java_class);
913 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
914 return NULL;
915 }
916 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 return local_result;
920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700924 Class* c = Decode<Class*>(ts, java_class);
925 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
926 return NULL;
927 }
928 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 return local_result;
932 }
933
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
935 ScopedJniThreadState ts(env);
936 return FindMethodID(ts, c, name, sig, false);
937 }
938
939 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
940 ScopedJniThreadState ts(env);
941 return FindMethodID(ts, c, name, sig, true);
942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_list ap;
947 va_start(ap, mid);
948 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
949 va_end(ap);
950 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
956 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
962 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_list ap;
968 va_start(ap, mid);
969 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
970 va_end(ap);
971 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_list ap;
987 va_start(ap, mid);
988 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
989 va_end(ap);
990 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_list ap;
1006 va_start(ap, mid);
1007 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1008 va_end(ap);
1009 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 return InvokeVirtualWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 return InvokeVirtualWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_list ap;
1025 va_start(ap, mid);
1026 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1027 va_end(ap);
1028 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 return InvokeVirtualWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 return InvokeVirtualWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
1045 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1046 va_end(ap);
1047 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 return InvokeVirtualWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 return InvokeVirtualWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_list ap;
1063 va_start(ap, mid);
1064 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1065 va_end(ap);
1066 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 return InvokeVirtualWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 return InvokeVirtualWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_list ap;
1082 va_start(ap, mid);
1083 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1084 va_end(ap);
1085 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 return InvokeVirtualWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 return InvokeVirtualWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_list ap;
1101 va_start(ap, mid);
1102 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1103 va_end(ap);
1104 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 return InvokeVirtualWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 return InvokeVirtualWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_list ap;
1120 va_start(ap, mid);
1121 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1122 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
1135 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
1138 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_start(ap, mid);
1140 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1142 va_end(ap);
1143 return local_result;
1144 }
1145
1146 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 return AddLocalReference<jobject>(ts, result.l);
1151 }
1152
1153 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 return AddLocalReference<jobject>(ts, result.l);
1158 }
1159
1160 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
1163 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_start(ap, mid);
1165 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_end(ap);
1167 return result.z;
1168 }
1169
1170 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
1182 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
1185 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_start(ap, mid);
1187 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 va_end(ap);
1189 return result.b;
1190 }
1191
1192 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
1207 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_start(ap, mid);
1209 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_end(ap);
1211 return result.c;
1212 }
1213
1214 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
1229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
1231 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
1233 return result.s;
1234 }
1235
1236 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
1253 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.i;
1256 }
1257
1258 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
1273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
1275 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
1277 return result.j;
1278 }
1279
1280 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
1297 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
1299 return result.f;
1300 }
1301
1302 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
1319 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 return result.d;
1322 }
1323
1324 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
1341 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 }
1344
1345 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
1357 static jfieldID GetFieldID(JNIEnv* env,
1358 jclass c, const char* name, const char* sig) {
1359 ScopedJniThreadState ts(env);
1360 return FindFieldID(ts, c, name, sig, false);
1361 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001362
1363
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 static jfieldID GetStaticFieldID(JNIEnv* env,
1365 jclass c, const char* name, const char* sig) {
1366 ScopedJniThreadState ts(env);
1367 return FindFieldID(ts, c, name, sig, true);
1368 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001369
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 Object* o = Decode<Object*>(ts, obj);
1373 Field* f = DecodeField(ts, fid);
1374 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001376
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 Field* f = DecodeField(ts, fid);
1380 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 Object* o = Decode<Object*>(ts, java_object);
1386 Object* v = Decode<Object*>(ts, java_value);
1387 Field* f = DecodeField(ts, fid);
1388 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 Object* v = Decode<Object*>(ts, java_value);
1394 Field* f = DecodeField(ts, fid);
1395 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398#define GET_PRIMITIVE_FIELD(fn, instance) \
1399 ScopedJniThreadState ts(env); \
1400 Object* o = Decode<Object*>(ts, instance); \
1401 Field* f = DecodeField(ts, fid); \
1402 return f->fn(o)
1403
1404#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1405 ScopedJniThreadState ts(env); \
1406 Object* o = Decode<Object*>(ts, instance); \
1407 Field* f = DecodeField(ts, fid); \
1408 f->fn(o, value)
1409
1410 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1411 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1415 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetLong, NULL);
1464 }
1465
1466 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1468 }
1469
1470 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1472 }
1473
1474 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1475 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1476 }
1477
1478 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1479 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1480 }
1481
1482 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1483 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1484 }
1485
1486 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1487 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1488 }
1489
1490 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1491 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1492 }
1493
1494 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1495 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1496 }
1497
1498 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1499 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1500 }
1501
1502 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1503 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1504 }
1505
1506 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1507 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1508 }
1509
1510 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1511 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1512 }
1513
1514 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1515 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1516 }
1517
1518 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1519 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1520 }
1521
1522 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1523 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1524 }
1525
1526 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1527 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1528 }
1529
1530 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1531 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1532 }
1533
1534 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1535 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
1538 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 ScopedJniThreadState ts(env);
1541 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001542 va_start(ap, mid);
1543 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1545 va_end(ap);
1546 return local_result;
1547 }
1548
1549 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001552 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 return AddLocalReference<jobject>(ts, result.l);
1554 }
1555
1556 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001557 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001559 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 return AddLocalReference<jobject>(ts, result.l);
1561 }
1562
1563 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 ScopedJniThreadState ts(env);
1566 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 va_start(ap, mid);
1568 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_end(ap);
1570 return result.z;
1571 }
1572
1573 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001576 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
1579 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 ScopedJniThreadState ts(env);
1587 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 va_start(ap, mid);
1589 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_end(ap);
1591 return result.b;
1592 }
1593
1594 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
1600 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 ScopedJniThreadState ts(env);
1608 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 va_start(ap, mid);
1610 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 va_end(ap);
1612 return result.c;
1613 }
1614
1615 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
1621 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 ScopedJniThreadState ts(env);
1629 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 va_start(ap, mid);
1631 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 va_end(ap);
1633 return result.s;
1634 }
1635
1636 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
1642 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
1650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
1652 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 va_end(ap);
1654 return result.i;
1655 }
1656
1657 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
1663 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
1671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
1673 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_end(ap);
1675 return result.j;
1676 }
1677
1678 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
1684 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
1692 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 va_start(ap, mid);
1694 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 va_end(ap);
1696 return result.f;
1697 }
1698
1699 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
1705 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
1713 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 va_start(ap, mid);
1715 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_end(ap);
1717 return result.d;
1718 }
1719
1720 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
1726 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 ScopedJniThreadState ts(env);
1734 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 va_start(ap, mid);
1736 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 va_end(ap);
1738 }
1739
1740 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
1746 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes814e4032011-08-23 12:07:56 -07001752 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001754 if (chars == NULL && char_count == 0) {
1755 return NULL;
1756 }
1757 String* result = String::AllocFromUtf16(char_count, chars);
1758 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
1761 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1762 ScopedJniThreadState ts(env);
1763 if (utf == NULL) {
1764 return NULL;
1765 }
1766 String* result = String::AllocFromModifiedUtf8(utf);
1767 return AddLocalReference<jstring>(ts, result);
1768 }
1769
Elliott Hughes814e4032011-08-23 12:07:56 -07001770 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1771 ScopedJniThreadState ts(env);
1772 return Decode<String*>(ts, java_string)->GetLength();
1773 }
1774
1775 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1776 ScopedJniThreadState ts(env);
1777 return Decode<String*>(ts, java_string)->GetUtfLength();
1778 }
1779
Elliott Hughesb465ab02011-08-24 11:21:21 -07001780 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 String* s = Decode<String*>(ts, java_string);
1783 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1784 ThrowSIOOBE(ts, start, length, s->GetLength());
1785 } else {
1786 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1787 memcpy(buf, chars + start, length * sizeof(jchar));
1788 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 }
1790
Elliott Hughesb465ab02011-08-24 11:21:21 -07001791 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001792 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001793 String* s = Decode<String*>(ts, java_string);
1794 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1795 ThrowSIOOBE(ts, start, length, s->GetLength());
1796 } else {
1797 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1798 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1799 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 }
1801
Elliott Hughes75770752011-08-24 17:52:38 -07001802 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001803 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001804 String* s = Decode<String*>(ts, java_string);
1805 const CharArray* chars = s->GetCharArray();
1806 PinPrimitiveArray(ts, chars);
1807 if (is_copy != NULL) {
1808 *is_copy = JNI_FALSE;
1809 }
1810 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 }
1812
Elliott Hughes75770752011-08-24 17:52:38 -07001813 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001815 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes75770752011-08-24 17:52:38 -07001823 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001825 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes75770752011-08-24 17:52:38 -07001828 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001830 if (java_string == NULL) {
1831 return NULL;
1832 }
1833 if (is_copy != NULL) {
1834 *is_copy = JNI_TRUE;
1835 }
1836 String* s = Decode<String*>(ts, java_string);
1837 size_t byte_count = s->GetUtfLength();
1838 char* bytes = new char[byte_count + 1];
1839 if (bytes == NULL) {
1840 UNIMPLEMENTED(WARNING) << "need to Throw OOME";
1841 return NULL;
1842 }
1843 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1844 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1845 bytes[byte_count] = '\0';
1846 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001847 }
1848
Elliott Hughes75770752011-08-24 17:52:38 -07001849 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001850 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001851 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001852 }
1853
Elliott Hughesbd935992011-08-22 11:59:34 -07001854 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001856 Object* obj = Decode<Object*>(ts, java_array);
1857 CHECK(obj->IsArray()); // TODO: ReportJniError
1858 Array* array = obj->AsArray();
1859 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes814e4032011-08-23 12:07:56 -07001862 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1865 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
1868 static void SetObjectArrayElement(JNIEnv* env,
1869 jobjectArray java_array, jsize index, jobject java_value) {
1870 ScopedJniThreadState ts(env);
1871 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1872 Object* value = Decode<Object*>(ts, java_value);
1873 array->Set(index, value);
1874 }
1875
1876 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1877 ScopedJniThreadState ts(env);
1878 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1879 }
1880
1881 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1882 ScopedJniThreadState ts(env);
1883 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1884 }
1885
1886 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1887 ScopedJniThreadState ts(env);
1888 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1889 }
1890
1891 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1892 ScopedJniThreadState ts(env);
1893 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1894 }
1895
1896 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1897 ScopedJniThreadState ts(env);
1898 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1899 }
1900
1901 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1902 ScopedJniThreadState ts(env);
1903 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1904 }
1905
1906 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1907 ScopedJniThreadState ts(env);
1908 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1909 }
1910
1911 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1912 ScopedJniThreadState ts(env);
1913 CHECK_GE(length, 0); // TODO: ReportJniError
1914
1915 // Compute the array class corresponding to the given element class.
1916 Class* element_class = Decode<Class*>(ts, element_jclass);
1917 std::string descriptor;
1918 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001919 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001920
1921 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001922 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1923 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 return NULL;
1925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 // Allocate and initialize if necessary.
1928 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001930 if (initial_element != NULL) {
1931 Object* initial_object = Decode<Object*>(ts, initial_element);
1932 for (jsize i = 0; i < length; ++i) {
1933 result->Set(i, initial_object);
1934 }
1935 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 return AddLocalReference<jobjectArray>(ts, result);
1937 }
1938
1939 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1940 ScopedJniThreadState ts(env);
1941 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001945 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001950 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001951 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001956 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001961 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002116 Class* c = Decode<Class*>(ts, java_class);
2117
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 const char* name = methods[i].name;
2120 const char* sig = methods[i].signature;
2121
2122 if (*sig == '!') {
2123 // TODO: fast jni. it's too noisy to log all these.
2124 ++sig;
2125 }
2126
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 Method* m = c->FindDirectMethod(name, sig);
2128 if (m == NULL) {
2129 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2135 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002136 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2142 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002143 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 return JNI_ERR;
2145 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146
Elliott Hughes75770752011-08-24 17:52:38 -07002147 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 LOG(INFO) << "[Registering JNI native method "
2149 << PrettyMethod(m, true) << "]";
2150 }
2151
2152 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154 return JNI_OK;
2155 }
2156
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 Class* c = Decode<Class*>(ts, java_class);
2160
Elliott Hughes75770752011-08-24 17:52:38 -07002161 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 LOG(INFO) << "[Unregistering JNI native methods for "
2163 << PrettyDescriptor(c->GetDescriptor()) << "]";
2164 }
2165
2166 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2167 Method* m = c->GetDirectMethod(i);
2168 if (m->IsNative()) {
2169 m->UnregisterNative();
2170 }
2171 }
2172 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2173 Method* m = c->GetVirtualMethod(i);
2174 if (m->IsNative()) {
2175 m->UnregisterNative();
2176 }
2177 }
2178
2179 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes72025e52011-08-23 17:50:30 -07002182 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002184 Decode<Object*>(ts, java_object)->MonitorEnter();
2185 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes72025e52011-08-23 17:50:30 -07002188 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002190 Decode<Object*>(ts, java_object)->MonitorEnter();
2191 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
2194 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2195 ScopedJniThreadState ts(env);
2196 Runtime* runtime = Runtime::Current();
2197 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002198 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 } else {
2200 *vm = NULL;
2201 }
2202 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2203 }
2204
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2206 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207
2208 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002209 CHECK(address != NULL); // TODO: ReportJniError
2210 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002211
2212 jclass buffer_class = GetDirectByteBufferClass(env);
2213 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2214 if (mid == NULL) {
2215 return NULL;
2216 }
2217
2218 // At the moment, the Java side is limited to 32 bits.
2219 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2220 CHECK_LE(capacity, 0xffffffff);
2221 jint address_arg = reinterpret_cast<jint>(address);
2222 jint capacity_arg = static_cast<jint>(capacity);
2223
2224 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2225 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2231 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughesb465ab02011-08-24 11:21:21 -07002234 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2237 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002242
Elliott Hughes75770752011-08-24 17:52:38 -07002243 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244
2245 // Do we definitely know what kind of reference this is?
2246 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2247 IndirectRefKind kind = GetIndirectRefKind(ref);
2248 switch (kind) {
2249 case kLocal:
2250 return JNILocalRefType;
2251 case kGlobal:
2252 return JNIGlobalRefType;
2253 case kWeakGlobal:
2254 return JNIWeakGlobalRefType;
2255 case kSirtOrInvalid:
2256 // Is it in a stack IRT?
2257 if (ts.Self()->SirtContains(java_object)) {
2258 return JNILocalRefType;
2259 }
2260
2261 // If we're handing out direct pointers, check whether it's a direct pointer
2262 // to a local reference.
2263 // TODO: replace 'false' with the replacement for gDvmJni.workAroundAppJniBugs
2264 if (false && Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
2265 if (ts.Env()->locals.Contains(java_object)) {
2266 return JNILocalRefType;
2267 }
2268 }
2269
2270 return JNIInvalidRefType;
2271 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002274
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002275static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002276 NULL, // reserved0.
2277 NULL, // reserved1.
2278 NULL, // reserved2.
2279 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 JNI::GetVersion,
2281 JNI::DefineClass,
2282 JNI::FindClass,
2283 JNI::FromReflectedMethod,
2284 JNI::FromReflectedField,
2285 JNI::ToReflectedMethod,
2286 JNI::GetSuperclass,
2287 JNI::IsAssignableFrom,
2288 JNI::ToReflectedField,
2289 JNI::Throw,
2290 JNI::ThrowNew,
2291 JNI::ExceptionOccurred,
2292 JNI::ExceptionDescribe,
2293 JNI::ExceptionClear,
2294 JNI::FatalError,
2295 JNI::PushLocalFrame,
2296 JNI::PopLocalFrame,
2297 JNI::NewGlobalRef,
2298 JNI::DeleteGlobalRef,
2299 JNI::DeleteLocalRef,
2300 JNI::IsSameObject,
2301 JNI::NewLocalRef,
2302 JNI::EnsureLocalCapacity,
2303 JNI::AllocObject,
2304 JNI::NewObject,
2305 JNI::NewObjectV,
2306 JNI::NewObjectA,
2307 JNI::GetObjectClass,
2308 JNI::IsInstanceOf,
2309 JNI::GetMethodID,
2310 JNI::CallObjectMethod,
2311 JNI::CallObjectMethodV,
2312 JNI::CallObjectMethodA,
2313 JNI::CallBooleanMethod,
2314 JNI::CallBooleanMethodV,
2315 JNI::CallBooleanMethodA,
2316 JNI::CallByteMethod,
2317 JNI::CallByteMethodV,
2318 JNI::CallByteMethodA,
2319 JNI::CallCharMethod,
2320 JNI::CallCharMethodV,
2321 JNI::CallCharMethodA,
2322 JNI::CallShortMethod,
2323 JNI::CallShortMethodV,
2324 JNI::CallShortMethodA,
2325 JNI::CallIntMethod,
2326 JNI::CallIntMethodV,
2327 JNI::CallIntMethodA,
2328 JNI::CallLongMethod,
2329 JNI::CallLongMethodV,
2330 JNI::CallLongMethodA,
2331 JNI::CallFloatMethod,
2332 JNI::CallFloatMethodV,
2333 JNI::CallFloatMethodA,
2334 JNI::CallDoubleMethod,
2335 JNI::CallDoubleMethodV,
2336 JNI::CallDoubleMethodA,
2337 JNI::CallVoidMethod,
2338 JNI::CallVoidMethodV,
2339 JNI::CallVoidMethodA,
2340 JNI::CallNonvirtualObjectMethod,
2341 JNI::CallNonvirtualObjectMethodV,
2342 JNI::CallNonvirtualObjectMethodA,
2343 JNI::CallNonvirtualBooleanMethod,
2344 JNI::CallNonvirtualBooleanMethodV,
2345 JNI::CallNonvirtualBooleanMethodA,
2346 JNI::CallNonvirtualByteMethod,
2347 JNI::CallNonvirtualByteMethodV,
2348 JNI::CallNonvirtualByteMethodA,
2349 JNI::CallNonvirtualCharMethod,
2350 JNI::CallNonvirtualCharMethodV,
2351 JNI::CallNonvirtualCharMethodA,
2352 JNI::CallNonvirtualShortMethod,
2353 JNI::CallNonvirtualShortMethodV,
2354 JNI::CallNonvirtualShortMethodA,
2355 JNI::CallNonvirtualIntMethod,
2356 JNI::CallNonvirtualIntMethodV,
2357 JNI::CallNonvirtualIntMethodA,
2358 JNI::CallNonvirtualLongMethod,
2359 JNI::CallNonvirtualLongMethodV,
2360 JNI::CallNonvirtualLongMethodA,
2361 JNI::CallNonvirtualFloatMethod,
2362 JNI::CallNonvirtualFloatMethodV,
2363 JNI::CallNonvirtualFloatMethodA,
2364 JNI::CallNonvirtualDoubleMethod,
2365 JNI::CallNonvirtualDoubleMethodV,
2366 JNI::CallNonvirtualDoubleMethodA,
2367 JNI::CallNonvirtualVoidMethod,
2368 JNI::CallNonvirtualVoidMethodV,
2369 JNI::CallNonvirtualVoidMethodA,
2370 JNI::GetFieldID,
2371 JNI::GetObjectField,
2372 JNI::GetBooleanField,
2373 JNI::GetByteField,
2374 JNI::GetCharField,
2375 JNI::GetShortField,
2376 JNI::GetIntField,
2377 JNI::GetLongField,
2378 JNI::GetFloatField,
2379 JNI::GetDoubleField,
2380 JNI::SetObjectField,
2381 JNI::SetBooleanField,
2382 JNI::SetByteField,
2383 JNI::SetCharField,
2384 JNI::SetShortField,
2385 JNI::SetIntField,
2386 JNI::SetLongField,
2387 JNI::SetFloatField,
2388 JNI::SetDoubleField,
2389 JNI::GetStaticMethodID,
2390 JNI::CallStaticObjectMethod,
2391 JNI::CallStaticObjectMethodV,
2392 JNI::CallStaticObjectMethodA,
2393 JNI::CallStaticBooleanMethod,
2394 JNI::CallStaticBooleanMethodV,
2395 JNI::CallStaticBooleanMethodA,
2396 JNI::CallStaticByteMethod,
2397 JNI::CallStaticByteMethodV,
2398 JNI::CallStaticByteMethodA,
2399 JNI::CallStaticCharMethod,
2400 JNI::CallStaticCharMethodV,
2401 JNI::CallStaticCharMethodA,
2402 JNI::CallStaticShortMethod,
2403 JNI::CallStaticShortMethodV,
2404 JNI::CallStaticShortMethodA,
2405 JNI::CallStaticIntMethod,
2406 JNI::CallStaticIntMethodV,
2407 JNI::CallStaticIntMethodA,
2408 JNI::CallStaticLongMethod,
2409 JNI::CallStaticLongMethodV,
2410 JNI::CallStaticLongMethodA,
2411 JNI::CallStaticFloatMethod,
2412 JNI::CallStaticFloatMethodV,
2413 JNI::CallStaticFloatMethodA,
2414 JNI::CallStaticDoubleMethod,
2415 JNI::CallStaticDoubleMethodV,
2416 JNI::CallStaticDoubleMethodA,
2417 JNI::CallStaticVoidMethod,
2418 JNI::CallStaticVoidMethodV,
2419 JNI::CallStaticVoidMethodA,
2420 JNI::GetStaticFieldID,
2421 JNI::GetStaticObjectField,
2422 JNI::GetStaticBooleanField,
2423 JNI::GetStaticByteField,
2424 JNI::GetStaticCharField,
2425 JNI::GetStaticShortField,
2426 JNI::GetStaticIntField,
2427 JNI::GetStaticLongField,
2428 JNI::GetStaticFloatField,
2429 JNI::GetStaticDoubleField,
2430 JNI::SetStaticObjectField,
2431 JNI::SetStaticBooleanField,
2432 JNI::SetStaticByteField,
2433 JNI::SetStaticCharField,
2434 JNI::SetStaticShortField,
2435 JNI::SetStaticIntField,
2436 JNI::SetStaticLongField,
2437 JNI::SetStaticFloatField,
2438 JNI::SetStaticDoubleField,
2439 JNI::NewString,
2440 JNI::GetStringLength,
2441 JNI::GetStringChars,
2442 JNI::ReleaseStringChars,
2443 JNI::NewStringUTF,
2444 JNI::GetStringUTFLength,
2445 JNI::GetStringUTFChars,
2446 JNI::ReleaseStringUTFChars,
2447 JNI::GetArrayLength,
2448 JNI::NewObjectArray,
2449 JNI::GetObjectArrayElement,
2450 JNI::SetObjectArrayElement,
2451 JNI::NewBooleanArray,
2452 JNI::NewByteArray,
2453 JNI::NewCharArray,
2454 JNI::NewShortArray,
2455 JNI::NewIntArray,
2456 JNI::NewLongArray,
2457 JNI::NewFloatArray,
2458 JNI::NewDoubleArray,
2459 JNI::GetBooleanArrayElements,
2460 JNI::GetByteArrayElements,
2461 JNI::GetCharArrayElements,
2462 JNI::GetShortArrayElements,
2463 JNI::GetIntArrayElements,
2464 JNI::GetLongArrayElements,
2465 JNI::GetFloatArrayElements,
2466 JNI::GetDoubleArrayElements,
2467 JNI::ReleaseBooleanArrayElements,
2468 JNI::ReleaseByteArrayElements,
2469 JNI::ReleaseCharArrayElements,
2470 JNI::ReleaseShortArrayElements,
2471 JNI::ReleaseIntArrayElements,
2472 JNI::ReleaseLongArrayElements,
2473 JNI::ReleaseFloatArrayElements,
2474 JNI::ReleaseDoubleArrayElements,
2475 JNI::GetBooleanArrayRegion,
2476 JNI::GetByteArrayRegion,
2477 JNI::GetCharArrayRegion,
2478 JNI::GetShortArrayRegion,
2479 JNI::GetIntArrayRegion,
2480 JNI::GetLongArrayRegion,
2481 JNI::GetFloatArrayRegion,
2482 JNI::GetDoubleArrayRegion,
2483 JNI::SetBooleanArrayRegion,
2484 JNI::SetByteArrayRegion,
2485 JNI::SetCharArrayRegion,
2486 JNI::SetShortArrayRegion,
2487 JNI::SetIntArrayRegion,
2488 JNI::SetLongArrayRegion,
2489 JNI::SetFloatArrayRegion,
2490 JNI::SetDoubleArrayRegion,
2491 JNI::RegisterNatives,
2492 JNI::UnregisterNatives,
2493 JNI::MonitorEnter,
2494 JNI::MonitorExit,
2495 JNI::GetJavaVM,
2496 JNI::GetStringRegion,
2497 JNI::GetStringUTFRegion,
2498 JNI::GetPrimitiveArrayCritical,
2499 JNI::ReleasePrimitiveArrayCritical,
2500 JNI::GetStringCritical,
2501 JNI::ReleaseStringCritical,
2502 JNI::NewWeakGlobalRef,
2503 JNI::DeleteWeakGlobalRef,
2504 JNI::ExceptionCheck,
2505 JNI::NewDirectByteBuffer,
2506 JNI::GetDirectBufferAddress,
2507 JNI::GetDirectBufferCapacity,
2508 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002509};
2510
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002511static const size_t kMonitorsInitial = 32; // Arbitrary.
2512static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2513
2514static const size_t kLocalsInitial = 64; // Arbitrary.
2515static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002516
Elliott Hughes75770752011-08-24 17:52:38 -07002517JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002518 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002519 vm(vm),
2520 check_jni(vm->check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002521 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002522 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2523 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002524 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002525}
2526
Carl Shapiroea4dca82011-08-01 13:45:38 -07002527// JNI Invocation interface.
2528
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002529extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2530 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2531 if (args->version < JNI_VERSION_1_2) {
2532 return JNI_EVERSION;
2533 }
2534 Runtime::Options options;
2535 for (int i = 0; i < args->nOptions; ++i) {
2536 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002537 options.push_back(std::make_pair(StringPiece(option->optionString),
2538 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002539 }
2540 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002541 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542 if (runtime == NULL) {
2543 return JNI_ERR;
2544 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002545 *p_env = Thread::Current()->GetJniEnv();
2546 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 return JNI_OK;
2548 }
2549}
2550
Elliott Hughesf2682d52011-08-15 16:37:04 -07002551extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552 Runtime* runtime = Runtime::Current();
2553 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002554 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002556 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002557 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558 }
2559 return JNI_OK;
2560}
2561
2562// Historically unsupported.
2563extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2564 return JNI_ERR;
2565}
2566
Elliott Hughescdf53122011-08-19 15:46:09 -07002567class JII {
2568 public:
2569 static jint DestroyJavaVM(JavaVM* vm) {
2570 if (vm == NULL) {
2571 return JNI_ERR;
2572 } else {
2573 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2574 delete raw_vm->runtime;
2575 return JNI_OK;
2576 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002577 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002578
Elliott Hughescdf53122011-08-19 15:46:09 -07002579 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002580 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002581 }
2582
2583 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002584 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002585 }
2586
2587 static jint DetachCurrentThread(JavaVM* vm) {
2588 if (vm == NULL) {
2589 return JNI_ERR;
2590 } else {
2591 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2592 Runtime* runtime = raw_vm->runtime;
2593 runtime->DetachCurrentThread();
2594 return JNI_OK;
2595 }
2596 }
2597
2598 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2599 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2600 return JNI_EVERSION;
2601 }
2602 if (vm == NULL || env == NULL) {
2603 return JNI_ERR;
2604 }
2605 Thread* thread = Thread::Current();
2606 if (thread == NULL) {
2607 *env = NULL;
2608 return JNI_EDETACHED;
2609 }
2610 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 return JNI_OK;
2612 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002613};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614
Elliott Hughesf2682d52011-08-15 16:37:04 -07002615struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002616 NULL, // reserved0
2617 NULL, // reserved1
2618 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002619 JII::DestroyJavaVM,
2620 JII::AttachCurrentThread,
2621 JII::DetachCurrentThread,
2622 JII::GetEnv,
2623 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002624};
2625
Elliott Hughesbbd76712011-08-17 10:25:24 -07002626static const size_t kPinTableInitialSize = 16;
2627static const size_t kPinTableMaxSize = 1024;
2628
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002629static const size_t kGlobalsInitial = 512; // Arbitrary.
2630static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2631
2632static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2633static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2634
Elliott Hughes0af55432011-08-17 18:37:28 -07002635JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002636 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002637 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002638 verbose_jni(verbose_jni),
Elliott Hughes75770752011-08-24 17:52:38 -07002639 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002640 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002641 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002642 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002643 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002644 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002645 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002646}
2647
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002648JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002649 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002650 delete globals_lock;
2651 delete weak_globals_lock;
2652}
2653
Elliott Hughescdf53122011-08-19 15:46:09 -07002654/*
Elliott Hughescdf53122011-08-19 15:46:09 -07002655 */
Elliott Hughes75770752011-08-24 17:52:38 -07002656bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2657 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002658
2659 // See if we've already loaded this library. If we have, and the class loader
2660 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002661 // TODO: for better results we should canonicalize the pathname (or even compare
2662 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughescdf53122011-08-19 15:46:09 -07002663 SharedLibrary* library = libraries[path];
2664 if (library != NULL) {
2665 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002666 // The library will be associated with class_loader. The JNI
2667 // spec says we can't load the same library into more than one
2668 // class loader.
2669 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2670 "ClassLoader %p; can't open in ClassLoader %p",
2671 path.c_str(), library->GetClassLoader(), class_loader);
2672 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002673 return false;
2674 }
2675 if (verbose_jni) {
2676 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2677 << "ClassLoader " << class_loader << "]";
2678 }
2679 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002680 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2681 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 return false;
2683 }
2684 return true;
2685 }
2686
2687 // Open the shared library. Because we're using a full path, the system
2688 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2689 // resolve this library's dependencies though.)
2690
2691 // Failures here are expected when java.library.path has several entries
2692 // and we have to hunt for the lib.
2693
2694 // The current version of the dynamic linker prints detailed information
2695 // about dlopen() failures. Some things to check if the message is
2696 // cryptic:
2697 // - make sure the library exists on the device
2698 // - verify that the right path is being opened (the debug log message
2699 // above can help with that)
2700 // - check to see if the library is valid (e.g. not zero bytes long)
2701 // - check config/prelink-linux-arm.map to ensure that the library
2702 // is listed and is not being overrun by the previous entry (if
2703 // loading suddenly stops working on a prelinked library, this is
2704 // a good one to check)
2705 // - write a trivial app that calls sleep() then dlopen(), attach
2706 // to it with "strace -p <pid>" while it sleeps, and watch for
2707 // attempts to open nonexistent dependent shared libs
2708
2709 // TODO: automate some of these checks!
2710
2711 // This can execute slowly for a large library on a busy system, so we
2712 // want to switch from RUNNING to VMWAIT while it executes. This allows
2713 // the GC to ignore us.
2714 Thread* self = Thread::Current();
2715 Thread::State old_state = self->GetState();
2716 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2717 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2718 self->SetState(old_state);
2719
2720 if (verbose_jni) {
2721 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2722 }
2723
2724 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002725 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 return false;
2727 }
2728
2729 // Create a new entry.
2730 library = new SharedLibrary(path, handle, class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002731
2732 libraries[path] = library;
2733
2734 // if (pNewEntry != pActualEntry) {
2735 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2736 // freeSharedLibEntry(pNewEntry);
2737 // return CheckOnLoadResult(this, pActualEntry);
2738 // } else
2739 {
2740 if (verbose_jni) {
2741 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2742 }
2743
2744 bool result = true;
2745 void* sym = dlsym(handle, "JNI_OnLoad");
2746 if (sym == NULL) {
2747 if (verbose_jni) {
2748 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2749 }
2750 } else {
2751 // Call JNI_OnLoad. We have to override the current class
2752 // loader, which will always be "null" since the stuff at the
2753 // top of the stack is around Runtime.loadLibrary(). (See
2754 // the comments in the JNI FindClass function.)
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2756 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Elliott Hughes814e4032011-08-23 12:07:56 -07002757 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2758 self->SetClassLoaderOverride(class_loader);
Elliott Hughescdf53122011-08-19 15:46:09 -07002759
2760 old_state = self->GetState();
2761 self->SetState(Thread::kNative);
2762 if (verbose_jni) {
2763 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2764 }
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002765 int version = (*jni_on_load)(this, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07002766 self->SetState(old_state);
2767
Elliott Hughes814e4032011-08-23 12:07:56 -07002768 self->SetClassLoaderOverride(old_class_loader);;
Elliott Hughescdf53122011-08-19 15:46:09 -07002769
2770 if (version != JNI_VERSION_1_2 &&
2771 version != JNI_VERSION_1_4 &&
2772 version != JNI_VERSION_1_6) {
2773 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2774 << "bad version: " << version;
2775 // It's unwise to call dlclose() here, but we can mark it
2776 // as bad and ensure that future load attempts will fail.
2777 // We don't know how far JNI_OnLoad got, so there could
2778 // be some partially-initialized stuff accessible through
2779 // newly-registered native method calls. We could try to
2780 // unregister them, but that doesn't seem worthwhile.
2781 result = false;
2782 } else {
2783 if (verbose_jni) {
2784 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2785 << " from JNI_OnLoad in \"" << path << "\"]";
2786 }
2787 }
2788 }
2789
2790 library->SetResult(result);
2791 return result;
2792 }
2793}
2794
Ian Rogersdf20fe02011-07-20 20:34:16 -07002795} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002796
2797std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2798 switch (rhs) {
2799 case JNIInvalidRefType:
2800 os << "JNIInvalidRefType";
2801 return os;
2802 case JNILocalRefType:
2803 os << "JNILocalRefType";
2804 return os;
2805 case JNIGlobalRefType:
2806 os << "JNIGlobalRefType";
2807 return os;
2808 case JNIWeakGlobalRefType:
2809 os << "JNIWeakGlobalRefType";
2810 return os;
2811 }
2812}