blob: a7c7dc77707cfb894a2a4322c525aae0c612f519 [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;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700493 runtime->AttachCurrentThread(args.name, p_env, as_daemon);
494 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700495}
496
Elliott Hughes79082e32011-08-25 12:07:32 -0700497class SharedLibrary {
498 public:
499 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
500 : path_(path),
501 handle_(handle),
502 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
503 jni_on_load_tid_(Thread::Current()->GetId()),
504 jni_on_load_result_(kPending) {
505 pthread_cond_init(&jni_on_load_cond_, NULL);
506 }
507
508 ~SharedLibrary() {
509 delete jni_on_load_lock_;
510 }
511
512 Object* GetClassLoader() {
513 return class_loader_;
514 }
515
516 std::string GetPath() {
517 return path_;
518 }
519
520 /*
521 * Check the result of an earlier call to JNI_OnLoad on this library. If
522 * the call has not yet finished in another thread, wait for it.
523 */
524 bool CheckOnLoadResult(JavaVMExt* vm) {
525 Thread* self = Thread::Current();
526 if (jni_on_load_tid_ == self->GetId()) {
527 // Check this so we don't end up waiting for ourselves. We need
528 // to return "true" so the caller can continue.
529 LOG(INFO) << *self << " recursive attempt to load library "
530 << "\"" << path_ << "\"";
531 return true;
532 }
533
534 MutexLock mu(jni_on_load_lock_);
535 while (jni_on_load_result_ == kPending) {
536 if (vm->verbose_jni) {
537 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
538 << "JNI_OnLoad...]";
539 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700540 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 }
543
544 bool okay = (jni_on_load_result_ == kOkay);
545 if (vm->verbose_jni) {
546 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
547 << (okay ? "succeeded" : "failed") << "]";
548 }
549 return okay;
550 }
551
552 void SetResult(bool result) {
553 jni_on_load_result_ = result ? kOkay : kFailed;
554 jni_on_load_tid_ = 0;
555
556 // Broadcast a wakeup to anybody sleeping on the condition variable.
557 MutexLock mu(jni_on_load_lock_);
558 pthread_cond_broadcast(&jni_on_load_cond_);
559 }
560
561 void* FindSymbol(const std::string& symbol_name) {
562 return dlsym(handle_, symbol_name.c_str());
563 }
564
565 private:
566 enum JNI_OnLoadState {
567 kPending,
568 kFailed,
569 kOkay,
570 };
571
572 // Path to library "/system/lib/libjni.so".
573 std::string path_;
574
575 // The void* returned by dlopen(3).
576 void* handle_;
577
578 // The ClassLoader this library is associated with.
579 Object* class_loader_;
580
581 // Guards remaining items.
582 Mutex* jni_on_load_lock_;
583 // Wait for JNI_OnLoad in other thread.
584 pthread_cond_t jni_on_load_cond_;
585 // Recursive invocation guard.
586 uint32_t jni_on_load_tid_;
587 // Result of earlier JNI_OnLoad call.
588 JNI_OnLoadState jni_on_load_result_;
589};
590
Elliott Hughescdf53122011-08-19 15:46:09 -0700591} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700592
Elliott Hughes79082e32011-08-25 12:07:32 -0700593// This exists mainly to keep implementation details out of the header file.
594class Libraries {
595 public:
596 Libraries() {
597 }
598
599 ~Libraries() {
600 // Delete our map values. (The keys will be cleaned up by the map itself.)
601 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
602 delete it->second;
603 }
604 }
605
606 SharedLibrary* Get(const std::string& path) {
607 return libraries_[path];
608 }
609
610 void Put(const std::string& path, SharedLibrary* library) {
611 libraries_[path] = library;
612 }
613
614 // See section 11.3 "Linking Native Methods" of the JNI spec.
615 void* FindNativeMethod(const Method* m) {
616 std::string jni_short_name(JniShortName(m));
617 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700618 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
620 SharedLibrary* library = it->second;
621 if (library->GetClassLoader() != declaring_class_loader) {
622 // We only search libraries loaded by the appropriate ClassLoader.
623 continue;
624 }
625 // Try the short name then the long name...
626 void* fn = library->FindSymbol(jni_short_name);
627 if (fn == NULL) {
628 fn = library->FindSymbol(jni_long_name);
629 }
630 if (fn != NULL) {
631 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700632 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700633 << " in \"" << library->GetPath() << "\"]";
634 }
635 return fn;
636 }
637 }
638 std::string detail;
639 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700640 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700641 LOG(ERROR) << detail;
642 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
643 "%s", detail.c_str());
644 return NULL;
645 }
646
647 private:
648 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
649
650 std::map<std::string, SharedLibrary*> libraries_;
651};
652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653class JNI {
654 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 static jint GetVersion(JNIEnv* env) {
657 ScopedJniThreadState ts(env);
658 return JNI_VERSION_1_6;
659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
662 ScopedJniThreadState ts(env);
663 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700664 return NULL;
665 }
666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 static jclass FindClass(JNIEnv* env, const char* name) {
668 ScopedJniThreadState ts(env);
669 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
670 std::string descriptor(NormalizeJniClassDescriptor(name));
671 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700672 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700673 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700674 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
678 ScopedJniThreadState ts(env);
679 Method* method = Decode<Method*>(ts, java_method);
680 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
684 ScopedJniThreadState ts(env);
685 Field* field = Decode<Field*>(ts, java_field);
686 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
690 ScopedJniThreadState ts(env);
691 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700692 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
696 ScopedJniThreadState ts(env);
697 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700698 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
702 ScopedJniThreadState ts(env);
703 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700704 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 }
706
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700707 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700709 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700710 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700712
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 Class* c1 = Decode<Class*>(ts, java_class1);
716 Class* c2 = Decode<Class*>(ts, java_class2);
717 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700722 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700723 if (jobj == NULL) {
724 // NB. JNI is different from regular Java instanceof in this respect
725 return JNI_TRUE;
726 } else {
727 Object* obj = Decode<Object*>(ts, jobj);
728 Class* klass = Decode<Class*>(ts, clazz);
729 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
730 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 static jint Throw(JNIEnv* env, jthrowable java_exception) {
734 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700735 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 if (exception == NULL) {
737 return JNI_ERR;
738 }
739 ts.Self()->SetException(exception);
740 return JNI_OK;
741 }
742
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700743 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700744 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700745 // TODO: check for a pending exception to decide what constructor to call.
746 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
747 if (mid == NULL) {
748 return JNI_ERR;
749 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700750 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
751 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700752 return JNI_ERR;
753 }
754
755 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 args[0].l = s.get();
757 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
758 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700759 return JNI_ERR;
760 }
761
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700763 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700765
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 return JNI_OK;
767 }
768
769 static jboolean ExceptionCheck(JNIEnv* env) {
770 ScopedJniThreadState ts(env);
771 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
772 }
773
774 static void ExceptionClear(JNIEnv* env) {
775 ScopedJniThreadState ts(env);
776 ts.Self()->ClearException();
777 }
778
779 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700781
782 Thread* self = ts.Self();
783 Throwable* original_exception = self->GetException();
784 self->ClearException();
785
Elliott Hughesbf86d042011-08-31 17:53:14 -0700786 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
788 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
789 if (mid == NULL) {
790 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
791 << PrettyType(original_exception);
792 } else {
793 env->CallVoidMethod(exception.get(), mid);
794 if (self->IsExceptionPending()) {
795 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
796 << " thrown while calling printStackTrace";
797 self->ClearException();
798 }
799 }
800
801 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700803
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 static jthrowable ExceptionOccurred(JNIEnv* env) {
805 ScopedJniThreadState ts(env);
806 Object* exception = ts.Self()->GetException();
807 if (exception == NULL) {
808 return NULL;
809 } else {
810 // TODO: if adding a local reference failing causes the VM to abort
811 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700812 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 if (localException == NULL) {
814 // We were unable to add a new local reference, and threw a new
815 // exception. We can't return "exception", because it's not a
816 // local reference. So we have to return NULL, indicating that
817 // there was no exception, even though it's pretty much raining
818 // exceptions in here.
819 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
820 }
821 return localException;
822 }
823 }
824
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 static void FatalError(JNIEnv* env, const char* msg) {
826 ScopedJniThreadState ts(env);
827 LOG(FATAL) << "JNI FatalError called: " << msg;
828 }
829
830 static jint PushLocalFrame(JNIEnv* env, jint cap) {
831 ScopedJniThreadState ts(env);
832 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
833 return JNI_OK;
834 }
835
836 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
837 ScopedJniThreadState ts(env);
838 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
839 return res;
840 }
841
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
843 ScopedJniThreadState ts(env);
844 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
845 return 0;
846 }
847
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
849 ScopedJniThreadState ts(env);
850 if (obj == NULL) {
851 return NULL;
852 }
853
Elliott Hughes75770752011-08-24 17:52:38 -0700854 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 IndirectReferenceTable& globals = vm->globals;
856 MutexLock mu(vm->globals_lock);
857 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
858 return reinterpret_cast<jobject>(ref);
859 }
860
861 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
862 ScopedJniThreadState ts(env);
863 if (obj == NULL) {
864 return;
865 }
866
Elliott Hughes75770752011-08-24 17:52:38 -0700867 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 IndirectReferenceTable& globals = vm->globals;
869 MutexLock mu(vm->globals_lock);
870
871 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
872 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
873 << "failed to find entry";
874 }
875 }
876
877 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
878 ScopedJniThreadState ts(env);
879 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
880 }
881
882 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
883 ScopedJniThreadState ts(env);
884 if (obj == NULL) {
885 return;
886 }
887
Elliott Hughes75770752011-08-24 17:52:38 -0700888 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 IndirectReferenceTable& weak_globals = vm->weak_globals;
890 MutexLock mu(vm->weak_globals_lock);
891
892 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
893 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
894 << "failed to find entry";
895 }
896 }
897
898 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
899 ScopedJniThreadState ts(env);
900 if (obj == NULL) {
901 return NULL;
902 }
903
904 IndirectReferenceTable& locals = ts.Env()->locals;
905
906 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
907 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
908 return reinterpret_cast<jobject>(ref);
909 }
910
911 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
912 ScopedJniThreadState ts(env);
913 if (obj == NULL) {
914 return;
915 }
916
917 IndirectReferenceTable& locals = ts.Env()->locals;
918
919 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
920 if (!locals.Remove(cookie, obj)) {
921 // Attempting to delete a local reference that is not in the
922 // topmost local reference frame is a no-op. DeleteLocalRef returns
923 // void and doesn't throw any exceptions, but we should probably
924 // complain about it so the user will notice that things aren't
925 // going quite the way they expect.
926 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
927 << "failed to find entry";
928 }
929 }
930
931 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
932 ScopedJniThreadState ts(env);
933 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
934 ? JNI_TRUE : JNI_FALSE;
935 }
936
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700937 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700939 Class* c = Decode<Class*>(ts, java_class);
940 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
941 return NULL;
942 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700943 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 ScopedJniThreadState ts(env);
948 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 va_start(args, mid);
950 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 va_end(args);
952 return result;
953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700957 Class* c = Decode<Class*>(ts, java_class);
958 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
959 return NULL;
960 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700961 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700962 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 return local_result;
965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700969 Class* c = Decode<Class*>(ts, java_class);
970 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
971 return NULL;
972 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700973 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700974 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 return local_result;
977 }
978
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
980 ScopedJniThreadState ts(env);
981 return FindMethodID(ts, c, name, sig, false);
982 }
983
984 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
985 ScopedJniThreadState ts(env);
986 return FindMethodID(ts, c, name, sig, true);
987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700993 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700995 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001000 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001001 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001006 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001007 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001014 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_end(ap);
1016 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001021 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001026 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001033 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_end(ap);
1035 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001040 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001045 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_list ap;
1051 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001052 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_end(ap);
1054 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001059 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001064 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001071 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_end(ap);
1073 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001078 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001083 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001090 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
1092 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001097 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001102 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_list ap;
1108 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001109 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_end(ap);
1111 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001116 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001121 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_list ap;
1127 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001128 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
1130 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001135 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001140 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_list ap;
1146 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001147 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_end(ap);
1149 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001154 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001159 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_list ap;
1165 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001166 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001172 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001177 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
1183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
1185 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001186 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_end(ap);
1188 return local_result;
1189 }
1190
1191 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001195 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001202 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
1208 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 va_start(ap, mid);
1210 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_end(ap);
1212 return result.z;
1213 }
1214
1215 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
1227 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
1230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
1232 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_end(ap);
1234 return result.b;
1235 }
1236
1237 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
1252 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_start(ap, mid);
1254 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
1256 return result.c;
1257 }
1258
1259 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
1274 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 va_start(ap, mid);
1276 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
1278 return result.s;
1279 }
1280
1281 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
1296 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 va_start(ap, mid);
1298 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_end(ap);
1300 return result.i;
1301 }
1302
1303 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
1318 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 va_start(ap, mid);
1320 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
1322 return result.j;
1323 }
1324
1325 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
1340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
1342 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_end(ap);
1344 return result.f;
1345 }
1346
1347 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
1362 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 va_start(ap, mid);
1364 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_end(ap);
1366 return result.d;
1367 }
1368
1369 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
1375 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001378 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
1381 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
1384 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001385 va_start(ap, mid);
1386 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 va_end(ap);
1388 }
1389
1390 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
1396 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
1402 static jfieldID GetFieldID(JNIEnv* env,
1403 jclass c, const char* name, const char* sig) {
1404 ScopedJniThreadState ts(env);
1405 return FindFieldID(ts, c, name, sig, false);
1406 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001407
1408
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 static jfieldID GetStaticFieldID(JNIEnv* env,
1410 jclass c, const char* name, const char* sig) {
1411 ScopedJniThreadState ts(env);
1412 return FindFieldID(ts, c, name, sig, true);
1413 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 Object* o = Decode<Object*>(ts, obj);
1418 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001419 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001425 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 Object* o = Decode<Object*>(ts, java_object);
1431 Object* v = Decode<Object*>(ts, java_value);
1432 Field* f = DecodeField(ts, fid);
1433 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 Object* v = Decode<Object*>(ts, java_value);
1439 Field* f = DecodeField(ts, fid);
1440 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443#define GET_PRIMITIVE_FIELD(fn, instance) \
1444 ScopedJniThreadState ts(env); \
1445 Object* o = Decode<Object*>(ts, instance); \
1446 Field* f = DecodeField(ts, fid); \
1447 return f->fn(o)
1448
1449#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1450 ScopedJniThreadState ts(env); \
1451 Object* o = Decode<Object*>(ts, instance); \
1452 Field* f = DecodeField(ts, fid); \
1453 f->fn(o, value)
1454
1455 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetLong, NULL);
1509 }
1510
1511 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1513 }
1514
1515 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1517 }
1518
1519 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1520 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1521 }
1522
1523 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1524 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1525 }
1526
1527 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1528 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1529 }
1530
1531 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1532 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1533 }
1534
1535 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1536 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1537 }
1538
1539 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1540 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1541 }
1542
1543 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1544 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1545 }
1546
1547 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1548 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1549 }
1550
1551 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1552 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1553 }
1554
1555 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1556 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1557 }
1558
1559 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1560 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1561 }
1562
1563 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1564 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1565 }
1566
1567 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1568 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1569 }
1570
1571 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1572 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1573 }
1574
1575 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1576 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1577 }
1578
1579 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1580 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
1583 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 ScopedJniThreadState ts(env);
1586 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 va_start(ap, mid);
1588 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001589 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_end(ap);
1591 return local_result;
1592 }
1593
1594 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001598 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
1601 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001605 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
1608 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
1611 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 va_start(ap, mid);
1613 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_end(ap);
1615 return result.z;
1616 }
1617
1618 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
1624 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
1632 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 va_start(ap, mid);
1634 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 va_end(ap);
1636 return result.b;
1637 }
1638
1639 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
1645 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
1653 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 va_start(ap, mid);
1655 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 va_end(ap);
1657 return result.c;
1658 }
1659
1660 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
1666 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
1674 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 va_start(ap, mid);
1676 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 va_end(ap);
1678 return result.s;
1679 }
1680
1681 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
1687 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
1695 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 va_start(ap, mid);
1697 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_end(ap);
1699 return result.i;
1700 }
1701
1702 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
1708 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
1716 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 va_start(ap, mid);
1718 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_end(ap);
1720 return result.j;
1721 }
1722
1723 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
1729 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
1737 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 va_start(ap, mid);
1739 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_end(ap);
1741 return result.f;
1742 }
1743
1744 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
1750 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
1758 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001759 va_start(ap, mid);
1760 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 va_end(ap);
1762 return result.d;
1763 }
1764
1765 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001768 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
1771 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes72025e52011-08-23 17:50:30 -07001777 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
1779 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001780 va_start(ap, mid);
1781 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_end(ap);
1783 }
1784
1785 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001786 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001788 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
1791 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001792 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001794 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001799 if (chars == NULL && char_count == 0) {
1800 return NULL;
1801 }
1802 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001803 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
1806 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1807 ScopedJniThreadState ts(env);
1808 if (utf == NULL) {
1809 return NULL;
1810 }
1811 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001812 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes814e4032011-08-23 12:07:56 -07001815 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1816 ScopedJniThreadState ts(env);
1817 return Decode<String*>(ts, java_string)->GetLength();
1818 }
1819
1820 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1821 ScopedJniThreadState ts(env);
1822 return Decode<String*>(ts, java_string)->GetUtfLength();
1823 }
1824
Elliott Hughesb465ab02011-08-24 11:21:21 -07001825 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001827 String* s = Decode<String*>(ts, java_string);
1828 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1829 ThrowSIOOBE(ts, start, length, s->GetLength());
1830 } else {
1831 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1832 memcpy(buf, chars + start, length * sizeof(jchar));
1833 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001834 }
1835
Elliott Hughesb465ab02011-08-24 11:21:21 -07001836 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001838 String* s = Decode<String*>(ts, java_string);
1839 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1840 ThrowSIOOBE(ts, start, length, s->GetLength());
1841 } else {
1842 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1843 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1844 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001845 }
1846
Elliott Hughes75770752011-08-24 17:52:38 -07001847 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001848 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001849 String* s = Decode<String*>(ts, java_string);
1850 const CharArray* chars = s->GetCharArray();
1851 PinPrimitiveArray(ts, chars);
1852 if (is_copy != NULL) {
1853 *is_copy = JNI_FALSE;
1854 }
1855 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes75770752011-08-24 17:52:38 -07001863 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001865 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes75770752011-08-24 17:52:38 -07001868 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001870 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
Elliott Hughes75770752011-08-24 17:52:38 -07001873 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001874 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001875 if (java_string == NULL) {
1876 return NULL;
1877 }
1878 if (is_copy != NULL) {
1879 *is_copy = JNI_TRUE;
1880 }
1881 String* s = Decode<String*>(ts, java_string);
1882 size_t byte_count = s->GetUtfLength();
1883 char* bytes = new char[byte_count + 1];
1884 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001885 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001886 return NULL;
1887 }
1888 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1889 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1890 bytes[byte_count] = '\0';
1891 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 }
1893
Elliott Hughes75770752011-08-24 17:52:38 -07001894 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001895 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001896 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 }
1898
Elliott Hughesbd935992011-08-22 11:59:34 -07001899 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001901 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001902 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001903 Array* array = obj->AsArray();
1904 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
Elliott Hughes814e4032011-08-23 12:07:56 -07001907 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001909 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001910 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
1913 static void SetObjectArrayElement(JNIEnv* env,
1914 jobjectArray java_array, jsize index, jobject java_value) {
1915 ScopedJniThreadState ts(env);
1916 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1917 Object* value = Decode<Object*>(ts, java_value);
1918 array->Set(index, value);
1919 }
1920
1921 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1922 ScopedJniThreadState ts(env);
1923 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1924 }
1925
1926 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1927 ScopedJniThreadState ts(env);
1928 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1929 }
1930
1931 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1932 ScopedJniThreadState ts(env);
1933 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1934 }
1935
1936 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1937 ScopedJniThreadState ts(env);
1938 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1939 }
1940
1941 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1942 ScopedJniThreadState ts(env);
1943 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1944 }
1945
1946 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1947 ScopedJniThreadState ts(env);
1948 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1949 }
1950
1951 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1952 ScopedJniThreadState ts(env);
1953 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1954 }
1955
1956 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1957 ScopedJniThreadState ts(env);
1958 CHECK_GE(length, 0); // TODO: ReportJniError
1959
1960 // Compute the array class corresponding to the given element class.
1961 Class* element_class = Decode<Class*>(ts, element_jclass);
1962 std::string descriptor;
1963 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001964 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001965
1966 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001967 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1968 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 return NULL;
1970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 // Allocate and initialize if necessary.
1973 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 if (initial_element != NULL) {
1976 Object* initial_object = Decode<Object*>(ts, initial_element);
1977 for (jsize i = 0; i < length; ++i) {
1978 result->Set(i, initial_object);
1979 }
1980 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001981 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
1984 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1985 ScopedJniThreadState ts(env);
1986 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002041 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002046 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes75770752011-08-24 17:52:38 -07002054 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes75770752011-08-24 17:52:38 -07002059 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002061 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes75770752011-08-24 17:52:38 -07002064 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002066 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes75770752011-08-24 17:52:38 -07002069 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002071 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes75770752011-08-24 17:52:38 -07002074 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002076 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002161 Class* c = Decode<Class*>(ts, java_class);
2162
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 const char* name = methods[i].name;
2165 const char* sig = methods[i].signature;
2166
2167 if (*sig == '!') {
2168 // TODO: fast jni. it's too noisy to log all these.
2169 ++sig;
2170 }
2171
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 Method* m = c->FindDirectMethod(name, sig);
2173 if (m == NULL) {
2174 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2180 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002181 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002185 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2187 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002188 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 return JNI_ERR;
2190 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002191
Elliott Hughes75770752011-08-24 17:52:38 -07002192 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002193 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194 }
2195
2196 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198 return JNI_OK;
2199 }
2200
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002203 Class* c = Decode<Class*>(ts, java_class);
2204
Elliott Hughes75770752011-08-24 17:52:38 -07002205 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002206 LOG(INFO) << "[Unregistering JNI native methods for "
2207 << PrettyDescriptor(c->GetDescriptor()) << "]";
2208 }
2209
2210 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2211 Method* m = c->GetDirectMethod(i);
2212 if (m->IsNative()) {
2213 m->UnregisterNative();
2214 }
2215 }
2216 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2217 Method* m = c->GetVirtualMethod(i);
2218 if (m->IsNative()) {
2219 m->UnregisterNative();
2220 }
2221 }
2222
2223 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughes72025e52011-08-23 17:50:30 -07002226 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002228 Decode<Object*>(ts, java_object)->MonitorEnter();
2229 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes72025e52011-08-23 17:50:30 -07002232 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002234 Decode<Object*>(ts, java_object)->MonitorEnter();
2235 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
2238 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2239 ScopedJniThreadState ts(env);
2240 Runtime* runtime = Runtime::Current();
2241 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002242 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 } else {
2244 *vm = NULL;
2245 }
2246 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2247 }
2248
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2250 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
2252 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002253 CHECK(address != NULL); // TODO: ReportJniError
2254 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255
2256 jclass buffer_class = GetDirectByteBufferClass(env);
2257 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2258 if (mid == NULL) {
2259 return NULL;
2260 }
2261
2262 // At the moment, the Java side is limited to 32 bits.
2263 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2264 CHECK_LE(capacity, 0xffffffff);
2265 jint address_arg = reinterpret_cast<jint>(address);
2266 jint capacity_arg = static_cast<jint>(capacity);
2267
2268 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2269 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 }
2271
Elliott Hughesb465ab02011-08-24 11:21:21 -07002272 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2275 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2281 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286
Elliott Hughes75770752011-08-24 17:52:38 -07002287 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002288
2289 // Do we definitely know what kind of reference this is?
2290 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2291 IndirectRefKind kind = GetIndirectRefKind(ref);
2292 switch (kind) {
2293 case kLocal:
2294 return JNILocalRefType;
2295 case kGlobal:
2296 return JNIGlobalRefType;
2297 case kWeakGlobal:
2298 return JNIWeakGlobalRefType;
2299 case kSirtOrInvalid:
2300 // Is it in a stack IRT?
2301 if (ts.Self()->SirtContains(java_object)) {
2302 return JNILocalRefType;
2303 }
2304
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002305 if (!ts.Env()->work_around_app_jni_bugs) {
2306 return JNIInvalidRefType;
2307 }
2308
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309 // If we're handing out direct pointers, check whether it's a direct pointer
2310 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002311 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312 if (ts.Env()->locals.Contains(java_object)) {
2313 return JNILocalRefType;
2314 }
2315 }
2316
2317 return JNIInvalidRefType;
2318 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002321
Elliott Hughesa2501992011-08-26 19:39:54 -07002322const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002323 NULL, // reserved0.
2324 NULL, // reserved1.
2325 NULL, // reserved2.
2326 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 JNI::GetVersion,
2328 JNI::DefineClass,
2329 JNI::FindClass,
2330 JNI::FromReflectedMethod,
2331 JNI::FromReflectedField,
2332 JNI::ToReflectedMethod,
2333 JNI::GetSuperclass,
2334 JNI::IsAssignableFrom,
2335 JNI::ToReflectedField,
2336 JNI::Throw,
2337 JNI::ThrowNew,
2338 JNI::ExceptionOccurred,
2339 JNI::ExceptionDescribe,
2340 JNI::ExceptionClear,
2341 JNI::FatalError,
2342 JNI::PushLocalFrame,
2343 JNI::PopLocalFrame,
2344 JNI::NewGlobalRef,
2345 JNI::DeleteGlobalRef,
2346 JNI::DeleteLocalRef,
2347 JNI::IsSameObject,
2348 JNI::NewLocalRef,
2349 JNI::EnsureLocalCapacity,
2350 JNI::AllocObject,
2351 JNI::NewObject,
2352 JNI::NewObjectV,
2353 JNI::NewObjectA,
2354 JNI::GetObjectClass,
2355 JNI::IsInstanceOf,
2356 JNI::GetMethodID,
2357 JNI::CallObjectMethod,
2358 JNI::CallObjectMethodV,
2359 JNI::CallObjectMethodA,
2360 JNI::CallBooleanMethod,
2361 JNI::CallBooleanMethodV,
2362 JNI::CallBooleanMethodA,
2363 JNI::CallByteMethod,
2364 JNI::CallByteMethodV,
2365 JNI::CallByteMethodA,
2366 JNI::CallCharMethod,
2367 JNI::CallCharMethodV,
2368 JNI::CallCharMethodA,
2369 JNI::CallShortMethod,
2370 JNI::CallShortMethodV,
2371 JNI::CallShortMethodA,
2372 JNI::CallIntMethod,
2373 JNI::CallIntMethodV,
2374 JNI::CallIntMethodA,
2375 JNI::CallLongMethod,
2376 JNI::CallLongMethodV,
2377 JNI::CallLongMethodA,
2378 JNI::CallFloatMethod,
2379 JNI::CallFloatMethodV,
2380 JNI::CallFloatMethodA,
2381 JNI::CallDoubleMethod,
2382 JNI::CallDoubleMethodV,
2383 JNI::CallDoubleMethodA,
2384 JNI::CallVoidMethod,
2385 JNI::CallVoidMethodV,
2386 JNI::CallVoidMethodA,
2387 JNI::CallNonvirtualObjectMethod,
2388 JNI::CallNonvirtualObjectMethodV,
2389 JNI::CallNonvirtualObjectMethodA,
2390 JNI::CallNonvirtualBooleanMethod,
2391 JNI::CallNonvirtualBooleanMethodV,
2392 JNI::CallNonvirtualBooleanMethodA,
2393 JNI::CallNonvirtualByteMethod,
2394 JNI::CallNonvirtualByteMethodV,
2395 JNI::CallNonvirtualByteMethodA,
2396 JNI::CallNonvirtualCharMethod,
2397 JNI::CallNonvirtualCharMethodV,
2398 JNI::CallNonvirtualCharMethodA,
2399 JNI::CallNonvirtualShortMethod,
2400 JNI::CallNonvirtualShortMethodV,
2401 JNI::CallNonvirtualShortMethodA,
2402 JNI::CallNonvirtualIntMethod,
2403 JNI::CallNonvirtualIntMethodV,
2404 JNI::CallNonvirtualIntMethodA,
2405 JNI::CallNonvirtualLongMethod,
2406 JNI::CallNonvirtualLongMethodV,
2407 JNI::CallNonvirtualLongMethodA,
2408 JNI::CallNonvirtualFloatMethod,
2409 JNI::CallNonvirtualFloatMethodV,
2410 JNI::CallNonvirtualFloatMethodA,
2411 JNI::CallNonvirtualDoubleMethod,
2412 JNI::CallNonvirtualDoubleMethodV,
2413 JNI::CallNonvirtualDoubleMethodA,
2414 JNI::CallNonvirtualVoidMethod,
2415 JNI::CallNonvirtualVoidMethodV,
2416 JNI::CallNonvirtualVoidMethodA,
2417 JNI::GetFieldID,
2418 JNI::GetObjectField,
2419 JNI::GetBooleanField,
2420 JNI::GetByteField,
2421 JNI::GetCharField,
2422 JNI::GetShortField,
2423 JNI::GetIntField,
2424 JNI::GetLongField,
2425 JNI::GetFloatField,
2426 JNI::GetDoubleField,
2427 JNI::SetObjectField,
2428 JNI::SetBooleanField,
2429 JNI::SetByteField,
2430 JNI::SetCharField,
2431 JNI::SetShortField,
2432 JNI::SetIntField,
2433 JNI::SetLongField,
2434 JNI::SetFloatField,
2435 JNI::SetDoubleField,
2436 JNI::GetStaticMethodID,
2437 JNI::CallStaticObjectMethod,
2438 JNI::CallStaticObjectMethodV,
2439 JNI::CallStaticObjectMethodA,
2440 JNI::CallStaticBooleanMethod,
2441 JNI::CallStaticBooleanMethodV,
2442 JNI::CallStaticBooleanMethodA,
2443 JNI::CallStaticByteMethod,
2444 JNI::CallStaticByteMethodV,
2445 JNI::CallStaticByteMethodA,
2446 JNI::CallStaticCharMethod,
2447 JNI::CallStaticCharMethodV,
2448 JNI::CallStaticCharMethodA,
2449 JNI::CallStaticShortMethod,
2450 JNI::CallStaticShortMethodV,
2451 JNI::CallStaticShortMethodA,
2452 JNI::CallStaticIntMethod,
2453 JNI::CallStaticIntMethodV,
2454 JNI::CallStaticIntMethodA,
2455 JNI::CallStaticLongMethod,
2456 JNI::CallStaticLongMethodV,
2457 JNI::CallStaticLongMethodA,
2458 JNI::CallStaticFloatMethod,
2459 JNI::CallStaticFloatMethodV,
2460 JNI::CallStaticFloatMethodA,
2461 JNI::CallStaticDoubleMethod,
2462 JNI::CallStaticDoubleMethodV,
2463 JNI::CallStaticDoubleMethodA,
2464 JNI::CallStaticVoidMethod,
2465 JNI::CallStaticVoidMethodV,
2466 JNI::CallStaticVoidMethodA,
2467 JNI::GetStaticFieldID,
2468 JNI::GetStaticObjectField,
2469 JNI::GetStaticBooleanField,
2470 JNI::GetStaticByteField,
2471 JNI::GetStaticCharField,
2472 JNI::GetStaticShortField,
2473 JNI::GetStaticIntField,
2474 JNI::GetStaticLongField,
2475 JNI::GetStaticFloatField,
2476 JNI::GetStaticDoubleField,
2477 JNI::SetStaticObjectField,
2478 JNI::SetStaticBooleanField,
2479 JNI::SetStaticByteField,
2480 JNI::SetStaticCharField,
2481 JNI::SetStaticShortField,
2482 JNI::SetStaticIntField,
2483 JNI::SetStaticLongField,
2484 JNI::SetStaticFloatField,
2485 JNI::SetStaticDoubleField,
2486 JNI::NewString,
2487 JNI::GetStringLength,
2488 JNI::GetStringChars,
2489 JNI::ReleaseStringChars,
2490 JNI::NewStringUTF,
2491 JNI::GetStringUTFLength,
2492 JNI::GetStringUTFChars,
2493 JNI::ReleaseStringUTFChars,
2494 JNI::GetArrayLength,
2495 JNI::NewObjectArray,
2496 JNI::GetObjectArrayElement,
2497 JNI::SetObjectArrayElement,
2498 JNI::NewBooleanArray,
2499 JNI::NewByteArray,
2500 JNI::NewCharArray,
2501 JNI::NewShortArray,
2502 JNI::NewIntArray,
2503 JNI::NewLongArray,
2504 JNI::NewFloatArray,
2505 JNI::NewDoubleArray,
2506 JNI::GetBooleanArrayElements,
2507 JNI::GetByteArrayElements,
2508 JNI::GetCharArrayElements,
2509 JNI::GetShortArrayElements,
2510 JNI::GetIntArrayElements,
2511 JNI::GetLongArrayElements,
2512 JNI::GetFloatArrayElements,
2513 JNI::GetDoubleArrayElements,
2514 JNI::ReleaseBooleanArrayElements,
2515 JNI::ReleaseByteArrayElements,
2516 JNI::ReleaseCharArrayElements,
2517 JNI::ReleaseShortArrayElements,
2518 JNI::ReleaseIntArrayElements,
2519 JNI::ReleaseLongArrayElements,
2520 JNI::ReleaseFloatArrayElements,
2521 JNI::ReleaseDoubleArrayElements,
2522 JNI::GetBooleanArrayRegion,
2523 JNI::GetByteArrayRegion,
2524 JNI::GetCharArrayRegion,
2525 JNI::GetShortArrayRegion,
2526 JNI::GetIntArrayRegion,
2527 JNI::GetLongArrayRegion,
2528 JNI::GetFloatArrayRegion,
2529 JNI::GetDoubleArrayRegion,
2530 JNI::SetBooleanArrayRegion,
2531 JNI::SetByteArrayRegion,
2532 JNI::SetCharArrayRegion,
2533 JNI::SetShortArrayRegion,
2534 JNI::SetIntArrayRegion,
2535 JNI::SetLongArrayRegion,
2536 JNI::SetFloatArrayRegion,
2537 JNI::SetDoubleArrayRegion,
2538 JNI::RegisterNatives,
2539 JNI::UnregisterNatives,
2540 JNI::MonitorEnter,
2541 JNI::MonitorExit,
2542 JNI::GetJavaVM,
2543 JNI::GetStringRegion,
2544 JNI::GetStringUTFRegion,
2545 JNI::GetPrimitiveArrayCritical,
2546 JNI::ReleasePrimitiveArrayCritical,
2547 JNI::GetStringCritical,
2548 JNI::ReleaseStringCritical,
2549 JNI::NewWeakGlobalRef,
2550 JNI::DeleteWeakGlobalRef,
2551 JNI::ExceptionCheck,
2552 JNI::NewDirectByteBuffer,
2553 JNI::GetDirectBufferAddress,
2554 JNI::GetDirectBufferCapacity,
2555 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002556};
2557
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002558static const size_t kMonitorsInitial = 32; // Arbitrary.
2559static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2560
2561static const size_t kLocalsInitial = 64; // Arbitrary.
2562static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002563
Elliott Hughes75770752011-08-24 17:52:38 -07002564JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002565 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002566 vm(vm),
2567 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002568 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002569 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002570 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2571 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002572 functions = unchecked_functions = &gNativeInterface;
2573 if (check_jni) {
2574 functions = GetCheckJniNativeInterface();
2575 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002576}
2577
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002578JNIEnvExt::~JNIEnvExt() {
2579}
2580
Carl Shapiroea4dca82011-08-01 13:45:38 -07002581// JNI Invocation interface.
2582
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2584 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2585 if (args->version < JNI_VERSION_1_2) {
2586 return JNI_EVERSION;
2587 }
2588 Runtime::Options options;
2589 for (int i = 0; i < args->nOptions; ++i) {
2590 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002591 options.push_back(std::make_pair(StringPiece(option->optionString),
2592 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002593 }
2594 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002595 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596 if (runtime == NULL) {
2597 return JNI_ERR;
2598 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002599 *p_env = Thread::Current()->GetJniEnv();
2600 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002601 return JNI_OK;
2602 }
2603}
2604
Elliott Hughesf2682d52011-08-15 16:37:04 -07002605extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002606 Runtime* runtime = Runtime::Current();
2607 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002608 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002610 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002611 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612 }
2613 return JNI_OK;
2614}
2615
2616// Historically unsupported.
2617extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2618 return JNI_ERR;
2619}
2620
Elliott Hughescdf53122011-08-19 15:46:09 -07002621class JII {
2622 public:
2623 static jint DestroyJavaVM(JavaVM* vm) {
2624 if (vm == NULL) {
2625 return JNI_ERR;
2626 } else {
2627 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2628 delete raw_vm->runtime;
2629 return JNI_OK;
2630 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632
Elliott Hughescdf53122011-08-19 15:46:09 -07002633 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002634 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002635 }
2636
2637 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002638 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002639 }
2640
2641 static jint DetachCurrentThread(JavaVM* vm) {
2642 if (vm == NULL) {
2643 return JNI_ERR;
2644 } else {
2645 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2646 Runtime* runtime = raw_vm->runtime;
2647 runtime->DetachCurrentThread();
2648 return JNI_OK;
2649 }
2650 }
2651
2652 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2653 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2654 return JNI_EVERSION;
2655 }
2656 if (vm == NULL || env == NULL) {
2657 return JNI_ERR;
2658 }
2659 Thread* thread = Thread::Current();
2660 if (thread == NULL) {
2661 *env = NULL;
2662 return JNI_EDETACHED;
2663 }
2664 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002665 return JNI_OK;
2666 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002667};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668
Elliott Hughesa2501992011-08-26 19:39:54 -07002669const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002670 NULL, // reserved0
2671 NULL, // reserved1
2672 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002673 JII::DestroyJavaVM,
2674 JII::AttachCurrentThread,
2675 JII::DetachCurrentThread,
2676 JII::GetEnv,
2677 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002678};
2679
Elliott Hughesbbd76712011-08-17 10:25:24 -07002680static const size_t kPinTableInitialSize = 16;
2681static const size_t kPinTableMaxSize = 1024;
2682
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002683static const size_t kGlobalsInitial = 512; // Arbitrary.
2684static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2685
2686static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2687static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2688
Elliott Hughesa0957642011-09-02 14:27:33 -07002689JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002690 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002691 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002692 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002693 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002694 verbose_jni(options->IsVerbose("jni")),
2695 log_third_party_jni(options->IsVerbose("third-party-jni")),
2696 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002697 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002698 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002699 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002700 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002701 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002702 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002703 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2704 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2705 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002706 functions = unchecked_functions = &gInvokeInterface;
2707 if (check_jni) {
2708 functions = GetCheckJniInvokeInterface();
2709 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002710}
2711
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002712JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002713 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002714 delete globals_lock;
2715 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002716 delete libraries_lock;
2717 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002718}
2719
Elliott Hughes75770752011-08-24 17:52:38 -07002720bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2721 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002722
2723 // See if we've already loaded this library. If we have, and the class loader
2724 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002725 // TODO: for better results we should canonicalize the pathname (or even compare
2726 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002727 SharedLibrary* library;
2728 {
2729 // TODO: move the locking (and more of this logic) into Libraries.
2730 MutexLock mu(libraries_lock);
2731 library = libraries->Get(path);
2732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 if (library != NULL) {
2734 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 // The library will be associated with class_loader. The JNI
2736 // spec says we can't load the same library into more than one
2737 // class loader.
2738 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2739 "ClassLoader %p; can't open in ClassLoader %p",
2740 path.c_str(), library->GetClassLoader(), class_loader);
2741 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 return false;
2743 }
2744 if (verbose_jni) {
2745 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2746 << "ClassLoader " << class_loader << "]";
2747 }
2748 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002749 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2750 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 return false;
2752 }
2753 return true;
2754 }
2755
2756 // Open the shared library. Because we're using a full path, the system
2757 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2758 // resolve this library's dependencies though.)
2759
2760 // Failures here are expected when java.library.path has several entries
2761 // and we have to hunt for the lib.
2762
2763 // The current version of the dynamic linker prints detailed information
2764 // about dlopen() failures. Some things to check if the message is
2765 // cryptic:
2766 // - make sure the library exists on the device
2767 // - verify that the right path is being opened (the debug log message
2768 // above can help with that)
2769 // - check to see if the library is valid (e.g. not zero bytes long)
2770 // - check config/prelink-linux-arm.map to ensure that the library
2771 // is listed and is not being overrun by the previous entry (if
2772 // loading suddenly stops working on a prelinked library, this is
2773 // a good one to check)
2774 // - write a trivial app that calls sleep() then dlopen(), attach
2775 // to it with "strace -p <pid>" while it sleeps, and watch for
2776 // attempts to open nonexistent dependent shared libs
2777
2778 // TODO: automate some of these checks!
2779
2780 // This can execute slowly for a large library on a busy system, so we
2781 // want to switch from RUNNING to VMWAIT while it executes. This allows
2782 // the GC to ignore us.
2783 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002784 void* handle = NULL;
2785 {
2786 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2787 handle = dlopen(path.c_str(), RTLD_LAZY);
2788 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002789
2790 if (verbose_jni) {
2791 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2792 }
2793
2794 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002795 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 return false;
2797 }
2798
2799 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002800 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002801 // TODO: move the locking (and more of this logic) into Libraries.
2802 MutexLock mu(libraries_lock);
2803 library = libraries->Get(path);
2804 if (library != NULL) {
2805 LOG(INFO) << "WOW: we lost a race to add shared library: "
2806 << "\"" << path << "\" ClassLoader=" << class_loader;
2807 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 library = new SharedLibrary(path, handle, class_loader);
2810 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002811 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002812
2813 if (verbose_jni) {
2814 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2815 }
2816
2817 bool result = true;
2818 void* sym = dlsym(handle, "JNI_OnLoad");
2819 if (sym == NULL) {
2820 if (verbose_jni) {
2821 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2822 }
2823 } else {
2824 // Call JNI_OnLoad. We have to override the current class
2825 // loader, which will always be "null" since the stuff at the
2826 // top of the stack is around Runtime.loadLibrary(). (See
2827 // the comments in the JNI FindClass function.)
2828 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2829 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002830 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002831 self->SetClassLoaderOverride(class_loader);
2832
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002833 int version = 0;
2834 {
2835 ScopedThreadStateChange tsc(self, Thread::kNative);
2836 if (verbose_jni) {
2837 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2838 }
2839 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002840 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002841
2842 self->SetClassLoaderOverride(old_class_loader);;
2843
2844 if (version != JNI_VERSION_1_2 &&
2845 version != JNI_VERSION_1_4 &&
2846 version != JNI_VERSION_1_6) {
2847 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2848 << "bad version: " << version;
2849 // It's unwise to call dlclose() here, but we can mark it
2850 // as bad and ensure that future load attempts will fail.
2851 // We don't know how far JNI_OnLoad got, so there could
2852 // be some partially-initialized stuff accessible through
2853 // newly-registered native method calls. We could try to
2854 // unregister them, but that doesn't seem worthwhile.
2855 result = false;
2856 } else {
2857 if (verbose_jni) {
2858 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2859 << " from JNI_OnLoad in \"" << path << "\"]";
2860 }
2861 }
2862 }
2863
2864 library->SetResult(result);
2865 return result;
2866}
2867
2868void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2869 CHECK(m->IsNative());
2870
2871 Class* c = m->GetDeclaringClass();
2872
2873 // If this is a static method, it could be called before the class
2874 // has been initialized.
2875 if (m->IsStatic()) {
2876 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2877 return NULL;
2878 }
2879 } else {
2880 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2881 }
2882
2883 MutexLock mu(libraries_lock);
2884 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002885}
2886
Elliott Hughes410c0c82011-09-01 17:58:25 -07002887void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2888 {
2889 MutexLock mu(globals_lock);
2890 globals.VisitRoots(visitor, arg);
2891 }
2892 {
2893 MutexLock mu(pins_lock);
2894 pin_table.VisitRoots(visitor, arg);
2895 }
2896 // The weak_globals table is visited by the GC itself (because it mutates the table).
2897}
2898
Ian Rogersdf20fe02011-07-20 20:34:16 -07002899} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002900
2901std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2902 switch (rhs) {
2903 case JNIInvalidRefType:
2904 os << "JNIInvalidRefType";
2905 return os;
2906 case JNILocalRefType:
2907 os << "JNILocalRefType";
2908 return os;
2909 case JNIGlobalRefType:
2910 os << "JNIGlobalRefType";
2911 return os;
2912 case JNIWeakGlobalRefType:
2913 os << "JNIWeakGlobalRefType";
2914 return os;
2915 }
2916}