blob: 8fb1618fde66618eee5ee15818579b45a0830b22 [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 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700503 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 }
506
507 bool okay = (jni_on_load_result_ == kOkay);
508 if (vm->verbose_jni) {
509 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
510 << (okay ? "succeeded" : "failed") << "]";
511 }
512 return okay;
513 }
514
515 void SetResult(bool result) {
516 jni_on_load_result_ = result ? kOkay : kFailed;
517 jni_on_load_tid_ = 0;
518
519 // Broadcast a wakeup to anybody sleeping on the condition variable.
520 MutexLock mu(jni_on_load_lock_);
521 pthread_cond_broadcast(&jni_on_load_cond_);
522 }
523
524 void* FindSymbol(const std::string& symbol_name) {
525 return dlsym(handle_, symbol_name.c_str());
526 }
527
528 private:
529 enum JNI_OnLoadState {
530 kPending,
531 kFailed,
532 kOkay,
533 };
534
535 // Path to library "/system/lib/libjni.so".
536 std::string path_;
537
538 // The void* returned by dlopen(3).
539 void* handle_;
540
541 // The ClassLoader this library is associated with.
542 Object* class_loader_;
543
544 // Guards remaining items.
545 Mutex* jni_on_load_lock_;
546 // Wait for JNI_OnLoad in other thread.
547 pthread_cond_t jni_on_load_cond_;
548 // Recursive invocation guard.
549 uint32_t jni_on_load_tid_;
550 // Result of earlier JNI_OnLoad call.
551 JNI_OnLoadState jni_on_load_result_;
552};
553
Elliott Hughescdf53122011-08-19 15:46:09 -0700554} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700555
Elliott Hughes79082e32011-08-25 12:07:32 -0700556// This exists mainly to keep implementation details out of the header file.
557class Libraries {
558 public:
559 Libraries() {
560 }
561
562 ~Libraries() {
563 // Delete our map values. (The keys will be cleaned up by the map itself.)
564 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
565 delete it->second;
566 }
567 }
568
569 SharedLibrary* Get(const std::string& path) {
570 return libraries_[path];
571 }
572
573 void Put(const std::string& path, SharedLibrary* library) {
574 libraries_[path] = library;
575 }
576
577 // See section 11.3 "Linking Native Methods" of the JNI spec.
578 void* FindNativeMethod(const Method* m) {
579 std::string jni_short_name(JniShortName(m));
580 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700581 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
583 SharedLibrary* library = it->second;
584 if (library->GetClassLoader() != declaring_class_loader) {
585 // We only search libraries loaded by the appropriate ClassLoader.
586 continue;
587 }
588 // Try the short name then the long name...
589 void* fn = library->FindSymbol(jni_short_name);
590 if (fn == NULL) {
591 fn = library->FindSymbol(jni_long_name);
592 }
593 if (fn != NULL) {
594 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700595 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 << " in \"" << library->GetPath() << "\"]";
597 }
598 return fn;
599 }
600 }
601 std::string detail;
602 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700603 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 LOG(ERROR) << detail;
605 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
606 "%s", detail.c_str());
607 return NULL;
608 }
609
610 private:
611 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
612
613 std::map<std::string, SharedLibrary*> libraries_;
614};
615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616class JNI {
617 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jint GetVersion(JNIEnv* env) {
620 ScopedJniThreadState ts(env);
621 return JNI_VERSION_1_6;
622 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
625 ScopedJniThreadState ts(env);
626 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700627 return NULL;
628 }
629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jclass FindClass(JNIEnv* env, const char* name) {
631 ScopedJniThreadState ts(env);
632 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
633 std::string descriptor(NormalizeJniClassDescriptor(name));
634 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700635 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700636 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
641 ScopedJniThreadState ts(env);
642 Method* method = Decode<Method*>(ts, java_method);
643 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
647 ScopedJniThreadState ts(env);
648 Field* field = Decode<Field*>(ts, java_field);
649 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
653 ScopedJniThreadState ts(env);
654 Method* method = DecodeMethod(ts, mid);
655 return AddLocalReference<jobject>(ts, method);
656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
659 ScopedJniThreadState ts(env);
660 Field* field = DecodeField(ts, fid);
661 return AddLocalReference<jobject>(ts, field);
662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
665 ScopedJniThreadState ts(env);
666 Object* o = Decode<Object*>(ts, java_object);
667 return AddLocalReference<jclass>(ts, o->GetClass());
668 }
669
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700670 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700672 Class* c = Decode<Class*>(ts, java_class);
673 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 Class* c1 = Decode<Class*>(ts, java_class1);
679 Class* c2 = Decode<Class*>(ts, java_class2);
680 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700685 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 if (jobj == NULL) {
687 // NB. JNI is different from regular Java instanceof in this respect
688 return JNI_TRUE;
689 } else {
690 Object* obj = Decode<Object*>(ts, jobj);
691 Class* klass = Decode<Class*>(ts, clazz);
692 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
693 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 static jint Throw(JNIEnv* env, jthrowable java_exception) {
697 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700698 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 if (exception == NULL) {
700 return JNI_ERR;
701 }
702 ts.Self()->SetException(exception);
703 return JNI_OK;
704 }
705
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700706 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700707 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700708 // TODO: check for a pending exception to decide what constructor to call.
709 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
710 if (mid == NULL) {
711 return JNI_ERR;
712 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
714 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 return JNI_ERR;
716 }
717
718 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 args[0].l = s.get();
720 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
721 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700722 return JNI_ERR;
723 }
724
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700726 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 return JNI_OK;
730 }
731
732 static jboolean ExceptionCheck(JNIEnv* env) {
733 ScopedJniThreadState ts(env);
734 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
735 }
736
737 static void ExceptionClear(JNIEnv* env) {
738 ScopedJniThreadState ts(env);
739 ts.Self()->ClearException();
740 }
741
742 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700744
745 Thread* self = ts.Self();
746 Throwable* original_exception = self->GetException();
747 self->ClearException();
748
749 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
750 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
751 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
752 if (mid == NULL) {
753 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
754 << PrettyType(original_exception);
755 } else {
756 env->CallVoidMethod(exception.get(), mid);
757 if (self->IsExceptionPending()) {
758 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
759 << " thrown while calling printStackTrace";
760 self->ClearException();
761 }
762 }
763
764 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 static jthrowable ExceptionOccurred(JNIEnv* env) {
768 ScopedJniThreadState ts(env);
769 Object* exception = ts.Self()->GetException();
770 if (exception == NULL) {
771 return NULL;
772 } else {
773 // TODO: if adding a local reference failing causes the VM to abort
774 // then the following check will never occur.
775 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
776 if (localException == NULL) {
777 // We were unable to add a new local reference, and threw a new
778 // exception. We can't return "exception", because it's not a
779 // local reference. So we have to return NULL, indicating that
780 // there was no exception, even though it's pretty much raining
781 // exceptions in here.
782 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
783 }
784 return localException;
785 }
786 }
787
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 static void FatalError(JNIEnv* env, const char* msg) {
789 ScopedJniThreadState ts(env);
790 LOG(FATAL) << "JNI FatalError called: " << msg;
791 }
792
793 static jint PushLocalFrame(JNIEnv* env, jint cap) {
794 ScopedJniThreadState ts(env);
795 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
796 return JNI_OK;
797 }
798
799 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
800 ScopedJniThreadState ts(env);
801 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
802 return res;
803 }
804
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
806 ScopedJniThreadState ts(env);
807 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
808 return 0;
809 }
810
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
812 ScopedJniThreadState ts(env);
813 if (obj == NULL) {
814 return NULL;
815 }
816
Elliott Hughes75770752011-08-24 17:52:38 -0700817 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 IndirectReferenceTable& globals = vm->globals;
819 MutexLock mu(vm->globals_lock);
820 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
821 return reinterpret_cast<jobject>(ref);
822 }
823
824 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
825 ScopedJniThreadState ts(env);
826 if (obj == NULL) {
827 return;
828 }
829
Elliott Hughes75770752011-08-24 17:52:38 -0700830 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 IndirectReferenceTable& globals = vm->globals;
832 MutexLock mu(vm->globals_lock);
833
834 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
835 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
836 << "failed to find entry";
837 }
838 }
839
840 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
841 ScopedJniThreadState ts(env);
842 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
843 }
844
845 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
846 ScopedJniThreadState ts(env);
847 if (obj == NULL) {
848 return;
849 }
850
Elliott Hughes75770752011-08-24 17:52:38 -0700851 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 IndirectReferenceTable& weak_globals = vm->weak_globals;
853 MutexLock mu(vm->weak_globals_lock);
854
855 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
856 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
857 << "failed to find entry";
858 }
859 }
860
861 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
862 ScopedJniThreadState ts(env);
863 if (obj == NULL) {
864 return NULL;
865 }
866
867 IndirectReferenceTable& locals = ts.Env()->locals;
868
869 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
870 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
871 return reinterpret_cast<jobject>(ref);
872 }
873
874 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
875 ScopedJniThreadState ts(env);
876 if (obj == NULL) {
877 return;
878 }
879
880 IndirectReferenceTable& locals = ts.Env()->locals;
881
882 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
883 if (!locals.Remove(cookie, obj)) {
884 // Attempting to delete a local reference that is not in the
885 // topmost local reference frame is a no-op. DeleteLocalRef returns
886 // void and doesn't throw any exceptions, but we should probably
887 // complain about it so the user will notice that things aren't
888 // going quite the way they expect.
889 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
890 << "failed to find entry";
891 }
892 }
893
894 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
895 ScopedJniThreadState ts(env);
896 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
897 ? JNI_TRUE : JNI_FALSE;
898 }
899
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700900 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 Class* c = Decode<Class*>(ts, java_class);
903 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
904 return NULL;
905 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700906 return AddLocalReference<jobject>(ts, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 ScopedJniThreadState ts(env);
911 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 va_start(args, mid);
913 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 va_end(args);
915 return result;
916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700920 Class* c = Decode<Class*>(ts, java_class);
921 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
922 return NULL;
923 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700924 Object* result = c->AllocObject();
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 return local_result;
928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 Class* c = Decode<Class*>(ts, java_class);
933 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
934 return NULL;
935 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700936 Object* result = c->AllocObject();
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 return local_result;
940 }
941
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
943 ScopedJniThreadState ts(env);
944 return FindMethodID(ts, c, name, sig, false);
945 }
946
947 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
948 ScopedJniThreadState ts(env);
949 return FindMethodID(ts, c, name, sig, true);
950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 va_list ap;
955 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700956 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_end(ap);
958 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700963 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700969 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_list ap;
976 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700977 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_end(ap);
979 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700984 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700989 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_list ap;
995 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700996 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
998 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001003 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001008 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001015 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_end(ap);
1017 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001022 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001027 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_list ap;
1033 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001034 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_end(ap);
1036 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001041 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001046 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_list ap;
1052 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001053 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_end(ap);
1055 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001060 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001065 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001072 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
1074 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001084 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001091 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_end(ap);
1093 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001103 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_list ap;
1109 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001110 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_end(ap);
1112 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001117 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001122 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001129 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001135 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001140 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
1143 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
1146 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_start(ap, mid);
1148 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1150 va_end(ap);
1151 return local_result;
1152 }
1153
1154 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 return AddLocalReference<jobject>(ts, result.l);
1159 }
1160
1161 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 return AddLocalReference<jobject>(ts, result.l);
1166 }
1167
1168 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
1171 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_start(ap, mid);
1173 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 va_end(ap);
1175 return result.z;
1176 }
1177
1178 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
1184 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
1190 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
1193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
1195 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 va_end(ap);
1197 return result.b;
1198 }
1199
1200 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
1206 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
1215 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 va_start(ap, mid);
1217 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
1219 return result.c;
1220 }
1221
1222 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
1237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
1239 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_end(ap);
1241 return result.s;
1242 }
1243
1244 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
1250 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
1259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
1261 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 return result.i;
1264 }
1265
1266 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
1281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
1283 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
1285 return result.j;
1286 }
1287
1288 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
1303 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 va_start(ap, mid);
1305 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
1307 return result.f;
1308 }
1309
1310 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
1325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
1327 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 va_end(ap);
1329 return result.d;
1330 }
1331
1332 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
1338 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
1347 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 va_start(ap, mid);
1349 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 va_end(ap);
1351 }
1352
1353 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jfieldID GetFieldID(JNIEnv* env,
1366 jclass c, const char* name, const char* sig) {
1367 ScopedJniThreadState ts(env);
1368 return FindFieldID(ts, c, name, sig, false);
1369 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001370
1371
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 static jfieldID GetStaticFieldID(JNIEnv* env,
1373 jclass c, const char* name, const char* sig) {
1374 ScopedJniThreadState ts(env);
1375 return FindFieldID(ts, c, name, sig, true);
1376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001377
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 Object* o = Decode<Object*>(ts, obj);
1381 Field* f = DecodeField(ts, fid);
1382 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001384
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 Field* f = DecodeField(ts, fid);
1388 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 Object* o = Decode<Object*>(ts, java_object);
1394 Object* v = Decode<Object*>(ts, java_value);
1395 Field* f = DecodeField(ts, fid);
1396 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 Object* v = Decode<Object*>(ts, java_value);
1402 Field* f = DecodeField(ts, fid);
1403 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406#define GET_PRIMITIVE_FIELD(fn, instance) \
1407 ScopedJniThreadState ts(env); \
1408 Object* o = Decode<Object*>(ts, instance); \
1409 Field* f = DecodeField(ts, fid); \
1410 return f->fn(o)
1411
1412#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1413 ScopedJniThreadState ts(env); \
1414 Object* o = Decode<Object*>(ts, instance); \
1415 Field* f = DecodeField(ts, fid); \
1416 f->fn(o, value)
1417
1418 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetLong, NULL);
1472 }
1473
1474 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1476 }
1477
1478 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1480 }
1481
1482 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1483 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1484 }
1485
1486 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1487 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1488 }
1489
1490 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1491 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1492 }
1493
1494 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1495 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1496 }
1497
1498 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1499 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1500 }
1501
1502 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1503 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1504 }
1505
1506 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1507 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1508 }
1509
1510 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1511 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1512 }
1513
1514 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1515 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1516 }
1517
1518 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1519 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1520 }
1521
1522 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1523 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1524 }
1525
1526 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1527 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1528 }
1529
1530 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1531 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1532 }
1533
1534 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1535 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1536 }
1537
1538 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1539 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1540 }
1541
1542 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1543 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
1546 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 ScopedJniThreadState ts(env);
1549 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 va_start(ap, mid);
1551 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1553 va_end(ap);
1554 return local_result;
1555 }
1556
1557 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 return AddLocalReference<jobject>(ts, result.l);
1562 }
1563
1564 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 return AddLocalReference<jobject>(ts, result.l);
1569 }
1570
1571 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 ScopedJniThreadState ts(env);
1574 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 va_start(ap, mid);
1576 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 va_end(ap);
1578 return result.z;
1579 }
1580
1581 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
1587 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 ScopedJniThreadState ts(env);
1595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
1597 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_end(ap);
1599 return result.b;
1600 }
1601
1602 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
1608 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 ScopedJniThreadState ts(env);
1616 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 va_start(ap, mid);
1618 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_end(ap);
1620 return result.c;
1621 }
1622
1623 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
1629 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes72025e52011-08-23 17:50:30 -07001635 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
1637 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 va_start(ap, mid);
1639 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 va_end(ap);
1641 return result.s;
1642 }
1643
1644 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
1650 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
1658 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 va_start(ap, mid);
1660 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_end(ap);
1662 return result.i;
1663 }
1664
1665 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
1671 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
1679 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 va_start(ap, mid);
1681 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 va_end(ap);
1683 return result.j;
1684 }
1685
1686 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
1692 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
1700 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 va_start(ap, mid);
1702 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
1704 return result.f;
1705 }
1706
1707 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
1713 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
1721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
1723 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
1725 return result.d;
1726 }
1727
1728 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
1734 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
1742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
1744 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
1746 }
1747
1748 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
1754 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes814e4032011-08-23 12:07:56 -07001760 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001762 if (chars == NULL && char_count == 0) {
1763 return NULL;
1764 }
1765 String* result = String::AllocFromUtf16(char_count, chars);
1766 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
1769 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1770 ScopedJniThreadState ts(env);
1771 if (utf == NULL) {
1772 return NULL;
1773 }
1774 String* result = String::AllocFromModifiedUtf8(utf);
1775 return AddLocalReference<jstring>(ts, result);
1776 }
1777
Elliott Hughes814e4032011-08-23 12:07:56 -07001778 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1779 ScopedJniThreadState ts(env);
1780 return Decode<String*>(ts, java_string)->GetLength();
1781 }
1782
1783 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1784 ScopedJniThreadState ts(env);
1785 return Decode<String*>(ts, java_string)->GetUtfLength();
1786 }
1787
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 String* s = Decode<String*>(ts, java_string);
1791 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1792 ThrowSIOOBE(ts, start, length, s->GetLength());
1793 } else {
1794 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1795 memcpy(buf, chars + start, length * sizeof(jchar));
1796 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 }
1798
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001801 String* s = Decode<String*>(ts, java_string);
1802 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1803 ThrowSIOOBE(ts, start, length, s->GetLength());
1804 } else {
1805 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1806 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1807 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001812 String* s = Decode<String*>(ts, java_string);
1813 const CharArray* chars = s->GetCharArray();
1814 PinPrimitiveArray(ts, chars);
1815 if (is_copy != NULL) {
1816 *is_copy = JNI_FALSE;
1817 }
1818 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 }
1820
Elliott Hughes75770752011-08-24 17:52:38 -07001821 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001822 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001823 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes75770752011-08-24 17:52:38 -07001826 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001828 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes75770752011-08-24 17:52:38 -07001831 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001833 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes75770752011-08-24 17:52:38 -07001836 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001837 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001838 if (java_string == NULL) {
1839 return NULL;
1840 }
1841 if (is_copy != NULL) {
1842 *is_copy = JNI_TRUE;
1843 }
1844 String* s = Decode<String*>(ts, java_string);
1845 size_t byte_count = s->GetUtfLength();
1846 char* bytes = new char[byte_count + 1];
1847 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001848 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001849 return NULL;
1850 }
1851 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1852 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1853 bytes[byte_count] = '\0';
1854 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001855 }
1856
Elliott Hughes75770752011-08-24 17:52:38 -07001857 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001859 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001860 }
1861
Elliott Hughesbd935992011-08-22 11:59:34 -07001862 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001864 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001865 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001866 Array* array = obj->AsArray();
1867 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1873 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
1876 static void SetObjectArrayElement(JNIEnv* env,
1877 jobjectArray java_array, jsize index, jobject java_value) {
1878 ScopedJniThreadState ts(env);
1879 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1880 Object* value = Decode<Object*>(ts, java_value);
1881 array->Set(index, value);
1882 }
1883
1884 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1885 ScopedJniThreadState ts(env);
1886 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1887 }
1888
1889 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1890 ScopedJniThreadState ts(env);
1891 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1892 }
1893
1894 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1895 ScopedJniThreadState ts(env);
1896 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1897 }
1898
1899 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1900 ScopedJniThreadState ts(env);
1901 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1902 }
1903
1904 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1905 ScopedJniThreadState ts(env);
1906 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1907 }
1908
1909 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1910 ScopedJniThreadState ts(env);
1911 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1912 }
1913
1914 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1915 ScopedJniThreadState ts(env);
1916 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1917 }
1918
1919 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1920 ScopedJniThreadState ts(env);
1921 CHECK_GE(length, 0); // TODO: ReportJniError
1922
1923 // Compute the array class corresponding to the given element class.
1924 Class* element_class = Decode<Class*>(ts, element_jclass);
1925 std::string descriptor;
1926 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001927 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001928
1929 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001930 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1931 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 return NULL;
1933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 // Allocate and initialize if necessary.
1936 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001938 if (initial_element != NULL) {
1939 Object* initial_object = Decode<Object*>(ts, initial_element);
1940 for (jsize i = 0; i < length; ++i) {
1941 result->Set(i, initial_object);
1942 }
1943 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 return AddLocalReference<jobjectArray>(ts, result);
1945 }
1946
1947 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1948 ScopedJniThreadState ts(env);
1949 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001959 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001960 }
1961
Elliott Hughes75770752011-08-24 17:52:38 -07001962 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001964 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes75770752011-08-24 17:52:38 -07001967 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001969 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001979 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes75770752011-08-24 17:52:38 -07001982 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001984 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes75770752011-08-24 17:52:38 -07001987 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001989 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes75770752011-08-24 17:52:38 -07001992 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001994 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes75770752011-08-24 17:52:38 -07001997 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001999 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes75770752011-08-24 17:52:38 -07002007 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002009 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002014 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002019 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002024 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes75770752011-08-24 17:52:38 -07002027 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 Class* c = Decode<Class*>(ts, java_class);
2125
Elliott Hughes5174fe62011-08-23 15:12:35 -07002126 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 const char* name = methods[i].name;
2128 const char* sig = methods[i].signature;
2129
2130 if (*sig == '!') {
2131 // TODO: fast jni. it's too noisy to log all these.
2132 ++sig;
2133 }
2134
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 Method* m = c->FindDirectMethod(name, sig);
2136 if (m == NULL) {
2137 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2143 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002144 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2150 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002151 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 return JNI_ERR;
2153 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154
Elliott Hughes75770752011-08-24 17:52:38 -07002155 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002156 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 }
2158
2159 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161 return JNI_OK;
2162 }
2163
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 Class* c = Decode<Class*>(ts, java_class);
2167
Elliott Hughes75770752011-08-24 17:52:38 -07002168 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 LOG(INFO) << "[Unregistering JNI native methods for "
2170 << PrettyDescriptor(c->GetDescriptor()) << "]";
2171 }
2172
2173 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2174 Method* m = c->GetDirectMethod(i);
2175 if (m->IsNative()) {
2176 m->UnregisterNative();
2177 }
2178 }
2179 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2180 Method* m = c->GetVirtualMethod(i);
2181 if (m->IsNative()) {
2182 m->UnregisterNative();
2183 }
2184 }
2185
2186 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes72025e52011-08-23 17:50:30 -07002189 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 Decode<Object*>(ts, java_object)->MonitorEnter();
2192 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes72025e52011-08-23 17:50:30 -07002195 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002197 Decode<Object*>(ts, java_object)->MonitorEnter();
2198 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
2201 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2202 ScopedJniThreadState ts(env);
2203 Runtime* runtime = Runtime::Current();
2204 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002205 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 } else {
2207 *vm = NULL;
2208 }
2209 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2210 }
2211
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2213 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214
2215 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002216 CHECK(address != NULL); // TODO: ReportJniError
2217 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218
2219 jclass buffer_class = GetDirectByteBufferClass(env);
2220 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2221 if (mid == NULL) {
2222 return NULL;
2223 }
2224
2225 // At the moment, the Java side is limited to 32 bits.
2226 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2227 CHECK_LE(capacity, 0xffffffff);
2228 jint address_arg = reinterpret_cast<jint>(address);
2229 jint capacity_arg = static_cast<jint>(capacity);
2230
2231 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2232 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2238 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2244 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249
Elliott Hughes75770752011-08-24 17:52:38 -07002250 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
2252 // Do we definitely know what kind of reference this is?
2253 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2254 IndirectRefKind kind = GetIndirectRefKind(ref);
2255 switch (kind) {
2256 case kLocal:
2257 return JNILocalRefType;
2258 case kGlobal:
2259 return JNIGlobalRefType;
2260 case kWeakGlobal:
2261 return JNIWeakGlobalRefType;
2262 case kSirtOrInvalid:
2263 // Is it in a stack IRT?
2264 if (ts.Self()->SirtContains(java_object)) {
2265 return JNILocalRefType;
2266 }
2267
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002268 if (!ts.Env()->work_around_app_jni_bugs) {
2269 return JNIInvalidRefType;
2270 }
2271
Elliott Hughesb465ab02011-08-24 11:21:21 -07002272 // If we're handing out direct pointers, check whether it's a direct pointer
2273 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002274 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275 if (ts.Env()->locals.Contains(java_object)) {
2276 return JNILocalRefType;
2277 }
2278 }
2279
2280 return JNIInvalidRefType;
2281 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002284
Elliott Hughesa2501992011-08-26 19:39:54 -07002285const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002286 NULL, // reserved0.
2287 NULL, // reserved1.
2288 NULL, // reserved2.
2289 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 JNI::GetVersion,
2291 JNI::DefineClass,
2292 JNI::FindClass,
2293 JNI::FromReflectedMethod,
2294 JNI::FromReflectedField,
2295 JNI::ToReflectedMethod,
2296 JNI::GetSuperclass,
2297 JNI::IsAssignableFrom,
2298 JNI::ToReflectedField,
2299 JNI::Throw,
2300 JNI::ThrowNew,
2301 JNI::ExceptionOccurred,
2302 JNI::ExceptionDescribe,
2303 JNI::ExceptionClear,
2304 JNI::FatalError,
2305 JNI::PushLocalFrame,
2306 JNI::PopLocalFrame,
2307 JNI::NewGlobalRef,
2308 JNI::DeleteGlobalRef,
2309 JNI::DeleteLocalRef,
2310 JNI::IsSameObject,
2311 JNI::NewLocalRef,
2312 JNI::EnsureLocalCapacity,
2313 JNI::AllocObject,
2314 JNI::NewObject,
2315 JNI::NewObjectV,
2316 JNI::NewObjectA,
2317 JNI::GetObjectClass,
2318 JNI::IsInstanceOf,
2319 JNI::GetMethodID,
2320 JNI::CallObjectMethod,
2321 JNI::CallObjectMethodV,
2322 JNI::CallObjectMethodA,
2323 JNI::CallBooleanMethod,
2324 JNI::CallBooleanMethodV,
2325 JNI::CallBooleanMethodA,
2326 JNI::CallByteMethod,
2327 JNI::CallByteMethodV,
2328 JNI::CallByteMethodA,
2329 JNI::CallCharMethod,
2330 JNI::CallCharMethodV,
2331 JNI::CallCharMethodA,
2332 JNI::CallShortMethod,
2333 JNI::CallShortMethodV,
2334 JNI::CallShortMethodA,
2335 JNI::CallIntMethod,
2336 JNI::CallIntMethodV,
2337 JNI::CallIntMethodA,
2338 JNI::CallLongMethod,
2339 JNI::CallLongMethodV,
2340 JNI::CallLongMethodA,
2341 JNI::CallFloatMethod,
2342 JNI::CallFloatMethodV,
2343 JNI::CallFloatMethodA,
2344 JNI::CallDoubleMethod,
2345 JNI::CallDoubleMethodV,
2346 JNI::CallDoubleMethodA,
2347 JNI::CallVoidMethod,
2348 JNI::CallVoidMethodV,
2349 JNI::CallVoidMethodA,
2350 JNI::CallNonvirtualObjectMethod,
2351 JNI::CallNonvirtualObjectMethodV,
2352 JNI::CallNonvirtualObjectMethodA,
2353 JNI::CallNonvirtualBooleanMethod,
2354 JNI::CallNonvirtualBooleanMethodV,
2355 JNI::CallNonvirtualBooleanMethodA,
2356 JNI::CallNonvirtualByteMethod,
2357 JNI::CallNonvirtualByteMethodV,
2358 JNI::CallNonvirtualByteMethodA,
2359 JNI::CallNonvirtualCharMethod,
2360 JNI::CallNonvirtualCharMethodV,
2361 JNI::CallNonvirtualCharMethodA,
2362 JNI::CallNonvirtualShortMethod,
2363 JNI::CallNonvirtualShortMethodV,
2364 JNI::CallNonvirtualShortMethodA,
2365 JNI::CallNonvirtualIntMethod,
2366 JNI::CallNonvirtualIntMethodV,
2367 JNI::CallNonvirtualIntMethodA,
2368 JNI::CallNonvirtualLongMethod,
2369 JNI::CallNonvirtualLongMethodV,
2370 JNI::CallNonvirtualLongMethodA,
2371 JNI::CallNonvirtualFloatMethod,
2372 JNI::CallNonvirtualFloatMethodV,
2373 JNI::CallNonvirtualFloatMethodA,
2374 JNI::CallNonvirtualDoubleMethod,
2375 JNI::CallNonvirtualDoubleMethodV,
2376 JNI::CallNonvirtualDoubleMethodA,
2377 JNI::CallNonvirtualVoidMethod,
2378 JNI::CallNonvirtualVoidMethodV,
2379 JNI::CallNonvirtualVoidMethodA,
2380 JNI::GetFieldID,
2381 JNI::GetObjectField,
2382 JNI::GetBooleanField,
2383 JNI::GetByteField,
2384 JNI::GetCharField,
2385 JNI::GetShortField,
2386 JNI::GetIntField,
2387 JNI::GetLongField,
2388 JNI::GetFloatField,
2389 JNI::GetDoubleField,
2390 JNI::SetObjectField,
2391 JNI::SetBooleanField,
2392 JNI::SetByteField,
2393 JNI::SetCharField,
2394 JNI::SetShortField,
2395 JNI::SetIntField,
2396 JNI::SetLongField,
2397 JNI::SetFloatField,
2398 JNI::SetDoubleField,
2399 JNI::GetStaticMethodID,
2400 JNI::CallStaticObjectMethod,
2401 JNI::CallStaticObjectMethodV,
2402 JNI::CallStaticObjectMethodA,
2403 JNI::CallStaticBooleanMethod,
2404 JNI::CallStaticBooleanMethodV,
2405 JNI::CallStaticBooleanMethodA,
2406 JNI::CallStaticByteMethod,
2407 JNI::CallStaticByteMethodV,
2408 JNI::CallStaticByteMethodA,
2409 JNI::CallStaticCharMethod,
2410 JNI::CallStaticCharMethodV,
2411 JNI::CallStaticCharMethodA,
2412 JNI::CallStaticShortMethod,
2413 JNI::CallStaticShortMethodV,
2414 JNI::CallStaticShortMethodA,
2415 JNI::CallStaticIntMethod,
2416 JNI::CallStaticIntMethodV,
2417 JNI::CallStaticIntMethodA,
2418 JNI::CallStaticLongMethod,
2419 JNI::CallStaticLongMethodV,
2420 JNI::CallStaticLongMethodA,
2421 JNI::CallStaticFloatMethod,
2422 JNI::CallStaticFloatMethodV,
2423 JNI::CallStaticFloatMethodA,
2424 JNI::CallStaticDoubleMethod,
2425 JNI::CallStaticDoubleMethodV,
2426 JNI::CallStaticDoubleMethodA,
2427 JNI::CallStaticVoidMethod,
2428 JNI::CallStaticVoidMethodV,
2429 JNI::CallStaticVoidMethodA,
2430 JNI::GetStaticFieldID,
2431 JNI::GetStaticObjectField,
2432 JNI::GetStaticBooleanField,
2433 JNI::GetStaticByteField,
2434 JNI::GetStaticCharField,
2435 JNI::GetStaticShortField,
2436 JNI::GetStaticIntField,
2437 JNI::GetStaticLongField,
2438 JNI::GetStaticFloatField,
2439 JNI::GetStaticDoubleField,
2440 JNI::SetStaticObjectField,
2441 JNI::SetStaticBooleanField,
2442 JNI::SetStaticByteField,
2443 JNI::SetStaticCharField,
2444 JNI::SetStaticShortField,
2445 JNI::SetStaticIntField,
2446 JNI::SetStaticLongField,
2447 JNI::SetStaticFloatField,
2448 JNI::SetStaticDoubleField,
2449 JNI::NewString,
2450 JNI::GetStringLength,
2451 JNI::GetStringChars,
2452 JNI::ReleaseStringChars,
2453 JNI::NewStringUTF,
2454 JNI::GetStringUTFLength,
2455 JNI::GetStringUTFChars,
2456 JNI::ReleaseStringUTFChars,
2457 JNI::GetArrayLength,
2458 JNI::NewObjectArray,
2459 JNI::GetObjectArrayElement,
2460 JNI::SetObjectArrayElement,
2461 JNI::NewBooleanArray,
2462 JNI::NewByteArray,
2463 JNI::NewCharArray,
2464 JNI::NewShortArray,
2465 JNI::NewIntArray,
2466 JNI::NewLongArray,
2467 JNI::NewFloatArray,
2468 JNI::NewDoubleArray,
2469 JNI::GetBooleanArrayElements,
2470 JNI::GetByteArrayElements,
2471 JNI::GetCharArrayElements,
2472 JNI::GetShortArrayElements,
2473 JNI::GetIntArrayElements,
2474 JNI::GetLongArrayElements,
2475 JNI::GetFloatArrayElements,
2476 JNI::GetDoubleArrayElements,
2477 JNI::ReleaseBooleanArrayElements,
2478 JNI::ReleaseByteArrayElements,
2479 JNI::ReleaseCharArrayElements,
2480 JNI::ReleaseShortArrayElements,
2481 JNI::ReleaseIntArrayElements,
2482 JNI::ReleaseLongArrayElements,
2483 JNI::ReleaseFloatArrayElements,
2484 JNI::ReleaseDoubleArrayElements,
2485 JNI::GetBooleanArrayRegion,
2486 JNI::GetByteArrayRegion,
2487 JNI::GetCharArrayRegion,
2488 JNI::GetShortArrayRegion,
2489 JNI::GetIntArrayRegion,
2490 JNI::GetLongArrayRegion,
2491 JNI::GetFloatArrayRegion,
2492 JNI::GetDoubleArrayRegion,
2493 JNI::SetBooleanArrayRegion,
2494 JNI::SetByteArrayRegion,
2495 JNI::SetCharArrayRegion,
2496 JNI::SetShortArrayRegion,
2497 JNI::SetIntArrayRegion,
2498 JNI::SetLongArrayRegion,
2499 JNI::SetFloatArrayRegion,
2500 JNI::SetDoubleArrayRegion,
2501 JNI::RegisterNatives,
2502 JNI::UnregisterNatives,
2503 JNI::MonitorEnter,
2504 JNI::MonitorExit,
2505 JNI::GetJavaVM,
2506 JNI::GetStringRegion,
2507 JNI::GetStringUTFRegion,
2508 JNI::GetPrimitiveArrayCritical,
2509 JNI::ReleasePrimitiveArrayCritical,
2510 JNI::GetStringCritical,
2511 JNI::ReleaseStringCritical,
2512 JNI::NewWeakGlobalRef,
2513 JNI::DeleteWeakGlobalRef,
2514 JNI::ExceptionCheck,
2515 JNI::NewDirectByteBuffer,
2516 JNI::GetDirectBufferAddress,
2517 JNI::GetDirectBufferCapacity,
2518 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002519};
2520
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002521static const size_t kMonitorsInitial = 32; // Arbitrary.
2522static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2523
2524static const size_t kLocalsInitial = 64; // Arbitrary.
2525static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002526
Elliott Hughes75770752011-08-24 17:52:38 -07002527JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002528 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002529 vm(vm),
2530 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002531 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002532 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002533 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2534 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002535 functions = unchecked_functions = &gNativeInterface;
2536 if (check_jni) {
2537 functions = GetCheckJniNativeInterface();
2538 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002539}
2540
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002541JNIEnvExt::~JNIEnvExt() {
2542}
2543
Carl Shapiroea4dca82011-08-01 13:45:38 -07002544// JNI Invocation interface.
2545
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002546extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2547 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2548 if (args->version < JNI_VERSION_1_2) {
2549 return JNI_EVERSION;
2550 }
2551 Runtime::Options options;
2552 for (int i = 0; i < args->nOptions; ++i) {
2553 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002554 options.push_back(std::make_pair(StringPiece(option->optionString),
2555 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002556 }
2557 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002558 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002559 if (runtime == NULL) {
2560 return JNI_ERR;
2561 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002562 *p_env = Thread::Current()->GetJniEnv();
2563 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564 return JNI_OK;
2565 }
2566}
2567
Elliott Hughesf2682d52011-08-15 16:37:04 -07002568extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569 Runtime* runtime = Runtime::Current();
2570 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002571 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002572 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002573 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002574 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575 }
2576 return JNI_OK;
2577}
2578
2579// Historically unsupported.
2580extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2581 return JNI_ERR;
2582}
2583
Elliott Hughescdf53122011-08-19 15:46:09 -07002584class JII {
2585 public:
2586 static jint DestroyJavaVM(JavaVM* vm) {
2587 if (vm == NULL) {
2588 return JNI_ERR;
2589 } else {
2590 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2591 delete raw_vm->runtime;
2592 return JNI_OK;
2593 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002597 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 }
2599
2600 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002601 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002602 }
2603
2604 static jint DetachCurrentThread(JavaVM* vm) {
2605 if (vm == NULL) {
2606 return JNI_ERR;
2607 } else {
2608 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2609 Runtime* runtime = raw_vm->runtime;
2610 runtime->DetachCurrentThread();
2611 return JNI_OK;
2612 }
2613 }
2614
2615 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2616 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2617 return JNI_EVERSION;
2618 }
2619 if (vm == NULL || env == NULL) {
2620 return JNI_ERR;
2621 }
2622 Thread* thread = Thread::Current();
2623 if (thread == NULL) {
2624 *env = NULL;
2625 return JNI_EDETACHED;
2626 }
2627 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002628 return JNI_OK;
2629 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002630};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631
Elliott Hughesa2501992011-08-26 19:39:54 -07002632const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002633 NULL, // reserved0
2634 NULL, // reserved1
2635 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002636 JII::DestroyJavaVM,
2637 JII::AttachCurrentThread,
2638 JII::DetachCurrentThread,
2639 JII::GetEnv,
2640 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002641};
2642
Elliott Hughesbbd76712011-08-17 10:25:24 -07002643static const size_t kPinTableInitialSize = 16;
2644static const size_t kPinTableMaxSize = 1024;
2645
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002646static const size_t kGlobalsInitial = 512; // Arbitrary.
2647static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2648
2649static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2650static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2651
Elliott Hughes0af55432011-08-17 18:37:28 -07002652JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002653 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002654 check_jni_abort_hook(NULL),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002655 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002656 verbose_jni(verbose_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002657 force_copy(false), // TODO: add a way to enable this
2658 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002659 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002660 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002661 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002662 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002663 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002664 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2665 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2666 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002667 functions = unchecked_functions = &gInvokeInterface;
2668 if (check_jni) {
2669 functions = GetCheckJniInvokeInterface();
2670 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002671}
2672
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002673JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002674 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002675 delete globals_lock;
2676 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002677 delete libraries_lock;
2678 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002679}
2680
Elliott Hughes75770752011-08-24 17:52:38 -07002681bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2682 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002683
2684 // See if we've already loaded this library. If we have, and the class loader
2685 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002686 // TODO: for better results we should canonicalize the pathname (or even compare
2687 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002688 SharedLibrary* library;
2689 {
2690 // TODO: move the locking (and more of this logic) into Libraries.
2691 MutexLock mu(libraries_lock);
2692 library = libraries->Get(path);
2693 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 if (library != NULL) {
2695 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002696 // The library will be associated with class_loader. The JNI
2697 // spec says we can't load the same library into more than one
2698 // class loader.
2699 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2700 "ClassLoader %p; can't open in ClassLoader %p",
2701 path.c_str(), library->GetClassLoader(), class_loader);
2702 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002703 return false;
2704 }
2705 if (verbose_jni) {
2706 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2707 << "ClassLoader " << class_loader << "]";
2708 }
2709 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002710 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2711 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002712 return false;
2713 }
2714 return true;
2715 }
2716
2717 // Open the shared library. Because we're using a full path, the system
2718 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2719 // resolve this library's dependencies though.)
2720
2721 // Failures here are expected when java.library.path has several entries
2722 // and we have to hunt for the lib.
2723
2724 // The current version of the dynamic linker prints detailed information
2725 // about dlopen() failures. Some things to check if the message is
2726 // cryptic:
2727 // - make sure the library exists on the device
2728 // - verify that the right path is being opened (the debug log message
2729 // above can help with that)
2730 // - check to see if the library is valid (e.g. not zero bytes long)
2731 // - check config/prelink-linux-arm.map to ensure that the library
2732 // is listed and is not being overrun by the previous entry (if
2733 // loading suddenly stops working on a prelinked library, this is
2734 // a good one to check)
2735 // - write a trivial app that calls sleep() then dlopen(), attach
2736 // to it with "strace -p <pid>" while it sleeps, and watch for
2737 // attempts to open nonexistent dependent shared libs
2738
2739 // TODO: automate some of these checks!
2740
2741 // This can execute slowly for a large library on a busy system, so we
2742 // want to switch from RUNNING to VMWAIT while it executes. This allows
2743 // the GC to ignore us.
2744 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002745 void* handle = NULL;
2746 {
2747 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2748 handle = dlopen(path.c_str(), RTLD_LAZY);
2749 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002750
2751 if (verbose_jni) {
2752 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2753 }
2754
2755 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002756 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 return false;
2758 }
2759
2760 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002761 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002762 // TODO: move the locking (and more of this logic) into Libraries.
2763 MutexLock mu(libraries_lock);
2764 library = libraries->Get(path);
2765 if (library != NULL) {
2766 LOG(INFO) << "WOW: we lost a race to add shared library: "
2767 << "\"" << path << "\" ClassLoader=" << class_loader;
2768 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002769 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002770 library = new SharedLibrary(path, handle, class_loader);
2771 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002772 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002773
2774 if (verbose_jni) {
2775 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2776 }
2777
2778 bool result = true;
2779 void* sym = dlsym(handle, "JNI_OnLoad");
2780 if (sym == NULL) {
2781 if (verbose_jni) {
2782 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2783 }
2784 } else {
2785 // Call JNI_OnLoad. We have to override the current class
2786 // loader, which will always be "null" since the stuff at the
2787 // top of the stack is around Runtime.loadLibrary(). (See
2788 // the comments in the JNI FindClass function.)
2789 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2790 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002791 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 self->SetClassLoaderOverride(class_loader);
2793
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002794 int version = 0;
2795 {
2796 ScopedThreadStateChange tsc(self, Thread::kNative);
2797 if (verbose_jni) {
2798 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2799 }
2800 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002801 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002802
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}