blob: a892a28798b2a3cb7d6ffb0df20954ba31e22013 [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 Hughescdf53122011-08-19 15:46:09 -0700585 static jclass GetSuperclass(JNIEnv* env, jclass sub) {
586 ScopedJniThreadState ts(env);
587 UNIMPLEMENTED(FATAL);
588 return NULL;
589 }
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
752 static jobject AllocObject(JNIEnv* env, jclass clazz) {
753 ScopedJniThreadState ts(env);
754 UNIMPLEMENTED(FATAL);
755 return NULL;
756 }
757
758 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
759 ScopedJniThreadState ts(env);
760 va_list args;
761 va_start(args, methodID);
762 jobject result = NewObjectV(env, clazz, methodID, args);
763 va_end(args);
764 return result;
765 }
766
767 static jobject NewObjectV(JNIEnv* env,
768 jclass clazz, jmethodID methodID, va_list args) {
769 ScopedJniThreadState ts(env);
770 Class* klass = Decode<Class*>(ts, clazz);
771 Object* result = klass->NewInstance();
772 jobject local_result = AddLocalReference<jobject>(ts, result);
773 CallNonvirtualVoidMethodV(env, local_result, clazz, methodID, args);
774 return local_result;
775 }
776
777 static jobject NewObjectA(JNIEnv* env,
778 jclass clazz, jmethodID methodID, jvalue* args) {
779 ScopedJniThreadState ts(env);
780 Class* klass = Decode<Class*>(ts, clazz);
781 Object* result = klass->NewInstance();
782 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
783 CallNonvirtualVoidMethodA(env, local_result, clazz, methodID, args);
784 return local_result;
785 }
786
787 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
788 ScopedJniThreadState ts(env);
789 UNIMPLEMENTED(FATAL);
790 return NULL;
791 }
792
793 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
794 ScopedJniThreadState ts(env);
795 CHECK_NE(static_cast<jclass>(NULL), clazz);
796 if (jobj == NULL) {
797 // NB. JNI is different from regular Java instanceof in this respect
798 return JNI_TRUE;
799 } else {
800 Object* obj = Decode<Object*>(ts, jobj);
801 Class* klass = Decode<Class*>(ts, clazz);
802 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
803 }
804 }
805
806 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
807 ScopedJniThreadState ts(env);
808 return FindMethodID(ts, c, name, sig, false);
809 }
810
811 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
812 ScopedJniThreadState ts(env);
813 return FindMethodID(ts, c, name, sig, true);
814 }
815
816 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
817 ScopedJniThreadState ts(env);
818 UNIMPLEMENTED(FATAL);
819 return NULL;
820 }
821
822 static jobject CallObjectMethodV(JNIEnv* env,
823 jobject obj, jmethodID methodID, va_list args) {
824 ScopedJniThreadState ts(env);
825 UNIMPLEMENTED(FATAL);
826 return NULL;
827 }
828
829 static jobject CallObjectMethodA(JNIEnv* env,
830 jobject obj, jmethodID methodID, jvalue* args) {
831 ScopedJniThreadState ts(env);
832 UNIMPLEMENTED(FATAL);
833 return NULL;
834 }
835
836 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
837 ScopedJniThreadState ts(env);
838 UNIMPLEMENTED(FATAL);
839 return JNI_FALSE;
840 }
841
842 static jboolean CallBooleanMethodV(JNIEnv* env,
843 jobject obj, jmethodID methodID, va_list args) {
844 ScopedJniThreadState ts(env);
845 UNIMPLEMENTED(FATAL);
846 return JNI_FALSE;
847 }
848
849 static jboolean CallBooleanMethodA(JNIEnv* env,
850 jobject obj, jmethodID methodID, jvalue* args) {
851 ScopedJniThreadState ts(env);
852 UNIMPLEMENTED(FATAL);
853 return JNI_FALSE;
854 }
855
856 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
857 ScopedJniThreadState ts(env);
858 UNIMPLEMENTED(FATAL);
859 return 0;
860 }
861
862 static jbyte CallByteMethodV(JNIEnv* env,
863 jobject obj, jmethodID methodID, va_list args) {
864 ScopedJniThreadState ts(env);
865 UNIMPLEMENTED(FATAL);
866 return 0;
867 }
868
869 static jbyte CallByteMethodA(JNIEnv* env,
870 jobject obj, jmethodID methodID, jvalue* args) {
871 ScopedJniThreadState ts(env);
872 UNIMPLEMENTED(FATAL);
873 return 0;
874 }
875
876 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
877 ScopedJniThreadState ts(env);
878 UNIMPLEMENTED(FATAL);
879 return 0;
880 }
881
882 static jchar CallCharMethodV(JNIEnv* env,
883 jobject obj, jmethodID methodID, va_list args) {
884 ScopedJniThreadState ts(env);
885 UNIMPLEMENTED(FATAL);
886 return 0;
887 }
888
889 static jchar CallCharMethodA(JNIEnv* env,
890 jobject obj, jmethodID methodID, jvalue* args) {
891 ScopedJniThreadState ts(env);
892 UNIMPLEMENTED(FATAL);
893 return 0;
894 }
895
896 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
897 ScopedJniThreadState ts(env);
898 UNIMPLEMENTED(FATAL);
899 return 0;
900 }
901
902 static jshort CallShortMethodV(JNIEnv* env,
903 jobject obj, jmethodID methodID, va_list args) {
904 ScopedJniThreadState ts(env);
905 UNIMPLEMENTED(FATAL);
906 return 0;
907 }
908
909 static jshort CallShortMethodA(JNIEnv* env,
910 jobject obj, jmethodID methodID, jvalue* args) {
911 ScopedJniThreadState ts(env);
912 UNIMPLEMENTED(FATAL);
913 return 0;
914 }
915
916 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
917 ScopedJniThreadState ts(env);
918 UNIMPLEMENTED(FATAL);
919 return 0;
920 }
921
922 static jint CallIntMethodV(JNIEnv* env,
923 jobject obj, jmethodID methodID, va_list args) {
924 ScopedJniThreadState ts(env);
925 UNIMPLEMENTED(FATAL);
926 return 0;
927 }
928
929 static jint CallIntMethodA(JNIEnv* env,
930 jobject obj, jmethodID methodID, jvalue* args) {
931 ScopedJniThreadState ts(env);
932 UNIMPLEMENTED(FATAL);
933 return 0;
934 }
935
936 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
937 ScopedJniThreadState ts(env);
938 UNIMPLEMENTED(FATAL);
939 return 0;
940 }
941
942 static jlong CallLongMethodV(JNIEnv* env,
943 jobject obj, jmethodID methodID, va_list args) {
944 ScopedJniThreadState ts(env);
945 UNIMPLEMENTED(FATAL);
946 return 0;
947 }
948
949 static jlong CallLongMethodA(JNIEnv* env,
950 jobject obj, jmethodID methodID, jvalue* args) {
951 ScopedJniThreadState ts(env);
952 UNIMPLEMENTED(FATAL);
953 return 0;
954 }
955
956 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
957 ScopedJniThreadState ts(env);
958 UNIMPLEMENTED(FATAL);
959 return 0;
960 }
961
962 static jfloat CallFloatMethodV(JNIEnv* env,
963 jobject obj, jmethodID methodID, va_list args) {
964 ScopedJniThreadState ts(env);
965 UNIMPLEMENTED(FATAL);
966 return 0;
967 }
968
969 static jfloat CallFloatMethodA(JNIEnv* env,
970 jobject obj, jmethodID methodID, jvalue* args) {
971 ScopedJniThreadState ts(env);
972 UNIMPLEMENTED(FATAL);
973 return 0;
974 }
975
976 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
977 ScopedJniThreadState ts(env);
978 UNIMPLEMENTED(FATAL);
979 return 0;
980 }
981
982 static jdouble CallDoubleMethodV(JNIEnv* env,
983 jobject obj, jmethodID methodID, va_list args) {
984 ScopedJniThreadState ts(env);
985 UNIMPLEMENTED(FATAL);
986 return 0;
987 }
988
989 static jdouble CallDoubleMethodA(JNIEnv* env,
990 jobject obj, jmethodID methodID, jvalue* args) {
991 ScopedJniThreadState ts(env);
992 UNIMPLEMENTED(FATAL);
993 return 0;
994 }
995
996 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID methodID, ...) {
997 ScopedJniThreadState ts(env);
998 UNIMPLEMENTED(FATAL);
999 }
1000
1001 static void CallVoidMethodV(JNIEnv* env, jobject obj,
1002 jmethodID methodID, va_list args) {
1003 ScopedJniThreadState ts(env);
1004 UNIMPLEMENTED(FATAL);
1005 }
1006
1007 static void CallVoidMethodA(JNIEnv* env, jobject obj,
1008 jmethodID methodID, jvalue* args) {
1009 ScopedJniThreadState ts(env);
1010 UNIMPLEMENTED(FATAL);
1011 }
1012
1013 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
1014 jobject obj, jclass clazz, jmethodID methodID, ...) {
1015 ScopedJniThreadState ts(env);
1016 va_list ap;
1017 va_start(ap, methodID);
1018 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1019 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1020 va_end(ap);
1021 return local_result;
1022 }
1023
1024 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
1025 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1026 ScopedJniThreadState ts(env);
1027 JValue result = InvokeWithVarArgs(ts, obj, methodID, args);
1028 return AddLocalReference<jobject>(ts, result.l);
1029 }
1030
1031 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
1032 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1033 ScopedJniThreadState ts(env);
1034 JValue result = InvokeWithJValues(ts, obj, methodID, args);
1035 return AddLocalReference<jobject>(ts, result.l);
1036 }
1037
1038 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
1039 jobject obj, jclass clazz, jmethodID methodID, ...) {
1040 ScopedJniThreadState ts(env);
1041 va_list ap;
1042 va_start(ap, methodID);
1043 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1044 va_end(ap);
1045 return result.z;
1046 }
1047
1048 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
1049 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1050 ScopedJniThreadState ts(env);
1051 return InvokeWithVarArgs(ts, obj, methodID, args).z;
1052 }
1053
1054 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
1055 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1056 ScopedJniThreadState ts(env);
1057 return InvokeWithJValues(ts, obj, methodID, args).z;
1058 }
1059
1060 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
1061 jobject obj, jclass clazz, jmethodID methodID, ...) {
1062 ScopedJniThreadState ts(env);
1063 va_list ap;
1064 va_start(ap, methodID);
1065 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1066 va_end(ap);
1067 return result.b;
1068 }
1069
1070 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
1071 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1072 ScopedJniThreadState ts(env);
1073 return InvokeWithVarArgs(ts, obj, methodID, args).b;
1074 }
1075
1076 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
1077 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1078 ScopedJniThreadState ts(env);
1079 return InvokeWithJValues(ts, obj, methodID, args).b;
1080 }
1081
1082 static jchar CallNonvirtualCharMethod(JNIEnv* env,
1083 jobject obj, jclass clazz, jmethodID methodID, ...) {
1084 ScopedJniThreadState ts(env);
1085 va_list ap;
1086 va_start(ap, methodID);
1087 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1088 va_end(ap);
1089 return result.c;
1090 }
1091
1092 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
1093 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1094 ScopedJniThreadState ts(env);
1095 return InvokeWithVarArgs(ts, obj, methodID, args).c;
1096 }
1097
1098 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
1099 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1100 ScopedJniThreadState ts(env);
1101 return InvokeWithJValues(ts, obj, methodID, args).c;
1102 }
1103
1104 static jshort CallNonvirtualShortMethod(JNIEnv* env,
1105 jobject obj, jclass clazz, jmethodID methodID, ...) {
1106 ScopedJniThreadState ts(env);
1107 va_list ap;
1108 va_start(ap, methodID);
1109 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1110 va_end(ap);
1111 return result.s;
1112 }
1113
1114 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
1115 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1116 ScopedJniThreadState ts(env);
1117 return InvokeWithVarArgs(ts, obj, methodID, args).s;
1118 }
1119
1120 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
1121 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1122 ScopedJniThreadState ts(env);
1123 return InvokeWithJValues(ts, obj, methodID, args).s;
1124 }
1125
1126 static jint CallNonvirtualIntMethod(JNIEnv* env,
1127 jobject obj, jclass clazz, jmethodID methodID, ...) {
1128 ScopedJniThreadState ts(env);
1129 va_list ap;
1130 va_start(ap, methodID);
1131 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1132 va_end(ap);
1133 return result.i;
1134 }
1135
1136 static jint CallNonvirtualIntMethodV(JNIEnv* env,
1137 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1138 ScopedJniThreadState ts(env);
1139 return InvokeWithVarArgs(ts, obj, methodID, args).i;
1140 }
1141
1142 static jint CallNonvirtualIntMethodA(JNIEnv* env,
1143 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1144 ScopedJniThreadState ts(env);
1145 return InvokeWithJValues(ts, obj, methodID, args).i;
1146 }
1147
1148 static jlong CallNonvirtualLongMethod(JNIEnv* env,
1149 jobject obj, jclass clazz, jmethodID methodID, ...) {
1150 ScopedJniThreadState ts(env);
1151 va_list ap;
1152 va_start(ap, methodID);
1153 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1154 va_end(ap);
1155 return result.j;
1156 }
1157
1158 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
1159 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1160 ScopedJniThreadState ts(env);
1161 return InvokeWithVarArgs(ts, obj, methodID, args).j;
1162 }
1163
1164 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
1165 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1166 ScopedJniThreadState ts(env);
1167 return InvokeWithJValues(ts, obj, methodID, args).j;
1168 }
1169
1170 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
1171 jobject obj, jclass clazz, jmethodID methodID, ...) {
1172 ScopedJniThreadState ts(env);
1173 va_list ap;
1174 va_start(ap, methodID);
1175 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1176 va_end(ap);
1177 return result.f;
1178 }
1179
1180 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
1181 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1182 ScopedJniThreadState ts(env);
1183 return InvokeWithVarArgs(ts, obj, methodID, args).f;
1184 }
1185
1186 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
1187 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1188 ScopedJniThreadState ts(env);
1189 return InvokeWithJValues(ts, obj, methodID, args).f;
1190 }
1191
1192 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
1193 jobject obj, jclass clazz, jmethodID methodID, ...) {
1194 ScopedJniThreadState ts(env);
1195 va_list ap;
1196 va_start(ap, methodID);
1197 JValue result = InvokeWithVarArgs(ts, obj, methodID, ap);
1198 va_end(ap);
1199 return result.d;
1200 }
1201
1202 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
1203 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1204 ScopedJniThreadState ts(env);
1205 return InvokeWithVarArgs(ts, obj, methodID, args).d;
1206 }
1207
1208 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
1209 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1210 ScopedJniThreadState ts(env);
1211 return InvokeWithJValues(ts, obj, methodID, args).d;
1212 }
1213
1214 static void CallNonvirtualVoidMethod(JNIEnv* env,
1215 jobject obj, jclass clazz, jmethodID methodID, ...) {
1216 ScopedJniThreadState ts(env);
1217 va_list ap;
1218 va_start(ap, methodID);
1219 InvokeWithVarArgs(ts, obj, methodID, ap);
1220 va_end(ap);
1221 }
1222
1223 static void CallNonvirtualVoidMethodV(JNIEnv* env,
1224 jobject obj, jclass clazz, jmethodID methodID, va_list args) {
1225 ScopedJniThreadState ts(env);
1226 InvokeWithVarArgs(ts, obj, methodID, args);
1227 }
1228
1229 static void CallNonvirtualVoidMethodA(JNIEnv* env,
1230 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) {
1231 ScopedJniThreadState ts(env);
1232 InvokeWithJValues(ts, obj, methodID, args);
1233 }
1234
1235 static jfieldID GetFieldID(JNIEnv* env,
1236 jclass c, const char* name, const char* sig) {
1237 ScopedJniThreadState ts(env);
1238 return FindFieldID(ts, c, name, sig, false);
1239 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001240
1241
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 static jfieldID GetStaticFieldID(JNIEnv* env,
1243 jclass c, const char* name, const char* sig) {
1244 ScopedJniThreadState ts(env);
1245 return FindFieldID(ts, c, name, sig, true);
1246 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001247
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1249 ScopedJniThreadState ts(env);
1250 UNIMPLEMENTED(FATAL);
1251 return NULL;
1252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001253
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1255 ScopedJniThreadState ts(env);
1256 UNIMPLEMENTED(FATAL);
1257 return JNI_FALSE;
1258 }
1259
1260 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1261 ScopedJniThreadState ts(env);
1262 UNIMPLEMENTED(FATAL);
1263 return 0;
1264 }
1265
1266 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1267 ScopedJniThreadState ts(env);
1268 UNIMPLEMENTED(FATAL);
1269 return 0;
1270 }
1271
1272 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1273 ScopedJniThreadState ts(env);
1274 UNIMPLEMENTED(FATAL);
1275 return 0;
1276 }
1277
1278 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1279 ScopedJniThreadState ts(env);
1280 UNIMPLEMENTED(FATAL);
1281 return 0;
1282 }
1283
1284 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1285 ScopedJniThreadState ts(env);
1286 UNIMPLEMENTED(FATAL);
1287 return 0;
1288 }
1289
1290 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1291 ScopedJniThreadState ts(env);
1292 UNIMPLEMENTED(FATAL);
1293 return 0;
1294 }
1295
1296 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fieldID) {
1297 ScopedJniThreadState ts(env);
1298 UNIMPLEMENTED(FATAL);
1299 return 0;
1300 }
1301
1302 static void SetObjectField(JNIEnv* env, jobject obj, jfieldID fieldID, jobject val) {
1303 ScopedJniThreadState ts(env);
1304 UNIMPLEMENTED(FATAL);
1305 }
1306
1307 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fieldID, jboolean val) {
1308 ScopedJniThreadState ts(env);
1309 UNIMPLEMENTED(FATAL);
1310 }
1311
1312 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fieldID, jbyte val) {
1313 ScopedJniThreadState ts(env);
1314 UNIMPLEMENTED(FATAL);
1315 }
1316
1317 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fieldID, jchar val) {
1318 ScopedJniThreadState ts(env);
1319 UNIMPLEMENTED(FATAL);
1320 }
1321
1322 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fieldID, jshort val) {
1323 ScopedJniThreadState ts(env);
1324 UNIMPLEMENTED(FATAL);
1325 }
1326
1327 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fieldID, jint val) {
1328 ScopedJniThreadState ts(env);
1329 UNIMPLEMENTED(FATAL);
1330 }
1331
1332 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fieldID, jlong val) {
1333 ScopedJniThreadState ts(env);
1334 UNIMPLEMENTED(FATAL);
1335 }
1336
1337 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fieldID, jfloat val) {
1338 ScopedJniThreadState ts(env);
1339 UNIMPLEMENTED(FATAL);
1340 }
1341
1342 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fieldID, jdouble val) {
1343 ScopedJniThreadState ts(env);
1344 UNIMPLEMENTED(FATAL);
1345 }
1346
1347 static jobject CallStaticObjectMethod(JNIEnv* env,
1348 jclass clazz, jmethodID methodID, ...) {
1349 ScopedJniThreadState ts(env);
1350 va_list ap;
1351 va_start(ap, methodID);
1352 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1353 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1354 va_end(ap);
1355 return local_result;
1356 }
1357
1358 static jobject CallStaticObjectMethodV(JNIEnv* env,
1359 jclass clazz, jmethodID methodID, va_list args) {
1360 ScopedJniThreadState ts(env);
1361 JValue result = InvokeWithVarArgs(ts, NULL, methodID, args);
1362 return AddLocalReference<jobject>(ts, result.l);
1363 }
1364
1365 static jobject CallStaticObjectMethodA(JNIEnv* env,
1366 jclass clazz, jmethodID methodID, jvalue* args) {
1367 ScopedJniThreadState ts(env);
1368 JValue result = InvokeWithJValues(ts, NULL, methodID, args);
1369 return AddLocalReference<jobject>(ts, result.l);
1370 }
1371
1372 static jboolean CallStaticBooleanMethod(JNIEnv* env,
1373 jclass clazz, jmethodID methodID, ...) {
1374 ScopedJniThreadState ts(env);
1375 va_list ap;
1376 va_start(ap, methodID);
1377 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1378 va_end(ap);
1379 return result.z;
1380 }
1381
1382 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
1383 jclass clazz, jmethodID methodID, va_list args) {
1384 ScopedJniThreadState ts(env);
1385 return InvokeWithVarArgs(ts, NULL, methodID, args).z;
1386 }
1387
1388 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
1389 jclass clazz, jmethodID methodID, jvalue* args) {
1390 ScopedJniThreadState ts(env);
1391 return InvokeWithJValues(ts, NULL, methodID, args).z;
1392 }
1393
1394 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1395 ScopedJniThreadState ts(env);
1396 va_list ap;
1397 va_start(ap, methodID);
1398 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1399 va_end(ap);
1400 return result.b;
1401 }
1402
1403 static jbyte CallStaticByteMethodV(JNIEnv* env,
1404 jclass clazz, jmethodID methodID, va_list args) {
1405 ScopedJniThreadState ts(env);
1406 return InvokeWithVarArgs(ts, NULL, methodID, args).b;
1407 }
1408
1409 static jbyte CallStaticByteMethodA(JNIEnv* env,
1410 jclass clazz, jmethodID methodID, jvalue* args) {
1411 ScopedJniThreadState ts(env);
1412 return InvokeWithJValues(ts, NULL, methodID, args).b;
1413 }
1414
1415 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1416 ScopedJniThreadState ts(env);
1417 va_list ap;
1418 va_start(ap, methodID);
1419 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1420 va_end(ap);
1421 return result.c;
1422 }
1423
1424 static jchar CallStaticCharMethodV(JNIEnv* env,
1425 jclass clazz, jmethodID methodID, va_list args) {
1426 ScopedJniThreadState ts(env);
1427 return InvokeWithVarArgs(ts, NULL, methodID, args).c;
1428 }
1429
1430 static jchar CallStaticCharMethodA(JNIEnv* env,
1431 jclass clazz, jmethodID methodID, jvalue* args) {
1432 ScopedJniThreadState ts(env);
1433 return InvokeWithJValues(ts, NULL, methodID, args).c;
1434 }
1435
1436 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1437 ScopedJniThreadState ts(env);
1438 va_list ap;
1439 va_start(ap, methodID);
1440 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1441 va_end(ap);
1442 return result.s;
1443 }
1444
1445 static jshort CallStaticShortMethodV(JNIEnv* env,
1446 jclass clazz, jmethodID methodID, va_list args) {
1447 ScopedJniThreadState ts(env);
1448 return InvokeWithVarArgs(ts, NULL, methodID, args).s;
1449 }
1450
1451 static jshort CallStaticShortMethodA(JNIEnv* env,
1452 jclass clazz, jmethodID methodID, jvalue* args) {
1453 ScopedJniThreadState ts(env);
1454 return InvokeWithJValues(ts, NULL, methodID, args).s;
1455 }
1456
1457 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1458 ScopedJniThreadState ts(env);
1459 va_list ap;
1460 va_start(ap, methodID);
1461 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1462 va_end(ap);
1463 return result.i;
1464 }
1465
1466 static jint CallStaticIntMethodV(JNIEnv* env,
1467 jclass clazz, jmethodID methodID, va_list args) {
1468 ScopedJniThreadState ts(env);
1469 return InvokeWithVarArgs(ts, NULL, methodID, args).i;
1470 }
1471
1472 static jint CallStaticIntMethodA(JNIEnv* env,
1473 jclass clazz, jmethodID methodID, jvalue* args) {
1474 ScopedJniThreadState ts(env);
1475 return InvokeWithJValues(ts, NULL, methodID, args).i;
1476 }
1477
1478 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1479 ScopedJniThreadState ts(env);
1480 va_list ap;
1481 va_start(ap, methodID);
1482 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1483 va_end(ap);
1484 return result.j;
1485 }
1486
1487 static jlong CallStaticLongMethodV(JNIEnv* env,
1488 jclass clazz, jmethodID methodID, va_list args) {
1489 ScopedJniThreadState ts(env);
1490 return InvokeWithVarArgs(ts, NULL, methodID, args).j;
1491 }
1492
1493 static jlong CallStaticLongMethodA(JNIEnv* env,
1494 jclass clazz, jmethodID methodID, jvalue* args) {
1495 ScopedJniThreadState ts(env);
1496 return InvokeWithJValues(ts, NULL, methodID, args).j;
1497 }
1498
1499 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1500 ScopedJniThreadState ts(env);
1501 va_list ap;
1502 va_start(ap, methodID);
1503 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1504 va_end(ap);
1505 return result.f;
1506 }
1507
1508 static jfloat CallStaticFloatMethodV(JNIEnv* env,
1509 jclass clazz, jmethodID methodID, va_list args) {
1510 ScopedJniThreadState ts(env);
1511 return InvokeWithVarArgs(ts, NULL, methodID, args).f;
1512 }
1513
1514 static jfloat CallStaticFloatMethodA(JNIEnv* env,
1515 jclass clazz, jmethodID methodID, jvalue* args) {
1516 ScopedJniThreadState ts(env);
1517 return InvokeWithJValues(ts, NULL, methodID, args).f;
1518 }
1519
1520 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1521 ScopedJniThreadState ts(env);
1522 va_list ap;
1523 va_start(ap, methodID);
1524 JValue result = InvokeWithVarArgs(ts, NULL, methodID, ap);
1525 va_end(ap);
1526 return result.d;
1527 }
1528
1529 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
1530 jclass clazz, jmethodID methodID, va_list args) {
1531 ScopedJniThreadState ts(env);
1532 return InvokeWithVarArgs(ts, NULL, methodID, args).d;
1533 }
1534
1535 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
1536 jclass clazz, jmethodID methodID, jvalue* args) {
1537 ScopedJniThreadState ts(env);
1538 return InvokeWithJValues(ts, NULL, methodID, args).d;
1539 }
1540
1541 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID methodID, ...) {
1542 ScopedJniThreadState ts(env);
1543 va_list ap;
1544 va_start(ap, methodID);
1545 InvokeWithVarArgs(ts, NULL, methodID, ap);
1546 va_end(ap);
1547 }
1548
1549 static void CallStaticVoidMethodV(JNIEnv* env,
1550 jclass cls, jmethodID methodID, va_list args) {
1551 ScopedJniThreadState ts(env);
1552 InvokeWithVarArgs(ts, NULL, methodID, args);
1553 }
1554
1555 static void CallStaticVoidMethodA(JNIEnv* env,
1556 jclass cls, jmethodID methodID, jvalue* args) {
1557 ScopedJniThreadState ts(env);
1558 InvokeWithJValues(ts, NULL, methodID, args);
1559 }
1560
1561 static jobject GetStaticObjectField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1562 ScopedJniThreadState ts(env);
1563 UNIMPLEMENTED(FATAL);
1564 return NULL;
1565 }
1566
1567 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1568 ScopedJniThreadState ts(env);
1569 UNIMPLEMENTED(FATAL);
1570 return JNI_FALSE;
1571 }
1572
1573 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1574 ScopedJniThreadState ts(env);
1575 UNIMPLEMENTED(FATAL);
1576 return 0;
1577 }
1578
1579 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1580 ScopedJniThreadState ts(env);
1581 UNIMPLEMENTED(FATAL);
1582 return 0;
1583 }
1584
1585 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1586 ScopedJniThreadState ts(env);
1587 UNIMPLEMENTED(FATAL);
1588 return 0;
1589 }
1590
1591 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1592 ScopedJniThreadState ts(env);
1593 UNIMPLEMENTED(FATAL);
1594 return 0;
1595 }
1596
1597 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1598 ScopedJniThreadState ts(env);
1599 UNIMPLEMENTED(FATAL);
1600 return 0;
1601 }
1602
1603 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1604 ScopedJniThreadState ts(env);
1605 UNIMPLEMENTED(FATAL);
1606 return 0;
1607 }
1608
1609 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fieldID) {
1610 ScopedJniThreadState ts(env);
1611 UNIMPLEMENTED(FATAL);
1612 return 0;
1613 }
1614
1615 static void SetStaticObjectField(JNIEnv* env,
1616 jclass clazz, jfieldID fieldID, jobject value) {
1617 ScopedJniThreadState ts(env);
1618 UNIMPLEMENTED(FATAL);
1619 }
1620
1621 static void SetStaticBooleanField(JNIEnv* env,
1622 jclass clazz, jfieldID fieldID, jboolean value) {
1623 ScopedJniThreadState ts(env);
1624 UNIMPLEMENTED(FATAL);
1625 }
1626
1627 static void SetStaticByteField(JNIEnv* env,
1628 jclass clazz, jfieldID fieldID, jbyte value) {
1629 ScopedJniThreadState ts(env);
1630 UNIMPLEMENTED(FATAL);
1631 }
1632
1633 static void SetStaticCharField(JNIEnv* env,
1634 jclass clazz, jfieldID fieldID, jchar value) {
1635 ScopedJniThreadState ts(env);
1636 UNIMPLEMENTED(FATAL);
1637 }
1638
1639 static void SetStaticShortField(JNIEnv* env,
1640 jclass clazz, jfieldID fieldID, jshort value) {
1641 ScopedJniThreadState ts(env);
1642 UNIMPLEMENTED(FATAL);
1643 }
1644
1645 static void SetStaticIntField(JNIEnv* env,
1646 jclass clazz, jfieldID fieldID, jint value) {
1647 ScopedJniThreadState ts(env);
1648 UNIMPLEMENTED(FATAL);
1649 }
1650
1651 static void SetStaticLongField(JNIEnv* env,
1652 jclass clazz, jfieldID fieldID, jlong value) {
1653 ScopedJniThreadState ts(env);
1654 UNIMPLEMENTED(FATAL);
1655 }
1656
1657 static void SetStaticFloatField(JNIEnv* env,
1658 jclass clazz, jfieldID fieldID, jfloat value) {
1659 ScopedJniThreadState ts(env);
1660 UNIMPLEMENTED(FATAL);
1661 }
1662
1663 static void SetStaticDoubleField(JNIEnv* env,
1664 jclass clazz, jfieldID fieldID, jdouble value) {
1665 ScopedJniThreadState ts(env);
1666 UNIMPLEMENTED(FATAL);
1667 }
1668
1669 static jstring NewString(JNIEnv* env, const jchar* unicode, jsize len) {
1670 ScopedJniThreadState ts(env);
1671 UNIMPLEMENTED(FATAL);
1672 return NULL;
1673 }
1674
1675 static jsize GetStringLength(JNIEnv* env, jstring str) {
1676 ScopedJniThreadState ts(env);
1677 UNIMPLEMENTED(FATAL);
1678 return 0;
1679 }
1680
1681 static const jchar* GetStringChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1682 ScopedJniThreadState ts(env);
1683 UNIMPLEMENTED(FATAL);
1684 return NULL;
1685 }
1686
1687 static void ReleaseStringChars(JNIEnv* env, jstring str, const jchar* chars) {
1688 ScopedJniThreadState ts(env);
1689 UNIMPLEMENTED(FATAL);
1690 }
1691
1692 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1693 ScopedJniThreadState ts(env);
1694 if (utf == NULL) {
1695 return NULL;
1696 }
1697 String* result = String::AllocFromModifiedUtf8(utf);
1698 return AddLocalReference<jstring>(ts, result);
1699 }
1700
1701 static jsize GetStringUTFLength(JNIEnv* env, jstring str) {
1702 ScopedJniThreadState ts(env);
1703 UNIMPLEMENTED(FATAL);
1704 return 0;
1705 }
1706
1707 static const char* GetStringUTFChars(JNIEnv* env, jstring str, jboolean* isCopy) {
1708 ScopedJniThreadState ts(env);
1709 UNIMPLEMENTED(FATAL);
1710 return NULL;
1711 }
1712
1713 static void ReleaseStringUTFChars(JNIEnv* env, jstring str, const char* chars) {
1714 ScopedJniThreadState ts(env);
1715 UNIMPLEMENTED(FATAL);
1716 }
1717
Elliott Hughesbd935992011-08-22 11:59:34 -07001718 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001720 Object* obj = Decode<Object*>(ts, java_array);
1721 CHECK(obj->IsArray()); // TODO: ReportJniError
1722 Array* array = obj->AsArray();
1723 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
1726 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1727 ScopedJniThreadState ts(env);
1728 UNIMPLEMENTED(FATAL);
1729 return NULL;
1730 }
1731
1732 static void SetObjectArrayElement(JNIEnv* env,
1733 jobjectArray java_array, jsize index, jobject java_value) {
1734 ScopedJniThreadState ts(env);
1735 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1736 Object* value = Decode<Object*>(ts, java_value);
1737 array->Set(index, value);
1738 }
1739
1740 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1741 ScopedJniThreadState ts(env);
1742 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1743 }
1744
1745 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1746 ScopedJniThreadState ts(env);
1747 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1748 }
1749
1750 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1751 ScopedJniThreadState ts(env);
1752 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1753 }
1754
1755 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1756 ScopedJniThreadState ts(env);
1757 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1758 }
1759
1760 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1761 ScopedJniThreadState ts(env);
1762 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1763 }
1764
1765 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1766 ScopedJniThreadState ts(env);
1767 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1768 }
1769
1770 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1771 ScopedJniThreadState ts(env);
1772 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1773 }
1774
1775 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1776 ScopedJniThreadState ts(env);
1777 CHECK_GE(length, 0); // TODO: ReportJniError
1778
1779 // Compute the array class corresponding to the given element class.
1780 Class* element_class = Decode<Class*>(ts, element_jclass);
1781 std::string descriptor;
1782 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001783 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784
1785 // Find the class.
1786 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1787 // TODO: need to get the appropriate ClassLoader.
1788 Class* array_class = class_linker->FindClass(descriptor, NULL);
1789 if (array_class == NULL) {
1790 return NULL;
1791 }
1792
1793 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1794 CHECK(initial_element == NULL); // TODO: support initial_element
1795 return AddLocalReference<jobjectArray>(ts, result);
1796 }
1797
1798 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1799 ScopedJniThreadState ts(env);
1800 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1801 }
1802
1803 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1804 jbooleanArray array, jboolean* isCopy) {
1805 ScopedJniThreadState ts(env);
1806 UNIMPLEMENTED(FATAL);
1807 return NULL;
1808 }
1809
1810 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1811 ScopedJniThreadState ts(env);
1812 UNIMPLEMENTED(FATAL);
1813 return NULL;
1814 }
1815
1816 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1817 ScopedJniThreadState ts(env);
1818 UNIMPLEMENTED(FATAL);
1819 return NULL;
1820 }
1821
1822 static jshort* GetShortArrayElements(JNIEnv* env,
1823 jshortArray array, jboolean* isCopy) {
1824 ScopedJniThreadState ts(env);
1825 UNIMPLEMENTED(FATAL);
1826 return NULL;
1827 }
1828
1829 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1830 ScopedJniThreadState ts(env);
1831 UNIMPLEMENTED(FATAL);
1832 return NULL;
1833 }
1834
1835 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1836 ScopedJniThreadState ts(env);
1837 UNIMPLEMENTED(FATAL);
1838 return NULL;
1839 }
1840
1841 static jfloat* GetFloatArrayElements(JNIEnv* env,
1842 jfloatArray array, jboolean* isCopy) {
1843 ScopedJniThreadState ts(env);
1844 UNIMPLEMENTED(FATAL);
1845 return NULL;
1846 }
1847
1848 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1849 jdoubleArray array, jboolean* isCopy) {
1850 ScopedJniThreadState ts(env);
1851 UNIMPLEMENTED(FATAL);
1852 return NULL;
1853 }
1854
1855 static void ReleaseBooleanArrayElements(JNIEnv* env,
1856 jbooleanArray array, jboolean* elems, jint mode) {
1857 ScopedJniThreadState ts(env);
1858 UNIMPLEMENTED(FATAL);
1859 }
1860
1861 static void ReleaseByteArrayElements(JNIEnv* env,
1862 jbyteArray array, jbyte* elems, jint mode) {
1863 ScopedJniThreadState ts(env);
1864 UNIMPLEMENTED(FATAL);
1865 }
1866
1867 static void ReleaseCharArrayElements(JNIEnv* env,
1868 jcharArray array, jchar* elems, jint mode) {
1869 ScopedJniThreadState ts(env);
1870 UNIMPLEMENTED(FATAL);
1871 }
1872
1873 static void ReleaseShortArrayElements(JNIEnv* env,
1874 jshortArray array, jshort* elems, jint mode) {
1875 ScopedJniThreadState ts(env);
1876 UNIMPLEMENTED(FATAL);
1877 }
1878
1879 static void ReleaseIntArrayElements(JNIEnv* env,
1880 jintArray array, jint* elems, jint mode) {
1881 ScopedJniThreadState ts(env);
1882 UNIMPLEMENTED(FATAL);
1883 }
1884
1885 static void ReleaseLongArrayElements(JNIEnv* env,
1886 jlongArray array, jlong* elems, jint mode) {
1887 ScopedJniThreadState ts(env);
1888 UNIMPLEMENTED(FATAL);
1889 }
1890
1891 static void ReleaseFloatArrayElements(JNIEnv* env,
1892 jfloatArray array, jfloat* elems, jint mode) {
1893 ScopedJniThreadState ts(env);
1894 UNIMPLEMENTED(FATAL);
1895 }
1896
1897 static void ReleaseDoubleArrayElements(JNIEnv* env,
1898 jdoubleArray array, jdouble* elems, jint mode) {
1899 ScopedJniThreadState ts(env);
1900 UNIMPLEMENTED(FATAL);
1901 }
1902
1903 static void GetBooleanArrayRegion(JNIEnv* env,
1904 jbooleanArray array, jsize start, jsize l, jboolean* buf) {
1905 ScopedJniThreadState ts(env);
1906 UNIMPLEMENTED(FATAL);
1907 }
1908
1909 static void GetByteArrayRegion(JNIEnv* env,
1910 jbyteArray array, jsize start, jsize len, jbyte* buf) {
1911 ScopedJniThreadState ts(env);
1912 UNIMPLEMENTED(FATAL);
1913 }
1914
1915 static void GetCharArrayRegion(JNIEnv* env,
1916 jcharArray array, jsize start, jsize len, jchar* buf) {
1917 ScopedJniThreadState ts(env);
1918 UNIMPLEMENTED(FATAL);
1919 }
1920
1921 static void GetShortArrayRegion(JNIEnv* env,
1922 jshortArray array, jsize start, jsize len, jshort* buf) {
1923 ScopedJniThreadState ts(env);
1924 UNIMPLEMENTED(FATAL);
1925 }
1926
1927 static void GetIntArrayRegion(JNIEnv* env,
1928 jintArray array, jsize start, jsize len, jint* buf) {
1929 ScopedJniThreadState ts(env);
1930 UNIMPLEMENTED(FATAL);
1931 }
1932
1933 static void GetLongArrayRegion(JNIEnv* env,
1934 jlongArray array, jsize start, jsize len, jlong* buf) {
1935 ScopedJniThreadState ts(env);
1936 UNIMPLEMENTED(FATAL);
1937 }
1938
1939 static void GetFloatArrayRegion(JNIEnv* env,
1940 jfloatArray array, jsize start, jsize len, jfloat* buf) {
1941 ScopedJniThreadState ts(env);
1942 UNIMPLEMENTED(FATAL);
1943 }
1944
1945 static void GetDoubleArrayRegion(JNIEnv* env,
1946 jdoubleArray array, jsize start, jsize len, jdouble* buf) {
1947 ScopedJniThreadState ts(env);
1948 UNIMPLEMENTED(FATAL);
1949 }
1950
1951 static void SetBooleanArrayRegion(JNIEnv* env,
1952 jbooleanArray array, jsize start, jsize l, const jboolean* buf) {
1953 ScopedJniThreadState ts(env);
1954 UNIMPLEMENTED(FATAL);
1955 }
1956
1957 static void SetByteArrayRegion(JNIEnv* env,
1958 jbyteArray array, jsize start, jsize len, const jbyte* buf) {
1959 ScopedJniThreadState ts(env);
1960 UNIMPLEMENTED(FATAL);
1961 }
1962
1963 static void SetCharArrayRegion(JNIEnv* env,
1964 jcharArray array, jsize start, jsize len, const jchar* buf) {
1965 ScopedJniThreadState ts(env);
1966 UNIMPLEMENTED(FATAL);
1967 }
1968
1969 static void SetShortArrayRegion(JNIEnv* env,
1970 jshortArray array, jsize start, jsize len, const jshort* buf) {
1971 ScopedJniThreadState ts(env);
1972 UNIMPLEMENTED(FATAL);
1973 }
1974
1975 static void SetIntArrayRegion(JNIEnv* env,
1976 jintArray array, jsize start, jsize len, const jint* buf) {
1977 ScopedJniThreadState ts(env);
1978 UNIMPLEMENTED(FATAL);
1979 }
1980
1981 static void SetLongArrayRegion(JNIEnv* env,
1982 jlongArray array, jsize start, jsize len, const jlong* buf) {
1983 ScopedJniThreadState ts(env);
1984 UNIMPLEMENTED(FATAL);
1985 }
1986
1987 static void SetFloatArrayRegion(JNIEnv* env,
1988 jfloatArray array, jsize start, jsize len, const jfloat* buf) {
1989 ScopedJniThreadState ts(env);
1990 UNIMPLEMENTED(FATAL);
1991 }
1992
1993 static void SetDoubleArrayRegion(JNIEnv* env,
1994 jdoubleArray array, jsize start, jsize len, const jdouble* buf) {
1995 ScopedJniThreadState ts(env);
1996 UNIMPLEMENTED(FATAL);
1997 }
1998
1999 static jint RegisterNatives(JNIEnv* env,
2000 jclass clazz, const JNINativeMethod* methods, jint nMethods) {
2001 ScopedJniThreadState ts(env);
2002 Class* klass = Decode<Class*>(ts, clazz);
2003 for(int i = 0; i < nMethods; i++) {
2004 const char* name = methods[i].name;
2005 const char* sig = methods[i].signature;
2006
2007 if (*sig == '!') {
2008 // TODO: fast jni. it's too noisy to log all these.
2009 ++sig;
2010 }
2011
2012 Method* method = klass->FindDirectMethod(name, sig);
2013 if (method == NULL) {
2014 method = klass->FindVirtualMethod(name, sig);
2015 }
2016 if (method == NULL) {
2017 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002018 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2020 "no method \"%s.%s%s\"",
2021 class_name.c_str(), name, sig);
2022 return JNI_ERR;
2023 } else if (!method->IsNative()) {
2024 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002025 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2027 "method \"%s.%s%s\" is not native",
2028 class_name.c_str(), name, sig);
2029 return JNI_ERR;
2030 }
2031 method->RegisterNative(methods[i].fnPtr);
2032 }
2033 return JNI_OK;
2034 }
2035
2036 static jint UnregisterNatives(JNIEnv* env, jclass clazz) {
2037 ScopedJniThreadState ts(env);
2038 UNIMPLEMENTED(FATAL);
2039 return 0;
2040 }
2041
2042 static jint MonitorEnter(JNIEnv* env, jobject obj) {
2043 ScopedJniThreadState ts(env);
2044 UNIMPLEMENTED(WARNING);
2045 return 0;
2046 }
2047
2048 static jint MonitorExit(JNIEnv* env, jobject obj) {
2049 ScopedJniThreadState ts(env);
2050 UNIMPLEMENTED(WARNING);
2051 return 0;
2052 }
2053
2054 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2055 ScopedJniThreadState ts(env);
2056 Runtime* runtime = Runtime::Current();
2057 if (runtime != NULL) {
2058 *vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
2059 } else {
2060 *vm = NULL;
2061 }
2062 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2063 }
2064
2065 static void GetStringRegion(JNIEnv* env,
2066 jstring str, jsize start, jsize len, jchar* buf) {
2067 ScopedJniThreadState ts(env);
2068 UNIMPLEMENTED(FATAL);
2069 }
2070
2071 static void GetStringUTFRegion(JNIEnv* env,
2072 jstring str, jsize start, jsize len, char* buf) {
2073 ScopedJniThreadState ts(env);
2074 UNIMPLEMENTED(FATAL);
2075 }
2076
2077 static void* GetPrimitiveArrayCritical(JNIEnv* env,
2078 jarray array, jboolean* isCopy) {
2079 ScopedJniThreadState ts(env);
2080 UNIMPLEMENTED(FATAL);
2081 return NULL;
2082 }
2083
2084 static void ReleasePrimitiveArrayCritical(JNIEnv* env,
2085 jarray array, void* carray, jint mode) {
2086 ScopedJniThreadState ts(env);
2087 UNIMPLEMENTED(FATAL);
2088 }
2089
2090 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
2091 ScopedJniThreadState ts(env);
2092 UNIMPLEMENTED(FATAL);
2093 return NULL;
2094 }
2095
2096 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
2097 ScopedJniThreadState ts(env);
2098 UNIMPLEMENTED(FATAL);
2099 }
2100
2101 static jboolean ExceptionCheck(JNIEnv* env) {
2102 ScopedJniThreadState ts(env);
2103 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
2104 }
2105
2106 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2107 ScopedJniThreadState ts(env);
2108 UNIMPLEMENTED(FATAL);
2109 return NULL;
2110 }
2111
2112 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
2113 ScopedJniThreadState ts(env);
2114 UNIMPLEMENTED(FATAL);
2115 return NULL;
2116 }
2117
2118 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
2119 ScopedJniThreadState ts(env);
2120 UNIMPLEMENTED(FATAL);
2121 return 0;
2122 }
2123
2124 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2125 ScopedJniThreadState ts(env);
2126 UNIMPLEMENTED(FATAL);
2127 return JNIInvalidRefType;
2128 }
2129};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002130
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002131static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002132 NULL, // reserved0.
2133 NULL, // reserved1.
2134 NULL, // reserved2.
2135 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 JNI::GetVersion,
2137 JNI::DefineClass,
2138 JNI::FindClass,
2139 JNI::FromReflectedMethod,
2140 JNI::FromReflectedField,
2141 JNI::ToReflectedMethod,
2142 JNI::GetSuperclass,
2143 JNI::IsAssignableFrom,
2144 JNI::ToReflectedField,
2145 JNI::Throw,
2146 JNI::ThrowNew,
2147 JNI::ExceptionOccurred,
2148 JNI::ExceptionDescribe,
2149 JNI::ExceptionClear,
2150 JNI::FatalError,
2151 JNI::PushLocalFrame,
2152 JNI::PopLocalFrame,
2153 JNI::NewGlobalRef,
2154 JNI::DeleteGlobalRef,
2155 JNI::DeleteLocalRef,
2156 JNI::IsSameObject,
2157 JNI::NewLocalRef,
2158 JNI::EnsureLocalCapacity,
2159 JNI::AllocObject,
2160 JNI::NewObject,
2161 JNI::NewObjectV,
2162 JNI::NewObjectA,
2163 JNI::GetObjectClass,
2164 JNI::IsInstanceOf,
2165 JNI::GetMethodID,
2166 JNI::CallObjectMethod,
2167 JNI::CallObjectMethodV,
2168 JNI::CallObjectMethodA,
2169 JNI::CallBooleanMethod,
2170 JNI::CallBooleanMethodV,
2171 JNI::CallBooleanMethodA,
2172 JNI::CallByteMethod,
2173 JNI::CallByteMethodV,
2174 JNI::CallByteMethodA,
2175 JNI::CallCharMethod,
2176 JNI::CallCharMethodV,
2177 JNI::CallCharMethodA,
2178 JNI::CallShortMethod,
2179 JNI::CallShortMethodV,
2180 JNI::CallShortMethodA,
2181 JNI::CallIntMethod,
2182 JNI::CallIntMethodV,
2183 JNI::CallIntMethodA,
2184 JNI::CallLongMethod,
2185 JNI::CallLongMethodV,
2186 JNI::CallLongMethodA,
2187 JNI::CallFloatMethod,
2188 JNI::CallFloatMethodV,
2189 JNI::CallFloatMethodA,
2190 JNI::CallDoubleMethod,
2191 JNI::CallDoubleMethodV,
2192 JNI::CallDoubleMethodA,
2193 JNI::CallVoidMethod,
2194 JNI::CallVoidMethodV,
2195 JNI::CallVoidMethodA,
2196 JNI::CallNonvirtualObjectMethod,
2197 JNI::CallNonvirtualObjectMethodV,
2198 JNI::CallNonvirtualObjectMethodA,
2199 JNI::CallNonvirtualBooleanMethod,
2200 JNI::CallNonvirtualBooleanMethodV,
2201 JNI::CallNonvirtualBooleanMethodA,
2202 JNI::CallNonvirtualByteMethod,
2203 JNI::CallNonvirtualByteMethodV,
2204 JNI::CallNonvirtualByteMethodA,
2205 JNI::CallNonvirtualCharMethod,
2206 JNI::CallNonvirtualCharMethodV,
2207 JNI::CallNonvirtualCharMethodA,
2208 JNI::CallNonvirtualShortMethod,
2209 JNI::CallNonvirtualShortMethodV,
2210 JNI::CallNonvirtualShortMethodA,
2211 JNI::CallNonvirtualIntMethod,
2212 JNI::CallNonvirtualIntMethodV,
2213 JNI::CallNonvirtualIntMethodA,
2214 JNI::CallNonvirtualLongMethod,
2215 JNI::CallNonvirtualLongMethodV,
2216 JNI::CallNonvirtualLongMethodA,
2217 JNI::CallNonvirtualFloatMethod,
2218 JNI::CallNonvirtualFloatMethodV,
2219 JNI::CallNonvirtualFloatMethodA,
2220 JNI::CallNonvirtualDoubleMethod,
2221 JNI::CallNonvirtualDoubleMethodV,
2222 JNI::CallNonvirtualDoubleMethodA,
2223 JNI::CallNonvirtualVoidMethod,
2224 JNI::CallNonvirtualVoidMethodV,
2225 JNI::CallNonvirtualVoidMethodA,
2226 JNI::GetFieldID,
2227 JNI::GetObjectField,
2228 JNI::GetBooleanField,
2229 JNI::GetByteField,
2230 JNI::GetCharField,
2231 JNI::GetShortField,
2232 JNI::GetIntField,
2233 JNI::GetLongField,
2234 JNI::GetFloatField,
2235 JNI::GetDoubleField,
2236 JNI::SetObjectField,
2237 JNI::SetBooleanField,
2238 JNI::SetByteField,
2239 JNI::SetCharField,
2240 JNI::SetShortField,
2241 JNI::SetIntField,
2242 JNI::SetLongField,
2243 JNI::SetFloatField,
2244 JNI::SetDoubleField,
2245 JNI::GetStaticMethodID,
2246 JNI::CallStaticObjectMethod,
2247 JNI::CallStaticObjectMethodV,
2248 JNI::CallStaticObjectMethodA,
2249 JNI::CallStaticBooleanMethod,
2250 JNI::CallStaticBooleanMethodV,
2251 JNI::CallStaticBooleanMethodA,
2252 JNI::CallStaticByteMethod,
2253 JNI::CallStaticByteMethodV,
2254 JNI::CallStaticByteMethodA,
2255 JNI::CallStaticCharMethod,
2256 JNI::CallStaticCharMethodV,
2257 JNI::CallStaticCharMethodA,
2258 JNI::CallStaticShortMethod,
2259 JNI::CallStaticShortMethodV,
2260 JNI::CallStaticShortMethodA,
2261 JNI::CallStaticIntMethod,
2262 JNI::CallStaticIntMethodV,
2263 JNI::CallStaticIntMethodA,
2264 JNI::CallStaticLongMethod,
2265 JNI::CallStaticLongMethodV,
2266 JNI::CallStaticLongMethodA,
2267 JNI::CallStaticFloatMethod,
2268 JNI::CallStaticFloatMethodV,
2269 JNI::CallStaticFloatMethodA,
2270 JNI::CallStaticDoubleMethod,
2271 JNI::CallStaticDoubleMethodV,
2272 JNI::CallStaticDoubleMethodA,
2273 JNI::CallStaticVoidMethod,
2274 JNI::CallStaticVoidMethodV,
2275 JNI::CallStaticVoidMethodA,
2276 JNI::GetStaticFieldID,
2277 JNI::GetStaticObjectField,
2278 JNI::GetStaticBooleanField,
2279 JNI::GetStaticByteField,
2280 JNI::GetStaticCharField,
2281 JNI::GetStaticShortField,
2282 JNI::GetStaticIntField,
2283 JNI::GetStaticLongField,
2284 JNI::GetStaticFloatField,
2285 JNI::GetStaticDoubleField,
2286 JNI::SetStaticObjectField,
2287 JNI::SetStaticBooleanField,
2288 JNI::SetStaticByteField,
2289 JNI::SetStaticCharField,
2290 JNI::SetStaticShortField,
2291 JNI::SetStaticIntField,
2292 JNI::SetStaticLongField,
2293 JNI::SetStaticFloatField,
2294 JNI::SetStaticDoubleField,
2295 JNI::NewString,
2296 JNI::GetStringLength,
2297 JNI::GetStringChars,
2298 JNI::ReleaseStringChars,
2299 JNI::NewStringUTF,
2300 JNI::GetStringUTFLength,
2301 JNI::GetStringUTFChars,
2302 JNI::ReleaseStringUTFChars,
2303 JNI::GetArrayLength,
2304 JNI::NewObjectArray,
2305 JNI::GetObjectArrayElement,
2306 JNI::SetObjectArrayElement,
2307 JNI::NewBooleanArray,
2308 JNI::NewByteArray,
2309 JNI::NewCharArray,
2310 JNI::NewShortArray,
2311 JNI::NewIntArray,
2312 JNI::NewLongArray,
2313 JNI::NewFloatArray,
2314 JNI::NewDoubleArray,
2315 JNI::GetBooleanArrayElements,
2316 JNI::GetByteArrayElements,
2317 JNI::GetCharArrayElements,
2318 JNI::GetShortArrayElements,
2319 JNI::GetIntArrayElements,
2320 JNI::GetLongArrayElements,
2321 JNI::GetFloatArrayElements,
2322 JNI::GetDoubleArrayElements,
2323 JNI::ReleaseBooleanArrayElements,
2324 JNI::ReleaseByteArrayElements,
2325 JNI::ReleaseCharArrayElements,
2326 JNI::ReleaseShortArrayElements,
2327 JNI::ReleaseIntArrayElements,
2328 JNI::ReleaseLongArrayElements,
2329 JNI::ReleaseFloatArrayElements,
2330 JNI::ReleaseDoubleArrayElements,
2331 JNI::GetBooleanArrayRegion,
2332 JNI::GetByteArrayRegion,
2333 JNI::GetCharArrayRegion,
2334 JNI::GetShortArrayRegion,
2335 JNI::GetIntArrayRegion,
2336 JNI::GetLongArrayRegion,
2337 JNI::GetFloatArrayRegion,
2338 JNI::GetDoubleArrayRegion,
2339 JNI::SetBooleanArrayRegion,
2340 JNI::SetByteArrayRegion,
2341 JNI::SetCharArrayRegion,
2342 JNI::SetShortArrayRegion,
2343 JNI::SetIntArrayRegion,
2344 JNI::SetLongArrayRegion,
2345 JNI::SetFloatArrayRegion,
2346 JNI::SetDoubleArrayRegion,
2347 JNI::RegisterNatives,
2348 JNI::UnregisterNatives,
2349 JNI::MonitorEnter,
2350 JNI::MonitorExit,
2351 JNI::GetJavaVM,
2352 JNI::GetStringRegion,
2353 JNI::GetStringUTFRegion,
2354 JNI::GetPrimitiveArrayCritical,
2355 JNI::ReleasePrimitiveArrayCritical,
2356 JNI::GetStringCritical,
2357 JNI::ReleaseStringCritical,
2358 JNI::NewWeakGlobalRef,
2359 JNI::DeleteWeakGlobalRef,
2360 JNI::ExceptionCheck,
2361 JNI::NewDirectByteBuffer,
2362 JNI::GetDirectBufferAddress,
2363 JNI::GetDirectBufferCapacity,
2364 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002365};
2366
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002367static const size_t kMonitorsInitial = 32; // Arbitrary.
2368static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2369
2370static const size_t kLocalsInitial = 64; // Arbitrary.
2371static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002372
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002373JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002374 : fns(&gNativeInterface),
2375 self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002376 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002377 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002378 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2379 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002380}
2381
Carl Shapiroea4dca82011-08-01 13:45:38 -07002382// JNI Invocation interface.
2383
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002384extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2385 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2386 if (args->version < JNI_VERSION_1_2) {
2387 return JNI_EVERSION;
2388 }
2389 Runtime::Options options;
2390 for (int i = 0; i < args->nOptions; ++i) {
2391 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002392 options.push_back(std::make_pair(StringPiece(option->optionString),
2393 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002394 }
2395 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002396 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002397 if (runtime == NULL) {
2398 return JNI_ERR;
2399 } else {
2400 *p_env = reinterpret_cast<JNIEnv*>(Thread::Current()->GetJniEnv());
Elliott Hughes0af55432011-08-17 18:37:28 -07002401 *p_vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002402 return JNI_OK;
2403 }
2404}
2405
Elliott Hughesf2682d52011-08-15 16:37:04 -07002406extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002407 Runtime* runtime = Runtime::Current();
2408 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002409 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002410 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002411 *vm_count = 1;
Elliott Hughes0af55432011-08-17 18:37:28 -07002412 vms[0] = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002413 }
2414 return JNI_OK;
2415}
2416
2417// Historically unsupported.
2418extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2419 return JNI_ERR;
2420}
2421
Elliott Hughescdf53122011-08-19 15:46:09 -07002422class JII {
2423 public:
2424 static jint DestroyJavaVM(JavaVM* vm) {
2425 if (vm == NULL) {
2426 return JNI_ERR;
2427 } else {
2428 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2429 delete raw_vm->runtime;
2430 return JNI_OK;
2431 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002432 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002433
Elliott Hughescdf53122011-08-19 15:46:09 -07002434 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2435 if (vm == NULL || p_env == NULL) {
2436 return JNI_ERR;
2437 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002438 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2439 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 const char* name = NULL;
2441 if (thr_args != NULL) {
2442 // TODO: check version
2443 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2444 // TODO: thread group
2445 }
2446 bool success = runtime->AttachCurrentThread(name, p_env);
2447 if (!success) {
2448 return JNI_ERR;
2449 } else {
2450 return JNI_OK;
2451 }
2452 }
2453
2454 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2455 if (vm == NULL || p_env == NULL) {
2456 return JNI_ERR;
2457 }
2458 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2459 Runtime* runtime = raw_vm->runtime;
2460 const char* name = NULL;
2461 if (thr_args != NULL) {
2462 // TODO: check version
2463 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2464 // TODO: thread group
2465 }
2466 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2467 if (!success) {
2468 return JNI_ERR;
2469 } else {
2470 return JNI_OK;
2471 }
2472 }
2473
2474 static jint DetachCurrentThread(JavaVM* vm) {
2475 if (vm == NULL) {
2476 return JNI_ERR;
2477 } else {
2478 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2479 Runtime* runtime = raw_vm->runtime;
2480 runtime->DetachCurrentThread();
2481 return JNI_OK;
2482 }
2483 }
2484
2485 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2486 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2487 return JNI_EVERSION;
2488 }
2489 if (vm == NULL || env == NULL) {
2490 return JNI_ERR;
2491 }
2492 Thread* thread = Thread::Current();
2493 if (thread == NULL) {
2494 *env = NULL;
2495 return JNI_EDETACHED;
2496 }
2497 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002498 return JNI_OK;
2499 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002500};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002501
Elliott Hughesf2682d52011-08-15 16:37:04 -07002502struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002503 NULL, // reserved0
2504 NULL, // reserved1
2505 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002506 JII::DestroyJavaVM,
2507 JII::AttachCurrentThread,
2508 JII::DetachCurrentThread,
2509 JII::GetEnv,
2510 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002511};
2512
Elliott Hughesbbd76712011-08-17 10:25:24 -07002513static const size_t kPinTableInitialSize = 16;
2514static const size_t kPinTableMaxSize = 1024;
2515
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002516static const size_t kGlobalsInitial = 512; // Arbitrary.
2517static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2518
2519static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2520static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2521
Elliott Hughes0af55432011-08-17 18:37:28 -07002522JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002523 : fns(&gInvokeInterface),
2524 runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002525 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002526 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002527 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002528 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002529 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002530 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002531 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002532}
2533
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002534JavaVMExt::~JavaVMExt() {
2535 delete globals_lock;
2536 delete weak_globals_lock;
2537}
2538
Elliott Hughescdf53122011-08-19 15:46:09 -07002539/*
2540 * Load native code from the specified absolute pathname. Per the spec,
2541 * if we've already loaded a library with the specified pathname, we
2542 * return without doing anything.
2543 *
2544 * TODO? for better results we should absolutify the pathname. For fully
2545 * correct results we should stat to get the inode and compare that. The
2546 * existing implementation is fine so long as everybody is using
2547 * System.loadLibrary.
2548 *
2549 * The library will be associated with the specified class loader. The JNI
2550 * spec says we can't load the same library into more than one class loader.
2551 *
2552 * Returns "true" on success. On failure, sets *detail to a
2553 * human-readable description of the error or NULL if no detail is
2554 * available; ownership of the string is transferred to the caller.
2555 */
2556bool JavaVMExt::LoadNativeLibrary(const std::string& path, Object* class_loader, char** detail) {
2557 *detail = NULL;
2558
2559 // See if we've already loaded this library. If we have, and the class loader
2560 // matches, return successfully without doing anything.
2561 SharedLibrary* library = libraries[path];
2562 if (library != NULL) {
2563 if (library->GetClassLoader() != class_loader) {
2564 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2565 << "ClassLoader " << library->GetClassLoader() << "; "
2566 << "can't open in " << class_loader;
2567 *detail = strdup("already opened by different ClassLoader");
2568 return false;
2569 }
2570 if (verbose_jni) {
2571 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2572 << "ClassLoader " << class_loader << "]";
2573 }
2574 if (!library->CheckOnLoadResult(this)) {
2575 *detail = strdup("JNI_OnLoad failed before");
2576 return false;
2577 }
2578 return true;
2579 }
2580
2581 // Open the shared library. Because we're using a full path, the system
2582 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2583 // resolve this library's dependencies though.)
2584
2585 // Failures here are expected when java.library.path has several entries
2586 // and we have to hunt for the lib.
2587
2588 // The current version of the dynamic linker prints detailed information
2589 // about dlopen() failures. Some things to check if the message is
2590 // cryptic:
2591 // - make sure the library exists on the device
2592 // - verify that the right path is being opened (the debug log message
2593 // above can help with that)
2594 // - check to see if the library is valid (e.g. not zero bytes long)
2595 // - check config/prelink-linux-arm.map to ensure that the library
2596 // is listed and is not being overrun by the previous entry (if
2597 // loading suddenly stops working on a prelinked library, this is
2598 // a good one to check)
2599 // - write a trivial app that calls sleep() then dlopen(), attach
2600 // to it with "strace -p <pid>" while it sleeps, and watch for
2601 // attempts to open nonexistent dependent shared libs
2602
2603 // TODO: automate some of these checks!
2604
2605 // This can execute slowly for a large library on a busy system, so we
2606 // want to switch from RUNNING to VMWAIT while it executes. This allows
2607 // the GC to ignore us.
2608 Thread* self = Thread::Current();
2609 Thread::State old_state = self->GetState();
2610 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2611 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2612 self->SetState(old_state);
2613
2614 if (verbose_jni) {
2615 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2616 }
2617
2618 if (handle == NULL) {
2619 *detail = strdup(dlerror());
2620 return false;
2621 }
2622
2623 // Create a new entry.
2624 library = new SharedLibrary(path, handle, class_loader);
2625 UNIMPLEMENTED(ERROR) << "missing pthread_cond_init";
2626 // pthread_cond_init(&library->onLoadCond, NULL);
2627
2628 libraries[path] = library;
2629
2630 // if (pNewEntry != pActualEntry) {
2631 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2632 // freeSharedLibEntry(pNewEntry);
2633 // return CheckOnLoadResult(this, pActualEntry);
2634 // } else
2635 {
2636 if (verbose_jni) {
2637 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2638 }
2639
2640 bool result = true;
2641 void* sym = dlsym(handle, "JNI_OnLoad");
2642 if (sym == NULL) {
2643 if (verbose_jni) {
2644 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2645 }
2646 } else {
2647 // Call JNI_OnLoad. We have to override the current class
2648 // loader, which will always be "null" since the stuff at the
2649 // top of the stack is around Runtime.loadLibrary(). (See
2650 // the comments in the JNI FindClass function.)
2651 UNIMPLEMENTED(WARNING) << "need to override current class loader";
2652 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2653 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
2654 //Object* prevOverride = self->classLoaderOverride;
2655 //self->classLoaderOverride = classLoader;
2656
2657 old_state = self->GetState();
2658 self->SetState(Thread::kNative);
2659 if (verbose_jni) {
2660 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2661 }
2662 int version = (*jni_on_load)(reinterpret_cast<JavaVM*>(this), NULL);
2663 self->SetState(old_state);
2664
2665 UNIMPLEMENTED(WARNING) << "need to restore current class loader";
2666 //self->classLoaderOverride = prevOverride;
2667
2668 if (version != JNI_VERSION_1_2 &&
2669 version != JNI_VERSION_1_4 &&
2670 version != JNI_VERSION_1_6) {
2671 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2672 << "bad version: " << version;
2673 // It's unwise to call dlclose() here, but we can mark it
2674 // as bad and ensure that future load attempts will fail.
2675 // We don't know how far JNI_OnLoad got, so there could
2676 // be some partially-initialized stuff accessible through
2677 // newly-registered native method calls. We could try to
2678 // unregister them, but that doesn't seem worthwhile.
2679 result = false;
2680 } else {
2681 if (verbose_jni) {
2682 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2683 << " from JNI_OnLoad in \"" << path << "\"]";
2684 }
2685 }
2686 }
2687
2688 library->SetResult(result);
2689 return result;
2690 }
2691}
2692
Ian Rogersdf20fe02011-07-20 20:34:16 -07002693} // namespace art