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