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