blob: 243a29d6d86651f64d4aa7ea23730f4dfc4bee5f [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 Hughes18c07532011-08-18 15:50:51 -070011#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070012#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070013#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070015#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070016#include "runtime.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070017#include "scoped_ptr.h"
18#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070019#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070020
buzbeec143c552011-08-20 17:38:58 -070021extern bool oatCompileMethod(art::Method*, art::InstructionSet);
22
Ian Rogersdf20fe02011-07-20 20:34:16 -070023namespace art {
24
Elliott Hughescdf53122011-08-19 15:46:09 -070025// This is private API, but with two different implementations: ARM and x86.
26void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070027
Elliott Hughescdf53122011-08-19 15:46:09 -070028// TODO: this should be in our anonymous namespace, but is currently needed
29// for testing in "jni_internal_test.cc".
30bool EnsureInvokeStub(Method* method) {
31 if (method->GetInvokeStub() != NULL) {
32 return true;
33 }
34 // TODO: use signature to find a matching stub
35 // TODO: failed, acquire a lock on the stub table
36 Assembler assembler;
37 CreateInvokeStub(&assembler, method);
38 // TODO: store native_entry in the stub table
39 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
40 size_t length = assembler.CodeSize();
41 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
42 if (addr == MAP_FAILED) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070043 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070044 }
45 MemoryRegion region(addr, length);
46 assembler.FinalizeInstructions(region);
47 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
48 return true;
49}
Elliott Hughes0af55432011-08-17 18:37:28 -070050
Elliott Hughescdf53122011-08-19 15:46:09 -070051// TODO: this can't be in our anonymous namespace because of the map in JavaVM.
52class SharedLibrary {
53public:
54 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
55 : path_(path),
56 handle_(handle),
57 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
58 jni_on_load_tid_(Thread::Current()->GetId()),
59 jni_on_load_result_(kPending) {
Elliott Hughes18c07532011-08-18 15:50:51 -070060 }
61
62 ~SharedLibrary() {
Elliott Hughescdf53122011-08-19 15:46:09 -070063 delete jni_on_load_lock_;
Elliott Hughes0af55432011-08-17 18:37:28 -070064 }
65
Elliott Hughescdf53122011-08-19 15:46:09 -070066 Object* GetClassLoader() {
67 return class_loader_;
Elliott Hughes0af55432011-08-17 18:37:28 -070068 }
69
Elliott Hughescdf53122011-08-19 15:46:09 -070070 /*
71 * Check the result of an earlier call to JNI_OnLoad on this library. If
72 * the call has not yet finished in another thread, wait for it.
73 */
74 bool CheckOnLoadResult(JavaVMExt* vm) {
75 Thread* self = Thread::Current();
76 if (jni_on_load_tid_ == self->GetId()) {
77 // Check this so we don't end up waiting for ourselves. We need
78 // to return "true" so the caller can continue.
79 LOG(INFO) << *self << " recursive attempt to load library "
80 << "\"" << path_ << "\"";
81 return true;
Elliott Hughes0af55432011-08-17 18:37:28 -070082 }
83
Elliott Hughescdf53122011-08-19 15:46:09 -070084 UNIMPLEMENTED(ERROR) << "need to pthread_cond_wait!";
85 // MutexLock mu(jni_on_load_lock_);
86 while (jni_on_load_result_ == kPending) {
87 if (vm->verbose_jni) {
88 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
89 << "JNI_OnLoad...]";
Elliott Hughes0af55432011-08-17 18:37:28 -070090 }
Elliott Hughescdf53122011-08-19 15:46:09 -070091 Thread::State old_state = self->GetState();
92 self->SetState(Thread::kWaiting); // TODO: VMWAIT
93 // pthread_cond_wait(&jni_on_load_cond_, &jni_on_load_lock_);
Elliott Hughes0af55432011-08-17 18:37:28 -070094 self->SetState(old_state);
Elliott Hughes0af55432011-08-17 18:37:28 -070095 }
96
Elliott Hughescdf53122011-08-19 15:46:09 -070097 bool okay = (jni_on_load_result_ == kOkay);
98 if (vm->verbose_jni) {
99 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
100 << (okay ? "succeeded" : "failed") << "]";
101 }
102 return okay;
103 }
104
105 void SetResult(bool result) {
106 jni_on_load_result_ = result ? kOkay : kFailed;
107 jni_on_load_tid_ = 0;
Elliott Hughes0af55432011-08-17 18:37:28 -0700108
109 // Broadcast a wakeup to anybody sleeping on the condition variable.
110 UNIMPLEMENTED(ERROR) << "missing pthread_cond_broadcast";
Elliott Hughescdf53122011-08-19 15:46:09 -0700111 // MutexLock mu(library->jni_on_load_lock_);
112 // pthread_cond_broadcast(&library->jni_on_load_cond_);
Elliott Hughes0af55432011-08-17 18:37:28 -0700113 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700114
115 private:
116 enum JNI_OnLoadState {
117 kPending,
118 kFailed,
119 kOkay,
120 };
121
122 // Path to library "/system/lib/libjni.so".
123 std::string path_;
124
125 // The void* returned by dlopen(3).
126 void* handle_;
127
128 // The ClassLoader this library is associated with.
129 Object* class_loader_;
130
131 // Guards remaining items.
132 Mutex* jni_on_load_lock_;
133 // Wait for JNI_OnLoad in other thread.
134 pthread_cond_t jni_on_load_cond_;
135 // Recursive invocation guard.
136 uint32_t jni_on_load_tid_;
137 // Result of earlier JNI_OnLoad call.
138 JNI_OnLoadState jni_on_load_result_;
139};
140
141namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -0700142
Elliott Hughes22f40932011-08-12 13:06:37 -0700143// Entry/exit processing for all JNI calls.
144//
145// This performs the necessary thread state switching, lets us amortize the
146// cost of working out the current thread, and lets us check (and repair) apps
147// that are using a JNIEnv on the wrong thread.
148class ScopedJniThreadState {
149 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700150 explicit ScopedJniThreadState(JNIEnv* env)
151 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700152 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -0700153 self_->SetState(Thread::kRunnable);
154 }
155
156 ~ScopedJniThreadState() {
157 self_->SetState(Thread::kNative);
158 }
159
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700160 JNIEnvExt* Env() {
161 return env_;
162 }
163
Elliott Hughesb20a5542011-08-12 18:03:12 -0700164 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -0700165 return self_;
166 }
167
Elliott Hughesb20a5542011-08-12 18:03:12 -0700168 private:
169 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -0700170 // TODO: need replacement for gDvmJni.
171 bool workAroundAppJniBugs = true;
172 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
173 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
174 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -0700175 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
176 << " used on " << *self;
177 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -0700178 }
179 return self;
180 }
181
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700182 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -0700183 Thread* self_;
184 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
185};
186
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700187/*
188 * Add a local reference for an object to the current stack frame. When
189 * the native function returns, the reference will be discarded.
190 *
191 * We need to allow the same reference to be added multiple times.
192 *
193 * This will be called on otherwise unreferenced objects. We cannot do
194 * GC allocations here, and it's best if we don't grab a mutex.
195 *
196 * Returns the local reference (currently just the same pointer that was
197 * passed in), or NULL on failure.
198 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700199template<typename T>
200T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700201 if (obj == NULL) {
202 return NULL;
203 }
204
205 IndirectReferenceTable& locals = ts.Env()->locals;
206
207 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
208 IndirectRef ref = locals.Add(cookie, obj);
209 if (ref == NULL) {
210 // TODO: just change Add's DCHECK to CHECK and lose this?
211 locals.Dump();
212 LOG(FATAL) << "Failed adding to JNI local reference table "
213 << "(has " << locals.Capacity() << " entries)";
214 // TODO: dvmDumpThread(dvmThreadSelf(), false);
215 }
216
217#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
218 if (ts.Env()->check_jni) {
219 size_t entry_count = locals.Capacity();
220 if (entry_count > 16) {
221 std::string class_name(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
222 LOG(WARNING) << "Warning: more than 16 JNI local references: "
223 << entry_count << " (most recent was a " << class_name << ")";
224 locals.Dump();
225 // TODO: dvmDumpThread(dvmThreadSelf(), false);
226 // dvmAbort();
227 }
228 }
229#endif
230
231 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
232 // Hand out direct pointers to support broken old apps.
233 return reinterpret_cast<T>(obj);
234 }
235
236 return reinterpret_cast<T>(ref);
237}
238
Elliott Hughescdf53122011-08-19 15:46:09 -0700239jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
240 if (obj == NULL) {
241 return NULL;
242 }
243 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
244 IndirectReferenceTable& weak_globals = vm->weak_globals;
245 MutexLock mu(vm->weak_globals_lock);
246 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
247 return reinterpret_cast<jweak>(ref);
248}
249
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700250template<typename T>
251T Decode(ScopedJniThreadState& ts, jobject obj) {
252 if (obj == NULL) {
253 return NULL;
254 }
255
256 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
257 IndirectRefKind kind = GetIndirectRefKind(ref);
258 Object* result;
259 switch (kind) {
260 case kLocal:
261 {
262 IndirectReferenceTable& locals = ts.Env()->locals;
263 result = locals.Get(ref);
264 break;
265 }
266 case kGlobal:
267 {
268 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
269 IndirectReferenceTable& globals = vm->globals;
Elliott Hughes18c07532011-08-18 15:50:51 -0700270 MutexLock mu(vm->globals_lock);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700271 result = globals.Get(ref);
272 break;
273 }
274 case kWeakGlobal:
275 {
276 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
277 IndirectReferenceTable& weak_globals = vm->weak_globals;
Elliott Hughes18c07532011-08-18 15:50:51 -0700278 MutexLock mu(vm->weak_globals_lock);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700279 result = weak_globals.Get(ref);
280 if (result == kClearedJniWeakGlobal) {
281 // This is a special case where it's okay to return NULL.
282 return NULL;
283 }
284 break;
285 }
286 case kInvalid:
287 default:
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700288 // TODO: make stack handle blocks more efficient
289 // Check if this is a local reference in a stack handle block
290 if (ts.Self()->ShbContains(obj)) {
291 return *reinterpret_cast<T*>(obj); // Read from stack handle block
292 }
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700293 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
294 // Assume an invalid local reference is actually a direct pointer.
295 return reinterpret_cast<T>(obj);
296 }
297 LOG(FATAL) << "Invalid indirect reference " << obj;
298 return reinterpret_cast<T>(kInvalidIndirectRefObject);
299 }
300
301 if (result == NULL) {
302 LOG(FATAL) << "JNI ERROR (app bug): use of deleted " << kind << ": "
303 << obj;
304 }
305 return reinterpret_cast<T>(result);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700306}
307
Elliott Hughescdf53122011-08-19 15:46:09 -0700308Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
309 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
310}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700311
Elliott Hughescdf53122011-08-19 15:46:09 -0700312Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
313 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700314}
315
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700316byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700317 size_t num_bytes = method->NumArgArrayBytes();
318 scoped_array<byte> arg_array(new byte[num_bytes]);
319 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700320 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700321 switch (shorty[i]) {
322 case 'Z':
323 case 'B':
324 case 'C':
325 case 'S':
326 case 'I':
327 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
328 offset += 4;
329 break;
330 case 'F':
331 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
332 offset += 4;
333 break;
334 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700335 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
337 offset += sizeof(Object*);
338 break;
339 }
340 case 'D':
341 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
342 offset += 8;
343 break;
344 case 'J':
345 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
346 offset += 8;
347 break;
348 }
349 }
350 return arg_array.release();
351}
352
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700353byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700354 size_t num_bytes = method->NumArgArrayBytes();
355 scoped_array<byte> arg_array(new byte[num_bytes]);
356 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700357 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700358 switch (shorty[i]) {
359 case 'Z':
360 case 'B':
361 case 'C':
362 case 'S':
363 case 'I':
364 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
365 offset += 4;
366 break;
367 case 'F':
368 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
369 offset += 4;
370 break;
371 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700372 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700373 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
374 offset += sizeof(Object*);
375 break;
376 }
377 case 'D':
378 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
379 offset += 8;
380 break;
381 case 'J':
382 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
383 offset += 8;
384 break;
385 }
386 }
387 return arg_array.release();
388}
389
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700390JValue InvokeWithArgArray(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700391 jmethodID mid, byte* args) {
392 Method* method = DecodeMethod(ts, mid);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700393 Object* rcvr = Decode<Object*>(ts, obj);
Ian Rogers6de08602011-08-19 14:52:39 -0700394 Thread* self = ts.Self();
395
396 // Push a transition back into managed code onto the linked list in thread
397 CHECK_EQ(Thread::kRunnable, self->GetState());
398 NativeToManagedRecord record;
399 self->PushNativeToManagedRecord(&record);
400
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700401 // Call the invoke stub associated with the method
402 // Pass everything as arguments
403 const Method::InvokeStub* stub = method->GetInvokeStub();
404 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700405
406#ifdef __arm__
407 // Compile...
408 // TODO: not here!
409 oatCompileMethod(method, kThumb2);
410#endif
411
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700412 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700413 if (method->HasCode()) {
Ian Rogers6de08602011-08-19 14:52:39 -0700414 (*stub)(method, rcvr, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700415 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700416 LOG(WARNING) << "Not invoking method with no associated code: "
417 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700418 result.j = 0;
419 }
buzbeec143c552011-08-20 17:38:58 -0700420
Ian Rogers6de08602011-08-19 14:52:39 -0700421 // Pop transition
422 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700423 return result;
424}
425
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700426JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700427 jmethodID mid, jvalue* args) {
428 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700429 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughescdf53122011-08-19 15:46:09 -0700430 return InvokeWithArgArray(ts, obj, mid, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700431}
432
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700433JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700434 jmethodID mid, va_list args) {
435 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700436 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughescdf53122011-08-19 15:46:09 -0700437 return InvokeWithArgArray(ts, obj, mid, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700438}
439
Elliott Hughes6b436852011-08-12 10:16:44 -0700440// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
441// separated with slashes but aren't wrapped with "L;" like regular descriptors
442// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
443// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
444// supported names with dots too (such as "a.b.C").
445std::string NormalizeJniClassDescriptor(const char* name) {
446 std::string result;
447 // Add the missing "L;" if necessary.
448 if (name[0] == '[') {
449 result = name;
450 } else {
451 result += 'L';
452 result += name;
453 result += ';';
454 }
455 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700456 if (result.find('.') != std::string::npos) {
457 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
458 << "\"" << name << "\"";
459 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700460 }
461 return result;
462}
463
Elliott Hughescdf53122011-08-19 15:46:09 -0700464jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
465 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700466 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
467 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700468 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700469
470 Method* method = NULL;
471 if (is_static) {
472 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700473 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700474 method = c->FindVirtualMethod(name, sig);
475 if (method == NULL) {
476 // No virtual method matching the signature. Search declared
477 // private methods and constructors.
478 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700479 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700480 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700481
Elliott Hughescdf53122011-08-19 15:46:09 -0700482 if (method == NULL || method->IsStatic() != is_static) {
483 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700484 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700485 // TODO: try searching for the opposite kind of method from is_static
486 // for better diagnostics?
487 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700488 "no %s method %s", is_static ? "static" : "non-static",
489 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700490 return NULL;
491 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700492
Elliott Hughescdf53122011-08-19 15:46:09 -0700493 bool success = EnsureInvokeStub(method);
494 if (!success) {
495 // TODO: throw OutOfMemoryException
496 return NULL;
497 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700498
Elliott Hughescdf53122011-08-19 15:46:09 -0700499 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700500}
501
Elliott Hughescdf53122011-08-19 15:46:09 -0700502jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
503 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700504 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
505 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700506 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700507
508 Field* field = NULL;
509 if (is_static) {
510 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700511 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700512 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700513 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700514
Elliott Hughescdf53122011-08-19 15:46:09 -0700515 if (field == NULL) {
516 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700517 std::string class_name(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700518 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
519 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
520 name, class_name.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700521 return NULL;
522 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700523
Elliott Hughescdf53122011-08-19 15:46:09 -0700524 jweak fid = AddWeakGlobalReference(ts, field);
525 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700526}
527
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700528template<typename JniT, typename ArtT>
529JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
530 CHECK_GE(length, 0); // TODO: ReportJniError
531 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700532 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700533}
534
Elliott Hughescdf53122011-08-19 15:46:09 -0700535} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700536
Elliott Hughescdf53122011-08-19 15:46:09 -0700537class JNI {
538 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700539
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 static jint GetVersion(JNIEnv* env) {
541 ScopedJniThreadState ts(env);
542 return JNI_VERSION_1_6;
543 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700544
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
546 ScopedJniThreadState ts(env);
547 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700548 return NULL;
549 }
550
Elliott Hughescdf53122011-08-19 15:46:09 -0700551 static jclass FindClass(JNIEnv* env, const char* name) {
552 ScopedJniThreadState ts(env);
553 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
554 std::string descriptor(NormalizeJniClassDescriptor(name));
555 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700556 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700557 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700558 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700559 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700560
Elliott Hughescdf53122011-08-19 15:46:09 -0700561 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
562 ScopedJniThreadState ts(env);
563 Method* method = Decode<Method*>(ts, java_method);
564 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700565 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700566
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
568 ScopedJniThreadState ts(env);
569 Field* field = Decode<Field*>(ts, java_field);
570 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
571 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Elliott Hughescdf53122011-08-19 15:46:09 -0700573 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
574 ScopedJniThreadState ts(env);
575 Method* method = DecodeMethod(ts, mid);
576 return AddLocalReference<jobject>(ts, method);
577 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700578
Elliott Hughescdf53122011-08-19 15:46:09 -0700579 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
580 ScopedJniThreadState ts(env);
581 Field* field = DecodeField(ts, fid);
582 return AddLocalReference<jobject>(ts, field);
583 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700584
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700585 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700587 Class* c = Decode<Class*>(ts, java_class);
588 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700590
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 static jboolean IsAssignableFrom(JNIEnv* env, jclass sub, jclass sup) {
592 ScopedJniThreadState ts(env);
593 UNIMPLEMENTED(FATAL);
594 return JNI_FALSE;
595 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700596
Elliott Hughescdf53122011-08-19 15:46:09 -0700597 static jint Throw(JNIEnv* env, jthrowable obj) {
598 ScopedJniThreadState ts(env);
599 UNIMPLEMENTED(FATAL);
600 return 0;
601 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jint ThrowNew(JNIEnv* env, jclass clazz, const char* msg) {
604 ScopedJniThreadState ts(env);
605 UNIMPLEMENTED(FATAL);
606 return 0;
607 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jthrowable ExceptionOccurred(JNIEnv* env) {
610 ScopedJniThreadState ts(env);
611 Object* exception = ts.Self()->GetException();
612 if (exception == NULL) {
613 return NULL;
614 } else {
615 // TODO: if adding a local reference failing causes the VM to abort
616 // then the following check will never occur.
617 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
618 if (localException == NULL) {
619 // We were unable to add a new local reference, and threw a new
620 // exception. We can't return "exception", because it's not a
621 // local reference. So we have to return NULL, indicating that
622 // there was no exception, even though it's pretty much raining
623 // exceptions in here.
624 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
625 }
626 return localException;
627 }
628 }
629
630 static void ExceptionDescribe(JNIEnv* env) {
631 ScopedJniThreadState ts(env);
632 UNIMPLEMENTED(FATAL);
633 }
634
635 static void ExceptionClear(JNIEnv* env) {
636 ScopedJniThreadState ts(env);
637 ts.Self()->ClearException();
638 }
639
640 static void FatalError(JNIEnv* env, const char* msg) {
641 ScopedJniThreadState ts(env);
642 LOG(FATAL) << "JNI FatalError called: " << msg;
643 }
644
645 static jint PushLocalFrame(JNIEnv* env, jint cap) {
646 ScopedJniThreadState ts(env);
647 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
648 return JNI_OK;
649 }
650
651 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
652 ScopedJniThreadState ts(env);
653 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
654 return res;
655 }
656
657 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
658 ScopedJniThreadState ts(env);
659 if (obj == NULL) {
660 return NULL;
661 }
662
663 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
664 IndirectReferenceTable& globals = vm->globals;
665 MutexLock mu(vm->globals_lock);
666 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
667 return reinterpret_cast<jobject>(ref);
668 }
669
670 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
671 ScopedJniThreadState ts(env);
672 if (obj == NULL) {
673 return;
674 }
675
676 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
677 IndirectReferenceTable& globals = vm->globals;
678 MutexLock mu(vm->globals_lock);
679
680 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
681 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
682 << "failed to find entry";
683 }
684 }
685
686 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
687 ScopedJniThreadState ts(env);
688 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
689 }
690
691 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
692 ScopedJniThreadState ts(env);
693 if (obj == NULL) {
694 return;
695 }
696
697 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
698 IndirectReferenceTable& weak_globals = vm->weak_globals;
699 MutexLock mu(vm->weak_globals_lock);
700
701 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
702 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
703 << "failed to find entry";
704 }
705 }
706
707 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
708 ScopedJniThreadState ts(env);
709 if (obj == NULL) {
710 return NULL;
711 }
712
713 IndirectReferenceTable& locals = ts.Env()->locals;
714
715 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
716 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
717 return reinterpret_cast<jobject>(ref);
718 }
719
720 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
721 ScopedJniThreadState ts(env);
722 if (obj == NULL) {
723 return;
724 }
725
726 IndirectReferenceTable& locals = ts.Env()->locals;
727
728 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
729 if (!locals.Remove(cookie, obj)) {
730 // Attempting to delete a local reference that is not in the
731 // topmost local reference frame is a no-op. DeleteLocalRef returns
732 // void and doesn't throw any exceptions, but we should probably
733 // complain about it so the user will notice that things aren't
734 // going quite the way they expect.
735 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
736 << "failed to find entry";
737 }
738 }
739
740 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
741 ScopedJniThreadState ts(env);
742 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
743 ? JNI_TRUE : JNI_FALSE;
744 }
745
746 static jint EnsureLocalCapacity(JNIEnv* env, jint) {
747 ScopedJniThreadState ts(env);
748 UNIMPLEMENTED(FATAL);
749 return 0;
750 }
751
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700752 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700754 Class* c = Decode<Class*>(ts, java_class);
755 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
756 return NULL;
757 }
758 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 }
760
761 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
762 ScopedJniThreadState ts(env);
763 va_list args;
764 va_start(args, methodID);
765 jobject result = NewObjectV(env, clazz, methodID, args);
766 va_end(args);
767 return result;
768 }
769
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700770 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID methodID, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700772 Class* c = Decode<Class*>(ts, java_class);
773 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
774 return NULL;
775 }
776 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700778 CallNonvirtualVoidMethodV(env, local_result, java_class, methodID, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 return local_result;
780 }
781
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700782 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID methodID, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700784 Class* c = Decode<Class*>(ts, java_class);
785 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
786 return NULL;
787 }
788 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700790 CallNonvirtualVoidMethodA(env, local_result, java_class, methodID, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return local_result;
792 }
793
794 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
795 ScopedJniThreadState ts(env);
796 UNIMPLEMENTED(FATAL);
797 return NULL;
798 }
799
800 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
801 ScopedJniThreadState ts(env);
802 CHECK_NE(static_cast<jclass>(NULL), clazz);
803 if (jobj == NULL) {
804 // NB. JNI is different from regular Java instanceof in this respect
805 return JNI_TRUE;
806 } else {
807 Object* obj = Decode<Object*>(ts, jobj);
808 Class* klass = Decode<Class*>(ts, clazz);
809 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
810 }
811 }
812
813 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
814 ScopedJniThreadState ts(env);
815 return FindMethodID(ts, c, name, sig, false);
816 }
817
818 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
819 ScopedJniThreadState ts(env);
820 return FindMethodID(ts, c, name, sig, true);
821 }
822
823 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
824 ScopedJniThreadState ts(env);
825 UNIMPLEMENTED(FATAL);
826 return NULL;
827 }
828
829 static jobject CallObjectMethodV(JNIEnv* env,
830 jobject obj, jmethodID methodID, va_list args) {
831 ScopedJniThreadState ts(env);
832 UNIMPLEMENTED(FATAL);
833 return NULL;
834 }
835
836 static jobject CallObjectMethodA(JNIEnv* env,
837 jobject obj, jmethodID methodID, jvalue* args) {
838 ScopedJniThreadState ts(env);
839 UNIMPLEMENTED(FATAL);
840 return NULL;
841 }
842
843 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
844 ScopedJniThreadState ts(env);
845 UNIMPLEMENTED(FATAL);
846 return JNI_FALSE;
847 }
848
849 static jboolean CallBooleanMethodV(JNIEnv* env,
850 jobject obj, jmethodID methodID, va_list args) {
851 ScopedJniThreadState ts(env);
852 UNIMPLEMENTED(FATAL);
853 return JNI_FALSE;
854 }
855
856 static jboolean CallBooleanMethodA(JNIEnv* env,
857 jobject obj, jmethodID methodID, jvalue* args) {
858 ScopedJniThreadState ts(env);
859 UNIMPLEMENTED(FATAL);
860 return JNI_FALSE;
861 }
862
863 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
864 ScopedJniThreadState ts(env);
865 UNIMPLEMENTED(FATAL);
866 return 0;
867 }
868
869 static jbyte CallByteMethodV(JNIEnv* env,
870 jobject obj, jmethodID methodID, va_list args) {
871 ScopedJniThreadState ts(env);
872 UNIMPLEMENTED(FATAL);
873 return 0;
874 }
875
876 static jbyte CallByteMethodA(JNIEnv* env,
877 jobject obj, jmethodID methodID, jvalue* args) {
878 ScopedJniThreadState ts(env);
879 UNIMPLEMENTED(FATAL);
880 return 0;
881 }
882
883 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
884 ScopedJniThreadState ts(env);
885 UNIMPLEMENTED(FATAL);
886 return 0;
887 }
888
889 static jchar CallCharMethodV(JNIEnv* env,
890 jobject obj, jmethodID methodID, va_list args) {
891 ScopedJniThreadState ts(env);
892 UNIMPLEMENTED(FATAL);
893 return 0;
894 }
895
896 static jchar CallCharMethodA(JNIEnv* env,
897 jobject obj, jmethodID methodID, jvalue* args) {
898 ScopedJniThreadState ts(env);
899 UNIMPLEMENTED(FATAL);
900 return 0;
901 }
902
903 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
904 ScopedJniThreadState ts(env);
905 UNIMPLEMENTED(FATAL);
906 return 0;
907 }
908
909 static jshort CallShortMethodV(JNIEnv* env,
910 jobject obj, jmethodID methodID, va_list args) {
911 ScopedJniThreadState ts(env);
912 UNIMPLEMENTED(FATAL);
913 return 0;
914 }
915
916 static jshort CallShortMethodA(JNIEnv* env,
917 jobject obj, jmethodID methodID, jvalue* args) {
918 ScopedJniThreadState ts(env);
919 UNIMPLEMENTED(FATAL);
920 return 0;
921 }
922
923 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
924 ScopedJniThreadState ts(env);
925 UNIMPLEMENTED(FATAL);
926 return 0;
927 }
928
929 static jint CallIntMethodV(JNIEnv* env,
930 jobject obj, jmethodID methodID, va_list args) {
931 ScopedJniThreadState ts(env);
932 UNIMPLEMENTED(FATAL);
933 return 0;
934 }
935
936 static jint CallIntMethodA(JNIEnv* env,
937 jobject obj, jmethodID methodID, jvalue* args) {
938 ScopedJniThreadState ts(env);
939 UNIMPLEMENTED(FATAL);
940 return 0;
941 }
942
943 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
944 ScopedJniThreadState ts(env);
945 UNIMPLEMENTED(FATAL);
946 return 0;
947 }
948
949 static jlong CallLongMethodV(JNIEnv* env,
950 jobject obj, jmethodID methodID, va_list args) {
951 ScopedJniThreadState ts(env);
952 UNIMPLEMENTED(FATAL);
953 return 0;
954 }
955
956 static jlong CallLongMethodA(JNIEnv* env,
957 jobject obj, jmethodID methodID, jvalue* args) {
958 ScopedJniThreadState ts(env);
959 UNIMPLEMENTED(FATAL);
960 return 0;
961 }
962
963 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
964 ScopedJniThreadState ts(env);
965 UNIMPLEMENTED(FATAL);
966 return 0;
967 }
968
969 static jfloat CallFloatMethodV(JNIEnv* env,
970 jobject obj, jmethodID methodID, va_list args) {
971 ScopedJniThreadState ts(env);
972 UNIMPLEMENTED(FATAL);
973 return 0;
974 }
975
976 static jfloat CallFloatMethodA(JNIEnv* env,
977 jobject obj, jmethodID methodID, jvalue* args) {
978 ScopedJniThreadState ts(env);
979 UNIMPLEMENTED(FATAL);
980 return 0;
981 }
982
983 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
984 ScopedJniThreadState ts(env);
985 UNIMPLEMENTED(FATAL);
986 return 0;
987 }
988
989 static jdouble CallDoubleMethodV(JNIEnv* env,
990 jobject obj, jmethodID methodID, va_list args) {
991 ScopedJniThreadState ts(env);
992 UNIMPLEMENTED(FATAL);
993 return 0;
994 }
995
996 static jdouble CallDoubleMethodA(JNIEnv* env,
997 jobject obj, jmethodID methodID, jvalue* args) {
998 ScopedJniThreadState ts(env);
999 UNIMPLEMENTED(FATAL);
1000 return 0;
1001 }
1002
1003 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
1004 ScopedJniThreadState ts(env);
1005 UNIMPLEMENTED(FATAL);
1006 }
1007
1008 static void CallVoidMethodV(JNIEnv* env, jobject obj,
1009 jmethodID methodID, va_list args) {
1010 ScopedJniThreadState ts(env);
1011 UNIMPLEMENTED(FATAL);
1012 }
1013
1014 static void CallVoidMethodA(JNIEnv* env, jobject obj,
1015 jmethodID methodID, jvalue* args) {
1016 ScopedJniThreadState ts(env);
1017 UNIMPLEMENTED(FATAL);
1018 }
1019
1020 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
1021 jobject obj, jclass clazz, jmethodID methodID, ...) {
1022 ScopedJniThreadState ts(env);
1023 va_list ap;
1024 va_start(ap, methodID);
1025 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1026 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1027 va_end(ap);
1028 return local_result;
1029 }
1030
1031 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
1032 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1033 ScopedJniThreadState ts(env);
1034 JValue result = InvokeWithVarArgs(ts, obj, methodID, args);
1035 return AddLocalReference<jobject>(ts, result.l);
1036 }
1037
1038 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
1039 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1040 ScopedJniThreadState ts(env);
1041 JValue result = InvokeWithJValues(ts, obj, methodID, args);
1042 return AddLocalReference<jobject>(ts, result.l);
1043 }
1044
1045 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
1046 jobject obj, jclass clazz, jmethodID methodID, ...) {
1047 ScopedJniThreadState ts(env);
1048 va_list ap;
1049 va_start(ap, methodID);
1050 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1051 va_end(ap);
1052 return result.z;
1053 }
1054
1055 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
1056 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1057 ScopedJniThreadState ts(env);
1058 return InvokeWithVarArgs(ts, obj, methodID, args).z;
1059 }
1060
1061 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
1062 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1063 ScopedJniThreadState ts(env);
1064 return InvokeWithJValues(ts, obj, methodID, args).z;
1065 }
1066
1067 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
1068 jobject obj, jclass clazz, jmethodID methodID, ...) {
1069 ScopedJniThreadState ts(env);
1070 va_list ap;
1071 va_start(ap, methodID);
1072 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1073 va_end(ap);
1074 return result.b;
1075 }
1076
1077 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
1078 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1079 ScopedJniThreadState ts(env);
1080 return InvokeWithVarArgs(ts, obj, methodID, args).b;
1081 }
1082
1083 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
1084 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1085 ScopedJniThreadState ts(env);
1086 return InvokeWithJValues(ts, obj, methodID, args).b;
1087 }
1088
1089 static jchar CallNonvirtualCharMethod(JNIEnv* env,
1090 jobject obj, jclass clazz, jmethodID methodID, ...) {
1091 ScopedJniThreadState ts(env);
1092 va_list ap;
1093 va_start(ap, methodID);
1094 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1095 va_end(ap);
1096 return result.c;
1097 }
1098
1099 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
1100 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1101 ScopedJniThreadState ts(env);
1102 return InvokeWithVarArgs(ts, obj, methodID, args).c;
1103 }
1104
1105 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
1106 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1107 ScopedJniThreadState ts(env);
1108 return InvokeWithJValues(ts, obj, methodID, args).c;
1109 }
1110
1111 static jshort CallNonvirtualShortMethod(JNIEnv* env,
1112 jobject obj, jclass clazz, jmethodID methodID, ...) {
1113 ScopedJniThreadState ts(env);
1114 va_list ap;
1115 va_start(ap, methodID);
1116 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1117 va_end(ap);
1118 return result.s;
1119 }
1120
1121 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
1122 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1123 ScopedJniThreadState ts(env);
1124 return InvokeWithVarArgs(ts, obj, methodID, args).s;
1125 }
1126
1127 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
1128 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1129 ScopedJniThreadState ts(env);
1130 return InvokeWithJValues(ts, obj, methodID, args).s;
1131 }
1132
1133 static jint CallNonvirtualIntMethod(JNIEnv* env,
1134 jobject obj, jclass clazz, jmethodID methodID, ...) {
1135 ScopedJniThreadState ts(env);
1136 va_list ap;
1137 va_start(ap, methodID);
1138 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1139 va_end(ap);
1140 return result.i;
1141 }
1142
1143 static jint CallNonvirtualIntMethodV(JNIEnv* env,
1144 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1145 ScopedJniThreadState ts(env);
1146 return InvokeWithVarArgs(ts, obj, methodID, args).i;
1147 }
1148
1149 static jint CallNonvirtualIntMethodA(JNIEnv* env,
1150 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1151 ScopedJniThreadState ts(env);
1152 return InvokeWithJValues(ts, obj, methodID, args).i;
1153 }
1154
1155 static jlong CallNonvirtualLongMethod(JNIEnv* env,
1156 jobject obj, jclass clazz, jmethodID methodID, ...) {
1157 ScopedJniThreadState ts(env);
1158 va_list ap;
1159 va_start(ap, methodID);
1160 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1161 va_end(ap);
1162 return result.j;
1163 }
1164
1165 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
1166 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1167 ScopedJniThreadState ts(env);
1168 return InvokeWithVarArgs(ts, obj, methodID, args).j;
1169 }
1170
1171 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
1172 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1173 ScopedJniThreadState ts(env);
1174 return InvokeWithJValues(ts, obj, methodID, args).j;
1175 }
1176
1177 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
1178 jobject obj, jclass clazz, jmethodID methodID, ...) {
1179 ScopedJniThreadState ts(env);
1180 va_list ap;
1181 va_start(ap, methodID);
1182 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1183 va_end(ap);
1184 return result.f;
1185 }
1186
1187 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
1188 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1189 ScopedJniThreadState ts(env);
1190 return InvokeWithVarArgs(ts, obj, methodID, args).f;
1191 }
1192
1193 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
1194 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1195 ScopedJniThreadState ts(env);
1196 return InvokeWithJValues(ts, obj, methodID, args).f;
1197 }
1198
1199 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
1200 jobject obj, jclass clazz, jmethodID methodID, ...) {
1201 ScopedJniThreadState ts(env);
1202 va_list ap;
1203 va_start(ap, methodID);
1204 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1205 va_end(ap);
1206 return result.d;
1207 }
1208
1209 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
1210 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1211 ScopedJniThreadState ts(env);
1212 return InvokeWithVarArgs(ts, obj, methodID, args).d;
1213 }
1214
1215 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
1216 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1217 ScopedJniThreadState ts(env);
1218 return InvokeWithJValues(ts, obj, methodID, args).d;
1219 }
1220
1221 static void CallNonvirtualVoidMethod(JNIEnv* env,
1222 jobject obj, jclass clazz, jmethodID methodID, ...) {
1223 ScopedJniThreadState ts(env);
1224 va_list ap;
1225 va_start(ap, methodID);
1226 InvokeWithVarArgs(ts, obj, methodID, ap);
1227 va_end(ap);
1228 }
1229
1230 static void CallNonvirtualVoidMethodV(JNIEnv* env,
1231 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1232 ScopedJniThreadState ts(env);
1233 InvokeWithVarArgs(ts, obj, methodID, args);
1234 }
1235
1236 static void CallNonvirtualVoidMethodA(JNIEnv* env,
1237 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1238 ScopedJniThreadState ts(env);
1239 InvokeWithJValues(ts, obj, methodID, args);
1240 }
1241
1242 static jfieldID GetFieldID(JNIEnv* env,
1243 jclass c, const char* name, const char* sig) {
1244 ScopedJniThreadState ts(env);
1245 return FindFieldID(ts, c, name, sig, false);
1246 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001247
1248
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 static jfieldID GetStaticFieldID(JNIEnv* env,
1250 jclass c, const char* name, const char* sig) {
1251 ScopedJniThreadState ts(env);
1252 return FindFieldID(ts, c, name, sig, true);
1253 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001254
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001255 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001257 Object* o = Decode<Object*>(ts, obj);
1258 Field* f = DecodeField(ts, fid);
1259 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001261
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001262 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001264 Field* f = DecodeField(ts, fid);
1265 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001268 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001270 Object* o = Decode<Object*>(ts, java_object);
1271 Object* v = Decode<Object*>(ts, java_value);
1272 Field* f = DecodeField(ts, fid);
1273 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001276 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001278 Object* v = Decode<Object*>(ts, java_value);
1279 Field* f = DecodeField(ts, fid);
1280 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001283#define GET_PRIMITIVE_FIELD(fn, instance) \
1284 ScopedJniThreadState ts(env); \
1285 Object* o = Decode<Object*>(ts, instance); \
1286 Field* f = DecodeField(ts, fid); \
1287 return f->fn(o)
1288
1289#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1290 ScopedJniThreadState ts(env); \
1291 Object* o = Decode<Object*>(ts, instance); \
1292 Field* f = DecodeField(ts, fid); \
1293 f->fn(o, value)
1294
1295 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1296 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001299 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1300 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001303 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1304 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001307 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1308 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001311 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1312 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001315 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1316 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001319 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1320 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001323 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1324 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001327 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1328 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001331 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1332 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001335 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1336 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001339 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1340 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1344 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1348 GET_PRIMITIVE_FIELD(GetLong, NULL);
1349 }
1350
1351 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1352 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1353 }
1354
1355 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1356 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1357 }
1358
1359 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1360 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1361 }
1362
1363 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1364 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1365 }
1366
1367 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1368 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1369 }
1370
1371 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1372 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1373 }
1374
1375 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1376 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1377 }
1378
1379 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1380 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1381 }
1382
1383 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1384 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1385 }
1386
1387 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1388 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1389 }
1390
1391 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1392 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1393 }
1394
1395 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1396 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1397 }
1398
1399 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1400 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1401 }
1402
1403 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1404 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1405 }
1406
1407 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1408 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1409 }
1410
1411 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1412 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1413 }
1414
1415 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1416 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1417 }
1418
1419 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1420 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
1423 static jobject CallStaticObjectMethod(JNIEnv* env,
1424 jclass clazz, jmethodID methodID, ...) {
1425 ScopedJniThreadState ts(env);
1426 va_list ap;
1427 va_start(ap, methodID);
1428 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1429 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1430 va_end(ap);
1431 return local_result;
1432 }
1433
1434 static jobject CallStaticObjectMethodV(JNIEnv* env,
1435 jclass clazz, jmethodID methodID, va_list args) {
1436 ScopedJniThreadState ts(env);
1437 JValue result = InvokeWithVarArgs(ts, NULL, methodID, args);
1438 return AddLocalReference<jobject>(ts, result.l);
1439 }
1440
1441 static jobject CallStaticObjectMethodA(JNIEnv* env,
1442 jclass clazz, jmethodID methodID, jvalue* args) {
1443 ScopedJniThreadState ts(env);
1444 JValue result = InvokeWithJValues(ts, NULL, methodID, args);
1445 return AddLocalReference<jobject>(ts, result.l);
1446 }
1447
1448 static jboolean CallStaticBooleanMethod(JNIEnv* env,
1449 jclass clazz, jmethodID methodID, ...) {
1450 ScopedJniThreadState ts(env);
1451 va_list ap;
1452 va_start(ap, methodID);
1453 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1454 va_end(ap);
1455 return result.z;
1456 }
1457
1458 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
1459 jclass clazz, jmethodID methodID, va_list args) {
1460 ScopedJniThreadState ts(env);
1461 return InvokeWithVarArgs(ts, NULL, methodID, args).z;
1462 }
1463
1464 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
1465 jclass clazz, jmethodID methodID, jvalue* args) {
1466 ScopedJniThreadState ts(env);
1467 return InvokeWithJValues(ts, NULL, methodID, args).z;
1468 }
1469
1470 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1471 ScopedJniThreadState ts(env);
1472 va_list ap;
1473 va_start(ap, methodID);
1474 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1475 va_end(ap);
1476 return result.b;
1477 }
1478
1479 static jbyte CallStaticByteMethodV(JNIEnv* env,
1480 jclass clazz, jmethodID methodID, va_list args) {
1481 ScopedJniThreadState ts(env);
1482 return InvokeWithVarArgs(ts, NULL, methodID, args).b;
1483 }
1484
1485 static jbyte CallStaticByteMethodA(JNIEnv* env,
1486 jclass clazz, jmethodID methodID, jvalue* args) {
1487 ScopedJniThreadState ts(env);
1488 return InvokeWithJValues(ts, NULL, methodID, args).b;
1489 }
1490
1491 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1492 ScopedJniThreadState ts(env);
1493 va_list ap;
1494 va_start(ap, methodID);
1495 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1496 va_end(ap);
1497 return result.c;
1498 }
1499
1500 static jchar CallStaticCharMethodV(JNIEnv* env,
1501 jclass clazz, jmethodID methodID, va_list args) {
1502 ScopedJniThreadState ts(env);
1503 return InvokeWithVarArgs(ts, NULL, methodID, args).c;
1504 }
1505
1506 static jchar CallStaticCharMethodA(JNIEnv* env,
1507 jclass clazz, jmethodID methodID, jvalue* args) {
1508 ScopedJniThreadState ts(env);
1509 return InvokeWithJValues(ts, NULL, methodID, args).c;
1510 }
1511
1512 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1513 ScopedJniThreadState ts(env);
1514 va_list ap;
1515 va_start(ap, methodID);
1516 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1517 va_end(ap);
1518 return result.s;
1519 }
1520
1521 static jshort CallStaticShortMethodV(JNIEnv* env,
1522 jclass clazz, jmethodID methodID, va_list args) {
1523 ScopedJniThreadState ts(env);
1524 return InvokeWithVarArgs(ts, NULL, methodID, args).s;
1525 }
1526
1527 static jshort CallStaticShortMethodA(JNIEnv* env,
1528 jclass clazz, jmethodID methodID, jvalue* args) {
1529 ScopedJniThreadState ts(env);
1530 return InvokeWithJValues(ts, NULL, methodID, args).s;
1531 }
1532
1533 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1534 ScopedJniThreadState ts(env);
1535 va_list ap;
1536 va_start(ap, methodID);
1537 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1538 va_end(ap);
1539 return result.i;
1540 }
1541
1542 static jint CallStaticIntMethodV(JNIEnv* env,
1543 jclass clazz, jmethodID methodID, va_list args) {
1544 ScopedJniThreadState ts(env);
1545 return InvokeWithVarArgs(ts, NULL, methodID, args).i;
1546 }
1547
1548 static jint CallStaticIntMethodA(JNIEnv* env,
1549 jclass clazz, jmethodID methodID, jvalue* args) {
1550 ScopedJniThreadState ts(env);
1551 return InvokeWithJValues(ts, NULL, methodID, args).i;
1552 }
1553
1554 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1555 ScopedJniThreadState ts(env);
1556 va_list ap;
1557 va_start(ap, methodID);
1558 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1559 va_end(ap);
1560 return result.j;
1561 }
1562
1563 static jlong CallStaticLongMethodV(JNIEnv* env,
1564 jclass clazz, jmethodID methodID, va_list args) {
1565 ScopedJniThreadState ts(env);
1566 return InvokeWithVarArgs(ts, NULL, methodID, args).j;
1567 }
1568
1569 static jlong CallStaticLongMethodA(JNIEnv* env,
1570 jclass clazz, jmethodID methodID, jvalue* args) {
1571 ScopedJniThreadState ts(env);
1572 return InvokeWithJValues(ts, NULL, methodID, args).j;
1573 }
1574
1575 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1576 ScopedJniThreadState ts(env);
1577 va_list ap;
1578 va_start(ap, methodID);
1579 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1580 va_end(ap);
1581 return result.f;
1582 }
1583
1584 static jfloat CallStaticFloatMethodV(JNIEnv* env,
1585 jclass clazz, jmethodID methodID, va_list args) {
1586 ScopedJniThreadState ts(env);
1587 return InvokeWithVarArgs(ts, NULL, methodID, args).f;
1588 }
1589
1590 static jfloat CallStaticFloatMethodA(JNIEnv* env,
1591 jclass clazz, jmethodID methodID, jvalue* args) {
1592 ScopedJniThreadState ts(env);
1593 return InvokeWithJValues(ts, NULL, methodID, args).f;
1594 }
1595
1596 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1597 ScopedJniThreadState ts(env);
1598 va_list ap;
1599 va_start(ap, methodID);
1600 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1601 va_end(ap);
1602 return result.d;
1603 }
1604
1605 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
1606 jclass clazz, jmethodID methodID, va_list args) {
1607 ScopedJniThreadState ts(env);
1608 return InvokeWithVarArgs(ts, NULL, methodID, args).d;
1609 }
1610
1611 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
1612 jclass clazz, jmethodID methodID, jvalue* args) {
1613 ScopedJniThreadState ts(env);
1614 return InvokeWithJValues(ts, NULL, methodID, args).d;
1615 }
1616
1617 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1618 ScopedJniThreadState ts(env);
1619 va_list ap;
1620 va_start(ap, methodID);
1621 InvokeWithVarArgs(ts, NULL, methodID, ap);
1622 va_end(ap);
1623 }
1624
1625 static void CallStaticVoidMethodV(JNIEnv* env,
1626 jclass cls, jmethodID methodID, va_list args) {
1627 ScopedJniThreadState ts(env);
1628 InvokeWithVarArgs(ts, NULL, methodID, args);
1629 }
1630
1631 static void CallStaticVoidMethodA(JNIEnv* env,
1632 jclass cls, jmethodID methodID, jvalue* args) {
1633 ScopedJniThreadState ts(env);
1634 InvokeWithJValues(ts, NULL, methodID, args);
1635 }
1636
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 static jstring NewString(JNIEnv* env, const jchar* unicode, jsize len) {
1638 ScopedJniThreadState ts(env);
1639 UNIMPLEMENTED(FATAL);
1640 return NULL;
1641 }
1642
1643 static jsize GetStringLength(JNIEnv* env, jstring str) {
1644 ScopedJniThreadState ts(env);
1645 UNIMPLEMENTED(FATAL);
1646 return 0;
1647 }
1648
1649 static const jchar* GetStringChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1650 ScopedJniThreadState ts(env);
1651 UNIMPLEMENTED(FATAL);
1652 return NULL;
1653 }
1654
1655 static void ReleaseStringChars(JNIEnv* env, jstring str, const jchar* chars) {
1656 ScopedJniThreadState ts(env);
1657 UNIMPLEMENTED(FATAL);
1658 }
1659
1660 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1661 ScopedJniThreadState ts(env);
1662 if (utf == NULL) {
1663 return NULL;
1664 }
1665 String* result = String::AllocFromModifiedUtf8(utf);
1666 return AddLocalReference<jstring>(ts, result);
1667 }
1668
1669 static jsize GetStringUTFLength(JNIEnv* env, jstring str) {
1670 ScopedJniThreadState ts(env);
1671 UNIMPLEMENTED(FATAL);
1672 return 0;
1673 }
1674
1675 static const char* GetStringUTFChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1676 ScopedJniThreadState ts(env);
1677 UNIMPLEMENTED(FATAL);
1678 return NULL;
1679 }
1680
1681 static void ReleaseStringUTFChars(JNIEnv* env, jstring str, const char* chars) {
1682 ScopedJniThreadState ts(env);
1683 UNIMPLEMENTED(FATAL);
1684 }
1685
Elliott Hughesbd935992011-08-22 11:59:34 -07001686 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001688 Object* obj = Decode<Object*>(ts, java_array);
1689 CHECK(obj->IsArray()); // TODO: ReportJniError
1690 Array* array = obj->AsArray();
1691 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
1694 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1695 ScopedJniThreadState ts(env);
1696 UNIMPLEMENTED(FATAL);
1697 return NULL;
1698 }
1699
1700 static void SetObjectArrayElement(JNIEnv* env,
1701 jobjectArray java_array, jsize index, jobject java_value) {
1702 ScopedJniThreadState ts(env);
1703 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1704 Object* value = Decode<Object*>(ts, java_value);
1705 array->Set(index, value);
1706 }
1707
1708 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1709 ScopedJniThreadState ts(env);
1710 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1711 }
1712
1713 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1714 ScopedJniThreadState ts(env);
1715 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1716 }
1717
1718 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1719 ScopedJniThreadState ts(env);
1720 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1721 }
1722
1723 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1724 ScopedJniThreadState ts(env);
1725 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1726 }
1727
1728 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1729 ScopedJniThreadState ts(env);
1730 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1731 }
1732
1733 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1734 ScopedJniThreadState ts(env);
1735 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1736 }
1737
1738 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1739 ScopedJniThreadState ts(env);
1740 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1741 }
1742
1743 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1744 ScopedJniThreadState ts(env);
1745 CHECK_GE(length, 0); // TODO: ReportJniError
1746
1747 // Compute the array class corresponding to the given element class.
1748 Class* element_class = Decode<Class*>(ts, element_jclass);
1749 std::string descriptor;
1750 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001751 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001752
1753 // Find the class.
1754 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1755 // TODO: need to get the appropriate ClassLoader.
1756 Class* array_class = class_linker->FindClass(descriptor, NULL);
1757 if (array_class == NULL) {
1758 return NULL;
1759 }
1760
1761 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1762 CHECK(initial_element == NULL); // TODO: support initial_element
1763 return AddLocalReference<jobjectArray>(ts, result);
1764 }
1765
1766 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1767 ScopedJniThreadState ts(env);
1768 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1769 }
1770
1771 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1772 jbooleanArray array, jboolean* isCopy) {
1773 ScopedJniThreadState ts(env);
1774 UNIMPLEMENTED(FATAL);
1775 return NULL;
1776 }
1777
1778 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1779 ScopedJniThreadState ts(env);
1780 UNIMPLEMENTED(FATAL);
1781 return NULL;
1782 }
1783
1784 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1785 ScopedJniThreadState ts(env);
1786 UNIMPLEMENTED(FATAL);
1787 return NULL;
1788 }
1789
1790 static jshort* GetShortArrayElements(JNIEnv* env,
1791 jshortArray array, jboolean* isCopy) {
1792 ScopedJniThreadState ts(env);
1793 UNIMPLEMENTED(FATAL);
1794 return NULL;
1795 }
1796
1797 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1798 ScopedJniThreadState ts(env);
1799 UNIMPLEMENTED(FATAL);
1800 return NULL;
1801 }
1802
1803 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1804 ScopedJniThreadState ts(env);
1805 UNIMPLEMENTED(FATAL);
1806 return NULL;
1807 }
1808
1809 static jfloat* GetFloatArrayElements(JNIEnv* env,
1810 jfloatArray array, jboolean* isCopy) {
1811 ScopedJniThreadState ts(env);
1812 UNIMPLEMENTED(FATAL);
1813 return NULL;
1814 }
1815
1816 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1817 jdoubleArray array, jboolean* isCopy) {
1818 ScopedJniThreadState ts(env);
1819 UNIMPLEMENTED(FATAL);
1820 return NULL;
1821 }
1822
1823 static void ReleaseBooleanArrayElements(JNIEnv* env,
1824 jbooleanArray array, jboolean* elems, jint mode) {
1825 ScopedJniThreadState ts(env);
1826 UNIMPLEMENTED(FATAL);
1827 }
1828
1829 static void ReleaseByteArrayElements(JNIEnv* env,
1830 jbyteArray array, jbyte* elems, jint mode) {
1831 ScopedJniThreadState ts(env);
1832 UNIMPLEMENTED(FATAL);
1833 }
1834
1835 static void ReleaseCharArrayElements(JNIEnv* env,
1836 jcharArray array, jchar* elems, jint mode) {
1837 ScopedJniThreadState ts(env);
1838 UNIMPLEMENTED(FATAL);
1839 }
1840
1841 static void ReleaseShortArrayElements(JNIEnv* env,
1842 jshortArray array, jshort* elems, jint mode) {
1843 ScopedJniThreadState ts(env);
1844 UNIMPLEMENTED(FATAL);
1845 }
1846
1847 static void ReleaseIntArrayElements(JNIEnv* env,
1848 jintArray array, jint* elems, jint mode) {
1849 ScopedJniThreadState ts(env);
1850 UNIMPLEMENTED(FATAL);
1851 }
1852
1853 static void ReleaseLongArrayElements(JNIEnv* env,
1854 jlongArray array, jlong* elems, jint mode) {
1855 ScopedJniThreadState ts(env);
1856 UNIMPLEMENTED(FATAL);
1857 }
1858
1859 static void ReleaseFloatArrayElements(JNIEnv* env,
1860 jfloatArray array, jfloat* elems, jint mode) {
1861 ScopedJniThreadState ts(env);
1862 UNIMPLEMENTED(FATAL);
1863 }
1864
1865 static void ReleaseDoubleArrayElements(JNIEnv* env,
1866 jdoubleArray array, jdouble* elems, jint mode) {
1867 ScopedJniThreadState ts(env);
1868 UNIMPLEMENTED(FATAL);
1869 }
1870
1871 static void GetBooleanArrayRegion(JNIEnv* env,
1872 jbooleanArray array, jsize start, jsize l, jboolean* buf) {
1873 ScopedJniThreadState ts(env);
1874 UNIMPLEMENTED(FATAL);
1875 }
1876
1877 static void GetByteArrayRegion(JNIEnv* env,
1878 jbyteArray array, jsize start, jsize len, jbyte* buf) {
1879 ScopedJniThreadState ts(env);
1880 UNIMPLEMENTED(FATAL);
1881 }
1882
1883 static void GetCharArrayRegion(JNIEnv* env,
1884 jcharArray array, jsize start, jsize len, jchar* buf) {
1885 ScopedJniThreadState ts(env);
1886 UNIMPLEMENTED(FATAL);
1887 }
1888
1889 static void GetShortArrayRegion(JNIEnv* env,
1890 jshortArray array, jsize start, jsize len, jshort* buf) {
1891 ScopedJniThreadState ts(env);
1892 UNIMPLEMENTED(FATAL);
1893 }
1894
1895 static void GetIntArrayRegion(JNIEnv* env,
1896 jintArray array, jsize start, jsize len, jint* buf) {
1897 ScopedJniThreadState ts(env);
1898 UNIMPLEMENTED(FATAL);
1899 }
1900
1901 static void GetLongArrayRegion(JNIEnv* env,
1902 jlongArray array, jsize start, jsize len, jlong* buf) {
1903 ScopedJniThreadState ts(env);
1904 UNIMPLEMENTED(FATAL);
1905 }
1906
1907 static void GetFloatArrayRegion(JNIEnv* env,
1908 jfloatArray array, jsize start, jsize len, jfloat* buf) {
1909 ScopedJniThreadState ts(env);
1910 UNIMPLEMENTED(FATAL);
1911 }
1912
1913 static void GetDoubleArrayRegion(JNIEnv* env,
1914 jdoubleArray array, jsize start, jsize len, jdouble* buf) {
1915 ScopedJniThreadState ts(env);
1916 UNIMPLEMENTED(FATAL);
1917 }
1918
1919 static void SetBooleanArrayRegion(JNIEnv* env,
1920 jbooleanArray array, jsize start, jsize l, const jboolean* buf) {
1921 ScopedJniThreadState ts(env);
1922 UNIMPLEMENTED(FATAL);
1923 }
1924
1925 static void SetByteArrayRegion(JNIEnv* env,
1926 jbyteArray array, jsize start, jsize len, const jbyte* buf) {
1927 ScopedJniThreadState ts(env);
1928 UNIMPLEMENTED(FATAL);
1929 }
1930
1931 static void SetCharArrayRegion(JNIEnv* env,
1932 jcharArray array, jsize start, jsize len, const jchar* buf) {
1933 ScopedJniThreadState ts(env);
1934 UNIMPLEMENTED(FATAL);
1935 }
1936
1937 static void SetShortArrayRegion(JNIEnv* env,
1938 jshortArray array, jsize start, jsize len, const jshort* buf) {
1939 ScopedJniThreadState ts(env);
1940 UNIMPLEMENTED(FATAL);
1941 }
1942
1943 static void SetIntArrayRegion(JNIEnv* env,
1944 jintArray array, jsize start, jsize len, const jint* buf) {
1945 ScopedJniThreadState ts(env);
1946 UNIMPLEMENTED(FATAL);
1947 }
1948
1949 static void SetLongArrayRegion(JNIEnv* env,
1950 jlongArray array, jsize start, jsize len, const jlong* buf) {
1951 ScopedJniThreadState ts(env);
1952 UNIMPLEMENTED(FATAL);
1953 }
1954
1955 static void SetFloatArrayRegion(JNIEnv* env,
1956 jfloatArray array, jsize start, jsize len, const jfloat* buf) {
1957 ScopedJniThreadState ts(env);
1958 UNIMPLEMENTED(FATAL);
1959 }
1960
1961 static void SetDoubleArrayRegion(JNIEnv* env,
1962 jdoubleArray array, jsize start, jsize len, const jdouble* buf) {
1963 ScopedJniThreadState ts(env);
1964 UNIMPLEMENTED(FATAL);
1965 }
1966
1967 static jint RegisterNatives(JNIEnv* env,
1968 jclass clazz, const JNINativeMethod* methods, jint nMethods) {
1969 ScopedJniThreadState ts(env);
1970 Class* klass = Decode<Class*>(ts, clazz);
1971 for(int i = 0; i < nMethods; i++) {
1972 const char* name = methods[i].name;
1973 const char* sig = methods[i].signature;
1974
1975 if (*sig == '!') {
1976 // TODO: fast jni. it's too noisy to log all these.
1977 ++sig;
1978 }
1979
1980 Method* method = klass->FindDirectMethod(name, sig);
1981 if (method == NULL) {
1982 method = klass->FindVirtualMethod(name, sig);
1983 }
1984 if (method == NULL) {
1985 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001986 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
1988 "no method \"%s.%s%s\"",
1989 class_name.c_str(), name, sig);
1990 return JNI_ERR;
1991 } else if (!method->IsNative()) {
1992 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001993 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
1995 "method \"%s.%s%s\" is not native",
1996 class_name.c_str(), name, sig);
1997 return JNI_ERR;
1998 }
1999 method->RegisterNative(methods[i].fnPtr);
2000 }
2001 return JNI_OK;
2002 }
2003
2004 static jint UnregisterNatives(JNIEnv* env, jclass clazz) {
2005 ScopedJniThreadState ts(env);
2006 UNIMPLEMENTED(FATAL);
2007 return 0;
2008 }
2009
2010 static jint MonitorEnter(JNIEnv* env, jobject obj) {
2011 ScopedJniThreadState ts(env);
2012 UNIMPLEMENTED(WARNING);
2013 return 0;
2014 }
2015
2016 static jint MonitorExit(JNIEnv* env, jobject obj) {
2017 ScopedJniThreadState ts(env);
2018 UNIMPLEMENTED(WARNING);
2019 return 0;
2020 }
2021
2022 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2023 ScopedJniThreadState ts(env);
2024 Runtime* runtime = Runtime::Current();
2025 if (runtime != NULL) {
2026 *vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
2027 } else {
2028 *vm = NULL;
2029 }
2030 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2031 }
2032
2033 static void GetStringRegion(JNIEnv* env,
2034 jstring str, jsize start, jsize len, jchar* buf) {
2035 ScopedJniThreadState ts(env);
2036 UNIMPLEMENTED(FATAL);
2037 }
2038
2039 static void GetStringUTFRegion(JNIEnv* env,
2040 jstring str, jsize start, jsize len, char* buf) {
2041 ScopedJniThreadState ts(env);
2042 UNIMPLEMENTED(FATAL);
2043 }
2044
2045 static void* GetPrimitiveArrayCritical(JNIEnv* env,
2046 jarray array, jboolean* isCopy) {
2047 ScopedJniThreadState ts(env);
2048 UNIMPLEMENTED(FATAL);
2049 return NULL;
2050 }
2051
2052 static void ReleasePrimitiveArrayCritical(JNIEnv* env,
2053 jarray array, void* carray, jint mode) {
2054 ScopedJniThreadState ts(env);
2055 UNIMPLEMENTED(FATAL);
2056 }
2057
2058 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
2059 ScopedJniThreadState ts(env);
2060 UNIMPLEMENTED(FATAL);
2061 return NULL;
2062 }
2063
2064 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
2065 ScopedJniThreadState ts(env);
2066 UNIMPLEMENTED(FATAL);
2067 }
2068
2069 static jboolean ExceptionCheck(JNIEnv* env) {
2070 ScopedJniThreadState ts(env);
2071 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
2072 }
2073
2074 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2075 ScopedJniThreadState ts(env);
2076 UNIMPLEMENTED(FATAL);
2077 return NULL;
2078 }
2079
2080 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
2081 ScopedJniThreadState ts(env);
2082 UNIMPLEMENTED(FATAL);
2083 return NULL;
2084 }
2085
2086 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
2087 ScopedJniThreadState ts(env);
2088 UNIMPLEMENTED(FATAL);
2089 return 0;
2090 }
2091
2092 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2093 ScopedJniThreadState ts(env);
2094 UNIMPLEMENTED(FATAL);
2095 return JNIInvalidRefType;
2096 }
2097};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002098
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002099static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002100 NULL, // reserved0.
2101 NULL, // reserved1.
2102 NULL, // reserved2.
2103 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 JNI::GetVersion,
2105 JNI::DefineClass,
2106 JNI::FindClass,
2107 JNI::FromReflectedMethod,
2108 JNI::FromReflectedField,
2109 JNI::ToReflectedMethod,
2110 JNI::GetSuperclass,
2111 JNI::IsAssignableFrom,
2112 JNI::ToReflectedField,
2113 JNI::Throw,
2114 JNI::ThrowNew,
2115 JNI::ExceptionOccurred,
2116 JNI::ExceptionDescribe,
2117 JNI::ExceptionClear,
2118 JNI::FatalError,
2119 JNI::PushLocalFrame,
2120 JNI::PopLocalFrame,
2121 JNI::NewGlobalRef,
2122 JNI::DeleteGlobalRef,
2123 JNI::DeleteLocalRef,
2124 JNI::IsSameObject,
2125 JNI::NewLocalRef,
2126 JNI::EnsureLocalCapacity,
2127 JNI::AllocObject,
2128 JNI::NewObject,
2129 JNI::NewObjectV,
2130 JNI::NewObjectA,
2131 JNI::GetObjectClass,
2132 JNI::IsInstanceOf,
2133 JNI::GetMethodID,
2134 JNI::CallObjectMethod,
2135 JNI::CallObjectMethodV,
2136 JNI::CallObjectMethodA,
2137 JNI::CallBooleanMethod,
2138 JNI::CallBooleanMethodV,
2139 JNI::CallBooleanMethodA,
2140 JNI::CallByteMethod,
2141 JNI::CallByteMethodV,
2142 JNI::CallByteMethodA,
2143 JNI::CallCharMethod,
2144 JNI::CallCharMethodV,
2145 JNI::CallCharMethodA,
2146 JNI::CallShortMethod,
2147 JNI::CallShortMethodV,
2148 JNI::CallShortMethodA,
2149 JNI::CallIntMethod,
2150 JNI::CallIntMethodV,
2151 JNI::CallIntMethodA,
2152 JNI::CallLongMethod,
2153 JNI::CallLongMethodV,
2154 JNI::CallLongMethodA,
2155 JNI::CallFloatMethod,
2156 JNI::CallFloatMethodV,
2157 JNI::CallFloatMethodA,
2158 JNI::CallDoubleMethod,
2159 JNI::CallDoubleMethodV,
2160 JNI::CallDoubleMethodA,
2161 JNI::CallVoidMethod,
2162 JNI::CallVoidMethodV,
2163 JNI::CallVoidMethodA,
2164 JNI::CallNonvirtualObjectMethod,
2165 JNI::CallNonvirtualObjectMethodV,
2166 JNI::CallNonvirtualObjectMethodA,
2167 JNI::CallNonvirtualBooleanMethod,
2168 JNI::CallNonvirtualBooleanMethodV,
2169 JNI::CallNonvirtualBooleanMethodA,
2170 JNI::CallNonvirtualByteMethod,
2171 JNI::CallNonvirtualByteMethodV,
2172 JNI::CallNonvirtualByteMethodA,
2173 JNI::CallNonvirtualCharMethod,
2174 JNI::CallNonvirtualCharMethodV,
2175 JNI::CallNonvirtualCharMethodA,
2176 JNI::CallNonvirtualShortMethod,
2177 JNI::CallNonvirtualShortMethodV,
2178 JNI::CallNonvirtualShortMethodA,
2179 JNI::CallNonvirtualIntMethod,
2180 JNI::CallNonvirtualIntMethodV,
2181 JNI::CallNonvirtualIntMethodA,
2182 JNI::CallNonvirtualLongMethod,
2183 JNI::CallNonvirtualLongMethodV,
2184 JNI::CallNonvirtualLongMethodA,
2185 JNI::CallNonvirtualFloatMethod,
2186 JNI::CallNonvirtualFloatMethodV,
2187 JNI::CallNonvirtualFloatMethodA,
2188 JNI::CallNonvirtualDoubleMethod,
2189 JNI::CallNonvirtualDoubleMethodV,
2190 JNI::CallNonvirtualDoubleMethodA,
2191 JNI::CallNonvirtualVoidMethod,
2192 JNI::CallNonvirtualVoidMethodV,
2193 JNI::CallNonvirtualVoidMethodA,
2194 JNI::GetFieldID,
2195 JNI::GetObjectField,
2196 JNI::GetBooleanField,
2197 JNI::GetByteField,
2198 JNI::GetCharField,
2199 JNI::GetShortField,
2200 JNI::GetIntField,
2201 JNI::GetLongField,
2202 JNI::GetFloatField,
2203 JNI::GetDoubleField,
2204 JNI::SetObjectField,
2205 JNI::SetBooleanField,
2206 JNI::SetByteField,
2207 JNI::SetCharField,
2208 JNI::SetShortField,
2209 JNI::SetIntField,
2210 JNI::SetLongField,
2211 JNI::SetFloatField,
2212 JNI::SetDoubleField,
2213 JNI::GetStaticMethodID,
2214 JNI::CallStaticObjectMethod,
2215 JNI::CallStaticObjectMethodV,
2216 JNI::CallStaticObjectMethodA,
2217 JNI::CallStaticBooleanMethod,
2218 JNI::CallStaticBooleanMethodV,
2219 JNI::CallStaticBooleanMethodA,
2220 JNI::CallStaticByteMethod,
2221 JNI::CallStaticByteMethodV,
2222 JNI::CallStaticByteMethodA,
2223 JNI::CallStaticCharMethod,
2224 JNI::CallStaticCharMethodV,
2225 JNI::CallStaticCharMethodA,
2226 JNI::CallStaticShortMethod,
2227 JNI::CallStaticShortMethodV,
2228 JNI::CallStaticShortMethodA,
2229 JNI::CallStaticIntMethod,
2230 JNI::CallStaticIntMethodV,
2231 JNI::CallStaticIntMethodA,
2232 JNI::CallStaticLongMethod,
2233 JNI::CallStaticLongMethodV,
2234 JNI::CallStaticLongMethodA,
2235 JNI::CallStaticFloatMethod,
2236 JNI::CallStaticFloatMethodV,
2237 JNI::CallStaticFloatMethodA,
2238 JNI::CallStaticDoubleMethod,
2239 JNI::CallStaticDoubleMethodV,
2240 JNI::CallStaticDoubleMethodA,
2241 JNI::CallStaticVoidMethod,
2242 JNI::CallStaticVoidMethodV,
2243 JNI::CallStaticVoidMethodA,
2244 JNI::GetStaticFieldID,
2245 JNI::GetStaticObjectField,
2246 JNI::GetStaticBooleanField,
2247 JNI::GetStaticByteField,
2248 JNI::GetStaticCharField,
2249 JNI::GetStaticShortField,
2250 JNI::GetStaticIntField,
2251 JNI::GetStaticLongField,
2252 JNI::GetStaticFloatField,
2253 JNI::GetStaticDoubleField,
2254 JNI::SetStaticObjectField,
2255 JNI::SetStaticBooleanField,
2256 JNI::SetStaticByteField,
2257 JNI::SetStaticCharField,
2258 JNI::SetStaticShortField,
2259 JNI::SetStaticIntField,
2260 JNI::SetStaticLongField,
2261 JNI::SetStaticFloatField,
2262 JNI::SetStaticDoubleField,
2263 JNI::NewString,
2264 JNI::GetStringLength,
2265 JNI::GetStringChars,
2266 JNI::ReleaseStringChars,
2267 JNI::NewStringUTF,
2268 JNI::GetStringUTFLength,
2269 JNI::GetStringUTFChars,
2270 JNI::ReleaseStringUTFChars,
2271 JNI::GetArrayLength,
2272 JNI::NewObjectArray,
2273 JNI::GetObjectArrayElement,
2274 JNI::SetObjectArrayElement,
2275 JNI::NewBooleanArray,
2276 JNI::NewByteArray,
2277 JNI::NewCharArray,
2278 JNI::NewShortArray,
2279 JNI::NewIntArray,
2280 JNI::NewLongArray,
2281 JNI::NewFloatArray,
2282 JNI::NewDoubleArray,
2283 JNI::GetBooleanArrayElements,
2284 JNI::GetByteArrayElements,
2285 JNI::GetCharArrayElements,
2286 JNI::GetShortArrayElements,
2287 JNI::GetIntArrayElements,
2288 JNI::GetLongArrayElements,
2289 JNI::GetFloatArrayElements,
2290 JNI::GetDoubleArrayElements,
2291 JNI::ReleaseBooleanArrayElements,
2292 JNI::ReleaseByteArrayElements,
2293 JNI::ReleaseCharArrayElements,
2294 JNI::ReleaseShortArrayElements,
2295 JNI::ReleaseIntArrayElements,
2296 JNI::ReleaseLongArrayElements,
2297 JNI::ReleaseFloatArrayElements,
2298 JNI::ReleaseDoubleArrayElements,
2299 JNI::GetBooleanArrayRegion,
2300 JNI::GetByteArrayRegion,
2301 JNI::GetCharArrayRegion,
2302 JNI::GetShortArrayRegion,
2303 JNI::GetIntArrayRegion,
2304 JNI::GetLongArrayRegion,
2305 JNI::GetFloatArrayRegion,
2306 JNI::GetDoubleArrayRegion,
2307 JNI::SetBooleanArrayRegion,
2308 JNI::SetByteArrayRegion,
2309 JNI::SetCharArrayRegion,
2310 JNI::SetShortArrayRegion,
2311 JNI::SetIntArrayRegion,
2312 JNI::SetLongArrayRegion,
2313 JNI::SetFloatArrayRegion,
2314 JNI::SetDoubleArrayRegion,
2315 JNI::RegisterNatives,
2316 JNI::UnregisterNatives,
2317 JNI::MonitorEnter,
2318 JNI::MonitorExit,
2319 JNI::GetJavaVM,
2320 JNI::GetStringRegion,
2321 JNI::GetStringUTFRegion,
2322 JNI::GetPrimitiveArrayCritical,
2323 JNI::ReleasePrimitiveArrayCritical,
2324 JNI::GetStringCritical,
2325 JNI::ReleaseStringCritical,
2326 JNI::NewWeakGlobalRef,
2327 JNI::DeleteWeakGlobalRef,
2328 JNI::ExceptionCheck,
2329 JNI::NewDirectByteBuffer,
2330 JNI::GetDirectBufferAddress,
2331 JNI::GetDirectBufferCapacity,
2332 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002333};
2334
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002335static const size_t kMonitorsInitial = 32; // Arbitrary.
2336static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2337
2338static const size_t kLocalsInitial = 64; // Arbitrary.
2339static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002340
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002341JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002342 : fns(&gNativeInterface),
2343 self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002344 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002345 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002346 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2347 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002348}
2349
Carl Shapiroea4dca82011-08-01 13:45:38 -07002350// JNI Invocation interface.
2351
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002352extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2353 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2354 if (args->version < JNI_VERSION_1_2) {
2355 return JNI_EVERSION;
2356 }
2357 Runtime::Options options;
2358 for (int i = 0; i < args->nOptions; ++i) {
2359 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002360 options.push_back(std::make_pair(StringPiece(option->optionString),
2361 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002362 }
2363 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002364 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002365 if (runtime == NULL) {
2366 return JNI_ERR;
2367 } else {
2368 *p_env = reinterpret_cast<JNIEnv*>(Thread::Current()->GetJniEnv());
Elliott Hughes0af55432011-08-17 18:37:28 -07002369 *p_vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002370 return JNI_OK;
2371 }
2372}
2373
Elliott Hughesf2682d52011-08-15 16:37:04 -07002374extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002375 Runtime* runtime = Runtime::Current();
2376 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002377 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002378 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002379 *vm_count = 1;
Elliott Hughes0af55432011-08-17 18:37:28 -07002380 vms[0] = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002381 }
2382 return JNI_OK;
2383}
2384
2385// Historically unsupported.
2386extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2387 return JNI_ERR;
2388}
2389
Elliott Hughescdf53122011-08-19 15:46:09 -07002390class JII {
2391 public:
2392 static jint DestroyJavaVM(JavaVM* vm) {
2393 if (vm == NULL) {
2394 return JNI_ERR;
2395 } else {
2396 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2397 delete raw_vm->runtime;
2398 return JNI_OK;
2399 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002400 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002401
Elliott Hughescdf53122011-08-19 15:46:09 -07002402 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2403 if (vm == NULL || p_env == NULL) {
2404 return JNI_ERR;
2405 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002406 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2407 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002408 const char* name = NULL;
2409 if (thr_args != NULL) {
2410 // TODO: check version
2411 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2412 // TODO: thread group
2413 }
2414 bool success = runtime->AttachCurrentThread(name, p_env);
2415 if (!success) {
2416 return JNI_ERR;
2417 } else {
2418 return JNI_OK;
2419 }
2420 }
2421
2422 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2423 if (vm == NULL || p_env == NULL) {
2424 return JNI_ERR;
2425 }
2426 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2427 Runtime* runtime = raw_vm->runtime;
2428 const char* name = NULL;
2429 if (thr_args != NULL) {
2430 // TODO: check version
2431 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2432 // TODO: thread group
2433 }
2434 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2435 if (!success) {
2436 return JNI_ERR;
2437 } else {
2438 return JNI_OK;
2439 }
2440 }
2441
2442 static jint DetachCurrentThread(JavaVM* vm) {
2443 if (vm == NULL) {
2444 return JNI_ERR;
2445 } else {
2446 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2447 Runtime* runtime = raw_vm->runtime;
2448 runtime->DetachCurrentThread();
2449 return JNI_OK;
2450 }
2451 }
2452
2453 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2454 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2455 return JNI_EVERSION;
2456 }
2457 if (vm == NULL || env == NULL) {
2458 return JNI_ERR;
2459 }
2460 Thread* thread = Thread::Current();
2461 if (thread == NULL) {
2462 *env = NULL;
2463 return JNI_EDETACHED;
2464 }
2465 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002466 return JNI_OK;
2467 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002468};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002469
Elliott Hughesf2682d52011-08-15 16:37:04 -07002470struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002471 NULL, // reserved0
2472 NULL, // reserved1
2473 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002474 JII::DestroyJavaVM,
2475 JII::AttachCurrentThread,
2476 JII::DetachCurrentThread,
2477 JII::GetEnv,
2478 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002479};
2480
Elliott Hughesbbd76712011-08-17 10:25:24 -07002481static const size_t kPinTableInitialSize = 16;
2482static const size_t kPinTableMaxSize = 1024;
2483
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002484static const size_t kGlobalsInitial = 512; // Arbitrary.
2485static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2486
2487static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2488static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2489
Elliott Hughes0af55432011-08-17 18:37:28 -07002490JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002491 : fns(&gInvokeInterface),
2492 runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002493 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002494 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002495 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002496 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002497 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002498 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002499 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002500}
2501
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002502JavaVMExt::~JavaVMExt() {
2503 delete globals_lock;
2504 delete weak_globals_lock;
2505}
2506
Elliott Hughescdf53122011-08-19 15:46:09 -07002507/*
2508 * Load native code from the specified absolute pathname. Per the spec,
2509 * if we've already loaded a library with the specified pathname, we
2510 * return without doing anything.
2511 *
2512 * TODO? for better results we should absolutify the pathname. For fully
2513 * correct results we should stat to get the inode and compare that. The
2514 * existing implementation is fine so long as everybody is using
2515 * System.loadLibrary.
2516 *
2517 * The library will be associated with the specified class loader. The JNI
2518 * spec says we can't load the same library into more than one class loader.
2519 *
2520 * Returns "true" on success. On failure, sets *detail to a
2521 * human-readable description of the error or NULL if no detail is
2522 * available; ownership of the string is transferred to the caller.
2523 */
2524bool JavaVMExt::LoadNativeLibrary(const std::string& path, Object* class_loader, char** detail) {
2525 *detail = NULL;
2526
2527 // See if we've already loaded this library. If we have, and the class loader
2528 // matches, return successfully without doing anything.
2529 SharedLibrary* library = libraries[path];
2530 if (library != NULL) {
2531 if (library->GetClassLoader() != class_loader) {
2532 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2533 << "ClassLoader " << library->GetClassLoader() << "; "
2534 << "can't open in " << class_loader;
2535 *detail = strdup("already opened by different ClassLoader");
2536 return false;
2537 }
2538 if (verbose_jni) {
2539 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2540 << "ClassLoader " << class_loader << "]";
2541 }
2542 if (!library->CheckOnLoadResult(this)) {
2543 *detail = strdup("JNI_OnLoad failed before");
2544 return false;
2545 }
2546 return true;
2547 }
2548
2549 // Open the shared library. Because we're using a full path, the system
2550 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2551 // resolve this library's dependencies though.)
2552
2553 // Failures here are expected when java.library.path has several entries
2554 // and we have to hunt for the lib.
2555
2556 // The current version of the dynamic linker prints detailed information
2557 // about dlopen() failures. Some things to check if the message is
2558 // cryptic:
2559 // - make sure the library exists on the device
2560 // - verify that the right path is being opened (the debug log message
2561 // above can help with that)
2562 // - check to see if the library is valid (e.g. not zero bytes long)
2563 // - check config/prelink-linux-arm.map to ensure that the library
2564 // is listed and is not being overrun by the previous entry (if
2565 // loading suddenly stops working on a prelinked library, this is
2566 // a good one to check)
2567 // - write a trivial app that calls sleep() then dlopen(), attach
2568 // to it with "strace -p <pid>" while it sleeps, and watch for
2569 // attempts to open nonexistent dependent shared libs
2570
2571 // TODO: automate some of these checks!
2572
2573 // This can execute slowly for a large library on a busy system, so we
2574 // want to switch from RUNNING to VMWAIT while it executes. This allows
2575 // the GC to ignore us.
2576 Thread* self = Thread::Current();
2577 Thread::State old_state = self->GetState();
2578 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2579 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2580 self->SetState(old_state);
2581
2582 if (verbose_jni) {
2583 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2584 }
2585
2586 if (handle == NULL) {
2587 *detail = strdup(dlerror());
2588 return false;
2589 }
2590
2591 // Create a new entry.
2592 library = new SharedLibrary(path, handle, class_loader);
2593 UNIMPLEMENTED(ERROR) << "missing pthread_cond_init";
2594 // pthread_cond_init(&library->onLoadCond, NULL);
2595
2596 libraries[path] = library;
2597
2598 // if (pNewEntry != pActualEntry) {
2599 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2600 // freeSharedLibEntry(pNewEntry);
2601 // return CheckOnLoadResult(this, pActualEntry);
2602 // } else
2603 {
2604 if (verbose_jni) {
2605 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2606 }
2607
2608 bool result = true;
2609 void* sym = dlsym(handle, "JNI_OnLoad");
2610 if (sym == NULL) {
2611 if (verbose_jni) {
2612 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2613 }
2614 } else {
2615 // Call JNI_OnLoad. We have to override the current class
2616 // loader, which will always be "null" since the stuff at the
2617 // top of the stack is around Runtime.loadLibrary(). (See
2618 // the comments in the JNI FindClass function.)
2619 UNIMPLEMENTED(WARNING) << "need to override current class loader";
2620 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2621 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
2622 //Object* prevOverride = self->classLoaderOverride;
2623 //self->classLoaderOverride = classLoader;
2624
2625 old_state = self->GetState();
2626 self->SetState(Thread::kNative);
2627 if (verbose_jni) {
2628 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2629 }
2630 int version = (*jni_on_load)(reinterpret_cast<JavaVM*>(this), NULL);
2631 self->SetState(old_state);
2632
2633 UNIMPLEMENTED(WARNING) << "need to restore current class loader";
2634 //self->classLoaderOverride = prevOverride;
2635
2636 if (version != JNI_VERSION_1_2 &&
2637 version != JNI_VERSION_1_4 &&
2638 version != JNI_VERSION_1_6) {
2639 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2640 << "bad version: " << version;
2641 // It's unwise to call dlclose() here, but we can mark it
2642 // as bad and ensure that future load attempts will fail.
2643 // We don't know how far JNI_OnLoad got, so there could
2644 // be some partially-initialized stuff accessible through
2645 // newly-registered native method calls. We could try to
2646 // unregister them, but that doesn't seem worthwhile.
2647 result = false;
2648 } else {
2649 if (verbose_jni) {
2650 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2651 << " from JNI_OnLoad in \"" << path << "\"]";
2652 }
2653 }
2654 }
2655
2656 library->SetResult(result);
2657 return result;
2658 }
2659}
2660
Ian Rogersdf20fe02011-07-20 20:34:16 -07002661} // namespace art