blob: 921305658a7fa9a09def33cf2863ccbf99c9f1b7 [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
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070017#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070019#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070021#include "scoped_jni_thread_state.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070022#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070024
25namespace art {
26
Elliott Hughescdf53122011-08-19 15:46:09 -070027// This is private API, but with two different implementations: ARM and x86.
28void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070029
Elliott Hughescdf53122011-08-19 15:46:09 -070030// TODO: this should be in our anonymous namespace, but is currently needed
31// for testing in "jni_internal_test.cc".
Elliott Hughes79082e32011-08-25 12:07:32 -070032void EnsureInvokeStub(Method* method) {
Elliott Hughescdf53122011-08-19 15:46:09 -070033 if (method->GetInvokeStub() != NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -070034 return;
Elliott Hughescdf53122011-08-19 15:46:09 -070035 }
36 // TODO: use signature to find a matching stub
37 // TODO: failed, acquire a lock on the stub table
38 Assembler assembler;
39 CreateInvokeStub(&assembler, method);
40 // TODO: store native_entry in the stub table
41 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
42 size_t length = assembler.CodeSize();
43 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
44 if (addr == MAP_FAILED) {
Elliott Hughesa2501992011-08-26 19:39:54 -070045 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method);
Elliott Hughescdf53122011-08-19 15:46:09 -070046 }
47 MemoryRegion region(addr, length);
48 assembler.FinalizeInstructions(region);
49 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
Elliott Hughescdf53122011-08-19 15:46:09 -070050}
Elliott Hughes0af55432011-08-17 18:37:28 -070051
Elliott Hughescdf53122011-08-19 15:46:09 -070052namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -070053
Elliott Hughesc5f7c912011-08-18 14:00:42 -070054/*
55 * Add a local reference for an object to the current stack frame. When
56 * the native function returns, the reference will be discarded.
57 *
58 * We need to allow the same reference to be added multiple times.
59 *
60 * This will be called on otherwise unreferenced objects. We cannot do
61 * GC allocations here, and it's best if we don't grab a mutex.
62 *
63 * Returns the local reference (currently just the same pointer that was
64 * passed in), or NULL on failure.
65 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070066template<typename T>
67T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
72 IndirectReferenceTable& locals = ts.Env()->locals;
73
74 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
75 IndirectRef ref = locals.Add(cookie, obj);
76 if (ref == NULL) {
77 // TODO: just change Add's DCHECK to CHECK and lose this?
78 locals.Dump();
79 LOG(FATAL) << "Failed adding to JNI local reference table "
80 << "(has " << locals.Capacity() << " entries)";
81 // TODO: dvmDumpThread(dvmThreadSelf(), false);
82 }
83
84#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
85 if (ts.Env()->check_jni) {
86 size_t entry_count = locals.Capacity();
87 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070088 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070090 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
92 // TODO: dvmDumpThread(dvmThreadSelf(), false);
93 // dvmAbort();
94 }
95 }
96#endif
97
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070098 if (ts.Env()->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070099 // Hand out direct pointers to support broken old apps.
100 return reinterpret_cast<T>(obj);
101 }
102
103 return reinterpret_cast<T>(ref);
104}
105
Elliott Hughescdf53122011-08-19 15:46:09 -0700106jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
107 if (obj == NULL) {
108 return NULL;
109 }
Elliott Hughes75770752011-08-24 17:52:38 -0700110 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700111 IndirectReferenceTable& weak_globals = vm->weak_globals;
112 MutexLock mu(vm->weak_globals_lock);
113 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
114 return reinterpret_cast<jweak>(ref);
115}
116
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117template<typename T>
118T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700119 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700120}
121
Elliott Hughescdf53122011-08-19 15:46:09 -0700122Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
123 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
124}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700125
Elliott Hughescdf53122011-08-19 15:46:09 -0700126Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
127 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700128}
129
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700130byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700131 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700132 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700133 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700134 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700135 switch (shorty[i]) {
136 case 'Z':
137 case 'B':
138 case 'C':
139 case 'S':
140 case 'I':
141 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
142 offset += 4;
143 break;
144 case 'F':
145 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
146 offset += 4;
147 break;
148 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700149 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700150 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
151 offset += sizeof(Object*);
152 break;
153 }
154 case 'D':
155 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
156 offset += 8;
157 break;
158 case 'J':
159 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
160 offset += 8;
161 break;
162 }
163 }
164 return arg_array.release();
165}
166
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700167byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700168 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700169 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700170 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700171 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700172 switch (shorty[i]) {
173 case 'Z':
174 case 'B':
175 case 'C':
176 case 'S':
177 case 'I':
178 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
179 offset += 4;
180 break;
181 case 'F':
182 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
183 offset += 4;
184 break;
185 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700186 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700187 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
188 offset += sizeof(Object*);
189 break;
190 }
191 case 'D':
192 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
193 offset += 8;
194 break;
195 case 'J':
196 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
197 offset += 8;
198 break;
199 }
200 }
201 return arg_array.release();
202}
203
Elliott Hughes72025e52011-08-23 17:50:30 -0700204JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
205 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700206 Thread* self = ts.Self();
207
208 // Push a transition back into managed code onto the linked list in thread
209 CHECK_EQ(Thread::kRunnable, self->GetState());
210 NativeToManagedRecord record;
211 self->PushNativeToManagedRecord(&record);
212
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700213 // Call the invoke stub associated with the method
214 // Pass everything as arguments
215 const Method::InvokeStub* stub = method->GetInvokeStub();
216 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700217
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700218 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700219 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700220 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700221 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700222 LOG(WARNING) << "Not invoking method with no associated code: "
Elliott Hughesa2501992011-08-26 19:39:54 -0700223 << PrettyMethod(method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700224 result.j = 0;
225 }
buzbeec143c552011-08-20 17:38:58 -0700226
Ian Rogers6de08602011-08-19 14:52:39 -0700227 // Pop transition
228 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700229 return result;
230}
231
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700232JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700233 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700234 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700235 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700236 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700237 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700238}
239
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700240JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700241 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700242 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700244 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700245 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
246}
247
Elliott Hughes75770752011-08-24 17:52:38 -0700248Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700249 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700250}
251
Brian Carlstrom30b94452011-08-25 21:35:26 -0700252JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700253 Object* receiver = Decode<Object*>(ts, obj);
254 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700255 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700256 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
257}
258
Brian Carlstrom30b94452011-08-25 21:35:26 -0700259JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700260 Object* receiver = Decode<Object*>(ts, obj);
261 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700262 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700263 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700264}
265
Elliott Hughes6b436852011-08-12 10:16:44 -0700266// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
267// separated with slashes but aren't wrapped with "L;" like regular descriptors
268// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
269// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
270// supported names with dots too (such as "a.b.C").
271std::string NormalizeJniClassDescriptor(const char* name) {
272 std::string result;
273 // Add the missing "L;" if necessary.
274 if (name[0] == '[') {
275 result = name;
276 } else {
277 result += 'L';
278 result += name;
279 result += ';';
280 }
281 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700282 if (result.find('.') != std::string::npos) {
283 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
284 << "\"" << name << "\"";
285 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700286 }
287 return result;
288}
289
Elliott Hughescdf53122011-08-19 15:46:09 -0700290jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
291 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700292 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
293 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700294 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700295
296 Method* method = NULL;
297 if (is_static) {
298 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700299 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700300 method = c->FindVirtualMethod(name, sig);
301 if (method == NULL) {
302 // No virtual method matching the signature. Search declared
303 // private methods and constructors.
304 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700305 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700307
Elliott Hughescdf53122011-08-19 15:46:09 -0700308 if (method == NULL || method->IsStatic() != is_static) {
309 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700310 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700311 // TODO: try searching for the opposite kind of method from is_static
312 // for better diagnostics?
313 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700314 "no %s method %s", is_static ? "static" : "non-static",
315 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700316 return NULL;
317 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700318
Elliott Hughes79082e32011-08-25 12:07:32 -0700319 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700320
Elliott Hughescdf53122011-08-19 15:46:09 -0700321 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700322}
323
Elliott Hughescdf53122011-08-19 15:46:09 -0700324jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
325 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700326 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
327 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700328 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700329
330 Field* field = NULL;
331 if (is_static) {
332 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700333 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700334 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700336
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 if (field == NULL) {
338 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700339 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700340 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
341 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700342 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700343 return NULL;
344 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700345
Elliott Hughescdf53122011-08-19 15:46:09 -0700346 jweak fid = AddWeakGlobalReference(ts, field);
347 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700348}
349
Elliott Hughes75770752011-08-24 17:52:38 -0700350void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
351 JavaVMExt* vm = ts.Vm();
352 MutexLock mu(vm->pins_lock);
353 vm->pin_table.Add(array);
354}
355
356void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
357 JavaVMExt* vm = ts.Vm();
358 MutexLock mu(vm->pins_lock);
359 vm->pin_table.Remove(array);
360}
361
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700362template<typename JniT, typename ArtT>
363JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
364 CHECK_GE(length, 0); // TODO: ReportJniError
365 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700366 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700367}
368
Elliott Hughes75770752011-08-24 17:52:38 -0700369template <typename ArrayT, typename CArrayT, typename ArtArrayT>
370CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
371 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
372 PinPrimitiveArray(ts, array);
373 if (is_copy != NULL) {
374 *is_copy = JNI_FALSE;
375 }
376 return array->GetData();
377}
378
379template <typename ArrayT>
380void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
381 if (mode != JNI_COMMIT) {
382 Array* array = Decode<Array*>(ts, java_array);
383 UnpinPrimitiveArray(ts, array);
384 }
385}
386
Elliott Hughes814e4032011-08-23 12:07:56 -0700387void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
388 std::string type(PrettyType(array));
389 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
390 "%s offset=%d length=%d %s.length=%d",
391 type.c_str(), start, length, identifier, array->GetLength());
392}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700393void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
394 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
395 "offset=%d length=%d string.length()=%d", start, length, array_length);
396}
Elliott Hughes814e4032011-08-23 12:07:56 -0700397
398template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700399void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700400 ArrayT* array = Decode<ArrayT*>(ts, java_array);
401 if (start < 0 || length < 0 || start + length > array->GetLength()) {
402 ThrowAIOOBE(ts, array, start, length, "src");
403 } else {
404 JavaT* data = array->GetData();
405 memcpy(buf, data + start, length * sizeof(JavaT));
406 }
407}
408
409template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700410void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700411 ArrayT* array = Decode<ArrayT*>(ts, java_array);
412 if (start < 0 || length < 0 || start + length > array->GetLength()) {
413 ThrowAIOOBE(ts, array, start, length, "dst");
414 } else {
415 JavaT* data = array->GetData();
416 memcpy(data + start, buf, length * sizeof(JavaT));
417 }
418}
419
Elliott Hughes75770752011-08-24 17:52:38 -0700420jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700421 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
422 CHECK(buffer_class.get() != NULL);
423 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
424}
425
Elliott Hughes75770752011-08-24 17:52:38 -0700426jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700427 static jclass buffer_class = InitDirectByteBufferClass(env);
428 return buffer_class;
429}
430
Elliott Hughes75770752011-08-24 17:52:38 -0700431jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
432 if (vm == NULL || p_env == NULL) {
433 return JNI_ERR;
434 }
435
436 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
437 JavaVMAttachArgs args;
438 if (thr_args == NULL) {
439 // Allow the v1.1 calling convention.
440 args.version = JNI_VERSION_1_2;
441 args.name = NULL;
442 args.group = NULL; // TODO: get "main" thread group
443 } else {
444 args.version = in_args->version;
445 args.name = in_args->name;
446 if (in_args->group != NULL) {
447 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
448 args.group = NULL; // TODO: decode in_args->group
449 } else {
450 args.group = NULL; // TODO: get "main" thread group
451 }
452 }
453 CHECK_GE(args.version, JNI_VERSION_1_2);
454
455 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
456 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
457}
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459class SharedLibrary {
460 public:
461 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
462 : path_(path),
463 handle_(handle),
464 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
465 jni_on_load_tid_(Thread::Current()->GetId()),
466 jni_on_load_result_(kPending) {
467 pthread_cond_init(&jni_on_load_cond_, NULL);
468 }
469
470 ~SharedLibrary() {
471 delete jni_on_load_lock_;
472 }
473
474 Object* GetClassLoader() {
475 return class_loader_;
476 }
477
478 std::string GetPath() {
479 return path_;
480 }
481
482 /*
483 * Check the result of an earlier call to JNI_OnLoad on this library. If
484 * the call has not yet finished in another thread, wait for it.
485 */
486 bool CheckOnLoadResult(JavaVMExt* vm) {
487 Thread* self = Thread::Current();
488 if (jni_on_load_tid_ == self->GetId()) {
489 // Check this so we don't end up waiting for ourselves. We need
490 // to return "true" so the caller can continue.
491 LOG(INFO) << *self << " recursive attempt to load library "
492 << "\"" << path_ << "\"";
493 return true;
494 }
495
496 MutexLock mu(jni_on_load_lock_);
497 while (jni_on_load_result_ == kPending) {
498 if (vm->verbose_jni) {
499 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
500 << "JNI_OnLoad...]";
501 }
502 Thread::State old_state = self->GetState();
503 self->SetState(Thread::kWaiting); // TODO: VMWAIT
504 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
505 self->SetState(old_state);
506 }
507
508 bool okay = (jni_on_load_result_ == kOkay);
509 if (vm->verbose_jni) {
510 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
511 << (okay ? "succeeded" : "failed") << "]";
512 }
513 return okay;
514 }
515
516 void SetResult(bool result) {
517 jni_on_load_result_ = result ? kOkay : kFailed;
518 jni_on_load_tid_ = 0;
519
520 // Broadcast a wakeup to anybody sleeping on the condition variable.
521 MutexLock mu(jni_on_load_lock_);
522 pthread_cond_broadcast(&jni_on_load_cond_);
523 }
524
525 void* FindSymbol(const std::string& symbol_name) {
526 return dlsym(handle_, symbol_name.c_str());
527 }
528
529 private:
530 enum JNI_OnLoadState {
531 kPending,
532 kFailed,
533 kOkay,
534 };
535
536 // Path to library "/system/lib/libjni.so".
537 std::string path_;
538
539 // The void* returned by dlopen(3).
540 void* handle_;
541
542 // The ClassLoader this library is associated with.
543 Object* class_loader_;
544
545 // Guards remaining items.
546 Mutex* jni_on_load_lock_;
547 // Wait for JNI_OnLoad in other thread.
548 pthread_cond_t jni_on_load_cond_;
549 // Recursive invocation guard.
550 uint32_t jni_on_load_tid_;
551 // Result of earlier JNI_OnLoad call.
552 JNI_OnLoadState jni_on_load_result_;
553};
554
Elliott Hughescdf53122011-08-19 15:46:09 -0700555} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700556
Elliott Hughes79082e32011-08-25 12:07:32 -0700557// This exists mainly to keep implementation details out of the header file.
558class Libraries {
559 public:
560 Libraries() {
561 }
562
563 ~Libraries() {
564 // Delete our map values. (The keys will be cleaned up by the map itself.)
565 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
566 delete it->second;
567 }
568 }
569
570 SharedLibrary* Get(const std::string& path) {
571 return libraries_[path];
572 }
573
574 void Put(const std::string& path, SharedLibrary* library) {
575 libraries_[path] = library;
576 }
577
578 // See section 11.3 "Linking Native Methods" of the JNI spec.
579 void* FindNativeMethod(const Method* m) {
580 std::string jni_short_name(JniShortName(m));
581 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700582 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
584 SharedLibrary* library = it->second;
585 if (library->GetClassLoader() != declaring_class_loader) {
586 // We only search libraries loaded by the appropriate ClassLoader.
587 continue;
588 }
589 // Try the short name then the long name...
590 void* fn = library->FindSymbol(jni_short_name);
591 if (fn == NULL) {
592 fn = library->FindSymbol(jni_long_name);
593 }
594 if (fn != NULL) {
595 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700596 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 << " in \"" << library->GetPath() << "\"]";
598 }
599 return fn;
600 }
601 }
602 std::string detail;
603 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700604 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 LOG(ERROR) << detail;
606 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
607 "%s", detail.c_str());
608 return NULL;
609 }
610
611 private:
612 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
613
614 std::map<std::string, SharedLibrary*> libraries_;
615};
616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617class JNI {
618 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jint GetVersion(JNIEnv* env) {
621 ScopedJniThreadState ts(env);
622 return JNI_VERSION_1_6;
623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
626 ScopedJniThreadState ts(env);
627 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700628 return NULL;
629 }
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 static jclass FindClass(JNIEnv* env, const char* name) {
632 ScopedJniThreadState ts(env);
633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
634 std::string descriptor(NormalizeJniClassDescriptor(name));
635 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700636 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700637 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
642 ScopedJniThreadState ts(env);
643 Method* method = Decode<Method*>(ts, java_method);
644 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
648 ScopedJniThreadState ts(env);
649 Field* field = Decode<Field*>(ts, java_field);
650 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
654 ScopedJniThreadState ts(env);
655 Method* method = DecodeMethod(ts, mid);
656 return AddLocalReference<jobject>(ts, method);
657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
660 ScopedJniThreadState ts(env);
661 Field* field = DecodeField(ts, fid);
662 return AddLocalReference<jobject>(ts, field);
663 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700664
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
666 ScopedJniThreadState ts(env);
667 Object* o = Decode<Object*>(ts, java_object);
668 return AddLocalReference<jclass>(ts, o->GetClass());
669 }
670
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700671 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700673 Class* c = Decode<Class*>(ts, java_class);
674 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 Class* c1 = Decode<Class*>(ts, java_class1);
680 Class* c2 = Decode<Class*>(ts, java_class2);
681 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700683
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700686 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 if (jobj == NULL) {
688 // NB. JNI is different from regular Java instanceof in this respect
689 return JNI_TRUE;
690 } else {
691 Object* obj = Decode<Object*>(ts, jobj);
692 Class* klass = Decode<Class*>(ts, clazz);
693 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
694 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 static jint Throw(JNIEnv* env, jthrowable java_exception) {
698 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700699 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 if (exception == NULL) {
701 return JNI_ERR;
702 }
703 ts.Self()->SetException(exception);
704 return JNI_OK;
705 }
706
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700709 // TODO: check for a pending exception to decide what constructor to call.
710 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
711 if (mid == NULL) {
712 return JNI_ERR;
713 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
715 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700716 return JNI_ERR;
717 }
718
719 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 args[0].l = s.get();
721 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
722 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700723 return JNI_ERR;
724 }
725
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700727 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700729
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 return JNI_OK;
731 }
732
733 static jboolean ExceptionCheck(JNIEnv* env) {
734 ScopedJniThreadState ts(env);
735 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
736 }
737
738 static void ExceptionClear(JNIEnv* env) {
739 ScopedJniThreadState ts(env);
740 ts.Self()->ClearException();
741 }
742
743 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700745
746 Thread* self = ts.Self();
747 Throwable* original_exception = self->GetException();
748 self->ClearException();
749
750 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
751 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
752 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
753 if (mid == NULL) {
754 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
755 << PrettyType(original_exception);
756 } else {
757 env->CallVoidMethod(exception.get(), mid);
758 if (self->IsExceptionPending()) {
759 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
760 << " thrown while calling printStackTrace";
761 self->ClearException();
762 }
763 }
764
765 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700767
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 static jthrowable ExceptionOccurred(JNIEnv* env) {
769 ScopedJniThreadState ts(env);
770 Object* exception = ts.Self()->GetException();
771 if (exception == NULL) {
772 return NULL;
773 } else {
774 // TODO: if adding a local reference failing causes the VM to abort
775 // then the following check will never occur.
776 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
777 if (localException == NULL) {
778 // We were unable to add a new local reference, and threw a new
779 // exception. We can't return "exception", because it's not a
780 // local reference. So we have to return NULL, indicating that
781 // there was no exception, even though it's pretty much raining
782 // exceptions in here.
783 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
784 }
785 return localException;
786 }
787 }
788
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 static void FatalError(JNIEnv* env, const char* msg) {
790 ScopedJniThreadState ts(env);
791 LOG(FATAL) << "JNI FatalError called: " << msg;
792 }
793
794 static jint PushLocalFrame(JNIEnv* env, jint cap) {
795 ScopedJniThreadState ts(env);
796 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
797 return JNI_OK;
798 }
799
800 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
801 ScopedJniThreadState ts(env);
802 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
803 return res;
804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
807 ScopedJniThreadState ts(env);
808 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
809 return 0;
810 }
811
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
813 ScopedJniThreadState ts(env);
814 if (obj == NULL) {
815 return NULL;
816 }
817
Elliott Hughes75770752011-08-24 17:52:38 -0700818 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 IndirectReferenceTable& globals = vm->globals;
820 MutexLock mu(vm->globals_lock);
821 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
822 return reinterpret_cast<jobject>(ref);
823 }
824
825 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
826 ScopedJniThreadState ts(env);
827 if (obj == NULL) {
828 return;
829 }
830
Elliott Hughes75770752011-08-24 17:52:38 -0700831 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 IndirectReferenceTable& globals = vm->globals;
833 MutexLock mu(vm->globals_lock);
834
835 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
836 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
837 << "failed to find entry";
838 }
839 }
840
841 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
842 ScopedJniThreadState ts(env);
843 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
844 }
845
846 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
847 ScopedJniThreadState ts(env);
848 if (obj == NULL) {
849 return;
850 }
851
Elliott Hughes75770752011-08-24 17:52:38 -0700852 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 IndirectReferenceTable& weak_globals = vm->weak_globals;
854 MutexLock mu(vm->weak_globals_lock);
855
856 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
857 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
858 << "failed to find entry";
859 }
860 }
861
862 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
863 ScopedJniThreadState ts(env);
864 if (obj == NULL) {
865 return NULL;
866 }
867
868 IndirectReferenceTable& locals = ts.Env()->locals;
869
870 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
871 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
872 return reinterpret_cast<jobject>(ref);
873 }
874
875 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
876 ScopedJniThreadState ts(env);
877 if (obj == NULL) {
878 return;
879 }
880
881 IndirectReferenceTable& locals = ts.Env()->locals;
882
883 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
884 if (!locals.Remove(cookie, obj)) {
885 // Attempting to delete a local reference that is not in the
886 // topmost local reference frame is a no-op. DeleteLocalRef returns
887 // void and doesn't throw any exceptions, but we should probably
888 // complain about it so the user will notice that things aren't
889 // going quite the way they expect.
890 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
891 << "failed to find entry";
892 }
893 }
894
895 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
896 ScopedJniThreadState ts(env);
897 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
898 ? JNI_TRUE : JNI_FALSE;
899 }
900
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700901 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700903 Class* c = Decode<Class*>(ts, java_class);
904 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
905 return NULL;
906 }
907 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 ScopedJniThreadState ts(env);
912 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 va_start(args, mid);
914 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 va_end(args);
916 return result;
917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700921 Class* c = Decode<Class*>(ts, java_class);
922 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
923 return NULL;
924 }
925 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 return local_result;
929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700933 Class* c = Decode<Class*>(ts, java_class);
934 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
935 return NULL;
936 }
937 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 return local_result;
941 }
942
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
944 ScopedJniThreadState ts(env);
945 return FindMethodID(ts, c, name, sig, false);
946 }
947
948 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
949 ScopedJniThreadState ts(env);
950 return FindMethodID(ts, c, name, sig, true);
951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 va_list ap;
956 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700957 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 va_end(ap);
959 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700970 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 va_list ap;
977 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700978 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_end(ap);
980 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700985 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700990 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_list ap;
996 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700997 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_end(ap);
999 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001004 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001009 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_list ap;
1015 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001016 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_end(ap);
1018 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001023 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001028 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_list ap;
1034 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001035 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_end(ap);
1037 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001042 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001047 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_list ap;
1053 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001054 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_end(ap);
1056 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001061 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001066 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_list ap;
1072 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001073 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
1075 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001080 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001085 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_list ap;
1091 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001092 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_end(ap);
1094 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001099 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001104 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_list ap;
1110 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001111 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_end(ap);
1113 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001118 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001123 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_list ap;
1129 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001130 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001136 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001141 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
1144 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
1147 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_start(ap, mid);
1149 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1151 va_end(ap);
1152 return local_result;
1153 }
1154
1155 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 return AddLocalReference<jobject>(ts, result.l);
1160 }
1161
1162 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 return AddLocalReference<jobject>(ts, result.l);
1167 }
1168
1169 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
1172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
1174 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 va_end(ap);
1176 return result.z;
1177 }
1178
1179 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
1185 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
1194 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_start(ap, mid);
1196 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_end(ap);
1198 return result.b;
1199 }
1200
1201 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 ScopedJniThreadState ts(env);
1216 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_start(ap, mid);
1218 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 va_end(ap);
1220 return result.c;
1221 }
1222
1223 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
1229 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
1238 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 va_start(ap, mid);
1240 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 va_end(ap);
1242 return result.s;
1243 }
1244
1245 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
1251 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
1257 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
1260 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 va_start(ap, mid);
1262 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_end(ap);
1264 return result.i;
1265 }
1266
1267 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
1273 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
1279 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
1282 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 va_start(ap, mid);
1284 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 va_end(ap);
1286 return result.j;
1287 }
1288
1289 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
1301 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 ScopedJniThreadState ts(env);
1304 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 va_start(ap, mid);
1306 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 va_end(ap);
1308 return result.f;
1309 }
1310
1311 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
1317 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
1326 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 va_start(ap, mid);
1328 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 va_end(ap);
1330 return result.d;
1331 }
1332
1333 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
1339 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
1345 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
1348 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 va_start(ap, mid);
1350 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 va_end(ap);
1352 }
1353
1354 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
1360 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
1366 static jfieldID GetFieldID(JNIEnv* env,
1367 jclass c, const char* name, const char* sig) {
1368 ScopedJniThreadState ts(env);
1369 return FindFieldID(ts, c, name, sig, false);
1370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001371
1372
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 static jfieldID GetStaticFieldID(JNIEnv* env,
1374 jclass c, const char* name, const char* sig) {
1375 ScopedJniThreadState ts(env);
1376 return FindFieldID(ts, c, name, sig, true);
1377 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001378
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 Object* o = Decode<Object*>(ts, obj);
1382 Field* f = DecodeField(ts, fid);
1383 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001385
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 Field* f = DecodeField(ts, fid);
1389 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 Object* o = Decode<Object*>(ts, java_object);
1395 Object* v = Decode<Object*>(ts, java_value);
1396 Field* f = DecodeField(ts, fid);
1397 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 Object* v = Decode<Object*>(ts, java_value);
1403 Field* f = DecodeField(ts, fid);
1404 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407#define GET_PRIMITIVE_FIELD(fn, instance) \
1408 ScopedJniThreadState ts(env); \
1409 Object* o = Decode<Object*>(ts, instance); \
1410 Field* f = DecodeField(ts, fid); \
1411 return f->fn(o)
1412
1413#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1414 ScopedJniThreadState ts(env); \
1415 Object* o = Decode<Object*>(ts, instance); \
1416 Field* f = DecodeField(ts, fid); \
1417 f->fn(o, value)
1418
1419 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetLong, NULL);
1473 }
1474
1475 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1477 }
1478
1479 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1481 }
1482
1483 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1484 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1485 }
1486
1487 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1488 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1489 }
1490
1491 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1492 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1493 }
1494
1495 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1496 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1497 }
1498
1499 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1500 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1501 }
1502
1503 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1504 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1505 }
1506
1507 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1508 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1509 }
1510
1511 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1512 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1513 }
1514
1515 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1516 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1517 }
1518
1519 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1520 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1521 }
1522
1523 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1524 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1525 }
1526
1527 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1528 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1529 }
1530
1531 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1532 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1533 }
1534
1535 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1536 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1537 }
1538
1539 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1540 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1541 }
1542
1543 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1544 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
1547 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 ScopedJniThreadState ts(env);
1550 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001551 va_start(ap, mid);
1552 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1554 va_end(ap);
1555 return local_result;
1556 }
1557
1558 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001559 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 return AddLocalReference<jobject>(ts, result.l);
1563 }
1564
1565 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 return AddLocalReference<jobject>(ts, result.l);
1570 }
1571
1572 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 ScopedJniThreadState ts(env);
1575 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001576 va_start(ap, mid);
1577 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
1579 return result.z;
1580 }
1581
1582 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
1588 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
1596 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 va_start(ap, mid);
1598 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_end(ap);
1600 return result.b;
1601 }
1602
1603 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
1609 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
1617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
1619 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
1621 return result.c;
1622 }
1623
1624 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
1630 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
1638 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 va_start(ap, mid);
1640 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
1642 return result.s;
1643 }
1644
1645 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
1651 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
1659 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 va_start(ap, mid);
1661 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_end(ap);
1663 return result.i;
1664 }
1665
1666 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
1672 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
1680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
1682 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 va_end(ap);
1684 return result.j;
1685 }
1686
1687 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
1693 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
1701 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 va_start(ap, mid);
1703 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 va_end(ap);
1705 return result.f;
1706 }
1707
1708 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
1714 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
1722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
1724 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
1726 return result.d;
1727 }
1728
1729 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
1735 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
1743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
1745 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
1747 }
1748
1749 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
1755 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes814e4032011-08-23 12:07:56 -07001761 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 if (chars == NULL && char_count == 0) {
1764 return NULL;
1765 }
1766 String* result = String::AllocFromUtf16(char_count, chars);
1767 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
1770 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1771 ScopedJniThreadState ts(env);
1772 if (utf == NULL) {
1773 return NULL;
1774 }
1775 String* result = String::AllocFromModifiedUtf8(utf);
1776 return AddLocalReference<jstring>(ts, result);
1777 }
1778
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1780 ScopedJniThreadState ts(env);
1781 return Decode<String*>(ts, java_string)->GetLength();
1782 }
1783
1784 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1785 ScopedJniThreadState ts(env);
1786 return Decode<String*>(ts, java_string)->GetUtfLength();
1787 }
1788
Elliott Hughesb465ab02011-08-24 11:21:21 -07001789 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001791 String* s = Decode<String*>(ts, java_string);
1792 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1793 ThrowSIOOBE(ts, start, length, s->GetLength());
1794 } else {
1795 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1796 memcpy(buf, chars + start, length * sizeof(jchar));
1797 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 }
1799
Elliott Hughesb465ab02011-08-24 11:21:21 -07001800 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 String* s = Decode<String*>(ts, java_string);
1803 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1804 ThrowSIOOBE(ts, start, length, s->GetLength());
1805 } else {
1806 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1807 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1808 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 }
1810
Elliott Hughes75770752011-08-24 17:52:38 -07001811 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001813 String* s = Decode<String*>(ts, java_string);
1814 const CharArray* chars = s->GetCharArray();
1815 PinPrimitiveArray(ts, chars);
1816 if (is_copy != NULL) {
1817 *is_copy = JNI_FALSE;
1818 }
1819 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes75770752011-08-24 17:52:38 -07001827 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001829 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes75770752011-08-24 17:52:38 -07001832 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001834 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes75770752011-08-24 17:52:38 -07001837 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001838 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001839 if (java_string == NULL) {
1840 return NULL;
1841 }
1842 if (is_copy != NULL) {
1843 *is_copy = JNI_TRUE;
1844 }
1845 String* s = Decode<String*>(ts, java_string);
1846 size_t byte_count = s->GetUtfLength();
1847 char* bytes = new char[byte_count + 1];
1848 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001849 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001850 return NULL;
1851 }
1852 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1853 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1854 bytes[byte_count] = '\0';
1855 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001861 }
1862
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001865 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001866 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001867 Array* array = obj->AsArray();
1868 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1874 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
1877 static void SetObjectArrayElement(JNIEnv* env,
1878 jobjectArray java_array, jsize index, jobject java_value) {
1879 ScopedJniThreadState ts(env);
1880 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1881 Object* value = Decode<Object*>(ts, java_value);
1882 array->Set(index, value);
1883 }
1884
1885 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1886 ScopedJniThreadState ts(env);
1887 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1888 }
1889
1890 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1891 ScopedJniThreadState ts(env);
1892 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1893 }
1894
1895 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1896 ScopedJniThreadState ts(env);
1897 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1898 }
1899
1900 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1901 ScopedJniThreadState ts(env);
1902 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1903 }
1904
1905 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1906 ScopedJniThreadState ts(env);
1907 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1908 }
1909
1910 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1913 }
1914
1915 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1918 }
1919
1920 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1921 ScopedJniThreadState ts(env);
1922 CHECK_GE(length, 0); // TODO: ReportJniError
1923
1924 // Compute the array class corresponding to the given element class.
1925 Class* element_class = Decode<Class*>(ts, element_jclass);
1926 std::string descriptor;
1927 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001928 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001929
1930 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001931 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1932 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 return NULL;
1934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 // Allocate and initialize if necessary.
1937 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 if (initial_element != NULL) {
1940 Object* initial_object = Decode<Object*>(ts, initial_element);
1941 for (jsize i = 0; i < length; ++i) {
1942 result->Set(i, initial_object);
1943 }
1944 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 return AddLocalReference<jobjectArray>(ts, result);
1946 }
1947
1948 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1949 ScopedJniThreadState ts(env);
1950 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 Class* c = Decode<Class*>(ts, java_class);
2126
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 const char* name = methods[i].name;
2129 const char* sig = methods[i].signature;
2130
2131 if (*sig == '!') {
2132 // TODO: fast jni. it's too noisy to log all these.
2133 ++sig;
2134 }
2135
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 Method* m = c->FindDirectMethod(name, sig);
2137 if (m == NULL) {
2138 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2144 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002145 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2151 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002152 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 return JNI_ERR;
2154 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
Elliott Hughes75770752011-08-24 17:52:38 -07002156 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002157 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 }
2159
2160 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162 return JNI_OK;
2163 }
2164
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 Class* c = Decode<Class*>(ts, java_class);
2168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 LOG(INFO) << "[Unregistering JNI native methods for "
2171 << PrettyDescriptor(c->GetDescriptor()) << "]";
2172 }
2173
2174 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2175 Method* m = c->GetDirectMethod(i);
2176 if (m->IsNative()) {
2177 m->UnregisterNative();
2178 }
2179 }
2180 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2181 Method* m = c->GetVirtualMethod(i);
2182 if (m->IsNative()) {
2183 m->UnregisterNative();
2184 }
2185 }
2186
2187 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes72025e52011-08-23 17:50:30 -07002190 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002192 Decode<Object*>(ts, java_object)->MonitorEnter();
2193 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes72025e52011-08-23 17:50:30 -07002196 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002198 Decode<Object*>(ts, java_object)->MonitorEnter();
2199 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
2202 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2203 ScopedJniThreadState ts(env);
2204 Runtime* runtime = Runtime::Current();
2205 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002206 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 } else {
2208 *vm = NULL;
2209 }
2210 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2211 }
2212
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2214 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215
2216 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002217 CHECK(address != NULL); // TODO: ReportJniError
2218 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219
2220 jclass buffer_class = GetDirectByteBufferClass(env);
2221 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2222 if (mid == NULL) {
2223 return NULL;
2224 }
2225
2226 // At the moment, the Java side is limited to 32 bits.
2227 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2228 CHECK_LE(capacity, 0xffffffff);
2229 jint address_arg = reinterpret_cast<jint>(address);
2230 jint capacity_arg = static_cast<jint>(capacity);
2231
2232 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2233 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2239 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Elliott Hughesb465ab02011-08-24 11:21:21 -07002242 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2245 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250
Elliott Hughes75770752011-08-24 17:52:38 -07002251 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252
2253 // Do we definitely know what kind of reference this is?
2254 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2255 IndirectRefKind kind = GetIndirectRefKind(ref);
2256 switch (kind) {
2257 case kLocal:
2258 return JNILocalRefType;
2259 case kGlobal:
2260 return JNIGlobalRefType;
2261 case kWeakGlobal:
2262 return JNIWeakGlobalRefType;
2263 case kSirtOrInvalid:
2264 // Is it in a stack IRT?
2265 if (ts.Self()->SirtContains(java_object)) {
2266 return JNILocalRefType;
2267 }
2268
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002269 if (!ts.Env()->work_around_app_jni_bugs) {
2270 return JNIInvalidRefType;
2271 }
2272
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 // If we're handing out direct pointers, check whether it's a direct pointer
2274 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002275 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 if (ts.Env()->locals.Contains(java_object)) {
2277 return JNILocalRefType;
2278 }
2279 }
2280
2281 return JNIInvalidRefType;
2282 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002285
Elliott Hughesa2501992011-08-26 19:39:54 -07002286const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002287 NULL, // reserved0.
2288 NULL, // reserved1.
2289 NULL, // reserved2.
2290 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 JNI::GetVersion,
2292 JNI::DefineClass,
2293 JNI::FindClass,
2294 JNI::FromReflectedMethod,
2295 JNI::FromReflectedField,
2296 JNI::ToReflectedMethod,
2297 JNI::GetSuperclass,
2298 JNI::IsAssignableFrom,
2299 JNI::ToReflectedField,
2300 JNI::Throw,
2301 JNI::ThrowNew,
2302 JNI::ExceptionOccurred,
2303 JNI::ExceptionDescribe,
2304 JNI::ExceptionClear,
2305 JNI::FatalError,
2306 JNI::PushLocalFrame,
2307 JNI::PopLocalFrame,
2308 JNI::NewGlobalRef,
2309 JNI::DeleteGlobalRef,
2310 JNI::DeleteLocalRef,
2311 JNI::IsSameObject,
2312 JNI::NewLocalRef,
2313 JNI::EnsureLocalCapacity,
2314 JNI::AllocObject,
2315 JNI::NewObject,
2316 JNI::NewObjectV,
2317 JNI::NewObjectA,
2318 JNI::GetObjectClass,
2319 JNI::IsInstanceOf,
2320 JNI::GetMethodID,
2321 JNI::CallObjectMethod,
2322 JNI::CallObjectMethodV,
2323 JNI::CallObjectMethodA,
2324 JNI::CallBooleanMethod,
2325 JNI::CallBooleanMethodV,
2326 JNI::CallBooleanMethodA,
2327 JNI::CallByteMethod,
2328 JNI::CallByteMethodV,
2329 JNI::CallByteMethodA,
2330 JNI::CallCharMethod,
2331 JNI::CallCharMethodV,
2332 JNI::CallCharMethodA,
2333 JNI::CallShortMethod,
2334 JNI::CallShortMethodV,
2335 JNI::CallShortMethodA,
2336 JNI::CallIntMethod,
2337 JNI::CallIntMethodV,
2338 JNI::CallIntMethodA,
2339 JNI::CallLongMethod,
2340 JNI::CallLongMethodV,
2341 JNI::CallLongMethodA,
2342 JNI::CallFloatMethod,
2343 JNI::CallFloatMethodV,
2344 JNI::CallFloatMethodA,
2345 JNI::CallDoubleMethod,
2346 JNI::CallDoubleMethodV,
2347 JNI::CallDoubleMethodA,
2348 JNI::CallVoidMethod,
2349 JNI::CallVoidMethodV,
2350 JNI::CallVoidMethodA,
2351 JNI::CallNonvirtualObjectMethod,
2352 JNI::CallNonvirtualObjectMethodV,
2353 JNI::CallNonvirtualObjectMethodA,
2354 JNI::CallNonvirtualBooleanMethod,
2355 JNI::CallNonvirtualBooleanMethodV,
2356 JNI::CallNonvirtualBooleanMethodA,
2357 JNI::CallNonvirtualByteMethod,
2358 JNI::CallNonvirtualByteMethodV,
2359 JNI::CallNonvirtualByteMethodA,
2360 JNI::CallNonvirtualCharMethod,
2361 JNI::CallNonvirtualCharMethodV,
2362 JNI::CallNonvirtualCharMethodA,
2363 JNI::CallNonvirtualShortMethod,
2364 JNI::CallNonvirtualShortMethodV,
2365 JNI::CallNonvirtualShortMethodA,
2366 JNI::CallNonvirtualIntMethod,
2367 JNI::CallNonvirtualIntMethodV,
2368 JNI::CallNonvirtualIntMethodA,
2369 JNI::CallNonvirtualLongMethod,
2370 JNI::CallNonvirtualLongMethodV,
2371 JNI::CallNonvirtualLongMethodA,
2372 JNI::CallNonvirtualFloatMethod,
2373 JNI::CallNonvirtualFloatMethodV,
2374 JNI::CallNonvirtualFloatMethodA,
2375 JNI::CallNonvirtualDoubleMethod,
2376 JNI::CallNonvirtualDoubleMethodV,
2377 JNI::CallNonvirtualDoubleMethodA,
2378 JNI::CallNonvirtualVoidMethod,
2379 JNI::CallNonvirtualVoidMethodV,
2380 JNI::CallNonvirtualVoidMethodA,
2381 JNI::GetFieldID,
2382 JNI::GetObjectField,
2383 JNI::GetBooleanField,
2384 JNI::GetByteField,
2385 JNI::GetCharField,
2386 JNI::GetShortField,
2387 JNI::GetIntField,
2388 JNI::GetLongField,
2389 JNI::GetFloatField,
2390 JNI::GetDoubleField,
2391 JNI::SetObjectField,
2392 JNI::SetBooleanField,
2393 JNI::SetByteField,
2394 JNI::SetCharField,
2395 JNI::SetShortField,
2396 JNI::SetIntField,
2397 JNI::SetLongField,
2398 JNI::SetFloatField,
2399 JNI::SetDoubleField,
2400 JNI::GetStaticMethodID,
2401 JNI::CallStaticObjectMethod,
2402 JNI::CallStaticObjectMethodV,
2403 JNI::CallStaticObjectMethodA,
2404 JNI::CallStaticBooleanMethod,
2405 JNI::CallStaticBooleanMethodV,
2406 JNI::CallStaticBooleanMethodA,
2407 JNI::CallStaticByteMethod,
2408 JNI::CallStaticByteMethodV,
2409 JNI::CallStaticByteMethodA,
2410 JNI::CallStaticCharMethod,
2411 JNI::CallStaticCharMethodV,
2412 JNI::CallStaticCharMethodA,
2413 JNI::CallStaticShortMethod,
2414 JNI::CallStaticShortMethodV,
2415 JNI::CallStaticShortMethodA,
2416 JNI::CallStaticIntMethod,
2417 JNI::CallStaticIntMethodV,
2418 JNI::CallStaticIntMethodA,
2419 JNI::CallStaticLongMethod,
2420 JNI::CallStaticLongMethodV,
2421 JNI::CallStaticLongMethodA,
2422 JNI::CallStaticFloatMethod,
2423 JNI::CallStaticFloatMethodV,
2424 JNI::CallStaticFloatMethodA,
2425 JNI::CallStaticDoubleMethod,
2426 JNI::CallStaticDoubleMethodV,
2427 JNI::CallStaticDoubleMethodA,
2428 JNI::CallStaticVoidMethod,
2429 JNI::CallStaticVoidMethodV,
2430 JNI::CallStaticVoidMethodA,
2431 JNI::GetStaticFieldID,
2432 JNI::GetStaticObjectField,
2433 JNI::GetStaticBooleanField,
2434 JNI::GetStaticByteField,
2435 JNI::GetStaticCharField,
2436 JNI::GetStaticShortField,
2437 JNI::GetStaticIntField,
2438 JNI::GetStaticLongField,
2439 JNI::GetStaticFloatField,
2440 JNI::GetStaticDoubleField,
2441 JNI::SetStaticObjectField,
2442 JNI::SetStaticBooleanField,
2443 JNI::SetStaticByteField,
2444 JNI::SetStaticCharField,
2445 JNI::SetStaticShortField,
2446 JNI::SetStaticIntField,
2447 JNI::SetStaticLongField,
2448 JNI::SetStaticFloatField,
2449 JNI::SetStaticDoubleField,
2450 JNI::NewString,
2451 JNI::GetStringLength,
2452 JNI::GetStringChars,
2453 JNI::ReleaseStringChars,
2454 JNI::NewStringUTF,
2455 JNI::GetStringUTFLength,
2456 JNI::GetStringUTFChars,
2457 JNI::ReleaseStringUTFChars,
2458 JNI::GetArrayLength,
2459 JNI::NewObjectArray,
2460 JNI::GetObjectArrayElement,
2461 JNI::SetObjectArrayElement,
2462 JNI::NewBooleanArray,
2463 JNI::NewByteArray,
2464 JNI::NewCharArray,
2465 JNI::NewShortArray,
2466 JNI::NewIntArray,
2467 JNI::NewLongArray,
2468 JNI::NewFloatArray,
2469 JNI::NewDoubleArray,
2470 JNI::GetBooleanArrayElements,
2471 JNI::GetByteArrayElements,
2472 JNI::GetCharArrayElements,
2473 JNI::GetShortArrayElements,
2474 JNI::GetIntArrayElements,
2475 JNI::GetLongArrayElements,
2476 JNI::GetFloatArrayElements,
2477 JNI::GetDoubleArrayElements,
2478 JNI::ReleaseBooleanArrayElements,
2479 JNI::ReleaseByteArrayElements,
2480 JNI::ReleaseCharArrayElements,
2481 JNI::ReleaseShortArrayElements,
2482 JNI::ReleaseIntArrayElements,
2483 JNI::ReleaseLongArrayElements,
2484 JNI::ReleaseFloatArrayElements,
2485 JNI::ReleaseDoubleArrayElements,
2486 JNI::GetBooleanArrayRegion,
2487 JNI::GetByteArrayRegion,
2488 JNI::GetCharArrayRegion,
2489 JNI::GetShortArrayRegion,
2490 JNI::GetIntArrayRegion,
2491 JNI::GetLongArrayRegion,
2492 JNI::GetFloatArrayRegion,
2493 JNI::GetDoubleArrayRegion,
2494 JNI::SetBooleanArrayRegion,
2495 JNI::SetByteArrayRegion,
2496 JNI::SetCharArrayRegion,
2497 JNI::SetShortArrayRegion,
2498 JNI::SetIntArrayRegion,
2499 JNI::SetLongArrayRegion,
2500 JNI::SetFloatArrayRegion,
2501 JNI::SetDoubleArrayRegion,
2502 JNI::RegisterNatives,
2503 JNI::UnregisterNatives,
2504 JNI::MonitorEnter,
2505 JNI::MonitorExit,
2506 JNI::GetJavaVM,
2507 JNI::GetStringRegion,
2508 JNI::GetStringUTFRegion,
2509 JNI::GetPrimitiveArrayCritical,
2510 JNI::ReleasePrimitiveArrayCritical,
2511 JNI::GetStringCritical,
2512 JNI::ReleaseStringCritical,
2513 JNI::NewWeakGlobalRef,
2514 JNI::DeleteWeakGlobalRef,
2515 JNI::ExceptionCheck,
2516 JNI::NewDirectByteBuffer,
2517 JNI::GetDirectBufferAddress,
2518 JNI::GetDirectBufferCapacity,
2519 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002520};
2521
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002522static const size_t kMonitorsInitial = 32; // Arbitrary.
2523static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2524
2525static const size_t kLocalsInitial = 64; // Arbitrary.
2526static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002527
Elliott Hughes75770752011-08-24 17:52:38 -07002528JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002529 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002530 vm(vm),
2531 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002532 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002533 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002534 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2535 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002536 functions = unchecked_functions = &gNativeInterface;
2537 if (check_jni) {
2538 functions = GetCheckJniNativeInterface();
2539 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002540}
2541
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002542JNIEnvExt::~JNIEnvExt() {
2543}
2544
Carl Shapiroea4dca82011-08-01 13:45:38 -07002545// JNI Invocation interface.
2546
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2548 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2549 if (args->version < JNI_VERSION_1_2) {
2550 return JNI_EVERSION;
2551 }
2552 Runtime::Options options;
2553 for (int i = 0; i < args->nOptions; ++i) {
2554 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002555 options.push_back(std::make_pair(StringPiece(option->optionString),
2556 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
2558 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 if (runtime == NULL) {
2561 return JNI_ERR;
2562 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002563 *p_env = Thread::Current()->GetJniEnv();
2564 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 return JNI_OK;
2566 }
2567}
2568
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 Runtime* runtime = Runtime::Current();
2571 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002572 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002574 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002575 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 }
2577 return JNI_OK;
2578}
2579
2580// Historically unsupported.
2581extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2582 return JNI_ERR;
2583}
2584
Elliott Hughescdf53122011-08-19 15:46:09 -07002585class JII {
2586 public:
2587 static jint DestroyJavaVM(JavaVM* vm) {
2588 if (vm == NULL) {
2589 return JNI_ERR;
2590 } else {
2591 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2592 delete raw_vm->runtime;
2593 return JNI_OK;
2594 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596
Elliott Hughescdf53122011-08-19 15:46:09 -07002597 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002598 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002599 }
2600
2601 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002602 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002603 }
2604
2605 static jint DetachCurrentThread(JavaVM* vm) {
2606 if (vm == NULL) {
2607 return JNI_ERR;
2608 } else {
2609 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2610 Runtime* runtime = raw_vm->runtime;
2611 runtime->DetachCurrentThread();
2612 return JNI_OK;
2613 }
2614 }
2615
2616 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2617 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2618 return JNI_EVERSION;
2619 }
2620 if (vm == NULL || env == NULL) {
2621 return JNI_ERR;
2622 }
2623 Thread* thread = Thread::Current();
2624 if (thread == NULL) {
2625 *env = NULL;
2626 return JNI_EDETACHED;
2627 }
2628 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 return JNI_OK;
2630 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002631};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632
Elliott Hughesa2501992011-08-26 19:39:54 -07002633const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002634 NULL, // reserved0
2635 NULL, // reserved1
2636 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002637 JII::DestroyJavaVM,
2638 JII::AttachCurrentThread,
2639 JII::DetachCurrentThread,
2640 JII::GetEnv,
2641 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002642};
2643
Elliott Hughesbbd76712011-08-17 10:25:24 -07002644static const size_t kPinTableInitialSize = 16;
2645static const size_t kPinTableMaxSize = 1024;
2646
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002647static const size_t kGlobalsInitial = 512; // Arbitrary.
2648static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2649
2650static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2651static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2652
Elliott Hughes0af55432011-08-17 18:37:28 -07002653JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002654 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002655 check_jni_abort_hook(NULL),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002656 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002657 verbose_jni(verbose_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002658 force_copy(false), // TODO: add a way to enable this
2659 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002660 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002661 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002662 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002663 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002664 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002665 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2666 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2667 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002668 functions = unchecked_functions = &gInvokeInterface;
2669 if (check_jni) {
2670 functions = GetCheckJniInvokeInterface();
2671 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002672}
2673
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002674JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002675 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002676 delete globals_lock;
2677 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002678 delete libraries_lock;
2679 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002680}
2681
Elliott Hughes75770752011-08-24 17:52:38 -07002682bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2683 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002684
2685 // See if we've already loaded this library. If we have, and the class loader
2686 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002687 // TODO: for better results we should canonicalize the pathname (or even compare
2688 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002689 SharedLibrary* library;
2690 {
2691 // TODO: move the locking (and more of this logic) into Libraries.
2692 MutexLock mu(libraries_lock);
2693 library = libraries->Get(path);
2694 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 if (library != NULL) {
2696 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002697 // The library will be associated with class_loader. The JNI
2698 // spec says we can't load the same library into more than one
2699 // class loader.
2700 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2701 "ClassLoader %p; can't open in ClassLoader %p",
2702 path.c_str(), library->GetClassLoader(), class_loader);
2703 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 return false;
2705 }
2706 if (verbose_jni) {
2707 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2708 << "ClassLoader " << class_loader << "]";
2709 }
2710 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002711 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2712 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002713 return false;
2714 }
2715 return true;
2716 }
2717
2718 // Open the shared library. Because we're using a full path, the system
2719 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2720 // resolve this library's dependencies though.)
2721
2722 // Failures here are expected when java.library.path has several entries
2723 // and we have to hunt for the lib.
2724
2725 // The current version of the dynamic linker prints detailed information
2726 // about dlopen() failures. Some things to check if the message is
2727 // cryptic:
2728 // - make sure the library exists on the device
2729 // - verify that the right path is being opened (the debug log message
2730 // above can help with that)
2731 // - check to see if the library is valid (e.g. not zero bytes long)
2732 // - check config/prelink-linux-arm.map to ensure that the library
2733 // is listed and is not being overrun by the previous entry (if
2734 // loading suddenly stops working on a prelinked library, this is
2735 // a good one to check)
2736 // - write a trivial app that calls sleep() then dlopen(), attach
2737 // to it with "strace -p <pid>" while it sleeps, and watch for
2738 // attempts to open nonexistent dependent shared libs
2739
2740 // TODO: automate some of these checks!
2741
2742 // This can execute slowly for a large library on a busy system, so we
2743 // want to switch from RUNNING to VMWAIT while it executes. This allows
2744 // the GC to ignore us.
2745 Thread* self = Thread::Current();
2746 Thread::State old_state = self->GetState();
2747 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2748 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2749 self->SetState(old_state);
2750
2751 if (verbose_jni) {
2752 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2753 }
2754
2755 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002756 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 return false;
2758 }
2759
2760 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002761 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002762 // TODO: move the locking (and more of this logic) into Libraries.
2763 MutexLock mu(libraries_lock);
2764 library = libraries->Get(path);
2765 if (library != NULL) {
2766 LOG(INFO) << "WOW: we lost a race to add shared library: "
2767 << "\"" << path << "\" ClassLoader=" << class_loader;
2768 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002769 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002770 library = new SharedLibrary(path, handle, class_loader);
2771 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002772 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002773
2774 if (verbose_jni) {
2775 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2776 }
2777
2778 bool result = true;
2779 void* sym = dlsym(handle, "JNI_OnLoad");
2780 if (sym == NULL) {
2781 if (verbose_jni) {
2782 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2783 }
2784 } else {
2785 // Call JNI_OnLoad. We have to override the current class
2786 // loader, which will always be "null" since the stuff at the
2787 // top of the stack is around Runtime.loadLibrary(). (See
2788 // the comments in the JNI FindClass function.)
2789 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2790 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002791 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 self->SetClassLoaderOverride(class_loader);
2793
2794 old_state = self->GetState();
2795 self->SetState(Thread::kNative);
2796 if (verbose_jni) {
2797 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2798 }
2799 int version = (*jni_on_load)(this, NULL);
2800 self->SetState(old_state);
2801
2802 self->SetClassLoaderOverride(old_class_loader);;
2803
2804 if (version != JNI_VERSION_1_2 &&
2805 version != JNI_VERSION_1_4 &&
2806 version != JNI_VERSION_1_6) {
2807 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2808 << "bad version: " << version;
2809 // It's unwise to call dlclose() here, but we can mark it
2810 // as bad and ensure that future load attempts will fail.
2811 // We don't know how far JNI_OnLoad got, so there could
2812 // be some partially-initialized stuff accessible through
2813 // newly-registered native method calls. We could try to
2814 // unregister them, but that doesn't seem worthwhile.
2815 result = false;
2816 } else {
2817 if (verbose_jni) {
2818 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2819 << " from JNI_OnLoad in \"" << path << "\"]";
2820 }
2821 }
2822 }
2823
2824 library->SetResult(result);
2825 return result;
2826}
2827
2828void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2829 CHECK(m->IsNative());
2830
2831 Class* c = m->GetDeclaringClass();
2832
2833 // If this is a static method, it could be called before the class
2834 // has been initialized.
2835 if (m->IsStatic()) {
2836 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2837 return NULL;
2838 }
2839 } else {
2840 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2841 }
2842
2843 MutexLock mu(libraries_lock);
2844 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002845}
2846
Ian Rogersdf20fe02011-07-20 20:34:16 -07002847} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002848
2849std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2850 switch (rhs) {
2851 case JNIInvalidRefType:
2852 os << "JNIInvalidRefType";
2853 return os;
2854 case JNILocalRefType:
2855 os << "JNILocalRefType";
2856 return os;
2857 case JNIGlobalRefType:
2858 os << "JNIGlobalRefType";
2859 return os;
2860 case JNIWeakGlobalRefType:
2861 os << "JNIWeakGlobalRefType";
2862 return os;
2863 }
2864}