blob: 4cd809e298e98a8c34b1d16ee4dfcc7536a18daa [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 Hughesc5f7c912011-08-18 14:00:42 -070053/*
54 * Add a local reference for an object to the current stack frame. When
55 * the native function returns, the reference will be discarded.
56 *
57 * We need to allow the same reference to be added multiple times.
58 *
59 * This will be called on otherwise unreferenced objects. We cannot do
60 * GC allocations here, and it's best if we don't grab a mutex.
61 *
62 * Returns the local reference (currently just the same pointer that was
63 * passed in), or NULL on failure.
64 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070065template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070066T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
67 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
68 Object* obj = const_cast<Object*>(const_obj);
69
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070 if (obj == NULL) {
71 return NULL;
72 }
73
Elliott Hughesbf86d042011-08-31 17:53:14 -070074 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
75 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076
77 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
78 IndirectRef ref = locals.Add(cookie, obj);
79 if (ref == NULL) {
80 // TODO: just change Add's DCHECK to CHECK and lose this?
81 locals.Dump();
82 LOG(FATAL) << "Failed adding to JNI local reference table "
83 << "(has " << locals.Capacity() << " entries)";
84 // TODO: dvmDumpThread(dvmThreadSelf(), false);
85 }
86
87#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 size_t entry_count = locals.Capacity();
90 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070091 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070093 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070094 locals.Dump();
95 // TODO: dvmDumpThread(dvmThreadSelf(), false);
96 // dvmAbort();
97 }
98 }
99#endif
100
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700102 // Hand out direct pointers to support broken old apps.
103 return reinterpret_cast<T>(obj);
104 }
105
106 return reinterpret_cast<T>(ref);
107}
108
Elliott Hughesbf86d042011-08-31 17:53:14 -0700109// For external use.
110template<typename T>
111T Decode(JNIEnv* public_env, jobject obj) {
112 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
113 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
114}
115// Explicit instantiations.
116template Class* Decode<Class*>(JNIEnv*, jobject);
117template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
118template Object* Decode<Object*>(JNIEnv*, jobject);
119template String* Decode<String*>(JNIEnv*, jobject);
120
121namespace {
122
Elliott Hughescdf53122011-08-19 15:46:09 -0700123jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
124 if (obj == NULL) {
125 return NULL;
126 }
Elliott Hughes75770752011-08-24 17:52:38 -0700127 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700128 IndirectReferenceTable& weak_globals = vm->weak_globals;
129 MutexLock mu(vm->weak_globals_lock);
130 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
131 return reinterpret_cast<jweak>(ref);
132}
133
Elliott Hughesbf86d042011-08-31 17:53:14 -0700134// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700135template<typename T>
136T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700137 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700138}
139
Elliott Hughescdf53122011-08-19 15:46:09 -0700140Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
141 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
142}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700143
Elliott Hughescdf53122011-08-19 15:46:09 -0700144Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
145 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700146}
147
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700148byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700149 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700150 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700152 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700153 switch (shorty[i]) {
154 case 'Z':
155 case 'B':
156 case 'C':
157 case 'S':
158 case 'I':
159 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
160 offset += 4;
161 break;
162 case 'F':
163 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
164 offset += 4;
165 break;
166 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700167 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700168 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
169 offset += sizeof(Object*);
170 break;
171 }
172 case 'D':
173 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
174 offset += 8;
175 break;
176 case 'J':
177 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
178 offset += 8;
179 break;
180 }
181 }
182 return arg_array.release();
183}
184
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700185byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700186 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700187 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700188 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700189 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700190 switch (shorty[i]) {
191 case 'Z':
192 case 'B':
193 case 'C':
194 case 'S':
195 case 'I':
196 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
197 offset += 4;
198 break;
199 case 'F':
200 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
201 offset += 4;
202 break;
203 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700204 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700205 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
206 offset += sizeof(Object*);
207 break;
208 }
209 case 'D':
210 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
211 offset += 8;
212 break;
213 case 'J':
214 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
215 offset += 8;
216 break;
217 }
218 }
219 return arg_array.release();
220}
221
Elliott Hughes72025e52011-08-23 17:50:30 -0700222JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
223 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700224 Thread* self = ts.Self();
225
226 // Push a transition back into managed code onto the linked list in thread
227 CHECK_EQ(Thread::kRunnable, self->GetState());
228 NativeToManagedRecord record;
229 self->PushNativeToManagedRecord(&record);
230
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700231 // Call the invoke stub associated with the method
232 // Pass everything as arguments
233 const Method::InvokeStub* stub = method->GetInvokeStub();
234 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700235
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700236 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700237 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700238 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700239 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700240 LOG(WARNING) << "Not invoking method with no associated code: "
Elliott Hughesa2501992011-08-26 19:39:54 -0700241 << PrettyMethod(method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700242 result.j = 0;
243 }
buzbeec143c552011-08-20 17:38:58 -0700244
Ian Rogers6de08602011-08-19 14:52:39 -0700245 // Pop transition
246 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700247 return result;
248}
249
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700250JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700251 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700252 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700253 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700254 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700255 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700256}
257
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700258JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700259 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700260 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700261 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700262 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700263 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
264}
265
Elliott Hughes75770752011-08-24 17:52:38 -0700266Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700267 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700268}
269
Brian Carlstrom30b94452011-08-25 21:35:26 -0700270JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700271 Object* receiver = Decode<Object*>(ts, obj);
272 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700273 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700274 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
275}
276
Brian Carlstrom30b94452011-08-25 21:35:26 -0700277JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700278 Object* receiver = Decode<Object*>(ts, obj);
279 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700280 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700281 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700282}
283
Elliott Hughes6b436852011-08-12 10:16:44 -0700284// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
285// separated with slashes but aren't wrapped with "L;" like regular descriptors
286// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
287// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
288// supported names with dots too (such as "a.b.C").
289std::string NormalizeJniClassDescriptor(const char* name) {
290 std::string result;
291 // Add the missing "L;" if necessary.
292 if (name[0] == '[') {
293 result = name;
294 } else {
295 result += 'L';
296 result += name;
297 result += ';';
298 }
299 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700300 if (result.find('.') != std::string::npos) {
301 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
302 << "\"" << name << "\"";
303 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700304 }
305 return result;
306}
307
Elliott Hughescdf53122011-08-19 15:46:09 -0700308jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
309 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700310 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
311 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700312 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700313
314 Method* method = NULL;
315 if (is_static) {
316 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700317 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700318 method = c->FindVirtualMethod(name, sig);
319 if (method == NULL) {
320 // No virtual method matching the signature. Search declared
321 // private methods and constructors.
322 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700323 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700324 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700325
Elliott Hughescdf53122011-08-19 15:46:09 -0700326 if (method == NULL || method->IsStatic() != is_static) {
327 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700328 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700329 // TODO: try searching for the opposite kind of method from is_static
330 // for better diagnostics?
331 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700332 "no %s method %s", is_static ? "static" : "non-static",
333 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700334 return NULL;
335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700336
Elliott Hughes79082e32011-08-25 12:07:32 -0700337 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700338
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700340}
341
Elliott Hughescdf53122011-08-19 15:46:09 -0700342jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
343 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700344 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
345 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700346 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700347
348 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 Class* field_type;
350 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
351 if (sig[1] != '\0') {
352 // TODO: need to get the appropriate ClassLoader.
353 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
354 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700355 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700357 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 if (field_type == NULL) {
359 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700360 DCHECK(ts.Self()->IsExceptionPending());
361 ts.Self()->ClearException();
362 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
363 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
364 "no type \"%s\" found and so no field \"%s\" could be found in class "
365 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 return NULL;
367 }
368 if (is_static) {
369 field = c->FindStaticField(name, field_type);
370 } else {
371 field = c->FindInstanceField(name, field_type);
372 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700373 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700374 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700375 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700377 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700378 return NULL;
379 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 // Check invariant that all jfieldIDs have resolved types (how else would
381 // the type equality in Find...Field hold?)
382 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700383 jweak fid = AddWeakGlobalReference(ts, field);
384 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700385}
386
Elliott Hughes75770752011-08-24 17:52:38 -0700387void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
388 JavaVMExt* vm = ts.Vm();
389 MutexLock mu(vm->pins_lock);
390 vm->pin_table.Add(array);
391}
392
393void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
394 JavaVMExt* vm = ts.Vm();
395 MutexLock mu(vm->pins_lock);
396 vm->pin_table.Remove(array);
397}
398
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700399template<typename JniT, typename ArtT>
400JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
401 CHECK_GE(length, 0); // TODO: ReportJniError
402 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700403 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700404}
405
Elliott Hughes75770752011-08-24 17:52:38 -0700406template <typename ArrayT, typename CArrayT, typename ArtArrayT>
407CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
408 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
409 PinPrimitiveArray(ts, array);
410 if (is_copy != NULL) {
411 *is_copy = JNI_FALSE;
412 }
413 return array->GetData();
414}
415
416template <typename ArrayT>
417void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
418 if (mode != JNI_COMMIT) {
419 Array* array = Decode<Array*>(ts, java_array);
420 UnpinPrimitiveArray(ts, array);
421 }
422}
423
Elliott Hughes814e4032011-08-23 12:07:56 -0700424void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
425 std::string type(PrettyType(array));
426 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
427 "%s offset=%d length=%d %s.length=%d",
428 type.c_str(), start, length, identifier, array->GetLength());
429}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700430void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
431 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
432 "offset=%d length=%d string.length()=%d", start, length, array_length);
433}
Elliott Hughes814e4032011-08-23 12:07:56 -0700434
435template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700436void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700437 ArrayT* array = Decode<ArrayT*>(ts, java_array);
438 if (start < 0 || length < 0 || start + length > array->GetLength()) {
439 ThrowAIOOBE(ts, array, start, length, "src");
440 } else {
441 JavaT* data = array->GetData();
442 memcpy(buf, data + start, length * sizeof(JavaT));
443 }
444}
445
446template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700447void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700448 ArrayT* array = Decode<ArrayT*>(ts, java_array);
449 if (start < 0 || length < 0 || start + length > array->GetLength()) {
450 ThrowAIOOBE(ts, array, start, length, "dst");
451 } else {
452 JavaT* data = array->GetData();
453 memcpy(data + start, buf, length * sizeof(JavaT));
454 }
455}
456
Elliott Hughes75770752011-08-24 17:52:38 -0700457jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700458 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
459 CHECK(buffer_class.get() != NULL);
460 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
461}
462
Elliott Hughes75770752011-08-24 17:52:38 -0700463jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700464 static jclass buffer_class = InitDirectByteBufferClass(env);
465 return buffer_class;
466}
467
Elliott Hughes75770752011-08-24 17:52:38 -0700468jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
469 if (vm == NULL || p_env == NULL) {
470 return JNI_ERR;
471 }
472
473 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
474 JavaVMAttachArgs args;
475 if (thr_args == NULL) {
476 // Allow the v1.1 calling convention.
477 args.version = JNI_VERSION_1_2;
478 args.name = NULL;
479 args.group = NULL; // TODO: get "main" thread group
480 } else {
481 args.version = in_args->version;
482 args.name = in_args->name;
483 if (in_args->group != NULL) {
484 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
485 args.group = NULL; // TODO: decode in_args->group
486 } else {
487 args.group = NULL; // TODO: get "main" thread group
488 }
489 }
490 CHECK_GE(args.version, JNI_VERSION_1_2);
491
492 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
493 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
494}
495
Elliott Hughes79082e32011-08-25 12:07:32 -0700496class SharedLibrary {
497 public:
498 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
499 : path_(path),
500 handle_(handle),
501 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
502 jni_on_load_tid_(Thread::Current()->GetId()),
503 jni_on_load_result_(kPending) {
504 pthread_cond_init(&jni_on_load_cond_, NULL);
505 }
506
507 ~SharedLibrary() {
508 delete jni_on_load_lock_;
509 }
510
511 Object* GetClassLoader() {
512 return class_loader_;
513 }
514
515 std::string GetPath() {
516 return path_;
517 }
518
519 /*
520 * Check the result of an earlier call to JNI_OnLoad on this library. If
521 * the call has not yet finished in another thread, wait for it.
522 */
523 bool CheckOnLoadResult(JavaVMExt* vm) {
524 Thread* self = Thread::Current();
525 if (jni_on_load_tid_ == self->GetId()) {
526 // Check this so we don't end up waiting for ourselves. We need
527 // to return "true" so the caller can continue.
528 LOG(INFO) << *self << " recursive attempt to load library "
529 << "\"" << path_ << "\"";
530 return true;
531 }
532
533 MutexLock mu(jni_on_load_lock_);
534 while (jni_on_load_result_ == kPending) {
535 if (vm->verbose_jni) {
536 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
537 << "JNI_OnLoad...]";
538 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700539 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700540 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 }
542
543 bool okay = (jni_on_load_result_ == kOkay);
544 if (vm->verbose_jni) {
545 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
546 << (okay ? "succeeded" : "failed") << "]";
547 }
548 return okay;
549 }
550
551 void SetResult(bool result) {
552 jni_on_load_result_ = result ? kOkay : kFailed;
553 jni_on_load_tid_ = 0;
554
555 // Broadcast a wakeup to anybody sleeping on the condition variable.
556 MutexLock mu(jni_on_load_lock_);
557 pthread_cond_broadcast(&jni_on_load_cond_);
558 }
559
560 void* FindSymbol(const std::string& symbol_name) {
561 return dlsym(handle_, symbol_name.c_str());
562 }
563
564 private:
565 enum JNI_OnLoadState {
566 kPending,
567 kFailed,
568 kOkay,
569 };
570
571 // Path to library "/system/lib/libjni.so".
572 std::string path_;
573
574 // The void* returned by dlopen(3).
575 void* handle_;
576
577 // The ClassLoader this library is associated with.
578 Object* class_loader_;
579
580 // Guards remaining items.
581 Mutex* jni_on_load_lock_;
582 // Wait for JNI_OnLoad in other thread.
583 pthread_cond_t jni_on_load_cond_;
584 // Recursive invocation guard.
585 uint32_t jni_on_load_tid_;
586 // Result of earlier JNI_OnLoad call.
587 JNI_OnLoadState jni_on_load_result_;
588};
589
Elliott Hughescdf53122011-08-19 15:46:09 -0700590} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700591
Elliott Hughes79082e32011-08-25 12:07:32 -0700592// This exists mainly to keep implementation details out of the header file.
593class Libraries {
594 public:
595 Libraries() {
596 }
597
598 ~Libraries() {
599 // Delete our map values. (The keys will be cleaned up by the map itself.)
600 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
601 delete it->second;
602 }
603 }
604
605 SharedLibrary* Get(const std::string& path) {
606 return libraries_[path];
607 }
608
609 void Put(const std::string& path, SharedLibrary* library) {
610 libraries_[path] = library;
611 }
612
613 // See section 11.3 "Linking Native Methods" of the JNI spec.
614 void* FindNativeMethod(const Method* m) {
615 std::string jni_short_name(JniShortName(m));
616 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700617 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700618 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
619 SharedLibrary* library = it->second;
620 if (library->GetClassLoader() != declaring_class_loader) {
621 // We only search libraries loaded by the appropriate ClassLoader.
622 continue;
623 }
624 // Try the short name then the long name...
625 void* fn = library->FindSymbol(jni_short_name);
626 if (fn == NULL) {
627 fn = library->FindSymbol(jni_long_name);
628 }
629 if (fn != NULL) {
630 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700631 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700632 << " in \"" << library->GetPath() << "\"]";
633 }
634 return fn;
635 }
636 }
637 std::string detail;
638 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700639 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 LOG(ERROR) << detail;
641 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
642 "%s", detail.c_str());
643 return NULL;
644 }
645
646 private:
647 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
648
649 std::map<std::string, SharedLibrary*> libraries_;
650};
651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652class JNI {
653 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jint GetVersion(JNIEnv* env) {
656 ScopedJniThreadState ts(env);
657 return JNI_VERSION_1_6;
658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
661 ScopedJniThreadState ts(env);
662 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700663 return NULL;
664 }
665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jclass FindClass(JNIEnv* env, const char* name) {
667 ScopedJniThreadState ts(env);
668 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
669 std::string descriptor(NormalizeJniClassDescriptor(name));
670 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700671 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700672 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
677 ScopedJniThreadState ts(env);
678 Method* method = Decode<Method*>(ts, java_method);
679 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
683 ScopedJniThreadState ts(env);
684 Field* field = Decode<Field*>(ts, java_field);
685 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
689 ScopedJniThreadState ts(env);
690 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700691 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
695 ScopedJniThreadState ts(env);
696 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700697 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700699
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
701 ScopedJniThreadState ts(env);
702 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700703 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700704 }
705
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700706 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700708 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700709 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 Class* c1 = Decode<Class*>(ts, java_class1);
715 Class* c2 = Decode<Class*>(ts, java_class2);
716 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700718
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700721 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700722 if (jobj == NULL) {
723 // NB. JNI is different from regular Java instanceof in this respect
724 return JNI_TRUE;
725 } else {
726 Object* obj = Decode<Object*>(ts, jobj);
727 Class* klass = Decode<Class*>(ts, clazz);
728 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
729 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 static jint Throw(JNIEnv* env, jthrowable java_exception) {
733 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700734 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 if (exception == NULL) {
736 return JNI_ERR;
737 }
738 ts.Self()->SetException(exception);
739 return JNI_OK;
740 }
741
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700742 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700744 // TODO: check for a pending exception to decide what constructor to call.
745 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
746 if (mid == NULL) {
747 return JNI_ERR;
748 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
750 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700751 return JNI_ERR;
752 }
753
754 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 args[0].l = s.get();
756 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
757 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700758 return JNI_ERR;
759 }
760
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700762 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700764
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 return JNI_OK;
766 }
767
768 static jboolean ExceptionCheck(JNIEnv* env) {
769 ScopedJniThreadState ts(env);
770 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
771 }
772
773 static void ExceptionClear(JNIEnv* env) {
774 ScopedJniThreadState ts(env);
775 ts.Self()->ClearException();
776 }
777
778 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700780
781 Thread* self = ts.Self();
782 Throwable* original_exception = self->GetException();
783 self->ClearException();
784
Elliott Hughesbf86d042011-08-31 17:53:14 -0700785 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
787 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
788 if (mid == NULL) {
789 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
790 << PrettyType(original_exception);
791 } else {
792 env->CallVoidMethod(exception.get(), mid);
793 if (self->IsExceptionPending()) {
794 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
795 << " thrown while calling printStackTrace";
796 self->ClearException();
797 }
798 }
799
800 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700802
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 static jthrowable ExceptionOccurred(JNIEnv* env) {
804 ScopedJniThreadState ts(env);
805 Object* exception = ts.Self()->GetException();
806 if (exception == NULL) {
807 return NULL;
808 } else {
809 // TODO: if adding a local reference failing causes the VM to abort
810 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700811 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 if (localException == NULL) {
813 // We were unable to add a new local reference, and threw a new
814 // exception. We can't return "exception", because it's not a
815 // local reference. So we have to return NULL, indicating that
816 // there was no exception, even though it's pretty much raining
817 // exceptions in here.
818 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
819 }
820 return localException;
821 }
822 }
823
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 static void FatalError(JNIEnv* env, const char* msg) {
825 ScopedJniThreadState ts(env);
826 LOG(FATAL) << "JNI FatalError called: " << msg;
827 }
828
829 static jint PushLocalFrame(JNIEnv* env, jint cap) {
830 ScopedJniThreadState ts(env);
831 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
832 return JNI_OK;
833 }
834
835 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
836 ScopedJniThreadState ts(env);
837 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
838 return res;
839 }
840
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
842 ScopedJniThreadState ts(env);
843 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
844 return 0;
845 }
846
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
848 ScopedJniThreadState ts(env);
849 if (obj == NULL) {
850 return NULL;
851 }
852
Elliott Hughes75770752011-08-24 17:52:38 -0700853 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& globals = vm->globals;
855 MutexLock mu(vm->globals_lock);
856 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
857 return reinterpret_cast<jobject>(ref);
858 }
859
860 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
861 ScopedJniThreadState ts(env);
862 if (obj == NULL) {
863 return;
864 }
865
Elliott Hughes75770752011-08-24 17:52:38 -0700866 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 IndirectReferenceTable& globals = vm->globals;
868 MutexLock mu(vm->globals_lock);
869
870 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
871 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
872 << "failed to find entry";
873 }
874 }
875
876 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
877 ScopedJniThreadState ts(env);
878 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
879 }
880
881 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
882 ScopedJniThreadState ts(env);
883 if (obj == NULL) {
884 return;
885 }
886
Elliott Hughes75770752011-08-24 17:52:38 -0700887 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 IndirectReferenceTable& weak_globals = vm->weak_globals;
889 MutexLock mu(vm->weak_globals_lock);
890
891 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
892 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
893 << "failed to find entry";
894 }
895 }
896
897 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
898 ScopedJniThreadState ts(env);
899 if (obj == NULL) {
900 return NULL;
901 }
902
903 IndirectReferenceTable& locals = ts.Env()->locals;
904
905 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
906 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
907 return reinterpret_cast<jobject>(ref);
908 }
909
910 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
911 ScopedJniThreadState ts(env);
912 if (obj == NULL) {
913 return;
914 }
915
916 IndirectReferenceTable& locals = ts.Env()->locals;
917
918 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
919 if (!locals.Remove(cookie, obj)) {
920 // Attempting to delete a local reference that is not in the
921 // topmost local reference frame is a no-op. DeleteLocalRef returns
922 // void and doesn't throw any exceptions, but we should probably
923 // complain about it so the user will notice that things aren't
924 // going quite the way they expect.
925 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
926 << "failed to find entry";
927 }
928 }
929
930 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
931 ScopedJniThreadState ts(env);
932 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
933 ? JNI_TRUE : JNI_FALSE;
934 }
935
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700936 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 Class* c = Decode<Class*>(ts, java_class);
939 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
940 return NULL;
941 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700942 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 ScopedJniThreadState ts(env);
947 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 va_start(args, mid);
949 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 va_end(args);
951 return result;
952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700956 Class* c = Decode<Class*>(ts, java_class);
957 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
958 return NULL;
959 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700960 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700961 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 return local_result;
964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700968 Class* c = Decode<Class*>(ts, java_class);
969 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
970 return NULL;
971 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700972 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 return local_result;
976 }
977
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
979 ScopedJniThreadState ts(env);
980 return FindMethodID(ts, c, name, sig, false);
981 }
982
983 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
984 ScopedJniThreadState ts(env);
985 return FindMethodID(ts, c, name, sig, true);
986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700994 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700999 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001000 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001006 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001013 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
1015 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001020 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001025 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001032 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_end(ap);
1034 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001039 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001044 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001051 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_end(ap);
1053 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001058 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001063 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001070 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
1072 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001077 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001082 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001089 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_end(ap);
1091 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001096 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001101 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001108 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
1110 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001115 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001120 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_list ap;
1126 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001127 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
1129 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001134 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001139 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_list ap;
1145 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001146 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
1148 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001153 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001158 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_list ap;
1164 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001165 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001171 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001176 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
1179 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
1182 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_start(ap, mid);
1184 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001185 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 va_end(ap);
1187 return local_result;
1188 }
1189
1190 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001194 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001201 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
1207 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_start(ap, mid);
1209 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_end(ap);
1211 return result.z;
1212 }
1213
1214 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
1229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
1231 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
1233 return result.b;
1234 }
1235
1236 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
1253 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.c;
1256 }
1257
1258 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
1273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
1275 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
1277 return result.s;
1278 }
1279
1280 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
1297 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
1299 return result.i;
1300 }
1301
1302 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
1319 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 return result.j;
1322 }
1323
1324 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
1341 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 return result.f;
1344 }
1345
1346 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
1361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
1363 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
1365 return result.d;
1366 }
1367
1368 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001371 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
1383 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001384 va_start(ap, mid);
1385 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
1387 }
1388
1389 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001392 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
1395 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001398 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
1401 static jfieldID GetFieldID(JNIEnv* env,
1402 jclass c, const char* name, const char* sig) {
1403 ScopedJniThreadState ts(env);
1404 return FindFieldID(ts, c, name, sig, false);
1405 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001406
1407
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 static jfieldID GetStaticFieldID(JNIEnv* env,
1409 jclass c, const char* name, const char* sig) {
1410 ScopedJniThreadState ts(env);
1411 return FindFieldID(ts, c, name, sig, true);
1412 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 Object* o = Decode<Object*>(ts, obj);
1417 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001418 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001420
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001424 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 Object* o = Decode<Object*>(ts, java_object);
1430 Object* v = Decode<Object*>(ts, java_value);
1431 Field* f = DecodeField(ts, fid);
1432 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 Object* v = Decode<Object*>(ts, java_value);
1438 Field* f = DecodeField(ts, fid);
1439 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442#define GET_PRIMITIVE_FIELD(fn, instance) \
1443 ScopedJniThreadState ts(env); \
1444 Object* o = Decode<Object*>(ts, instance); \
1445 Field* f = DecodeField(ts, fid); \
1446 return f->fn(o)
1447
1448#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1449 ScopedJniThreadState ts(env); \
1450 Object* o = Decode<Object*>(ts, instance); \
1451 Field* f = DecodeField(ts, fid); \
1452 f->fn(o, value)
1453
1454 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1491 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1495 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1499 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1503 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1507 GET_PRIMITIVE_FIELD(GetLong, NULL);
1508 }
1509
1510 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1511 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1512 }
1513
1514 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1515 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1516 }
1517
1518 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1519 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1520 }
1521
1522 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1523 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1524 }
1525
1526 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1527 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1528 }
1529
1530 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1531 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1532 }
1533
1534 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1535 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1536 }
1537
1538 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1539 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1540 }
1541
1542 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1543 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1544 }
1545
1546 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1547 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1548 }
1549
1550 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1551 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1552 }
1553
1554 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1555 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1556 }
1557
1558 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1559 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1560 }
1561
1562 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1563 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1564 }
1565
1566 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1567 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1568 }
1569
1570 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1571 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1572 }
1573
1574 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1575 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1576 }
1577
1578 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1579 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
1582 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
1585 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 va_start(ap, mid);
1587 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001588 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_end(ap);
1590 return local_result;
1591 }
1592
1593 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001597 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
1600 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001604 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
1607 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 ScopedJniThreadState ts(env);
1610 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 va_start(ap, mid);
1612 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 va_end(ap);
1614 return result.z;
1615 }
1616
1617 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
1623 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
1631 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 va_start(ap, mid);
1633 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 va_end(ap);
1635 return result.b;
1636 }
1637
1638 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
1644 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
1652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
1654 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
1656 return result.c;
1657 }
1658
1659 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
1665 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 ScopedJniThreadState ts(env);
1673 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 va_start(ap, mid);
1675 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 va_end(ap);
1677 return result.s;
1678 }
1679
1680 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
1686 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 ScopedJniThreadState ts(env);
1694 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 va_start(ap, mid);
1696 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 va_end(ap);
1698 return result.i;
1699 }
1700
1701 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
1707 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 ScopedJniThreadState ts(env);
1715 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 va_start(ap, mid);
1717 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_end(ap);
1719 return result.j;
1720 }
1721
1722 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
1728 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
1736 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 va_start(ap, mid);
1738 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 va_end(ap);
1740 return result.f;
1741 }
1742
1743 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
1749 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
1757 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 va_start(ap, mid);
1759 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_end(ap);
1761 return result.d;
1762 }
1763
1764 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
1770 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001771 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
1778 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 va_start(ap, mid);
1780 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 va_end(ap);
1782 }
1783
1784 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001787 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
1790 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001793 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 if (chars == NULL && char_count == 0) {
1799 return NULL;
1800 }
1801 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001802 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
1805 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1806 ScopedJniThreadState ts(env);
1807 if (utf == NULL) {
1808 return NULL;
1809 }
1810 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001811 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1815 ScopedJniThreadState ts(env);
1816 return Decode<String*>(ts, java_string)->GetLength();
1817 }
1818
1819 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1820 ScopedJniThreadState ts(env);
1821 return Decode<String*>(ts, java_string)->GetUtfLength();
1822 }
1823
Elliott Hughesb465ab02011-08-24 11:21:21 -07001824 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001826 String* s = Decode<String*>(ts, java_string);
1827 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1828 ThrowSIOOBE(ts, start, length, s->GetLength());
1829 } else {
1830 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1831 memcpy(buf, chars + start, length * sizeof(jchar));
1832 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 }
1834
Elliott Hughesb465ab02011-08-24 11:21:21 -07001835 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001836 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001837 String* s = Decode<String*>(ts, java_string);
1838 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1839 ThrowSIOOBE(ts, start, length, s->GetLength());
1840 } else {
1841 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1842 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1843 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001844 }
1845
Elliott Hughes75770752011-08-24 17:52:38 -07001846 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001847 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001848 String* s = Decode<String*>(ts, java_string);
1849 const CharArray* chars = s->GetCharArray();
1850 PinPrimitiveArray(ts, chars);
1851 if (is_copy != NULL) {
1852 *is_copy = JNI_FALSE;
1853 }
1854 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001855 }
1856
Elliott Hughes75770752011-08-24 17:52:38 -07001857 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001858 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001859 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes75770752011-08-24 17:52:38 -07001862 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001864 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes75770752011-08-24 17:52:38 -07001867 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001869 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes75770752011-08-24 17:52:38 -07001872 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001873 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001874 if (java_string == NULL) {
1875 return NULL;
1876 }
1877 if (is_copy != NULL) {
1878 *is_copy = JNI_TRUE;
1879 }
1880 String* s = Decode<String*>(ts, java_string);
1881 size_t byte_count = s->GetUtfLength();
1882 char* bytes = new char[byte_count + 1];
1883 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001884 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001885 return NULL;
1886 }
1887 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1888 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1889 bytes[byte_count] = '\0';
1890 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001891 }
1892
Elliott Hughes75770752011-08-24 17:52:38 -07001893 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001895 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 }
1897
Elliott Hughesbd935992011-08-22 11:59:34 -07001898 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001900 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001901 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001902 Array* array = obj->AsArray();
1903 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes814e4032011-08-23 12:07:56 -07001906 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001908 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001909 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
1912 static void SetObjectArrayElement(JNIEnv* env,
1913 jobjectArray java_array, jsize index, jobject java_value) {
1914 ScopedJniThreadState ts(env);
1915 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1916 Object* value = Decode<Object*>(ts, java_value);
1917 array->Set(index, value);
1918 }
1919
1920 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1921 ScopedJniThreadState ts(env);
1922 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1923 }
1924
1925 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1926 ScopedJniThreadState ts(env);
1927 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1928 }
1929
1930 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1931 ScopedJniThreadState ts(env);
1932 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1933 }
1934
1935 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1936 ScopedJniThreadState ts(env);
1937 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1938 }
1939
1940 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1941 ScopedJniThreadState ts(env);
1942 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1943 }
1944
1945 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1948 }
1949
1950 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1951 ScopedJniThreadState ts(env);
1952 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1953 }
1954
1955 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1956 ScopedJniThreadState ts(env);
1957 CHECK_GE(length, 0); // TODO: ReportJniError
1958
1959 // Compute the array class corresponding to the given element class.
1960 Class* element_class = Decode<Class*>(ts, element_jclass);
1961 std::string descriptor;
1962 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001963 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001964
1965 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001966 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1967 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 return NULL;
1969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 // Allocate and initialize if necessary.
1972 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 if (initial_element != NULL) {
1975 Object* initial_object = Decode<Object*>(ts, initial_element);
1976 for (jsize i = 0; i < length; ++i) {
1977 result->Set(i, initial_object);
1978 }
1979 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001980 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
1983 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1984 ScopedJniThreadState ts(env);
1985 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 Class* c = Decode<Class*>(ts, java_class);
2161
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 const char* name = methods[i].name;
2164 const char* sig = methods[i].signature;
2165
2166 if (*sig == '!') {
2167 // TODO: fast jni. it's too noisy to log all these.
2168 ++sig;
2169 }
2170
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 Method* m = c->FindDirectMethod(name, sig);
2172 if (m == NULL) {
2173 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002175 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002177 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2179 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002180 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002184 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2186 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002187 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 return JNI_ERR;
2189 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002190
Elliott Hughes75770752011-08-24 17:52:38 -07002191 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002192 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002193 }
2194
2195 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197 return JNI_OK;
2198 }
2199
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 Class* c = Decode<Class*>(ts, java_class);
2203
Elliott Hughes75770752011-08-24 17:52:38 -07002204 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205 LOG(INFO) << "[Unregistering JNI native methods for "
2206 << PrettyDescriptor(c->GetDescriptor()) << "]";
2207 }
2208
2209 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2210 Method* m = c->GetDirectMethod(i);
2211 if (m->IsNative()) {
2212 m->UnregisterNative();
2213 }
2214 }
2215 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2216 Method* m = c->GetVirtualMethod(i);
2217 if (m->IsNative()) {
2218 m->UnregisterNative();
2219 }
2220 }
2221
2222 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughes72025e52011-08-23 17:50:30 -07002225 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002227 Decode<Object*>(ts, java_object)->MonitorEnter();
2228 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes72025e52011-08-23 17:50:30 -07002231 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002233 Decode<Object*>(ts, java_object)->MonitorEnter();
2234 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
2237 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2238 ScopedJniThreadState ts(env);
2239 Runtime* runtime = Runtime::Current();
2240 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002241 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 } else {
2243 *vm = NULL;
2244 }
2245 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2246 }
2247
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2249 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250
2251 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002252 CHECK(address != NULL); // TODO: ReportJniError
2253 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254
2255 jclass buffer_class = GetDirectByteBufferClass(env);
2256 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2257 if (mid == NULL) {
2258 return NULL;
2259 }
2260
2261 // At the moment, the Java side is limited to 32 bits.
2262 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2263 CHECK_LE(capacity, 0xffffffff);
2264 jint address_arg = reinterpret_cast<jint>(address);
2265 jint capacity_arg = static_cast<jint>(capacity);
2266
2267 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2268 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2274 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2280 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Elliott Hughesb465ab02011-08-24 11:21:21 -07002283 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002285
Elliott Hughes75770752011-08-24 17:52:38 -07002286 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287
2288 // Do we definitely know what kind of reference this is?
2289 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2290 IndirectRefKind kind = GetIndirectRefKind(ref);
2291 switch (kind) {
2292 case kLocal:
2293 return JNILocalRefType;
2294 case kGlobal:
2295 return JNIGlobalRefType;
2296 case kWeakGlobal:
2297 return JNIWeakGlobalRefType;
2298 case kSirtOrInvalid:
2299 // Is it in a stack IRT?
2300 if (ts.Self()->SirtContains(java_object)) {
2301 return JNILocalRefType;
2302 }
2303
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002304 if (!ts.Env()->work_around_app_jni_bugs) {
2305 return JNIInvalidRefType;
2306 }
2307
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 // If we're handing out direct pointers, check whether it's a direct pointer
2309 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002310 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002311 if (ts.Env()->locals.Contains(java_object)) {
2312 return JNILocalRefType;
2313 }
2314 }
2315
2316 return JNIInvalidRefType;
2317 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002320
Elliott Hughesa2501992011-08-26 19:39:54 -07002321const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002322 NULL, // reserved0.
2323 NULL, // reserved1.
2324 NULL, // reserved2.
2325 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 JNI::GetVersion,
2327 JNI::DefineClass,
2328 JNI::FindClass,
2329 JNI::FromReflectedMethod,
2330 JNI::FromReflectedField,
2331 JNI::ToReflectedMethod,
2332 JNI::GetSuperclass,
2333 JNI::IsAssignableFrom,
2334 JNI::ToReflectedField,
2335 JNI::Throw,
2336 JNI::ThrowNew,
2337 JNI::ExceptionOccurred,
2338 JNI::ExceptionDescribe,
2339 JNI::ExceptionClear,
2340 JNI::FatalError,
2341 JNI::PushLocalFrame,
2342 JNI::PopLocalFrame,
2343 JNI::NewGlobalRef,
2344 JNI::DeleteGlobalRef,
2345 JNI::DeleteLocalRef,
2346 JNI::IsSameObject,
2347 JNI::NewLocalRef,
2348 JNI::EnsureLocalCapacity,
2349 JNI::AllocObject,
2350 JNI::NewObject,
2351 JNI::NewObjectV,
2352 JNI::NewObjectA,
2353 JNI::GetObjectClass,
2354 JNI::IsInstanceOf,
2355 JNI::GetMethodID,
2356 JNI::CallObjectMethod,
2357 JNI::CallObjectMethodV,
2358 JNI::CallObjectMethodA,
2359 JNI::CallBooleanMethod,
2360 JNI::CallBooleanMethodV,
2361 JNI::CallBooleanMethodA,
2362 JNI::CallByteMethod,
2363 JNI::CallByteMethodV,
2364 JNI::CallByteMethodA,
2365 JNI::CallCharMethod,
2366 JNI::CallCharMethodV,
2367 JNI::CallCharMethodA,
2368 JNI::CallShortMethod,
2369 JNI::CallShortMethodV,
2370 JNI::CallShortMethodA,
2371 JNI::CallIntMethod,
2372 JNI::CallIntMethodV,
2373 JNI::CallIntMethodA,
2374 JNI::CallLongMethod,
2375 JNI::CallLongMethodV,
2376 JNI::CallLongMethodA,
2377 JNI::CallFloatMethod,
2378 JNI::CallFloatMethodV,
2379 JNI::CallFloatMethodA,
2380 JNI::CallDoubleMethod,
2381 JNI::CallDoubleMethodV,
2382 JNI::CallDoubleMethodA,
2383 JNI::CallVoidMethod,
2384 JNI::CallVoidMethodV,
2385 JNI::CallVoidMethodA,
2386 JNI::CallNonvirtualObjectMethod,
2387 JNI::CallNonvirtualObjectMethodV,
2388 JNI::CallNonvirtualObjectMethodA,
2389 JNI::CallNonvirtualBooleanMethod,
2390 JNI::CallNonvirtualBooleanMethodV,
2391 JNI::CallNonvirtualBooleanMethodA,
2392 JNI::CallNonvirtualByteMethod,
2393 JNI::CallNonvirtualByteMethodV,
2394 JNI::CallNonvirtualByteMethodA,
2395 JNI::CallNonvirtualCharMethod,
2396 JNI::CallNonvirtualCharMethodV,
2397 JNI::CallNonvirtualCharMethodA,
2398 JNI::CallNonvirtualShortMethod,
2399 JNI::CallNonvirtualShortMethodV,
2400 JNI::CallNonvirtualShortMethodA,
2401 JNI::CallNonvirtualIntMethod,
2402 JNI::CallNonvirtualIntMethodV,
2403 JNI::CallNonvirtualIntMethodA,
2404 JNI::CallNonvirtualLongMethod,
2405 JNI::CallNonvirtualLongMethodV,
2406 JNI::CallNonvirtualLongMethodA,
2407 JNI::CallNonvirtualFloatMethod,
2408 JNI::CallNonvirtualFloatMethodV,
2409 JNI::CallNonvirtualFloatMethodA,
2410 JNI::CallNonvirtualDoubleMethod,
2411 JNI::CallNonvirtualDoubleMethodV,
2412 JNI::CallNonvirtualDoubleMethodA,
2413 JNI::CallNonvirtualVoidMethod,
2414 JNI::CallNonvirtualVoidMethodV,
2415 JNI::CallNonvirtualVoidMethodA,
2416 JNI::GetFieldID,
2417 JNI::GetObjectField,
2418 JNI::GetBooleanField,
2419 JNI::GetByteField,
2420 JNI::GetCharField,
2421 JNI::GetShortField,
2422 JNI::GetIntField,
2423 JNI::GetLongField,
2424 JNI::GetFloatField,
2425 JNI::GetDoubleField,
2426 JNI::SetObjectField,
2427 JNI::SetBooleanField,
2428 JNI::SetByteField,
2429 JNI::SetCharField,
2430 JNI::SetShortField,
2431 JNI::SetIntField,
2432 JNI::SetLongField,
2433 JNI::SetFloatField,
2434 JNI::SetDoubleField,
2435 JNI::GetStaticMethodID,
2436 JNI::CallStaticObjectMethod,
2437 JNI::CallStaticObjectMethodV,
2438 JNI::CallStaticObjectMethodA,
2439 JNI::CallStaticBooleanMethod,
2440 JNI::CallStaticBooleanMethodV,
2441 JNI::CallStaticBooleanMethodA,
2442 JNI::CallStaticByteMethod,
2443 JNI::CallStaticByteMethodV,
2444 JNI::CallStaticByteMethodA,
2445 JNI::CallStaticCharMethod,
2446 JNI::CallStaticCharMethodV,
2447 JNI::CallStaticCharMethodA,
2448 JNI::CallStaticShortMethod,
2449 JNI::CallStaticShortMethodV,
2450 JNI::CallStaticShortMethodA,
2451 JNI::CallStaticIntMethod,
2452 JNI::CallStaticIntMethodV,
2453 JNI::CallStaticIntMethodA,
2454 JNI::CallStaticLongMethod,
2455 JNI::CallStaticLongMethodV,
2456 JNI::CallStaticLongMethodA,
2457 JNI::CallStaticFloatMethod,
2458 JNI::CallStaticFloatMethodV,
2459 JNI::CallStaticFloatMethodA,
2460 JNI::CallStaticDoubleMethod,
2461 JNI::CallStaticDoubleMethodV,
2462 JNI::CallStaticDoubleMethodA,
2463 JNI::CallStaticVoidMethod,
2464 JNI::CallStaticVoidMethodV,
2465 JNI::CallStaticVoidMethodA,
2466 JNI::GetStaticFieldID,
2467 JNI::GetStaticObjectField,
2468 JNI::GetStaticBooleanField,
2469 JNI::GetStaticByteField,
2470 JNI::GetStaticCharField,
2471 JNI::GetStaticShortField,
2472 JNI::GetStaticIntField,
2473 JNI::GetStaticLongField,
2474 JNI::GetStaticFloatField,
2475 JNI::GetStaticDoubleField,
2476 JNI::SetStaticObjectField,
2477 JNI::SetStaticBooleanField,
2478 JNI::SetStaticByteField,
2479 JNI::SetStaticCharField,
2480 JNI::SetStaticShortField,
2481 JNI::SetStaticIntField,
2482 JNI::SetStaticLongField,
2483 JNI::SetStaticFloatField,
2484 JNI::SetStaticDoubleField,
2485 JNI::NewString,
2486 JNI::GetStringLength,
2487 JNI::GetStringChars,
2488 JNI::ReleaseStringChars,
2489 JNI::NewStringUTF,
2490 JNI::GetStringUTFLength,
2491 JNI::GetStringUTFChars,
2492 JNI::ReleaseStringUTFChars,
2493 JNI::GetArrayLength,
2494 JNI::NewObjectArray,
2495 JNI::GetObjectArrayElement,
2496 JNI::SetObjectArrayElement,
2497 JNI::NewBooleanArray,
2498 JNI::NewByteArray,
2499 JNI::NewCharArray,
2500 JNI::NewShortArray,
2501 JNI::NewIntArray,
2502 JNI::NewLongArray,
2503 JNI::NewFloatArray,
2504 JNI::NewDoubleArray,
2505 JNI::GetBooleanArrayElements,
2506 JNI::GetByteArrayElements,
2507 JNI::GetCharArrayElements,
2508 JNI::GetShortArrayElements,
2509 JNI::GetIntArrayElements,
2510 JNI::GetLongArrayElements,
2511 JNI::GetFloatArrayElements,
2512 JNI::GetDoubleArrayElements,
2513 JNI::ReleaseBooleanArrayElements,
2514 JNI::ReleaseByteArrayElements,
2515 JNI::ReleaseCharArrayElements,
2516 JNI::ReleaseShortArrayElements,
2517 JNI::ReleaseIntArrayElements,
2518 JNI::ReleaseLongArrayElements,
2519 JNI::ReleaseFloatArrayElements,
2520 JNI::ReleaseDoubleArrayElements,
2521 JNI::GetBooleanArrayRegion,
2522 JNI::GetByteArrayRegion,
2523 JNI::GetCharArrayRegion,
2524 JNI::GetShortArrayRegion,
2525 JNI::GetIntArrayRegion,
2526 JNI::GetLongArrayRegion,
2527 JNI::GetFloatArrayRegion,
2528 JNI::GetDoubleArrayRegion,
2529 JNI::SetBooleanArrayRegion,
2530 JNI::SetByteArrayRegion,
2531 JNI::SetCharArrayRegion,
2532 JNI::SetShortArrayRegion,
2533 JNI::SetIntArrayRegion,
2534 JNI::SetLongArrayRegion,
2535 JNI::SetFloatArrayRegion,
2536 JNI::SetDoubleArrayRegion,
2537 JNI::RegisterNatives,
2538 JNI::UnregisterNatives,
2539 JNI::MonitorEnter,
2540 JNI::MonitorExit,
2541 JNI::GetJavaVM,
2542 JNI::GetStringRegion,
2543 JNI::GetStringUTFRegion,
2544 JNI::GetPrimitiveArrayCritical,
2545 JNI::ReleasePrimitiveArrayCritical,
2546 JNI::GetStringCritical,
2547 JNI::ReleaseStringCritical,
2548 JNI::NewWeakGlobalRef,
2549 JNI::DeleteWeakGlobalRef,
2550 JNI::ExceptionCheck,
2551 JNI::NewDirectByteBuffer,
2552 JNI::GetDirectBufferAddress,
2553 JNI::GetDirectBufferCapacity,
2554 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002555};
2556
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002557static const size_t kMonitorsInitial = 32; // Arbitrary.
2558static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2559
2560static const size_t kLocalsInitial = 64; // Arbitrary.
2561static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002562
Elliott Hughes75770752011-08-24 17:52:38 -07002563JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002564 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002565 vm(vm),
2566 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002567 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002568 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002569 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2570 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002571 functions = unchecked_functions = &gNativeInterface;
2572 if (check_jni) {
2573 functions = GetCheckJniNativeInterface();
2574 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002575}
2576
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002577JNIEnvExt::~JNIEnvExt() {
2578}
2579
Carl Shapiroea4dca82011-08-01 13:45:38 -07002580// JNI Invocation interface.
2581
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2583 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2584 if (args->version < JNI_VERSION_1_2) {
2585 return JNI_EVERSION;
2586 }
2587 Runtime::Options options;
2588 for (int i = 0; i < args->nOptions; ++i) {
2589 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002590 options.push_back(std::make_pair(StringPiece(option->optionString),
2591 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 }
2593 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002594 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 if (runtime == NULL) {
2596 return JNI_ERR;
2597 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002598 *p_env = Thread::Current()->GetJniEnv();
2599 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002600 return JNI_OK;
2601 }
2602}
2603
Elliott Hughesf2682d52011-08-15 16:37:04 -07002604extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605 Runtime* runtime = Runtime::Current();
2606 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002607 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002609 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002610 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
2612 return JNI_OK;
2613}
2614
2615// Historically unsupported.
2616extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2617 return JNI_ERR;
2618}
2619
Elliott Hughescdf53122011-08-19 15:46:09 -07002620class JII {
2621 public:
2622 static jint DestroyJavaVM(JavaVM* vm) {
2623 if (vm == NULL) {
2624 return JNI_ERR;
2625 } else {
2626 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2627 delete raw_vm->runtime;
2628 return JNI_OK;
2629 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002630 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631
Elliott Hughescdf53122011-08-19 15:46:09 -07002632 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002633 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002634 }
2635
2636 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002637 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002638 }
2639
2640 static jint DetachCurrentThread(JavaVM* vm) {
2641 if (vm == NULL) {
2642 return JNI_ERR;
2643 } else {
2644 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2645 Runtime* runtime = raw_vm->runtime;
2646 runtime->DetachCurrentThread();
2647 return JNI_OK;
2648 }
2649 }
2650
2651 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2652 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2653 return JNI_EVERSION;
2654 }
2655 if (vm == NULL || env == NULL) {
2656 return JNI_ERR;
2657 }
2658 Thread* thread = Thread::Current();
2659 if (thread == NULL) {
2660 *env = NULL;
2661 return JNI_EDETACHED;
2662 }
2663 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 return JNI_OK;
2665 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002666};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002667
Elliott Hughesa2501992011-08-26 19:39:54 -07002668const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002669 NULL, // reserved0
2670 NULL, // reserved1
2671 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 JII::DestroyJavaVM,
2673 JII::AttachCurrentThread,
2674 JII::DetachCurrentThread,
2675 JII::GetEnv,
2676 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002677};
2678
Elliott Hughesbbd76712011-08-17 10:25:24 -07002679static const size_t kPinTableInitialSize = 16;
2680static const size_t kPinTableMaxSize = 1024;
2681
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002682static const size_t kGlobalsInitial = 512; // Arbitrary.
2683static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2684
2685static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2686static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2687
Elliott Hughesa0957642011-09-02 14:27:33 -07002688JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002689 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002690 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002691 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002692 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002693 verbose_jni(options->IsVerbose("jni")),
2694 log_third_party_jni(options->IsVerbose("third-party-jni")),
2695 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002696 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002697 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002698 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002699 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002700 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002701 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002702 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2703 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2704 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002705 functions = unchecked_functions = &gInvokeInterface;
2706 if (check_jni) {
2707 functions = GetCheckJniInvokeInterface();
2708 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002709}
2710
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002711JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002712 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002713 delete globals_lock;
2714 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002715 delete libraries_lock;
2716 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002717}
2718
Elliott Hughes75770752011-08-24 17:52:38 -07002719bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2720 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002721
2722 // See if we've already loaded this library. If we have, and the class loader
2723 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002724 // TODO: for better results we should canonicalize the pathname (or even compare
2725 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002726 SharedLibrary* library;
2727 {
2728 // TODO: move the locking (and more of this logic) into Libraries.
2729 MutexLock mu(libraries_lock);
2730 library = libraries->Get(path);
2731 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002732 if (library != NULL) {
2733 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002734 // The library will be associated with class_loader. The JNI
2735 // spec says we can't load the same library into more than one
2736 // class loader.
2737 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2738 "ClassLoader %p; can't open in ClassLoader %p",
2739 path.c_str(), library->GetClassLoader(), class_loader);
2740 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 return false;
2742 }
2743 if (verbose_jni) {
2744 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2745 << "ClassLoader " << class_loader << "]";
2746 }
2747 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002748 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2749 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002750 return false;
2751 }
2752 return true;
2753 }
2754
2755 // Open the shared library. Because we're using a full path, the system
2756 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2757 // resolve this library's dependencies though.)
2758
2759 // Failures here are expected when java.library.path has several entries
2760 // and we have to hunt for the lib.
2761
2762 // The current version of the dynamic linker prints detailed information
2763 // about dlopen() failures. Some things to check if the message is
2764 // cryptic:
2765 // - make sure the library exists on the device
2766 // - verify that the right path is being opened (the debug log message
2767 // above can help with that)
2768 // - check to see if the library is valid (e.g. not zero bytes long)
2769 // - check config/prelink-linux-arm.map to ensure that the library
2770 // is listed and is not being overrun by the previous entry (if
2771 // loading suddenly stops working on a prelinked library, this is
2772 // a good one to check)
2773 // - write a trivial app that calls sleep() then dlopen(), attach
2774 // to it with "strace -p <pid>" while it sleeps, and watch for
2775 // attempts to open nonexistent dependent shared libs
2776
2777 // TODO: automate some of these checks!
2778
2779 // This can execute slowly for a large library on a busy system, so we
2780 // want to switch from RUNNING to VMWAIT while it executes. This allows
2781 // the GC to ignore us.
2782 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002783 void* handle = NULL;
2784 {
2785 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2786 handle = dlopen(path.c_str(), RTLD_LAZY);
2787 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002788
2789 if (verbose_jni) {
2790 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2791 }
2792
2793 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002794 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002795 return false;
2796 }
2797
2798 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002799 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 // TODO: move the locking (and more of this logic) into Libraries.
2801 MutexLock mu(libraries_lock);
2802 library = libraries->Get(path);
2803 if (library != NULL) {
2804 LOG(INFO) << "WOW: we lost a race to add shared library: "
2805 << "\"" << path << "\" ClassLoader=" << class_loader;
2806 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002808 library = new SharedLibrary(path, handle, class_loader);
2809 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002811
2812 if (verbose_jni) {
2813 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2814 }
2815
2816 bool result = true;
2817 void* sym = dlsym(handle, "JNI_OnLoad");
2818 if (sym == NULL) {
2819 if (verbose_jni) {
2820 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2821 }
2822 } else {
2823 // Call JNI_OnLoad. We have to override the current class
2824 // loader, which will always be "null" since the stuff at the
2825 // top of the stack is around Runtime.loadLibrary(). (See
2826 // the comments in the JNI FindClass function.)
2827 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2828 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002829 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002830 self->SetClassLoaderOverride(class_loader);
2831
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002832 int version = 0;
2833 {
2834 ScopedThreadStateChange tsc(self, Thread::kNative);
2835 if (verbose_jni) {
2836 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2837 }
2838 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002840
2841 self->SetClassLoaderOverride(old_class_loader);;
2842
2843 if (version != JNI_VERSION_1_2 &&
2844 version != JNI_VERSION_1_4 &&
2845 version != JNI_VERSION_1_6) {
2846 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2847 << "bad version: " << version;
2848 // It's unwise to call dlclose() here, but we can mark it
2849 // as bad and ensure that future load attempts will fail.
2850 // We don't know how far JNI_OnLoad got, so there could
2851 // be some partially-initialized stuff accessible through
2852 // newly-registered native method calls. We could try to
2853 // unregister them, but that doesn't seem worthwhile.
2854 result = false;
2855 } else {
2856 if (verbose_jni) {
2857 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2858 << " from JNI_OnLoad in \"" << path << "\"]";
2859 }
2860 }
2861 }
2862
2863 library->SetResult(result);
2864 return result;
2865}
2866
2867void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2868 CHECK(m->IsNative());
2869
2870 Class* c = m->GetDeclaringClass();
2871
2872 // If this is a static method, it could be called before the class
2873 // has been initialized.
2874 if (m->IsStatic()) {
2875 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2876 return NULL;
2877 }
2878 } else {
2879 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2880 }
2881
2882 MutexLock mu(libraries_lock);
2883 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002884}
2885
Elliott Hughes410c0c82011-09-01 17:58:25 -07002886void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2887 {
2888 MutexLock mu(globals_lock);
2889 globals.VisitRoots(visitor, arg);
2890 }
2891 {
2892 MutexLock mu(pins_lock);
2893 pin_table.VisitRoots(visitor, arg);
2894 }
2895 // The weak_globals table is visited by the GC itself (because it mutates the table).
2896}
2897
Ian Rogersdf20fe02011-07-20 20:34:16 -07002898} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002899
2900std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2901 switch (rhs) {
2902 case JNIInvalidRefType:
2903 os << "JNIInvalidRefType";
2904 return os;
2905 case JNILocalRefType:
2906 os << "JNILocalRefType";
2907 return os;
2908 case JNIGlobalRefType:
2909 os << "JNIGlobalRefType";
2910 return os;
2911 case JNIWeakGlobalRefType:
2912 os << "JNIWeakGlobalRefType";
2913 return os;
2914 }
2915}