blob: bfbd83e87a614614c32031fb5b4201d550f0f799 [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.
buzbeec143c552011-08-20 17:38:58 -0700556 ClassLoader* cl = (ClassLoader*) ts.Self()->GetClassLoaderOverride(); // TODO: fix type in Thread
557 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
1718 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1719 ScopedJniThreadState ts(env);
1720 UNIMPLEMENTED(FATAL);
1721 return 0;
1722 }
1723
1724 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1725 ScopedJniThreadState ts(env);
1726 UNIMPLEMENTED(FATAL);
1727 return NULL;
1728 }
1729
1730 static void SetObjectArrayElement(JNIEnv* env,
1731 jobjectArray java_array, jsize index, jobject java_value) {
1732 ScopedJniThreadState ts(env);
1733 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1734 Object* value = Decode<Object*>(ts, java_value);
1735 array->Set(index, value);
1736 }
1737
1738 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1739 ScopedJniThreadState ts(env);
1740 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1741 }
1742
1743 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1744 ScopedJniThreadState ts(env);
1745 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1746 }
1747
1748 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1749 ScopedJniThreadState ts(env);
1750 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1751 }
1752
1753 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1754 ScopedJniThreadState ts(env);
1755 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1756 }
1757
1758 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1759 ScopedJniThreadState ts(env);
1760 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1761 }
1762
1763 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1764 ScopedJniThreadState ts(env);
1765 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1766 }
1767
1768 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1769 ScopedJniThreadState ts(env);
1770 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1771 }
1772
1773 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1774 ScopedJniThreadState ts(env);
1775 CHECK_GE(length, 0); // TODO: ReportJniError
1776
1777 // Compute the array class corresponding to the given element class.
1778 Class* element_class = Decode<Class*>(ts, element_jclass);
1779 std::string descriptor;
1780 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001781 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782
1783 // Find the class.
1784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1785 // TODO: need to get the appropriate ClassLoader.
1786 Class* array_class = class_linker->FindClass(descriptor, NULL);
1787 if (array_class == NULL) {
1788 return NULL;
1789 }
1790
1791 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
1792 CHECK(initial_element == NULL); // TODO: support initial_element
1793 return AddLocalReference<jobjectArray>(ts, result);
1794 }
1795
1796 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1797 ScopedJniThreadState ts(env);
1798 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1799 }
1800
1801 static jboolean* GetBooleanArrayElements(JNIEnv* env,
1802 jbooleanArray array, jboolean* isCopy) {
1803 ScopedJniThreadState ts(env);
1804 UNIMPLEMENTED(FATAL);
1805 return NULL;
1806 }
1807
1808 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* isCopy) {
1809 ScopedJniThreadState ts(env);
1810 UNIMPLEMENTED(FATAL);
1811 return NULL;
1812 }
1813
1814 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* isCopy) {
1815 ScopedJniThreadState ts(env);
1816 UNIMPLEMENTED(FATAL);
1817 return NULL;
1818 }
1819
1820 static jshort* GetShortArrayElements(JNIEnv* env,
1821 jshortArray array, jboolean* isCopy) {
1822 ScopedJniThreadState ts(env);
1823 UNIMPLEMENTED(FATAL);
1824 return NULL;
1825 }
1826
1827 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* isCopy) {
1828 ScopedJniThreadState ts(env);
1829 UNIMPLEMENTED(FATAL);
1830 return NULL;
1831 }
1832
1833 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* isCopy) {
1834 ScopedJniThreadState ts(env);
1835 UNIMPLEMENTED(FATAL);
1836 return NULL;
1837 }
1838
1839 static jfloat* GetFloatArrayElements(JNIEnv* env,
1840 jfloatArray array, jboolean* isCopy) {
1841 ScopedJniThreadState ts(env);
1842 UNIMPLEMENTED(FATAL);
1843 return NULL;
1844 }
1845
1846 static jdouble* GetDoubleArrayElements(JNIEnv* env,
1847 jdoubleArray array, jboolean* isCopy) {
1848 ScopedJniThreadState ts(env);
1849 UNIMPLEMENTED(FATAL);
1850 return NULL;
1851 }
1852
1853 static void ReleaseBooleanArrayElements(JNIEnv* env,
1854 jbooleanArray array, jboolean* elems, jint mode) {
1855 ScopedJniThreadState ts(env);
1856 UNIMPLEMENTED(FATAL);
1857 }
1858
1859 static void ReleaseByteArrayElements(JNIEnv* env,
1860 jbyteArray array, jbyte* elems, jint mode) {
1861 ScopedJniThreadState ts(env);
1862 UNIMPLEMENTED(FATAL);
1863 }
1864
1865 static void ReleaseCharArrayElements(JNIEnv* env,
1866 jcharArray array, jchar* elems, jint mode) {
1867 ScopedJniThreadState ts(env);
1868 UNIMPLEMENTED(FATAL);
1869 }
1870
1871 static void ReleaseShortArrayElements(JNIEnv* env,
1872 jshortArray array, jshort* elems, jint mode) {
1873 ScopedJniThreadState ts(env);
1874 UNIMPLEMENTED(FATAL);
1875 }
1876
1877 static void ReleaseIntArrayElements(JNIEnv* env,
1878 jintArray array, jint* elems, jint mode) {
1879 ScopedJniThreadState ts(env);
1880 UNIMPLEMENTED(FATAL);
1881 }
1882
1883 static void ReleaseLongArrayElements(JNIEnv* env,
1884 jlongArray array, jlong* elems, jint mode) {
1885 ScopedJniThreadState ts(env);
1886 UNIMPLEMENTED(FATAL);
1887 }
1888
1889 static void ReleaseFloatArrayElements(JNIEnv* env,
1890 jfloatArray array, jfloat* elems, jint mode) {
1891 ScopedJniThreadState ts(env);
1892 UNIMPLEMENTED(FATAL);
1893 }
1894
1895 static void ReleaseDoubleArrayElements(JNIEnv* env,
1896 jdoubleArray array, jdouble* elems, jint mode) {
1897 ScopedJniThreadState ts(env);
1898 UNIMPLEMENTED(FATAL);
1899 }
1900
1901 static void GetBooleanArrayRegion(JNIEnv* env,
1902 jbooleanArray array, jsize start, jsize l, jboolean* buf) {
1903 ScopedJniThreadState ts(env);
1904 UNIMPLEMENTED(FATAL);
1905 }
1906
1907 static void GetByteArrayRegion(JNIEnv* env,
1908 jbyteArray array, jsize start, jsize len, jbyte* buf) {
1909 ScopedJniThreadState ts(env);
1910 UNIMPLEMENTED(FATAL);
1911 }
1912
1913 static void GetCharArrayRegion(JNIEnv* env,
1914 jcharArray array, jsize start, jsize len, jchar* buf) {
1915 ScopedJniThreadState ts(env);
1916 UNIMPLEMENTED(FATAL);
1917 }
1918
1919 static void GetShortArrayRegion(JNIEnv* env,
1920 jshortArray array, jsize start, jsize len, jshort* buf) {
1921 ScopedJniThreadState ts(env);
1922 UNIMPLEMENTED(FATAL);
1923 }
1924
1925 static void GetIntArrayRegion(JNIEnv* env,
1926 jintArray array, jsize start, jsize len, jint* buf) {
1927 ScopedJniThreadState ts(env);
1928 UNIMPLEMENTED(FATAL);
1929 }
1930
1931 static void GetLongArrayRegion(JNIEnv* env,
1932 jlongArray array, jsize start, jsize len, jlong* buf) {
1933 ScopedJniThreadState ts(env);
1934 UNIMPLEMENTED(FATAL);
1935 }
1936
1937 static void GetFloatArrayRegion(JNIEnv* env,
1938 jfloatArray array, jsize start, jsize len, jfloat* buf) {
1939 ScopedJniThreadState ts(env);
1940 UNIMPLEMENTED(FATAL);
1941 }
1942
1943 static void GetDoubleArrayRegion(JNIEnv* env,
1944 jdoubleArray array, jsize start, jsize len, jdouble* buf) {
1945 ScopedJniThreadState ts(env);
1946 UNIMPLEMENTED(FATAL);
1947 }
1948
1949 static void SetBooleanArrayRegion(JNIEnv* env,
1950 jbooleanArray array, jsize start, jsize l, const jboolean* buf) {
1951 ScopedJniThreadState ts(env);
1952 UNIMPLEMENTED(FATAL);
1953 }
1954
1955 static void SetByteArrayRegion(JNIEnv* env,
1956 jbyteArray array, jsize start, jsize len, const jbyte* buf) {
1957 ScopedJniThreadState ts(env);
1958 UNIMPLEMENTED(FATAL);
1959 }
1960
1961 static void SetCharArrayRegion(JNIEnv* env,
1962 jcharArray array, jsize start, jsize len, const jchar* buf) {
1963 ScopedJniThreadState ts(env);
1964 UNIMPLEMENTED(FATAL);
1965 }
1966
1967 static void SetShortArrayRegion(JNIEnv* env,
1968 jshortArray array, jsize start, jsize len, const jshort* buf) {
1969 ScopedJniThreadState ts(env);
1970 UNIMPLEMENTED(FATAL);
1971 }
1972
1973 static void SetIntArrayRegion(JNIEnv* env,
1974 jintArray array, jsize start, jsize len, const jint* buf) {
1975 ScopedJniThreadState ts(env);
1976 UNIMPLEMENTED(FATAL);
1977 }
1978
1979 static void SetLongArrayRegion(JNIEnv* env,
1980 jlongArray array, jsize start, jsize len, const jlong* buf) {
1981 ScopedJniThreadState ts(env);
1982 UNIMPLEMENTED(FATAL);
1983 }
1984
1985 static void SetFloatArrayRegion(JNIEnv* env,
1986 jfloatArray array, jsize start, jsize len, const jfloat* buf) {
1987 ScopedJniThreadState ts(env);
1988 UNIMPLEMENTED(FATAL);
1989 }
1990
1991 static void SetDoubleArrayRegion(JNIEnv* env,
1992 jdoubleArray array, jsize start, jsize len, const jdouble* buf) {
1993 ScopedJniThreadState ts(env);
1994 UNIMPLEMENTED(FATAL);
1995 }
1996
1997 static jint RegisterNatives(JNIEnv* env,
1998 jclass clazz, const JNINativeMethod* methods, jint nMethods) {
1999 ScopedJniThreadState ts(env);
2000 Class* klass = Decode<Class*>(ts, clazz);
2001 for(int i = 0; i < nMethods; i++) {
2002 const char* name = methods[i].name;
2003 const char* sig = methods[i].signature;
2004
2005 if (*sig == '!') {
2006 // TODO: fast jni. it's too noisy to log all these.
2007 ++sig;
2008 }
2009
2010 Method* method = klass->FindDirectMethod(name, sig);
2011 if (method == NULL) {
2012 method = klass->FindVirtualMethod(name, sig);
2013 }
2014 if (method == NULL) {
2015 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002016 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2018 "no method \"%s.%s%s\"",
2019 class_name.c_str(), name, sig);
2020 return JNI_ERR;
2021 } else if (!method->IsNative()) {
2022 Thread* self = Thread::Current();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002023 std::string class_name = klass->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2025 "method \"%s.%s%s\" is not native",
2026 class_name.c_str(), name, sig);
2027 return JNI_ERR;
2028 }
2029 method->RegisterNative(methods[i].fnPtr);
2030 }
2031 return JNI_OK;
2032 }
2033
2034 static jint UnregisterNatives(JNIEnv* env, jclass clazz) {
2035 ScopedJniThreadState ts(env);
2036 UNIMPLEMENTED(FATAL);
2037 return 0;
2038 }
2039
2040 static jint MonitorEnter(JNIEnv* env, jobject obj) {
2041 ScopedJniThreadState ts(env);
2042 UNIMPLEMENTED(WARNING);
2043 return 0;
2044 }
2045
2046 static jint MonitorExit(JNIEnv* env, jobject obj) {
2047 ScopedJniThreadState ts(env);
2048 UNIMPLEMENTED(WARNING);
2049 return 0;
2050 }
2051
2052 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2053 ScopedJniThreadState ts(env);
2054 Runtime* runtime = Runtime::Current();
2055 if (runtime != NULL) {
2056 *vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
2057 } else {
2058 *vm = NULL;
2059 }
2060 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2061 }
2062
2063 static void GetStringRegion(JNIEnv* env,
2064 jstring str, jsize start, jsize len, jchar* buf) {
2065 ScopedJniThreadState ts(env);
2066 UNIMPLEMENTED(FATAL);
2067 }
2068
2069 static void GetStringUTFRegion(JNIEnv* env,
2070 jstring str, jsize start, jsize len, char* buf) {
2071 ScopedJniThreadState ts(env);
2072 UNIMPLEMENTED(FATAL);
2073 }
2074
2075 static void* GetPrimitiveArrayCritical(JNIEnv* env,
2076 jarray array, jboolean* isCopy) {
2077 ScopedJniThreadState ts(env);
2078 UNIMPLEMENTED(FATAL);
2079 return NULL;
2080 }
2081
2082 static void ReleasePrimitiveArrayCritical(JNIEnv* env,
2083 jarray array, void* carray, jint mode) {
2084 ScopedJniThreadState ts(env);
2085 UNIMPLEMENTED(FATAL);
2086 }
2087
2088 static const jchar* GetStringCritical(JNIEnv* env, jstring s, jboolean* isCopy) {
2089 ScopedJniThreadState ts(env);
2090 UNIMPLEMENTED(FATAL);
2091 return NULL;
2092 }
2093
2094 static void ReleaseStringCritical(JNIEnv* env, jstring s, const jchar* cstr) {
2095 ScopedJniThreadState ts(env);
2096 UNIMPLEMENTED(FATAL);
2097 }
2098
2099 static jboolean ExceptionCheck(JNIEnv* env) {
2100 ScopedJniThreadState ts(env);
2101 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
2102 }
2103
2104 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2105 ScopedJniThreadState ts(env);
2106 UNIMPLEMENTED(FATAL);
2107 return NULL;
2108 }
2109
2110 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
2111 ScopedJniThreadState ts(env);
2112 UNIMPLEMENTED(FATAL);
2113 return NULL;
2114 }
2115
2116 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
2117 ScopedJniThreadState ts(env);
2118 UNIMPLEMENTED(FATAL);
2119 return 0;
2120 }
2121
2122 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2123 ScopedJniThreadState ts(env);
2124 UNIMPLEMENTED(FATAL);
2125 return JNIInvalidRefType;
2126 }
2127};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002128
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002129static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002130 NULL, // reserved0.
2131 NULL, // reserved1.
2132 NULL, // reserved2.
2133 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 JNI::GetVersion,
2135 JNI::DefineClass,
2136 JNI::FindClass,
2137 JNI::FromReflectedMethod,
2138 JNI::FromReflectedField,
2139 JNI::ToReflectedMethod,
2140 JNI::GetSuperclass,
2141 JNI::IsAssignableFrom,
2142 JNI::ToReflectedField,
2143 JNI::Throw,
2144 JNI::ThrowNew,
2145 JNI::ExceptionOccurred,
2146 JNI::ExceptionDescribe,
2147 JNI::ExceptionClear,
2148 JNI::FatalError,
2149 JNI::PushLocalFrame,
2150 JNI::PopLocalFrame,
2151 JNI::NewGlobalRef,
2152 JNI::DeleteGlobalRef,
2153 JNI::DeleteLocalRef,
2154 JNI::IsSameObject,
2155 JNI::NewLocalRef,
2156 JNI::EnsureLocalCapacity,
2157 JNI::AllocObject,
2158 JNI::NewObject,
2159 JNI::NewObjectV,
2160 JNI::NewObjectA,
2161 JNI::GetObjectClass,
2162 JNI::IsInstanceOf,
2163 JNI::GetMethodID,
2164 JNI::CallObjectMethod,
2165 JNI::CallObjectMethodV,
2166 JNI::CallObjectMethodA,
2167 JNI::CallBooleanMethod,
2168 JNI::CallBooleanMethodV,
2169 JNI::CallBooleanMethodA,
2170 JNI::CallByteMethod,
2171 JNI::CallByteMethodV,
2172 JNI::CallByteMethodA,
2173 JNI::CallCharMethod,
2174 JNI::CallCharMethodV,
2175 JNI::CallCharMethodA,
2176 JNI::CallShortMethod,
2177 JNI::CallShortMethodV,
2178 JNI::CallShortMethodA,
2179 JNI::CallIntMethod,
2180 JNI::CallIntMethodV,
2181 JNI::CallIntMethodA,
2182 JNI::CallLongMethod,
2183 JNI::CallLongMethodV,
2184 JNI::CallLongMethodA,
2185 JNI::CallFloatMethod,
2186 JNI::CallFloatMethodV,
2187 JNI::CallFloatMethodA,
2188 JNI::CallDoubleMethod,
2189 JNI::CallDoubleMethodV,
2190 JNI::CallDoubleMethodA,
2191 JNI::CallVoidMethod,
2192 JNI::CallVoidMethodV,
2193 JNI::CallVoidMethodA,
2194 JNI::CallNonvirtualObjectMethod,
2195 JNI::CallNonvirtualObjectMethodV,
2196 JNI::CallNonvirtualObjectMethodA,
2197 JNI::CallNonvirtualBooleanMethod,
2198 JNI::CallNonvirtualBooleanMethodV,
2199 JNI::CallNonvirtualBooleanMethodA,
2200 JNI::CallNonvirtualByteMethod,
2201 JNI::CallNonvirtualByteMethodV,
2202 JNI::CallNonvirtualByteMethodA,
2203 JNI::CallNonvirtualCharMethod,
2204 JNI::CallNonvirtualCharMethodV,
2205 JNI::CallNonvirtualCharMethodA,
2206 JNI::CallNonvirtualShortMethod,
2207 JNI::CallNonvirtualShortMethodV,
2208 JNI::CallNonvirtualShortMethodA,
2209 JNI::CallNonvirtualIntMethod,
2210 JNI::CallNonvirtualIntMethodV,
2211 JNI::CallNonvirtualIntMethodA,
2212 JNI::CallNonvirtualLongMethod,
2213 JNI::CallNonvirtualLongMethodV,
2214 JNI::CallNonvirtualLongMethodA,
2215 JNI::CallNonvirtualFloatMethod,
2216 JNI::CallNonvirtualFloatMethodV,
2217 JNI::CallNonvirtualFloatMethodA,
2218 JNI::CallNonvirtualDoubleMethod,
2219 JNI::CallNonvirtualDoubleMethodV,
2220 JNI::CallNonvirtualDoubleMethodA,
2221 JNI::CallNonvirtualVoidMethod,
2222 JNI::CallNonvirtualVoidMethodV,
2223 JNI::CallNonvirtualVoidMethodA,
2224 JNI::GetFieldID,
2225 JNI::GetObjectField,
2226 JNI::GetBooleanField,
2227 JNI::GetByteField,
2228 JNI::GetCharField,
2229 JNI::GetShortField,
2230 JNI::GetIntField,
2231 JNI::GetLongField,
2232 JNI::GetFloatField,
2233 JNI::GetDoubleField,
2234 JNI::SetObjectField,
2235 JNI::SetBooleanField,
2236 JNI::SetByteField,
2237 JNI::SetCharField,
2238 JNI::SetShortField,
2239 JNI::SetIntField,
2240 JNI::SetLongField,
2241 JNI::SetFloatField,
2242 JNI::SetDoubleField,
2243 JNI::GetStaticMethodID,
2244 JNI::CallStaticObjectMethod,
2245 JNI::CallStaticObjectMethodV,
2246 JNI::CallStaticObjectMethodA,
2247 JNI::CallStaticBooleanMethod,
2248 JNI::CallStaticBooleanMethodV,
2249 JNI::CallStaticBooleanMethodA,
2250 JNI::CallStaticByteMethod,
2251 JNI::CallStaticByteMethodV,
2252 JNI::CallStaticByteMethodA,
2253 JNI::CallStaticCharMethod,
2254 JNI::CallStaticCharMethodV,
2255 JNI::CallStaticCharMethodA,
2256 JNI::CallStaticShortMethod,
2257 JNI::CallStaticShortMethodV,
2258 JNI::CallStaticShortMethodA,
2259 JNI::CallStaticIntMethod,
2260 JNI::CallStaticIntMethodV,
2261 JNI::CallStaticIntMethodA,
2262 JNI::CallStaticLongMethod,
2263 JNI::CallStaticLongMethodV,
2264 JNI::CallStaticLongMethodA,
2265 JNI::CallStaticFloatMethod,
2266 JNI::CallStaticFloatMethodV,
2267 JNI::CallStaticFloatMethodA,
2268 JNI::CallStaticDoubleMethod,
2269 JNI::CallStaticDoubleMethodV,
2270 JNI::CallStaticDoubleMethodA,
2271 JNI::CallStaticVoidMethod,
2272 JNI::CallStaticVoidMethodV,
2273 JNI::CallStaticVoidMethodA,
2274 JNI::GetStaticFieldID,
2275 JNI::GetStaticObjectField,
2276 JNI::GetStaticBooleanField,
2277 JNI::GetStaticByteField,
2278 JNI::GetStaticCharField,
2279 JNI::GetStaticShortField,
2280 JNI::GetStaticIntField,
2281 JNI::GetStaticLongField,
2282 JNI::GetStaticFloatField,
2283 JNI::GetStaticDoubleField,
2284 JNI::SetStaticObjectField,
2285 JNI::SetStaticBooleanField,
2286 JNI::SetStaticByteField,
2287 JNI::SetStaticCharField,
2288 JNI::SetStaticShortField,
2289 JNI::SetStaticIntField,
2290 JNI::SetStaticLongField,
2291 JNI::SetStaticFloatField,
2292 JNI::SetStaticDoubleField,
2293 JNI::NewString,
2294 JNI::GetStringLength,
2295 JNI::GetStringChars,
2296 JNI::ReleaseStringChars,
2297 JNI::NewStringUTF,
2298 JNI::GetStringUTFLength,
2299 JNI::GetStringUTFChars,
2300 JNI::ReleaseStringUTFChars,
2301 JNI::GetArrayLength,
2302 JNI::NewObjectArray,
2303 JNI::GetObjectArrayElement,
2304 JNI::SetObjectArrayElement,
2305 JNI::NewBooleanArray,
2306 JNI::NewByteArray,
2307 JNI::NewCharArray,
2308 JNI::NewShortArray,
2309 JNI::NewIntArray,
2310 JNI::NewLongArray,
2311 JNI::NewFloatArray,
2312 JNI::NewDoubleArray,
2313 JNI::GetBooleanArrayElements,
2314 JNI::GetByteArrayElements,
2315 JNI::GetCharArrayElements,
2316 JNI::GetShortArrayElements,
2317 JNI::GetIntArrayElements,
2318 JNI::GetLongArrayElements,
2319 JNI::GetFloatArrayElements,
2320 JNI::GetDoubleArrayElements,
2321 JNI::ReleaseBooleanArrayElements,
2322 JNI::ReleaseByteArrayElements,
2323 JNI::ReleaseCharArrayElements,
2324 JNI::ReleaseShortArrayElements,
2325 JNI::ReleaseIntArrayElements,
2326 JNI::ReleaseLongArrayElements,
2327 JNI::ReleaseFloatArrayElements,
2328 JNI::ReleaseDoubleArrayElements,
2329 JNI::GetBooleanArrayRegion,
2330 JNI::GetByteArrayRegion,
2331 JNI::GetCharArrayRegion,
2332 JNI::GetShortArrayRegion,
2333 JNI::GetIntArrayRegion,
2334 JNI::GetLongArrayRegion,
2335 JNI::GetFloatArrayRegion,
2336 JNI::GetDoubleArrayRegion,
2337 JNI::SetBooleanArrayRegion,
2338 JNI::SetByteArrayRegion,
2339 JNI::SetCharArrayRegion,
2340 JNI::SetShortArrayRegion,
2341 JNI::SetIntArrayRegion,
2342 JNI::SetLongArrayRegion,
2343 JNI::SetFloatArrayRegion,
2344 JNI::SetDoubleArrayRegion,
2345 JNI::RegisterNatives,
2346 JNI::UnregisterNatives,
2347 JNI::MonitorEnter,
2348 JNI::MonitorExit,
2349 JNI::GetJavaVM,
2350 JNI::GetStringRegion,
2351 JNI::GetStringUTFRegion,
2352 JNI::GetPrimitiveArrayCritical,
2353 JNI::ReleasePrimitiveArrayCritical,
2354 JNI::GetStringCritical,
2355 JNI::ReleaseStringCritical,
2356 JNI::NewWeakGlobalRef,
2357 JNI::DeleteWeakGlobalRef,
2358 JNI::ExceptionCheck,
2359 JNI::NewDirectByteBuffer,
2360 JNI::GetDirectBufferAddress,
2361 JNI::GetDirectBufferCapacity,
2362 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002363};
2364
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002365static const size_t kMonitorsInitial = 32; // Arbitrary.
2366static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2367
2368static const size_t kLocalsInitial = 64; // Arbitrary.
2369static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002370
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002371JNIEnvExt::JNIEnvExt(Thread* self, bool check_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002372 : fns(&gNativeInterface),
2373 self(self),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002374 check_jni(check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002375 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002376 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2377 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002378}
2379
Carl Shapiroea4dca82011-08-01 13:45:38 -07002380// JNI Invocation interface.
2381
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002382extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2383 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2384 if (args->version < JNI_VERSION_1_2) {
2385 return JNI_EVERSION;
2386 }
2387 Runtime::Options options;
2388 for (int i = 0; i < args->nOptions; ++i) {
2389 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002390 options.push_back(std::make_pair(StringPiece(option->optionString),
2391 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002392 }
2393 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002394 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002395 if (runtime == NULL) {
2396 return JNI_ERR;
2397 } else {
2398 *p_env = reinterpret_cast<JNIEnv*>(Thread::Current()->GetJniEnv());
Elliott Hughes0af55432011-08-17 18:37:28 -07002399 *p_vm = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002400 return JNI_OK;
2401 }
2402}
2403
Elliott Hughesf2682d52011-08-15 16:37:04 -07002404extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002405 Runtime* runtime = Runtime::Current();
2406 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002407 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002408 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002409 *vm_count = 1;
Elliott Hughes0af55432011-08-17 18:37:28 -07002410 vms[0] = reinterpret_cast<JavaVM*>(runtime->GetJavaVM());
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002411 }
2412 return JNI_OK;
2413}
2414
2415// Historically unsupported.
2416extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2417 return JNI_ERR;
2418}
2419
Elliott Hughescdf53122011-08-19 15:46:09 -07002420class JII {
2421 public:
2422 static jint DestroyJavaVM(JavaVM* vm) {
2423 if (vm == NULL) {
2424 return JNI_ERR;
2425 } else {
2426 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2427 delete raw_vm->runtime;
2428 return JNI_OK;
2429 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002430 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002431
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2433 if (vm == NULL || p_env == NULL) {
2434 return JNI_ERR;
2435 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002436 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2437 Runtime* runtime = raw_vm->runtime;
Elliott Hughescdf53122011-08-19 15:46:09 -07002438 const char* name = NULL;
2439 if (thr_args != NULL) {
2440 // TODO: check version
2441 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2442 // TODO: thread group
2443 }
2444 bool success = runtime->AttachCurrentThread(name, p_env);
2445 if (!success) {
2446 return JNI_ERR;
2447 } else {
2448 return JNI_OK;
2449 }
2450 }
2451
2452 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2453 if (vm == NULL || p_env == NULL) {
2454 return JNI_ERR;
2455 }
2456 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2457 Runtime* runtime = raw_vm->runtime;
2458 const char* name = NULL;
2459 if (thr_args != NULL) {
2460 // TODO: check version
2461 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
2462 // TODO: thread group
2463 }
2464 bool success = runtime->AttachCurrentThreadAsDaemon(name, p_env);
2465 if (!success) {
2466 return JNI_ERR;
2467 } else {
2468 return JNI_OK;
2469 }
2470 }
2471
2472 static jint DetachCurrentThread(JavaVM* vm) {
2473 if (vm == NULL) {
2474 return JNI_ERR;
2475 } else {
2476 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2477 Runtime* runtime = raw_vm->runtime;
2478 runtime->DetachCurrentThread();
2479 return JNI_OK;
2480 }
2481 }
2482
2483 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2484 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2485 return JNI_EVERSION;
2486 }
2487 if (vm == NULL || env == NULL) {
2488 return JNI_ERR;
2489 }
2490 Thread* thread = Thread::Current();
2491 if (thread == NULL) {
2492 *env = NULL;
2493 return JNI_EDETACHED;
2494 }
2495 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002496 return JNI_OK;
2497 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002498};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002499
Elliott Hughesf2682d52011-08-15 16:37:04 -07002500struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002501 NULL, // reserved0
2502 NULL, // reserved1
2503 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002504 JII::DestroyJavaVM,
2505 JII::AttachCurrentThread,
2506 JII::DetachCurrentThread,
2507 JII::GetEnv,
2508 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002509};
2510
Elliott Hughesbbd76712011-08-17 10:25:24 -07002511static const size_t kPinTableInitialSize = 16;
2512static const size_t kPinTableMaxSize = 1024;
2513
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002514static const size_t kGlobalsInitial = 512; // Arbitrary.
2515static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2516
2517static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2518static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2519
Elliott Hughes0af55432011-08-17 18:37:28 -07002520JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughesbbd76712011-08-17 10:25:24 -07002521 : fns(&gInvokeInterface),
2522 runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002523 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002524 verbose_jni(verbose_jni),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002525 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002526 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002527 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002528 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002529 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002530}
2531
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002532JavaVMExt::~JavaVMExt() {
2533 delete globals_lock;
2534 delete weak_globals_lock;
2535}
2536
Elliott Hughescdf53122011-08-19 15:46:09 -07002537/*
2538 * Load native code from the specified absolute pathname. Per the spec,
2539 * if we've already loaded a library with the specified pathname, we
2540 * return without doing anything.
2541 *
2542 * TODO? for better results we should absolutify the pathname. For fully
2543 * correct results we should stat to get the inode and compare that. The
2544 * existing implementation is fine so long as everybody is using
2545 * System.loadLibrary.
2546 *
2547 * The library will be associated with the specified class loader. The JNI
2548 * spec says we can't load the same library into more than one class loader.
2549 *
2550 * Returns "true" on success. On failure, sets *detail to a
2551 * human-readable description of the error or NULL if no detail is
2552 * available; ownership of the string is transferred to the caller.
2553 */
2554bool JavaVMExt::LoadNativeLibrary(const std::string& path, Object* class_loader, char** detail) {
2555 *detail = NULL;
2556
2557 // See if we've already loaded this library. If we have, and the class loader
2558 // matches, return successfully without doing anything.
2559 SharedLibrary* library = libraries[path];
2560 if (library != NULL) {
2561 if (library->GetClassLoader() != class_loader) {
2562 LOG(WARNING) << "Shared library \"" << path << "\" already opened by "
2563 << "ClassLoader " << library->GetClassLoader() << "; "
2564 << "can't open in " << class_loader;
2565 *detail = strdup("already opened by different ClassLoader");
2566 return false;
2567 }
2568 if (verbose_jni) {
2569 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2570 << "ClassLoader " << class_loader << "]";
2571 }
2572 if (!library->CheckOnLoadResult(this)) {
2573 *detail = strdup("JNI_OnLoad failed before");
2574 return false;
2575 }
2576 return true;
2577 }
2578
2579 // Open the shared library. Because we're using a full path, the system
2580 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2581 // resolve this library's dependencies though.)
2582
2583 // Failures here are expected when java.library.path has several entries
2584 // and we have to hunt for the lib.
2585
2586 // The current version of the dynamic linker prints detailed information
2587 // about dlopen() failures. Some things to check if the message is
2588 // cryptic:
2589 // - make sure the library exists on the device
2590 // - verify that the right path is being opened (the debug log message
2591 // above can help with that)
2592 // - check to see if the library is valid (e.g. not zero bytes long)
2593 // - check config/prelink-linux-arm.map to ensure that the library
2594 // is listed and is not being overrun by the previous entry (if
2595 // loading suddenly stops working on a prelinked library, this is
2596 // a good one to check)
2597 // - write a trivial app that calls sleep() then dlopen(), attach
2598 // to it with "strace -p <pid>" while it sleeps, and watch for
2599 // attempts to open nonexistent dependent shared libs
2600
2601 // TODO: automate some of these checks!
2602
2603 // This can execute slowly for a large library on a busy system, so we
2604 // want to switch from RUNNING to VMWAIT while it executes. This allows
2605 // the GC to ignore us.
2606 Thread* self = Thread::Current();
2607 Thread::State old_state = self->GetState();
2608 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2609 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2610 self->SetState(old_state);
2611
2612 if (verbose_jni) {
2613 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2614 }
2615
2616 if (handle == NULL) {
2617 *detail = strdup(dlerror());
2618 return false;
2619 }
2620
2621 // Create a new entry.
2622 library = new SharedLibrary(path, handle, class_loader);
2623 UNIMPLEMENTED(ERROR) << "missing pthread_cond_init";
2624 // pthread_cond_init(&library->onLoadCond, NULL);
2625
2626 libraries[path] = library;
2627
2628 // if (pNewEntry != pActualEntry) {
2629 // LOG(INFO) << "WOW: we lost a race to add a shared library (\"" << path << "\" ClassLoader=" << class_loader <<")";
2630 // freeSharedLibEntry(pNewEntry);
2631 // return CheckOnLoadResult(this, pActualEntry);
2632 // } else
2633 {
2634 if (verbose_jni) {
2635 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2636 }
2637
2638 bool result = true;
2639 void* sym = dlsym(handle, "JNI_OnLoad");
2640 if (sym == NULL) {
2641 if (verbose_jni) {
2642 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2643 }
2644 } else {
2645 // Call JNI_OnLoad. We have to override the current class
2646 // loader, which will always be "null" since the stuff at the
2647 // top of the stack is around Runtime.loadLibrary(). (See
2648 // the comments in the JNI FindClass function.)
2649 UNIMPLEMENTED(WARNING) << "need to override current class loader";
2650 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2651 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
2652 //Object* prevOverride = self->classLoaderOverride;
2653 //self->classLoaderOverride = classLoader;
2654
2655 old_state = self->GetState();
2656 self->SetState(Thread::kNative);
2657 if (verbose_jni) {
2658 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2659 }
2660 int version = (*jni_on_load)(reinterpret_cast<JavaVM*>(this), NULL);
2661 self->SetState(old_state);
2662
2663 UNIMPLEMENTED(WARNING) << "need to restore current class loader";
2664 //self->classLoaderOverride = prevOverride;
2665
2666 if (version != JNI_VERSION_1_2 &&
2667 version != JNI_VERSION_1_4 &&
2668 version != JNI_VERSION_1_6) {
2669 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2670 << "bad version: " << version;
2671 // It's unwise to call dlclose() here, but we can mark it
2672 // as bad and ensure that future load attempts will fail.
2673 // We don't know how far JNI_OnLoad got, so there could
2674 // be some partially-initialized stuff accessible through
2675 // newly-registered native method calls. We could try to
2676 // unregister them, but that doesn't seem worthwhile.
2677 result = false;
2678 } else {
2679 if (verbose_jni) {
2680 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2681 << " from JNI_OnLoad in \"" << path << "\"]";
2682 }
2683 }
2684 }
2685
2686 library->SetResult(result);
2687 return result;
2688 }
2689}
2690
Ian Rogersdf20fe02011-07-20 20:34:16 -07002691} // namespace art