blob: 5acb155f9cdfa22fe305c1eeba452b2d3ab53077 [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070023#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
26namespace art {
27
Elliott Hughescdf53122011-08-19 15:46:09 -070028// This is private API, but with two different implementations: ARM and x86.
29void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070030
Elliott Hughescdf53122011-08-19 15:46:09 -070031// TODO: this should be in our anonymous namespace, but is currently needed
32// for testing in "jni_internal_test.cc".
Elliott Hughes79082e32011-08-25 12:07:32 -070033void EnsureInvokeStub(Method* method) {
Elliott Hughescdf53122011-08-19 15:46:09 -070034 if (method->GetInvokeStub() != NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -070035 return;
Elliott Hughescdf53122011-08-19 15:46:09 -070036 }
37 // TODO: use signature to find a matching stub
38 // TODO: failed, acquire a lock on the stub table
39 Assembler assembler;
40 CreateInvokeStub(&assembler, method);
41 // TODO: store native_entry in the stub table
42 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
43 size_t length = assembler.CodeSize();
44 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
45 if (addr == MAP_FAILED) {
Elliott Hughesa2501992011-08-26 19:39:54 -070046 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method);
Elliott Hughescdf53122011-08-19 15:46:09 -070047 }
48 MemoryRegion region(addr, length);
49 assembler.FinalizeInstructions(region);
50 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
Elliott Hughescdf53122011-08-19 15:46:09 -070051}
Elliott Hughes0af55432011-08-17 18:37:28 -070052
Elliott Hughescdf53122011-08-19 15:46:09 -070053namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -070054
Elliott Hughesc5f7c912011-08-18 14:00:42 -070055/*
56 * Add a local reference for an object to the current stack frame. When
57 * the native function returns, the reference will be discarded.
58 *
59 * We need to allow the same reference to be added multiple times.
60 *
61 * This will be called on otherwise unreferenced objects. We cannot do
62 * GC allocations here, and it's best if we don't grab a mutex.
63 *
64 * Returns the local reference (currently just the same pointer that was
65 * passed in), or NULL on failure.
66 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070067template<typename T>
68T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 if (obj == NULL) {
70 return NULL;
71 }
72
73 IndirectReferenceTable& locals = ts.Env()->locals;
74
75 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
76 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
82 // TODO: dvmDumpThread(dvmThreadSelf(), false);
83 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
86 if (ts.Env()->check_jni) {
87 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070089 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070091 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 locals.Dump();
93 // TODO: dvmDumpThread(dvmThreadSelf(), false);
94 // dvmAbort();
95 }
96 }
97#endif
98
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070099 if (ts.Env()->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700100 // Hand out direct pointers to support broken old apps.
101 return reinterpret_cast<T>(obj);
102 }
103
104 return reinterpret_cast<T>(ref);
105}
106
Elliott Hughescdf53122011-08-19 15:46:09 -0700107jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
108 if (obj == NULL) {
109 return NULL;
110 }
Elliott Hughes75770752011-08-24 17:52:38 -0700111 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700112 IndirectReferenceTable& weak_globals = vm->weak_globals;
113 MutexLock mu(vm->weak_globals_lock);
114 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
115 return reinterpret_cast<jweak>(ref);
116}
117
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700118template<typename T>
119T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700120 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700121}
122
Elliott Hughescdf53122011-08-19 15:46:09 -0700123Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
124 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
125}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700126
Elliott Hughescdf53122011-08-19 15:46:09 -0700127Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
128 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700129}
130
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700131byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700132 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700133 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700134 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700136 switch (shorty[i]) {
137 case 'Z':
138 case 'B':
139 case 'C':
140 case 'S':
141 case 'I':
142 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
143 offset += 4;
144 break;
145 case 'F':
146 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
147 offset += 4;
148 break;
149 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700150 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
152 offset += sizeof(Object*);
153 break;
154 }
155 case 'D':
156 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
157 offset += 8;
158 break;
159 case 'J':
160 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
161 offset += 8;
162 break;
163 }
164 }
165 return arg_array.release();
166}
167
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700168byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700169 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700170 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700171 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700172 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700173 switch (shorty[i]) {
174 case 'Z':
175 case 'B':
176 case 'C':
177 case 'S':
178 case 'I':
179 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
180 offset += 4;
181 break;
182 case 'F':
183 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
184 offset += 4;
185 break;
186 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700187 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700188 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
189 offset += sizeof(Object*);
190 break;
191 }
192 case 'D':
193 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
194 offset += 8;
195 break;
196 case 'J':
197 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
198 offset += 8;
199 break;
200 }
201 }
202 return arg_array.release();
203}
204
Elliott Hughes72025e52011-08-23 17:50:30 -0700205JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
206 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700207 Thread* self = ts.Self();
208
209 // Push a transition back into managed code onto the linked list in thread
210 CHECK_EQ(Thread::kRunnable, self->GetState());
211 NativeToManagedRecord record;
212 self->PushNativeToManagedRecord(&record);
213
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700214 // Call the invoke stub associated with the method
215 // Pass everything as arguments
216 const Method::InvokeStub* stub = method->GetInvokeStub();
217 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700218
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700219 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700220 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700221 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700222 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700223 LOG(WARNING) << "Not invoking method with no associated code: "
Elliott Hughesa2501992011-08-26 19:39:54 -0700224 << PrettyMethod(method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700225 result.j = 0;
226 }
buzbeec143c552011-08-20 17:38:58 -0700227
Ian Rogers6de08602011-08-19 14:52:39 -0700228 // Pop transition
229 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700230 return result;
231}
232
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700233JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700235 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700236 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700237 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700238 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700239}
240
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700241JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700242 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700243 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700244 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700245 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700246 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
247}
248
Elliott Hughes75770752011-08-24 17:52:38 -0700249Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700250 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700251}
252
Brian Carlstrom30b94452011-08-25 21:35:26 -0700253JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700254 Object* receiver = Decode<Object*>(ts, obj);
255 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700256 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700257 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
258}
259
Brian Carlstrom30b94452011-08-25 21:35:26 -0700260JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700261 Object* receiver = Decode<Object*>(ts, obj);
262 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700263 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700264 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700265}
266
Elliott Hughes6b436852011-08-12 10:16:44 -0700267// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
268// separated with slashes but aren't wrapped with "L;" like regular descriptors
269// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
270// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
271// supported names with dots too (such as "a.b.C").
272std::string NormalizeJniClassDescriptor(const char* name) {
273 std::string result;
274 // Add the missing "L;" if necessary.
275 if (name[0] == '[') {
276 result = name;
277 } else {
278 result += 'L';
279 result += name;
280 result += ';';
281 }
282 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700283 if (result.find('.') != std::string::npos) {
284 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
285 << "\"" << name << "\"";
286 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700287 }
288 return result;
289}
290
Elliott Hughescdf53122011-08-19 15:46:09 -0700291jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
292 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700293 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
294 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700295 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700296
297 Method* method = NULL;
298 if (is_static) {
299 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700301 method = c->FindVirtualMethod(name, sig);
302 if (method == NULL) {
303 // No virtual method matching the signature. Search declared
304 // private methods and constructors.
305 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700306 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700307 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700308
Elliott Hughescdf53122011-08-19 15:46:09 -0700309 if (method == NULL || method->IsStatic() != is_static) {
310 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700311 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700312 // TODO: try searching for the opposite kind of method from is_static
313 // for better diagnostics?
314 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700315 "no %s method %s", is_static ? "static" : "non-static",
316 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700317 return NULL;
318 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700319
Elliott Hughes79082e32011-08-25 12:07:32 -0700320 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700321
Elliott Hughescdf53122011-08-19 15:46:09 -0700322 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700323}
324
Elliott Hughescdf53122011-08-19 15:46:09 -0700325jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
326 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700327 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
328 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700329 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700330
331 Field* field = NULL;
332 if (is_static) {
333 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700337
Elliott Hughescdf53122011-08-19 15:46:09 -0700338 if (field == NULL) {
339 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700340 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700341 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
342 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700343 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700344 return NULL;
345 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 jweak fid = AddWeakGlobalReference(ts, field);
348 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700349}
350
Elliott Hughes75770752011-08-24 17:52:38 -0700351void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
352 JavaVMExt* vm = ts.Vm();
353 MutexLock mu(vm->pins_lock);
354 vm->pin_table.Add(array);
355}
356
357void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
358 JavaVMExt* vm = ts.Vm();
359 MutexLock mu(vm->pins_lock);
360 vm->pin_table.Remove(array);
361}
362
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700363template<typename JniT, typename ArtT>
364JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
365 CHECK_GE(length, 0); // TODO: ReportJniError
366 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700367 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700368}
369
Elliott Hughes75770752011-08-24 17:52:38 -0700370template <typename ArrayT, typename CArrayT, typename ArtArrayT>
371CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
372 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
373 PinPrimitiveArray(ts, array);
374 if (is_copy != NULL) {
375 *is_copy = JNI_FALSE;
376 }
377 return array->GetData();
378}
379
380template <typename ArrayT>
381void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
382 if (mode != JNI_COMMIT) {
383 Array* array = Decode<Array*>(ts, java_array);
384 UnpinPrimitiveArray(ts, array);
385 }
386}
387
Elliott Hughes814e4032011-08-23 12:07:56 -0700388void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
389 std::string type(PrettyType(array));
390 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
391 "%s offset=%d length=%d %s.length=%d",
392 type.c_str(), start, length, identifier, array->GetLength());
393}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700394void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
395 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
396 "offset=%d length=%d string.length()=%d", start, length, array_length);
397}
Elliott Hughes814e4032011-08-23 12:07:56 -0700398
399template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700400void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700401 ArrayT* array = Decode<ArrayT*>(ts, java_array);
402 if (start < 0 || length < 0 || start + length > array->GetLength()) {
403 ThrowAIOOBE(ts, array, start, length, "src");
404 } else {
405 JavaT* data = array->GetData();
406 memcpy(buf, data + start, length * sizeof(JavaT));
407 }
408}
409
410template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700411void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700412 ArrayT* array = Decode<ArrayT*>(ts, java_array);
413 if (start < 0 || length < 0 || start + length > array->GetLength()) {
414 ThrowAIOOBE(ts, array, start, length, "dst");
415 } else {
416 JavaT* data = array->GetData();
417 memcpy(data + start, buf, length * sizeof(JavaT));
418 }
419}
420
Elliott Hughes75770752011-08-24 17:52:38 -0700421jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700422 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
423 CHECK(buffer_class.get() != NULL);
424 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
425}
426
Elliott Hughes75770752011-08-24 17:52:38 -0700427jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700428 static jclass buffer_class = InitDirectByteBufferClass(env);
429 return buffer_class;
430}
431
Elliott Hughes75770752011-08-24 17:52:38 -0700432jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
433 if (vm == NULL || p_env == NULL) {
434 return JNI_ERR;
435 }
436
437 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
438 JavaVMAttachArgs args;
439 if (thr_args == NULL) {
440 // Allow the v1.1 calling convention.
441 args.version = JNI_VERSION_1_2;
442 args.name = NULL;
443 args.group = NULL; // TODO: get "main" thread group
444 } else {
445 args.version = in_args->version;
446 args.name = in_args->name;
447 if (in_args->group != NULL) {
448 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
449 args.group = NULL; // TODO: decode in_args->group
450 } else {
451 args.group = NULL; // TODO: get "main" thread group
452 }
453 }
454 CHECK_GE(args.version, JNI_VERSION_1_2);
455
456 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
457 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
458}
459
Elliott Hughes79082e32011-08-25 12:07:32 -0700460class SharedLibrary {
461 public:
462 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
463 : path_(path),
464 handle_(handle),
465 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
466 jni_on_load_tid_(Thread::Current()->GetId()),
467 jni_on_load_result_(kPending) {
468 pthread_cond_init(&jni_on_load_cond_, NULL);
469 }
470
471 ~SharedLibrary() {
472 delete jni_on_load_lock_;
473 }
474
475 Object* GetClassLoader() {
476 return class_loader_;
477 }
478
479 std::string GetPath() {
480 return path_;
481 }
482
483 /*
484 * Check the result of an earlier call to JNI_OnLoad on this library. If
485 * the call has not yet finished in another thread, wait for it.
486 */
487 bool CheckOnLoadResult(JavaVMExt* vm) {
488 Thread* self = Thread::Current();
489 if (jni_on_load_tid_ == self->GetId()) {
490 // Check this so we don't end up waiting for ourselves. We need
491 // to return "true" so the caller can continue.
492 LOG(INFO) << *self << " recursive attempt to load library "
493 << "\"" << path_ << "\"";
494 return true;
495 }
496
497 MutexLock mu(jni_on_load_lock_);
498 while (jni_on_load_result_ == kPending) {
499 if (vm->verbose_jni) {
500 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
501 << "JNI_OnLoad...]";
502 }
503 Thread::State old_state = self->GetState();
504 self->SetState(Thread::kWaiting); // TODO: VMWAIT
505 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
506 self->SetState(old_state);
507 }
508
509 bool okay = (jni_on_load_result_ == kOkay);
510 if (vm->verbose_jni) {
511 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
512 << (okay ? "succeeded" : "failed") << "]";
513 }
514 return okay;
515 }
516
517 void SetResult(bool result) {
518 jni_on_load_result_ = result ? kOkay : kFailed;
519 jni_on_load_tid_ = 0;
520
521 // Broadcast a wakeup to anybody sleeping on the condition variable.
522 MutexLock mu(jni_on_load_lock_);
523 pthread_cond_broadcast(&jni_on_load_cond_);
524 }
525
526 void* FindSymbol(const std::string& symbol_name) {
527 return dlsym(handle_, symbol_name.c_str());
528 }
529
530 private:
531 enum JNI_OnLoadState {
532 kPending,
533 kFailed,
534 kOkay,
535 };
536
537 // Path to library "/system/lib/libjni.so".
538 std::string path_;
539
540 // The void* returned by dlopen(3).
541 void* handle_;
542
543 // The ClassLoader this library is associated with.
544 Object* class_loader_;
545
546 // Guards remaining items.
547 Mutex* jni_on_load_lock_;
548 // Wait for JNI_OnLoad in other thread.
549 pthread_cond_t jni_on_load_cond_;
550 // Recursive invocation guard.
551 uint32_t jni_on_load_tid_;
552 // Result of earlier JNI_OnLoad call.
553 JNI_OnLoadState jni_on_load_result_;
554};
555
Elliott Hughescdf53122011-08-19 15:46:09 -0700556} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700557
Elliott Hughes79082e32011-08-25 12:07:32 -0700558// This exists mainly to keep implementation details out of the header file.
559class Libraries {
560 public:
561 Libraries() {
562 }
563
564 ~Libraries() {
565 // Delete our map values. (The keys will be cleaned up by the map itself.)
566 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
567 delete it->second;
568 }
569 }
570
571 SharedLibrary* Get(const std::string& path) {
572 return libraries_[path];
573 }
574
575 void Put(const std::string& path, SharedLibrary* library) {
576 libraries_[path] = library;
577 }
578
579 // See section 11.3 "Linking Native Methods" of the JNI spec.
580 void* FindNativeMethod(const Method* m) {
581 std::string jni_short_name(JniShortName(m));
582 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700583 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
585 SharedLibrary* library = it->second;
586 if (library->GetClassLoader() != declaring_class_loader) {
587 // We only search libraries loaded by the appropriate ClassLoader.
588 continue;
589 }
590 // Try the short name then the long name...
591 void* fn = library->FindSymbol(jni_short_name);
592 if (fn == NULL) {
593 fn = library->FindSymbol(jni_long_name);
594 }
595 if (fn != NULL) {
596 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700597 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 << " in \"" << library->GetPath() << "\"]";
599 }
600 return fn;
601 }
602 }
603 std::string detail;
604 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700605 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700606 LOG(ERROR) << detail;
607 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
608 "%s", detail.c_str());
609 return NULL;
610 }
611
612 private:
613 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
614
615 std::map<std::string, SharedLibrary*> libraries_;
616};
617
Elliott Hughescdf53122011-08-19 15:46:09 -0700618class JNI {
619 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700620
Elliott Hughescdf53122011-08-19 15:46:09 -0700621 static jint GetVersion(JNIEnv* env) {
622 ScopedJniThreadState ts(env);
623 return JNI_VERSION_1_6;
624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
627 ScopedJniThreadState ts(env);
628 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700629 return NULL;
630 }
631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jclass FindClass(JNIEnv* env, const char* name) {
633 ScopedJniThreadState ts(env);
634 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
635 std::string descriptor(NormalizeJniClassDescriptor(name));
636 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700637 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700638 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700640 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
643 ScopedJniThreadState ts(env);
644 Method* method = Decode<Method*>(ts, java_method);
645 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
649 ScopedJniThreadState ts(env);
650 Field* field = Decode<Field*>(ts, java_field);
651 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
655 ScopedJniThreadState ts(env);
656 Method* method = DecodeMethod(ts, mid);
657 return AddLocalReference<jobject>(ts, method);
658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
661 ScopedJniThreadState ts(env);
662 Field* field = DecodeField(ts, fid);
663 return AddLocalReference<jobject>(ts, field);
664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughes37f7a402011-08-22 18:56:01 -0700666 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
667 ScopedJniThreadState ts(env);
668 Object* o = Decode<Object*>(ts, java_object);
669 return AddLocalReference<jclass>(ts, o->GetClass());
670 }
671
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700672 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700674 Class* c = Decode<Class*>(ts, java_class);
675 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 Class* c1 = Decode<Class*>(ts, java_class1);
681 Class* c2 = Decode<Class*>(ts, java_class2);
682 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700687 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 if (jobj == NULL) {
689 // NB. JNI is different from regular Java instanceof in this respect
690 return JNI_TRUE;
691 } else {
692 Object* obj = Decode<Object*>(ts, jobj);
693 Class* klass = Decode<Class*>(ts, clazz);
694 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
695 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 static jint Throw(JNIEnv* env, jthrowable java_exception) {
699 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700700 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 if (exception == NULL) {
702 return JNI_ERR;
703 }
704 ts.Self()->SetException(exception);
705 return JNI_OK;
706 }
707
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700708 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700710 // TODO: check for a pending exception to decide what constructor to call.
711 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
712 if (mid == NULL) {
713 return JNI_ERR;
714 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700715 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
716 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 return JNI_ERR;
718 }
719
720 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 args[0].l = s.get();
722 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
723 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724 return JNI_ERR;
725 }
726
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700729 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 return JNI_OK;
732 }
733
734 static jboolean ExceptionCheck(JNIEnv* env) {
735 ScopedJniThreadState ts(env);
736 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
737 }
738
739 static void ExceptionClear(JNIEnv* env) {
740 ScopedJniThreadState ts(env);
741 ts.Self()->ClearException();
742 }
743
744 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700746
747 Thread* self = ts.Self();
748 Throwable* original_exception = self->GetException();
749 self->ClearException();
750
751 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
752 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
753 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
754 if (mid == NULL) {
755 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
756 << PrettyType(original_exception);
757 } else {
758 env->CallVoidMethod(exception.get(), mid);
759 if (self->IsExceptionPending()) {
760 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
761 << " thrown while calling printStackTrace";
762 self->ClearException();
763 }
764 }
765
766 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 static jthrowable ExceptionOccurred(JNIEnv* env) {
770 ScopedJniThreadState ts(env);
771 Object* exception = ts.Self()->GetException();
772 if (exception == NULL) {
773 return NULL;
774 } else {
775 // TODO: if adding a local reference failing causes the VM to abort
776 // then the following check will never occur.
777 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
778 if (localException == NULL) {
779 // We were unable to add a new local reference, and threw a new
780 // exception. We can't return "exception", because it's not a
781 // local reference. So we have to return NULL, indicating that
782 // there was no exception, even though it's pretty much raining
783 // exceptions in here.
784 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
785 }
786 return localException;
787 }
788 }
789
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 static void FatalError(JNIEnv* env, const char* msg) {
791 ScopedJniThreadState ts(env);
792 LOG(FATAL) << "JNI FatalError called: " << msg;
793 }
794
795 static jint PushLocalFrame(JNIEnv* env, jint cap) {
796 ScopedJniThreadState ts(env);
797 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
798 return JNI_OK;
799 }
800
801 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
802 ScopedJniThreadState ts(env);
803 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
804 return res;
805 }
806
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
808 ScopedJniThreadState ts(env);
809 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
810 return 0;
811 }
812
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
814 ScopedJniThreadState ts(env);
815 if (obj == NULL) {
816 return NULL;
817 }
818
Elliott Hughes75770752011-08-24 17:52:38 -0700819 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 IndirectReferenceTable& globals = vm->globals;
821 MutexLock mu(vm->globals_lock);
822 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
823 return reinterpret_cast<jobject>(ref);
824 }
825
826 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
827 ScopedJniThreadState ts(env);
828 if (obj == NULL) {
829 return;
830 }
831
Elliott Hughes75770752011-08-24 17:52:38 -0700832 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 IndirectReferenceTable& globals = vm->globals;
834 MutexLock mu(vm->globals_lock);
835
836 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
837 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
838 << "failed to find entry";
839 }
840 }
841
842 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
843 ScopedJniThreadState ts(env);
844 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
845 }
846
847 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
848 ScopedJniThreadState ts(env);
849 if (obj == NULL) {
850 return;
851 }
852
Elliott Hughes75770752011-08-24 17:52:38 -0700853 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& weak_globals = vm->weak_globals;
855 MutexLock mu(vm->weak_globals_lock);
856
857 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
858 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
859 << "failed to find entry";
860 }
861 }
862
863 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
864 ScopedJniThreadState ts(env);
865 if (obj == NULL) {
866 return NULL;
867 }
868
869 IndirectReferenceTable& locals = ts.Env()->locals;
870
871 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
872 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
873 return reinterpret_cast<jobject>(ref);
874 }
875
876 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
877 ScopedJniThreadState ts(env);
878 if (obj == NULL) {
879 return;
880 }
881
882 IndirectReferenceTable& locals = ts.Env()->locals;
883
884 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
885 if (!locals.Remove(cookie, obj)) {
886 // Attempting to delete a local reference that is not in the
887 // topmost local reference frame is a no-op. DeleteLocalRef returns
888 // void and doesn't throw any exceptions, but we should probably
889 // complain about it so the user will notice that things aren't
890 // going quite the way they expect.
891 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
892 << "failed to find entry";
893 }
894 }
895
896 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
897 ScopedJniThreadState ts(env);
898 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
899 ? JNI_TRUE : JNI_FALSE;
900 }
901
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700904 Class* c = Decode<Class*>(ts, java_class);
905 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
906 return NULL;
907 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700908 return AddLocalReference<jobject>(ts, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 ScopedJniThreadState ts(env);
913 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_start(args, mid);
915 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 va_end(args);
917 return result;
918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700922 Class* c = Decode<Class*>(ts, java_class);
923 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
924 return NULL;
925 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700926 Object* result = c->AllocObject();
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 return local_result;
930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 Class* c = Decode<Class*>(ts, java_class);
935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
936 return NULL;
937 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700938 Object* result = c->AllocObject();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 return local_result;
942 }
943
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
945 ScopedJniThreadState ts(env);
946 return FindMethodID(ts, c, name, sig, false);
947 }
948
949 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
950 ScopedJniThreadState ts(env);
951 return FindMethodID(ts, c, name, sig, true);
952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_list ap;
957 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700958 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_end(ap);
960 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700965 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_list ap;
978 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700979 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 va_end(ap);
981 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700986 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700991 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_list ap;
997 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_end(ap);
1000 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001010 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_end(ap);
1019 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001024 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001029 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001036 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
1038 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001043 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001048 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_list ap;
1054 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001055 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_end(ap);
1057 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001062 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001067 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_list ap;
1073 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001074 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_end(ap);
1076 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001081 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001086 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001093 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_end(ap);
1095 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001100 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001105 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001112 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_end(ap);
1114 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001119 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001124 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001137 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001142 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
1145 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
1148 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_start(ap, mid);
1150 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1152 va_end(ap);
1153 return local_result;
1154 }
1155
1156 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 return AddLocalReference<jobject>(ts, result.l);
1161 }
1162
1163 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 return AddLocalReference<jobject>(ts, result.l);
1168 }
1169
1170 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
1173 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_start(ap, mid);
1175 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
1177 return result.z;
1178 }
1179
1180 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
1192 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
1195 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_start(ap, mid);
1197 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 va_end(ap);
1199 return result.b;
1200 }
1201
1202 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
1217 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_start(ap, mid);
1219 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_end(ap);
1221 return result.c;
1222 }
1223
1224 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
1239 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 va_start(ap, mid);
1241 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_end(ap);
1243 return result.s;
1244 }
1245
1246 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
1252 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
1261 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_start(ap, mid);
1263 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 va_end(ap);
1265 return result.i;
1266 }
1267
1268 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
1283 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 va_start(ap, mid);
1285 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_end(ap);
1287 return result.j;
1288 }
1289
1290 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
1296 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
1305 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 va_start(ap, mid);
1307 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 va_end(ap);
1309 return result.f;
1310 }
1311
1312 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
1318 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
1327 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 va_start(ap, mid);
1329 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 va_end(ap);
1331 return result.d;
1332 }
1333
1334 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
1340 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
1349 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 va_start(ap, mid);
1351 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_end(ap);
1353 }
1354
1355 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
1361 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001364 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static jfieldID GetFieldID(JNIEnv* env,
1368 jclass c, const char* name, const char* sig) {
1369 ScopedJniThreadState ts(env);
1370 return FindFieldID(ts, c, name, sig, false);
1371 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001372
1373
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 static jfieldID GetStaticFieldID(JNIEnv* env,
1375 jclass c, const char* name, const char* sig) {
1376 ScopedJniThreadState ts(env);
1377 return FindFieldID(ts, c, name, sig, true);
1378 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001379
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 Object* o = Decode<Object*>(ts, obj);
1383 Field* f = DecodeField(ts, fid);
1384 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001386
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 Field* f = DecodeField(ts, fid);
1390 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 Object* o = Decode<Object*>(ts, java_object);
1396 Object* v = Decode<Object*>(ts, java_value);
1397 Field* f = DecodeField(ts, fid);
1398 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 Object* v = Decode<Object*>(ts, java_value);
1404 Field* f = DecodeField(ts, fid);
1405 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408#define GET_PRIMITIVE_FIELD(fn, instance) \
1409 ScopedJniThreadState ts(env); \
1410 Object* o = Decode<Object*>(ts, instance); \
1411 Field* f = DecodeField(ts, fid); \
1412 return f->fn(o)
1413
1414#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1415 ScopedJniThreadState ts(env); \
1416 Object* o = Decode<Object*>(ts, instance); \
1417 Field* f = DecodeField(ts, fid); \
1418 f->fn(o, value)
1419
1420 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetLong, NULL);
1474 }
1475
1476 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1478 }
1479
1480 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1482 }
1483
1484 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1485 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1486 }
1487
1488 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1489 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1490 }
1491
1492 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1493 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1494 }
1495
1496 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1497 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1498 }
1499
1500 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1501 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1502 }
1503
1504 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1505 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1506 }
1507
1508 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1509 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1510 }
1511
1512 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1513 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1514 }
1515
1516 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1517 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1518 }
1519
1520 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1521 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1522 }
1523
1524 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1525 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1526 }
1527
1528 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1529 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1530 }
1531
1532 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1533 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1534 }
1535
1536 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1537 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1538 }
1539
1540 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1541 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1542 }
1543
1544 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1545 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
1548 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001549 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 ScopedJniThreadState ts(env);
1551 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001552 va_start(ap, mid);
1553 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1555 va_end(ap);
1556 return local_result;
1557 }
1558
1559 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001562 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 return AddLocalReference<jobject>(ts, result.l);
1564 }
1565
1566 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001569 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 return AddLocalReference<jobject>(ts, result.l);
1571 }
1572
1573 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 ScopedJniThreadState ts(env);
1576 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 va_start(ap, mid);
1578 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 va_end(ap);
1580 return result.z;
1581 }
1582
1583 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
1589 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
1597 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 va_start(ap, mid);
1599 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 va_end(ap);
1601 return result.b;
1602 }
1603
1604 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
1610 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
1618 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 va_start(ap, mid);
1620 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_end(ap);
1622 return result.c;
1623 }
1624
1625 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
1631 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
1639 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 va_start(ap, mid);
1641 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_end(ap);
1643 return result.s;
1644 }
1645
1646 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 }
1651
1652 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 ScopedJniThreadState ts(env);
1660 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 va_start(ap, mid);
1662 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 va_end(ap);
1664 return result.i;
1665 }
1666
1667 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
1673 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
1681 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 va_start(ap, mid);
1683 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 va_end(ap);
1685 return result.j;
1686 }
1687
1688 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
1694 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
1702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
1704 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 va_end(ap);
1706 return result.f;
1707 }
1708
1709 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
1715 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
1723 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 va_start(ap, mid);
1725 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_end(ap);
1727 return result.d;
1728 }
1729
1730 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
1736 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
1744 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 va_start(ap, mid);
1746 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_end(ap);
1748 }
1749
1750 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
1756 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001759 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes814e4032011-08-23 12:07:56 -07001762 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001764 if (chars == NULL && char_count == 0) {
1765 return NULL;
1766 }
1767 String* result = String::AllocFromUtf16(char_count, chars);
1768 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
1771 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1772 ScopedJniThreadState ts(env);
1773 if (utf == NULL) {
1774 return NULL;
1775 }
1776 String* result = String::AllocFromModifiedUtf8(utf);
1777 return AddLocalReference<jstring>(ts, result);
1778 }
1779
Elliott Hughes814e4032011-08-23 12:07:56 -07001780 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1781 ScopedJniThreadState ts(env);
1782 return Decode<String*>(ts, java_string)->GetLength();
1783 }
1784
1785 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1786 ScopedJniThreadState ts(env);
1787 return Decode<String*>(ts, java_string)->GetUtfLength();
1788 }
1789
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001792 String* s = Decode<String*>(ts, java_string);
1793 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1794 ThrowSIOOBE(ts, start, length, s->GetLength());
1795 } else {
1796 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1797 memcpy(buf, chars + start, length * sizeof(jchar));
1798 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001799 }
1800
Elliott Hughesb465ab02011-08-24 11:21:21 -07001801 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001802 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001803 String* s = Decode<String*>(ts, java_string);
1804 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1805 ThrowSIOOBE(ts, start, length, s->GetLength());
1806 } else {
1807 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1808 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1809 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001810 }
1811
Elliott Hughes75770752011-08-24 17:52:38 -07001812 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001813 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001814 String* s = Decode<String*>(ts, java_string);
1815 const CharArray* chars = s->GetCharArray();
1816 PinPrimitiveArray(ts, chars);
1817 if (is_copy != NULL) {
1818 *is_copy = JNI_FALSE;
1819 }
1820 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001821 }
1822
Elliott Hughes75770752011-08-24 17:52:38 -07001823 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001825 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes75770752011-08-24 17:52:38 -07001828 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001830 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes75770752011-08-24 17:52:38 -07001833 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001835 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001840 if (java_string == NULL) {
1841 return NULL;
1842 }
1843 if (is_copy != NULL) {
1844 *is_copy = JNI_TRUE;
1845 }
1846 String* s = Decode<String*>(ts, java_string);
1847 size_t byte_count = s->GetUtfLength();
1848 char* bytes = new char[byte_count + 1];
1849 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001850 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001851 return NULL;
1852 }
1853 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1854 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1855 bytes[byte_count] = '\0';
1856 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001857 }
1858
Elliott Hughes75770752011-08-24 17:52:38 -07001859 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001860 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001861 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001862 }
1863
Elliott Hughesbd935992011-08-22 11:59:34 -07001864 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001866 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001867 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001868 Array* array = obj->AsArray();
1869 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001874 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1875 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
1878 static void SetObjectArrayElement(JNIEnv* env,
1879 jobjectArray java_array, jsize index, jobject java_value) {
1880 ScopedJniThreadState ts(env);
1881 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1882 Object* value = Decode<Object*>(ts, java_value);
1883 array->Set(index, value);
1884 }
1885
1886 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1887 ScopedJniThreadState ts(env);
1888 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1889 }
1890
1891 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1892 ScopedJniThreadState ts(env);
1893 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1894 }
1895
1896 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1897 ScopedJniThreadState ts(env);
1898 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1899 }
1900
1901 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1902 ScopedJniThreadState ts(env);
1903 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1904 }
1905
1906 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1907 ScopedJniThreadState ts(env);
1908 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1909 }
1910
1911 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1912 ScopedJniThreadState ts(env);
1913 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1914 }
1915
1916 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1917 ScopedJniThreadState ts(env);
1918 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1919 }
1920
1921 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1922 ScopedJniThreadState ts(env);
1923 CHECK_GE(length, 0); // TODO: ReportJniError
1924
1925 // Compute the array class corresponding to the given element class.
1926 Class* element_class = Decode<Class*>(ts, element_jclass);
1927 std::string descriptor;
1928 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001929 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001930
1931 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001932 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1933 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 return NULL;
1935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 // Allocate and initialize if necessary.
1938 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 if (initial_element != NULL) {
1941 Object* initial_object = Decode<Object*>(ts, initial_element);
1942 for (jsize i = 0; i < length; ++i) {
1943 result->Set(i, initial_object);
1944 }
1945 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 return AddLocalReference<jobjectArray>(ts, result);
1947 }
1948
1949 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1950 ScopedJniThreadState ts(env);
1951 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001956 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001960 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001961 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002041 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002126 Class* c = Decode<Class*>(ts, java_class);
2127
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 const char* name = methods[i].name;
2130 const char* sig = methods[i].signature;
2131
2132 if (*sig == '!') {
2133 // TODO: fast jni. it's too noisy to log all these.
2134 ++sig;
2135 }
2136
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 Method* m = c->FindDirectMethod(name, sig);
2138 if (m == NULL) {
2139 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2145 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002146 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2152 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002153 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 return JNI_ERR;
2155 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156
Elliott Hughes75770752011-08-24 17:52:38 -07002157 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002158 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 }
2160
2161 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163 return JNI_OK;
2164 }
2165
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 Class* c = Decode<Class*>(ts, java_class);
2169
Elliott Hughes75770752011-08-24 17:52:38 -07002170 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 LOG(INFO) << "[Unregistering JNI native methods for "
2172 << PrettyDescriptor(c->GetDescriptor()) << "]";
2173 }
2174
2175 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2176 Method* m = c->GetDirectMethod(i);
2177 if (m->IsNative()) {
2178 m->UnregisterNative();
2179 }
2180 }
2181 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2182 Method* m = c->GetVirtualMethod(i);
2183 if (m->IsNative()) {
2184 m->UnregisterNative();
2185 }
2186 }
2187
2188 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002193 Decode<Object*>(ts, java_object)->MonitorEnter();
2194 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes72025e52011-08-23 17:50:30 -07002197 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002199 Decode<Object*>(ts, java_object)->MonitorEnter();
2200 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
2203 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2204 ScopedJniThreadState ts(env);
2205 Runtime* runtime = Runtime::Current();
2206 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002207 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 } else {
2209 *vm = NULL;
2210 }
2211 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2212 }
2213
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2215 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002216
2217 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002218 CHECK(address != NULL); // TODO: ReportJniError
2219 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002220
2221 jclass buffer_class = GetDirectByteBufferClass(env);
2222 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2223 if (mid == NULL) {
2224 return NULL;
2225 }
2226
2227 // At the moment, the Java side is limited to 32 bits.
2228 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2229 CHECK_LE(capacity, 0xffffffff);
2230 jint address_arg = reinterpret_cast<jint>(address);
2231 jint capacity_arg = static_cast<jint>(capacity);
2232
2233 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2234 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2240 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2246 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
Elliott Hughes75770752011-08-24 17:52:38 -07002252 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253
2254 // Do we definitely know what kind of reference this is?
2255 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2256 IndirectRefKind kind = GetIndirectRefKind(ref);
2257 switch (kind) {
2258 case kLocal:
2259 return JNILocalRefType;
2260 case kGlobal:
2261 return JNIGlobalRefType;
2262 case kWeakGlobal:
2263 return JNIWeakGlobalRefType;
2264 case kSirtOrInvalid:
2265 // Is it in a stack IRT?
2266 if (ts.Self()->SirtContains(java_object)) {
2267 return JNILocalRefType;
2268 }
2269
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002270 if (!ts.Env()->work_around_app_jni_bugs) {
2271 return JNIInvalidRefType;
2272 }
2273
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 // If we're handing out direct pointers, check whether it's a direct pointer
2275 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002276 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277 if (ts.Env()->locals.Contains(java_object)) {
2278 return JNILocalRefType;
2279 }
2280 }
2281
2282 return JNIInvalidRefType;
2283 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002286
Elliott Hughesa2501992011-08-26 19:39:54 -07002287const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002288 NULL, // reserved0.
2289 NULL, // reserved1.
2290 NULL, // reserved2.
2291 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 JNI::GetVersion,
2293 JNI::DefineClass,
2294 JNI::FindClass,
2295 JNI::FromReflectedMethod,
2296 JNI::FromReflectedField,
2297 JNI::ToReflectedMethod,
2298 JNI::GetSuperclass,
2299 JNI::IsAssignableFrom,
2300 JNI::ToReflectedField,
2301 JNI::Throw,
2302 JNI::ThrowNew,
2303 JNI::ExceptionOccurred,
2304 JNI::ExceptionDescribe,
2305 JNI::ExceptionClear,
2306 JNI::FatalError,
2307 JNI::PushLocalFrame,
2308 JNI::PopLocalFrame,
2309 JNI::NewGlobalRef,
2310 JNI::DeleteGlobalRef,
2311 JNI::DeleteLocalRef,
2312 JNI::IsSameObject,
2313 JNI::NewLocalRef,
2314 JNI::EnsureLocalCapacity,
2315 JNI::AllocObject,
2316 JNI::NewObject,
2317 JNI::NewObjectV,
2318 JNI::NewObjectA,
2319 JNI::GetObjectClass,
2320 JNI::IsInstanceOf,
2321 JNI::GetMethodID,
2322 JNI::CallObjectMethod,
2323 JNI::CallObjectMethodV,
2324 JNI::CallObjectMethodA,
2325 JNI::CallBooleanMethod,
2326 JNI::CallBooleanMethodV,
2327 JNI::CallBooleanMethodA,
2328 JNI::CallByteMethod,
2329 JNI::CallByteMethodV,
2330 JNI::CallByteMethodA,
2331 JNI::CallCharMethod,
2332 JNI::CallCharMethodV,
2333 JNI::CallCharMethodA,
2334 JNI::CallShortMethod,
2335 JNI::CallShortMethodV,
2336 JNI::CallShortMethodA,
2337 JNI::CallIntMethod,
2338 JNI::CallIntMethodV,
2339 JNI::CallIntMethodA,
2340 JNI::CallLongMethod,
2341 JNI::CallLongMethodV,
2342 JNI::CallLongMethodA,
2343 JNI::CallFloatMethod,
2344 JNI::CallFloatMethodV,
2345 JNI::CallFloatMethodA,
2346 JNI::CallDoubleMethod,
2347 JNI::CallDoubleMethodV,
2348 JNI::CallDoubleMethodA,
2349 JNI::CallVoidMethod,
2350 JNI::CallVoidMethodV,
2351 JNI::CallVoidMethodA,
2352 JNI::CallNonvirtualObjectMethod,
2353 JNI::CallNonvirtualObjectMethodV,
2354 JNI::CallNonvirtualObjectMethodA,
2355 JNI::CallNonvirtualBooleanMethod,
2356 JNI::CallNonvirtualBooleanMethodV,
2357 JNI::CallNonvirtualBooleanMethodA,
2358 JNI::CallNonvirtualByteMethod,
2359 JNI::CallNonvirtualByteMethodV,
2360 JNI::CallNonvirtualByteMethodA,
2361 JNI::CallNonvirtualCharMethod,
2362 JNI::CallNonvirtualCharMethodV,
2363 JNI::CallNonvirtualCharMethodA,
2364 JNI::CallNonvirtualShortMethod,
2365 JNI::CallNonvirtualShortMethodV,
2366 JNI::CallNonvirtualShortMethodA,
2367 JNI::CallNonvirtualIntMethod,
2368 JNI::CallNonvirtualIntMethodV,
2369 JNI::CallNonvirtualIntMethodA,
2370 JNI::CallNonvirtualLongMethod,
2371 JNI::CallNonvirtualLongMethodV,
2372 JNI::CallNonvirtualLongMethodA,
2373 JNI::CallNonvirtualFloatMethod,
2374 JNI::CallNonvirtualFloatMethodV,
2375 JNI::CallNonvirtualFloatMethodA,
2376 JNI::CallNonvirtualDoubleMethod,
2377 JNI::CallNonvirtualDoubleMethodV,
2378 JNI::CallNonvirtualDoubleMethodA,
2379 JNI::CallNonvirtualVoidMethod,
2380 JNI::CallNonvirtualVoidMethodV,
2381 JNI::CallNonvirtualVoidMethodA,
2382 JNI::GetFieldID,
2383 JNI::GetObjectField,
2384 JNI::GetBooleanField,
2385 JNI::GetByteField,
2386 JNI::GetCharField,
2387 JNI::GetShortField,
2388 JNI::GetIntField,
2389 JNI::GetLongField,
2390 JNI::GetFloatField,
2391 JNI::GetDoubleField,
2392 JNI::SetObjectField,
2393 JNI::SetBooleanField,
2394 JNI::SetByteField,
2395 JNI::SetCharField,
2396 JNI::SetShortField,
2397 JNI::SetIntField,
2398 JNI::SetLongField,
2399 JNI::SetFloatField,
2400 JNI::SetDoubleField,
2401 JNI::GetStaticMethodID,
2402 JNI::CallStaticObjectMethod,
2403 JNI::CallStaticObjectMethodV,
2404 JNI::CallStaticObjectMethodA,
2405 JNI::CallStaticBooleanMethod,
2406 JNI::CallStaticBooleanMethodV,
2407 JNI::CallStaticBooleanMethodA,
2408 JNI::CallStaticByteMethod,
2409 JNI::CallStaticByteMethodV,
2410 JNI::CallStaticByteMethodA,
2411 JNI::CallStaticCharMethod,
2412 JNI::CallStaticCharMethodV,
2413 JNI::CallStaticCharMethodA,
2414 JNI::CallStaticShortMethod,
2415 JNI::CallStaticShortMethodV,
2416 JNI::CallStaticShortMethodA,
2417 JNI::CallStaticIntMethod,
2418 JNI::CallStaticIntMethodV,
2419 JNI::CallStaticIntMethodA,
2420 JNI::CallStaticLongMethod,
2421 JNI::CallStaticLongMethodV,
2422 JNI::CallStaticLongMethodA,
2423 JNI::CallStaticFloatMethod,
2424 JNI::CallStaticFloatMethodV,
2425 JNI::CallStaticFloatMethodA,
2426 JNI::CallStaticDoubleMethod,
2427 JNI::CallStaticDoubleMethodV,
2428 JNI::CallStaticDoubleMethodA,
2429 JNI::CallStaticVoidMethod,
2430 JNI::CallStaticVoidMethodV,
2431 JNI::CallStaticVoidMethodA,
2432 JNI::GetStaticFieldID,
2433 JNI::GetStaticObjectField,
2434 JNI::GetStaticBooleanField,
2435 JNI::GetStaticByteField,
2436 JNI::GetStaticCharField,
2437 JNI::GetStaticShortField,
2438 JNI::GetStaticIntField,
2439 JNI::GetStaticLongField,
2440 JNI::GetStaticFloatField,
2441 JNI::GetStaticDoubleField,
2442 JNI::SetStaticObjectField,
2443 JNI::SetStaticBooleanField,
2444 JNI::SetStaticByteField,
2445 JNI::SetStaticCharField,
2446 JNI::SetStaticShortField,
2447 JNI::SetStaticIntField,
2448 JNI::SetStaticLongField,
2449 JNI::SetStaticFloatField,
2450 JNI::SetStaticDoubleField,
2451 JNI::NewString,
2452 JNI::GetStringLength,
2453 JNI::GetStringChars,
2454 JNI::ReleaseStringChars,
2455 JNI::NewStringUTF,
2456 JNI::GetStringUTFLength,
2457 JNI::GetStringUTFChars,
2458 JNI::ReleaseStringUTFChars,
2459 JNI::GetArrayLength,
2460 JNI::NewObjectArray,
2461 JNI::GetObjectArrayElement,
2462 JNI::SetObjectArrayElement,
2463 JNI::NewBooleanArray,
2464 JNI::NewByteArray,
2465 JNI::NewCharArray,
2466 JNI::NewShortArray,
2467 JNI::NewIntArray,
2468 JNI::NewLongArray,
2469 JNI::NewFloatArray,
2470 JNI::NewDoubleArray,
2471 JNI::GetBooleanArrayElements,
2472 JNI::GetByteArrayElements,
2473 JNI::GetCharArrayElements,
2474 JNI::GetShortArrayElements,
2475 JNI::GetIntArrayElements,
2476 JNI::GetLongArrayElements,
2477 JNI::GetFloatArrayElements,
2478 JNI::GetDoubleArrayElements,
2479 JNI::ReleaseBooleanArrayElements,
2480 JNI::ReleaseByteArrayElements,
2481 JNI::ReleaseCharArrayElements,
2482 JNI::ReleaseShortArrayElements,
2483 JNI::ReleaseIntArrayElements,
2484 JNI::ReleaseLongArrayElements,
2485 JNI::ReleaseFloatArrayElements,
2486 JNI::ReleaseDoubleArrayElements,
2487 JNI::GetBooleanArrayRegion,
2488 JNI::GetByteArrayRegion,
2489 JNI::GetCharArrayRegion,
2490 JNI::GetShortArrayRegion,
2491 JNI::GetIntArrayRegion,
2492 JNI::GetLongArrayRegion,
2493 JNI::GetFloatArrayRegion,
2494 JNI::GetDoubleArrayRegion,
2495 JNI::SetBooleanArrayRegion,
2496 JNI::SetByteArrayRegion,
2497 JNI::SetCharArrayRegion,
2498 JNI::SetShortArrayRegion,
2499 JNI::SetIntArrayRegion,
2500 JNI::SetLongArrayRegion,
2501 JNI::SetFloatArrayRegion,
2502 JNI::SetDoubleArrayRegion,
2503 JNI::RegisterNatives,
2504 JNI::UnregisterNatives,
2505 JNI::MonitorEnter,
2506 JNI::MonitorExit,
2507 JNI::GetJavaVM,
2508 JNI::GetStringRegion,
2509 JNI::GetStringUTFRegion,
2510 JNI::GetPrimitiveArrayCritical,
2511 JNI::ReleasePrimitiveArrayCritical,
2512 JNI::GetStringCritical,
2513 JNI::ReleaseStringCritical,
2514 JNI::NewWeakGlobalRef,
2515 JNI::DeleteWeakGlobalRef,
2516 JNI::ExceptionCheck,
2517 JNI::NewDirectByteBuffer,
2518 JNI::GetDirectBufferAddress,
2519 JNI::GetDirectBufferCapacity,
2520 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002521};
2522
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002523static const size_t kMonitorsInitial = 32; // Arbitrary.
2524static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2525
2526static const size_t kLocalsInitial = 64; // Arbitrary.
2527static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002528
Elliott Hughes75770752011-08-24 17:52:38 -07002529JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002530 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002531 vm(vm),
2532 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002533 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002534 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002535 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2536 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002537 functions = unchecked_functions = &gNativeInterface;
2538 if (check_jni) {
2539 functions = GetCheckJniNativeInterface();
2540 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002541}
2542
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002543JNIEnvExt::~JNIEnvExt() {
2544}
2545
Carl Shapiroea4dca82011-08-01 13:45:38 -07002546// JNI Invocation interface.
2547
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002548extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2549 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2550 if (args->version < JNI_VERSION_1_2) {
2551 return JNI_EVERSION;
2552 }
2553 Runtime::Options options;
2554 for (int i = 0; i < args->nOptions; ++i) {
2555 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002556 options.push_back(std::make_pair(StringPiece(option->optionString),
2557 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558 }
2559 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002560 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 if (runtime == NULL) {
2562 return JNI_ERR;
2563 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002564 *p_env = Thread::Current()->GetJniEnv();
2565 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566 return JNI_OK;
2567 }
2568}
2569
Elliott Hughesf2682d52011-08-15 16:37:04 -07002570extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002571 Runtime* runtime = Runtime::Current();
2572 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002573 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002574 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002575 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002576 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002577 }
2578 return JNI_OK;
2579}
2580
2581// Historically unsupported.
2582extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2583 return JNI_ERR;
2584}
2585
Elliott Hughescdf53122011-08-19 15:46:09 -07002586class JII {
2587 public:
2588 static jint DestroyJavaVM(JavaVM* vm) {
2589 if (vm == NULL) {
2590 return JNI_ERR;
2591 } else {
2592 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2593 delete raw_vm->runtime;
2594 return JNI_OK;
2595 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002599 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002600 }
2601
2602 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002603 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002604 }
2605
2606 static jint DetachCurrentThread(JavaVM* vm) {
2607 if (vm == NULL) {
2608 return JNI_ERR;
2609 } else {
2610 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2611 Runtime* runtime = raw_vm->runtime;
2612 runtime->DetachCurrentThread();
2613 return JNI_OK;
2614 }
2615 }
2616
2617 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2618 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2619 return JNI_EVERSION;
2620 }
2621 if (vm == NULL || env == NULL) {
2622 return JNI_ERR;
2623 }
2624 Thread* thread = Thread::Current();
2625 if (thread == NULL) {
2626 *env = NULL;
2627 return JNI_EDETACHED;
2628 }
2629 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002630 return JNI_OK;
2631 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002632};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633
Elliott Hughesa2501992011-08-26 19:39:54 -07002634const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002635 NULL, // reserved0
2636 NULL, // reserved1
2637 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002638 JII::DestroyJavaVM,
2639 JII::AttachCurrentThread,
2640 JII::DetachCurrentThread,
2641 JII::GetEnv,
2642 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002643};
2644
Elliott Hughesbbd76712011-08-17 10:25:24 -07002645static const size_t kPinTableInitialSize = 16;
2646static const size_t kPinTableMaxSize = 1024;
2647
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002648static const size_t kGlobalsInitial = 512; // Arbitrary.
2649static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2650
2651static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2652static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2653
Elliott Hughes0af55432011-08-17 18:37:28 -07002654JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002655 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002656 check_jni_abort_hook(NULL),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002657 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002658 verbose_jni(verbose_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002659 force_copy(false), // TODO: add a way to enable this
2660 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002661 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002662 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002663 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002664 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002665 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002666 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2667 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2668 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002669 functions = unchecked_functions = &gInvokeInterface;
2670 if (check_jni) {
2671 functions = GetCheckJniInvokeInterface();
2672 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002673}
2674
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002675JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002676 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002677 delete globals_lock;
2678 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002679 delete libraries_lock;
2680 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002681}
2682
Elliott Hughes75770752011-08-24 17:52:38 -07002683bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2684 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002685
2686 // See if we've already loaded this library. If we have, and the class loader
2687 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002688 // TODO: for better results we should canonicalize the pathname (or even compare
2689 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002690 SharedLibrary* library;
2691 {
2692 // TODO: move the locking (and more of this logic) into Libraries.
2693 MutexLock mu(libraries_lock);
2694 library = libraries->Get(path);
2695 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 if (library != NULL) {
2697 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002698 // The library will be associated with class_loader. The JNI
2699 // spec says we can't load the same library into more than one
2700 // class loader.
2701 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2702 "ClassLoader %p; can't open in ClassLoader %p",
2703 path.c_str(), library->GetClassLoader(), class_loader);
2704 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 return false;
2706 }
2707 if (verbose_jni) {
2708 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2709 << "ClassLoader " << class_loader << "]";
2710 }
2711 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002712 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2713 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002714 return false;
2715 }
2716 return true;
2717 }
2718
2719 // Open the shared library. Because we're using a full path, the system
2720 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2721 // resolve this library's dependencies though.)
2722
2723 // Failures here are expected when java.library.path has several entries
2724 // and we have to hunt for the lib.
2725
2726 // The current version of the dynamic linker prints detailed information
2727 // about dlopen() failures. Some things to check if the message is
2728 // cryptic:
2729 // - make sure the library exists on the device
2730 // - verify that the right path is being opened (the debug log message
2731 // above can help with that)
2732 // - check to see if the library is valid (e.g. not zero bytes long)
2733 // - check config/prelink-linux-arm.map to ensure that the library
2734 // is listed and is not being overrun by the previous entry (if
2735 // loading suddenly stops working on a prelinked library, this is
2736 // a good one to check)
2737 // - write a trivial app that calls sleep() then dlopen(), attach
2738 // to it with "strace -p <pid>" while it sleeps, and watch for
2739 // attempts to open nonexistent dependent shared libs
2740
2741 // TODO: automate some of these checks!
2742
2743 // This can execute slowly for a large library on a busy system, so we
2744 // want to switch from RUNNING to VMWAIT while it executes. This allows
2745 // the GC to ignore us.
2746 Thread* self = Thread::Current();
2747 Thread::State old_state = self->GetState();
2748 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2749 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2750 self->SetState(old_state);
2751
2752 if (verbose_jni) {
2753 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2754 }
2755
2756 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002758 return false;
2759 }
2760
2761 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002762 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 // TODO: move the locking (and more of this logic) into Libraries.
2764 MutexLock mu(libraries_lock);
2765 library = libraries->Get(path);
2766 if (library != NULL) {
2767 LOG(INFO) << "WOW: we lost a race to add shared library: "
2768 << "\"" << path << "\" ClassLoader=" << class_loader;
2769 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 library = new SharedLibrary(path, handle, class_loader);
2772 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002773 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002774
2775 if (verbose_jni) {
2776 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2777 }
2778
2779 bool result = true;
2780 void* sym = dlsym(handle, "JNI_OnLoad");
2781 if (sym == NULL) {
2782 if (verbose_jni) {
2783 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2784 }
2785 } else {
2786 // Call JNI_OnLoad. We have to override the current class
2787 // loader, which will always be "null" since the stuff at the
2788 // top of the stack is around Runtime.loadLibrary(). (See
2789 // the comments in the JNI FindClass function.)
2790 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2791 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002792 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 self->SetClassLoaderOverride(class_loader);
2794
2795 old_state = self->GetState();
2796 self->SetState(Thread::kNative);
2797 if (verbose_jni) {
2798 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2799 }
2800 int version = (*jni_on_load)(this, NULL);
2801 self->SetState(old_state);
2802
2803 self->SetClassLoaderOverride(old_class_loader);;
2804
2805 if (version != JNI_VERSION_1_2 &&
2806 version != JNI_VERSION_1_4 &&
2807 version != JNI_VERSION_1_6) {
2808 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2809 << "bad version: " << version;
2810 // It's unwise to call dlclose() here, but we can mark it
2811 // as bad and ensure that future load attempts will fail.
2812 // We don't know how far JNI_OnLoad got, so there could
2813 // be some partially-initialized stuff accessible through
2814 // newly-registered native method calls. We could try to
2815 // unregister them, but that doesn't seem worthwhile.
2816 result = false;
2817 } else {
2818 if (verbose_jni) {
2819 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2820 << " from JNI_OnLoad in \"" << path << "\"]";
2821 }
2822 }
2823 }
2824
2825 library->SetResult(result);
2826 return result;
2827}
2828
2829void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2830 CHECK(m->IsNative());
2831
2832 Class* c = m->GetDeclaringClass();
2833
2834 // If this is a static method, it could be called before the class
2835 // has been initialized.
2836 if (m->IsStatic()) {
2837 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2838 return NULL;
2839 }
2840 } else {
2841 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2842 }
2843
2844 MutexLock mu(libraries_lock);
2845 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002846}
2847
Ian Rogersdf20fe02011-07-20 20:34:16 -07002848} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002849
2850std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2851 switch (rhs) {
2852 case JNIInvalidRefType:
2853 os << "JNIInvalidRefType";
2854 return os;
2855 case JNILocalRefType:
2856 os << "JNILocalRefType";
2857 return os;
2858 case JNIGlobalRefType:
2859 os << "JNIGlobalRefType";
2860 return os;
2861 case JNIWeakGlobalRefType:
2862 os << "JNIWeakGlobalRefType";
2863 return os;
2864 }
2865}