blob: 125401eec595a05da22064ebd5576b3e99a00bf0 [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.
360 // TODO: Linkage or NoSuchFieldError?
361 CHECK(ts.Self()->IsExceptionPending());
362 return NULL;
363 }
364 if (is_static) {
365 field = c->FindStaticField(name, field_type);
366 } else {
367 field = c->FindInstanceField(name, field_type);
368 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 if (field == NULL) {
370 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700371 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700372 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
373 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700374 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700375 return NULL;
376 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 // Check invariant that all jfieldIDs have resolved types (how else would
378 // the type equality in Find...Field hold?)
379 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700380 jweak fid = AddWeakGlobalReference(ts, field);
381 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700382}
383
Elliott Hughes75770752011-08-24 17:52:38 -0700384void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
385 JavaVMExt* vm = ts.Vm();
386 MutexLock mu(vm->pins_lock);
387 vm->pin_table.Add(array);
388}
389
390void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
391 JavaVMExt* vm = ts.Vm();
392 MutexLock mu(vm->pins_lock);
393 vm->pin_table.Remove(array);
394}
395
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700396template<typename JniT, typename ArtT>
397JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
398 CHECK_GE(length, 0); // TODO: ReportJniError
399 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700400 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700401}
402
Elliott Hughes75770752011-08-24 17:52:38 -0700403template <typename ArrayT, typename CArrayT, typename ArtArrayT>
404CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
405 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
406 PinPrimitiveArray(ts, array);
407 if (is_copy != NULL) {
408 *is_copy = JNI_FALSE;
409 }
410 return array->GetData();
411}
412
413template <typename ArrayT>
414void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
415 if (mode != JNI_COMMIT) {
416 Array* array = Decode<Array*>(ts, java_array);
417 UnpinPrimitiveArray(ts, array);
418 }
419}
420
Elliott Hughes814e4032011-08-23 12:07:56 -0700421void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
422 std::string type(PrettyType(array));
423 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
424 "%s offset=%d length=%d %s.length=%d",
425 type.c_str(), start, length, identifier, array->GetLength());
426}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700427void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
428 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
429 "offset=%d length=%d string.length()=%d", start, length, array_length);
430}
Elliott Hughes814e4032011-08-23 12:07:56 -0700431
432template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700433void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700434 ArrayT* array = Decode<ArrayT*>(ts, java_array);
435 if (start < 0 || length < 0 || start + length > array->GetLength()) {
436 ThrowAIOOBE(ts, array, start, length, "src");
437 } else {
438 JavaT* data = array->GetData();
439 memcpy(buf, data + start, length * sizeof(JavaT));
440 }
441}
442
443template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700444void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700445 ArrayT* array = Decode<ArrayT*>(ts, java_array);
446 if (start < 0 || length < 0 || start + length > array->GetLength()) {
447 ThrowAIOOBE(ts, array, start, length, "dst");
448 } else {
449 JavaT* data = array->GetData();
450 memcpy(data + start, buf, length * sizeof(JavaT));
451 }
452}
453
Elliott Hughes75770752011-08-24 17:52:38 -0700454jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700455 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
456 CHECK(buffer_class.get() != NULL);
457 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
458}
459
Elliott Hughes75770752011-08-24 17:52:38 -0700460jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700461 static jclass buffer_class = InitDirectByteBufferClass(env);
462 return buffer_class;
463}
464
Elliott Hughes75770752011-08-24 17:52:38 -0700465jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
466 if (vm == NULL || p_env == NULL) {
467 return JNI_ERR;
468 }
469
470 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
471 JavaVMAttachArgs args;
472 if (thr_args == NULL) {
473 // Allow the v1.1 calling convention.
474 args.version = JNI_VERSION_1_2;
475 args.name = NULL;
476 args.group = NULL; // TODO: get "main" thread group
477 } else {
478 args.version = in_args->version;
479 args.name = in_args->name;
480 if (in_args->group != NULL) {
481 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
482 args.group = NULL; // TODO: decode in_args->group
483 } else {
484 args.group = NULL; // TODO: get "main" thread group
485 }
486 }
487 CHECK_GE(args.version, JNI_VERSION_1_2);
488
489 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
490 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
491}
492
Elliott Hughes79082e32011-08-25 12:07:32 -0700493class SharedLibrary {
494 public:
495 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
496 : path_(path),
497 handle_(handle),
498 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
499 jni_on_load_tid_(Thread::Current()->GetId()),
500 jni_on_load_result_(kPending) {
501 pthread_cond_init(&jni_on_load_cond_, NULL);
502 }
503
504 ~SharedLibrary() {
505 delete jni_on_load_lock_;
506 }
507
508 Object* GetClassLoader() {
509 return class_loader_;
510 }
511
512 std::string GetPath() {
513 return path_;
514 }
515
516 /*
517 * Check the result of an earlier call to JNI_OnLoad on this library. If
518 * the call has not yet finished in another thread, wait for it.
519 */
520 bool CheckOnLoadResult(JavaVMExt* vm) {
521 Thread* self = Thread::Current();
522 if (jni_on_load_tid_ == self->GetId()) {
523 // Check this so we don't end up waiting for ourselves. We need
524 // to return "true" so the caller can continue.
525 LOG(INFO) << *self << " recursive attempt to load library "
526 << "\"" << path_ << "\"";
527 return true;
528 }
529
530 MutexLock mu(jni_on_load_lock_);
531 while (jni_on_load_result_ == kPending) {
532 if (vm->verbose_jni) {
533 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
534 << "JNI_OnLoad...]";
535 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700536 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 }
539
540 bool okay = (jni_on_load_result_ == kOkay);
541 if (vm->verbose_jni) {
542 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
543 << (okay ? "succeeded" : "failed") << "]";
544 }
545 return okay;
546 }
547
548 void SetResult(bool result) {
549 jni_on_load_result_ = result ? kOkay : kFailed;
550 jni_on_load_tid_ = 0;
551
552 // Broadcast a wakeup to anybody sleeping on the condition variable.
553 MutexLock mu(jni_on_load_lock_);
554 pthread_cond_broadcast(&jni_on_load_cond_);
555 }
556
557 void* FindSymbol(const std::string& symbol_name) {
558 return dlsym(handle_, symbol_name.c_str());
559 }
560
561 private:
562 enum JNI_OnLoadState {
563 kPending,
564 kFailed,
565 kOkay,
566 };
567
568 // Path to library "/system/lib/libjni.so".
569 std::string path_;
570
571 // The void* returned by dlopen(3).
572 void* handle_;
573
574 // The ClassLoader this library is associated with.
575 Object* class_loader_;
576
577 // Guards remaining items.
578 Mutex* jni_on_load_lock_;
579 // Wait for JNI_OnLoad in other thread.
580 pthread_cond_t jni_on_load_cond_;
581 // Recursive invocation guard.
582 uint32_t jni_on_load_tid_;
583 // Result of earlier JNI_OnLoad call.
584 JNI_OnLoadState jni_on_load_result_;
585};
586
Elliott Hughescdf53122011-08-19 15:46:09 -0700587} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700588
Elliott Hughes79082e32011-08-25 12:07:32 -0700589// This exists mainly to keep implementation details out of the header file.
590class Libraries {
591 public:
592 Libraries() {
593 }
594
595 ~Libraries() {
596 // Delete our map values. (The keys will be cleaned up by the map itself.)
597 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
598 delete it->second;
599 }
600 }
601
602 SharedLibrary* Get(const std::string& path) {
603 return libraries_[path];
604 }
605
606 void Put(const std::string& path, SharedLibrary* library) {
607 libraries_[path] = library;
608 }
609
610 // See section 11.3 "Linking Native Methods" of the JNI spec.
611 void* FindNativeMethod(const Method* m) {
612 std::string jni_short_name(JniShortName(m));
613 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700614 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700615 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
616 SharedLibrary* library = it->second;
617 if (library->GetClassLoader() != declaring_class_loader) {
618 // We only search libraries loaded by the appropriate ClassLoader.
619 continue;
620 }
621 // Try the short name then the long name...
622 void* fn = library->FindSymbol(jni_short_name);
623 if (fn == NULL) {
624 fn = library->FindSymbol(jni_long_name);
625 }
626 if (fn != NULL) {
627 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700628 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700629 << " in \"" << library->GetPath() << "\"]";
630 }
631 return fn;
632 }
633 }
634 std::string detail;
635 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700636 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700637 LOG(ERROR) << detail;
638 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
639 "%s", detail.c_str());
640 return NULL;
641 }
642
643 private:
644 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
645
646 std::map<std::string, SharedLibrary*> libraries_;
647};
648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649class JNI {
650 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jint GetVersion(JNIEnv* env) {
653 ScopedJniThreadState ts(env);
654 return JNI_VERSION_1_6;
655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
658 ScopedJniThreadState ts(env);
659 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700660 return NULL;
661 }
662
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 static jclass FindClass(JNIEnv* env, const char* name) {
664 ScopedJniThreadState ts(env);
665 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
666 std::string descriptor(NormalizeJniClassDescriptor(name));
667 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700668 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700669 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700670 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
674 ScopedJniThreadState ts(env);
675 Method* method = Decode<Method*>(ts, java_method);
676 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700677 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700678
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
680 ScopedJniThreadState ts(env);
681 Field* field = Decode<Field*>(ts, java_field);
682 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
686 ScopedJniThreadState ts(env);
687 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700688 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
692 ScopedJniThreadState ts(env);
693 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700694 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
698 ScopedJniThreadState ts(env);
699 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700700 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 }
702
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700703 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700705 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700706 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 Class* c1 = Decode<Class*>(ts, java_class1);
712 Class* c2 = Decode<Class*>(ts, java_class2);
713 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700718 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 if (jobj == NULL) {
720 // NB. JNI is different from regular Java instanceof in this respect
721 return JNI_TRUE;
722 } else {
723 Object* obj = Decode<Object*>(ts, jobj);
724 Class* klass = Decode<Class*>(ts, clazz);
725 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
726 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700728
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 static jint Throw(JNIEnv* env, jthrowable java_exception) {
730 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700731 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 if (exception == NULL) {
733 return JNI_ERR;
734 }
735 ts.Self()->SetException(exception);
736 return JNI_OK;
737 }
738
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700739 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700741 // TODO: check for a pending exception to decide what constructor to call.
742 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
743 if (mid == NULL) {
744 return JNI_ERR;
745 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
747 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700748 return JNI_ERR;
749 }
750
751 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 args[0].l = s.get();
753 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
754 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700755 return JNI_ERR;
756 }
757
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700759 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700760 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700761
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 return JNI_OK;
763 }
764
765 static jboolean ExceptionCheck(JNIEnv* env) {
766 ScopedJniThreadState ts(env);
767 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
768 }
769
770 static void ExceptionClear(JNIEnv* env) {
771 ScopedJniThreadState ts(env);
772 ts.Self()->ClearException();
773 }
774
775 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700777
778 Thread* self = ts.Self();
779 Throwable* original_exception = self->GetException();
780 self->ClearException();
781
Elliott Hughesbf86d042011-08-31 17:53:14 -0700782 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
784 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
785 if (mid == NULL) {
786 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
787 << PrettyType(original_exception);
788 } else {
789 env->CallVoidMethod(exception.get(), mid);
790 if (self->IsExceptionPending()) {
791 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
792 << " thrown while calling printStackTrace";
793 self->ClearException();
794 }
795 }
796
797 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700799
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 static jthrowable ExceptionOccurred(JNIEnv* env) {
801 ScopedJniThreadState ts(env);
802 Object* exception = ts.Self()->GetException();
803 if (exception == NULL) {
804 return NULL;
805 } else {
806 // TODO: if adding a local reference failing causes the VM to abort
807 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700808 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 if (localException == NULL) {
810 // We were unable to add a new local reference, and threw a new
811 // exception. We can't return "exception", because it's not a
812 // local reference. So we have to return NULL, indicating that
813 // there was no exception, even though it's pretty much raining
814 // exceptions in here.
815 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
816 }
817 return localException;
818 }
819 }
820
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 static void FatalError(JNIEnv* env, const char* msg) {
822 ScopedJniThreadState ts(env);
823 LOG(FATAL) << "JNI FatalError called: " << msg;
824 }
825
826 static jint PushLocalFrame(JNIEnv* env, jint cap) {
827 ScopedJniThreadState ts(env);
828 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
829 return JNI_OK;
830 }
831
832 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
833 ScopedJniThreadState ts(env);
834 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
835 return res;
836 }
837
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
839 ScopedJniThreadState ts(env);
840 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
841 return 0;
842 }
843
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
845 ScopedJniThreadState ts(env);
846 if (obj == NULL) {
847 return NULL;
848 }
849
Elliott Hughes75770752011-08-24 17:52:38 -0700850 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 IndirectReferenceTable& globals = vm->globals;
852 MutexLock mu(vm->globals_lock);
853 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
854 return reinterpret_cast<jobject>(ref);
855 }
856
857 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
858 ScopedJniThreadState ts(env);
859 if (obj == NULL) {
860 return;
861 }
862
Elliott Hughes75770752011-08-24 17:52:38 -0700863 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 IndirectReferenceTable& globals = vm->globals;
865 MutexLock mu(vm->globals_lock);
866
867 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
868 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
869 << "failed to find entry";
870 }
871 }
872
873 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
874 ScopedJniThreadState ts(env);
875 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
876 }
877
878 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return;
882 }
883
Elliott Hughes75770752011-08-24 17:52:38 -0700884 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 IndirectReferenceTable& weak_globals = vm->weak_globals;
886 MutexLock mu(vm->weak_globals_lock);
887
888 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
889 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
890 << "failed to find entry";
891 }
892 }
893
894 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
895 ScopedJniThreadState ts(env);
896 if (obj == NULL) {
897 return NULL;
898 }
899
900 IndirectReferenceTable& locals = ts.Env()->locals;
901
902 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
903 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
904 return reinterpret_cast<jobject>(ref);
905 }
906
907 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
908 ScopedJniThreadState ts(env);
909 if (obj == NULL) {
910 return;
911 }
912
913 IndirectReferenceTable& locals = ts.Env()->locals;
914
915 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
916 if (!locals.Remove(cookie, obj)) {
917 // Attempting to delete a local reference that is not in the
918 // topmost local reference frame is a no-op. DeleteLocalRef returns
919 // void and doesn't throw any exceptions, but we should probably
920 // complain about it so the user will notice that things aren't
921 // going quite the way they expect.
922 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
923 << "failed to find entry";
924 }
925 }
926
927 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
928 ScopedJniThreadState ts(env);
929 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
930 ? JNI_TRUE : JNI_FALSE;
931 }
932
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700933 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700935 Class* c = Decode<Class*>(ts, java_class);
936 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
937 return NULL;
938 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700939 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 ScopedJniThreadState ts(env);
944 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 va_start(args, mid);
946 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 va_end(args);
948 return result;
949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700953 Class* c = Decode<Class*>(ts, java_class);
954 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
955 return NULL;
956 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700957 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700958 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 return local_result;
961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700965 Class* c = Decode<Class*>(ts, java_class);
966 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
967 return NULL;
968 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700969 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700970 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 return local_result;
973 }
974
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
976 ScopedJniThreadState ts(env);
977 return FindMethodID(ts, c, name, sig, false);
978 }
979
980 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
981 ScopedJniThreadState ts(env);
982 return FindMethodID(ts, c, name, sig, true);
983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700989 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700991 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700996 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700997 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001002 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001003 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001010 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_end(ap);
1012 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001022 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 va_list ap;
1028 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001029 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_end(ap);
1031 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001036 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001041 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_list ap;
1047 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001048 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
1050 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001055 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001060 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001067 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_end(ap);
1069 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001074 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_list ap;
1085 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001086 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_end(ap);
1088 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001093 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_list ap;
1104 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001105 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_end(ap);
1107 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001112 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001117 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_list ap;
1123 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001124 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_end(ap);
1126 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001136 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_list ap;
1142 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001143 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_end(ap);
1145 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001150 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001155 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_list ap;
1161 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001162 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001168 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001173 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
1179 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_start(ap, mid);
1181 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001182 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_end(ap);
1184 return local_result;
1185 }
1186
1187 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001191 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
1194 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001198 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
1204 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_start(ap, mid);
1206 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 va_end(ap);
1208 return result.z;
1209 }
1210
1211 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
1217 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
1223 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
1226 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_start(ap, mid);
1228 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
1230 return result.b;
1231 }
1232
1233 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
1248 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_start(ap, mid);
1250 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_end(ap);
1252 return result.c;
1253 }
1254
1255 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
1261 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
1270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
1272 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
1274 return result.s;
1275 }
1276
1277 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
1292 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 va_start(ap, mid);
1294 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_end(ap);
1296 return result.i;
1297 }
1298
1299 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
1314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
1316 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
1318 return result.j;
1319 }
1320
1321 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
1336 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 va_start(ap, mid);
1338 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 va_end(ap);
1340 return result.f;
1341 }
1342
1343 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
1349 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
1355 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
1358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
1360 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_end(ap);
1362 return result.d;
1363 }
1364
1365 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001378 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
1380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
1382 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 va_end(ap);
1384 }
1385
1386 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
1398 static jfieldID GetFieldID(JNIEnv* env,
1399 jclass c, const char* name, const char* sig) {
1400 ScopedJniThreadState ts(env);
1401 return FindFieldID(ts, c, name, sig, false);
1402 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001403
1404
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 static jfieldID GetStaticFieldID(JNIEnv* env,
1406 jclass c, const char* name, const char* sig) {
1407 ScopedJniThreadState ts(env);
1408 return FindFieldID(ts, c, name, sig, true);
1409 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001410
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 Object* o = Decode<Object*>(ts, obj);
1414 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001415 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001421 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 Object* o = Decode<Object*>(ts, java_object);
1427 Object* v = Decode<Object*>(ts, java_value);
1428 Field* f = DecodeField(ts, fid);
1429 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 Object* v = Decode<Object*>(ts, java_value);
1435 Field* f = DecodeField(ts, fid);
1436 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439#define GET_PRIMITIVE_FIELD(fn, instance) \
1440 ScopedJniThreadState ts(env); \
1441 Object* o = Decode<Object*>(ts, instance); \
1442 Field* f = DecodeField(ts, fid); \
1443 return f->fn(o)
1444
1445#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1446 ScopedJniThreadState ts(env); \
1447 Object* o = Decode<Object*>(ts, instance); \
1448 Field* f = DecodeField(ts, fid); \
1449 f->fn(o, value)
1450
1451 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetLong, NULL);
1505 }
1506
1507 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1509 }
1510
1511 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1513 }
1514
1515 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1516 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1517 }
1518
1519 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1520 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1521 }
1522
1523 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1524 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1525 }
1526
1527 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1528 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1529 }
1530
1531 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1532 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1533 }
1534
1535 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1536 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1537 }
1538
1539 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1540 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1541 }
1542
1543 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1544 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1545 }
1546
1547 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1548 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1549 }
1550
1551 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1552 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1553 }
1554
1555 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1556 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1557 }
1558
1559 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1560 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1561 }
1562
1563 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1564 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1565 }
1566
1567 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1568 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1569 }
1570
1571 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1572 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1573 }
1574
1575 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1576 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
1579 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
1582 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 va_start(ap, mid);
1584 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001585 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 va_end(ap);
1587 return local_result;
1588 }
1589
1590 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001594 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
1597 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001601 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
1604 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
1607 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 va_start(ap, mid);
1609 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 va_end(ap);
1611 return result.z;
1612 }
1613
1614 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
1620 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
1630 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return result.b;
1633 }
1634
1635 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
1641 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
1651 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return result.c;
1654 }
1655
1656 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
1662 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
1670 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 va_start(ap, mid);
1672 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_end(ap);
1674 return result.s;
1675 }
1676
1677 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
1683 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
1691 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 va_start(ap, mid);
1693 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
1695 return result.i;
1696 }
1697
1698 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
1704 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
1712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
1714 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
1716 return result.j;
1717 }
1718
1719 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
1725 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
1733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
1735 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
1737 return result.f;
1738 }
1739
1740 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
1746 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
1754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
1756 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
1758 return result.d;
1759 }
1760
1761 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
1767 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001768 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
1775 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 va_start(ap, mid);
1777 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_end(ap);
1779 }
1780
1781 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001784 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
1787 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001788 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001790 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001795 if (chars == NULL && char_count == 0) {
1796 return NULL;
1797 }
1798 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001799 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1803 ScopedJniThreadState ts(env);
1804 if (utf == NULL) {
1805 return NULL;
1806 }
1807 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001808 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1812 ScopedJniThreadState ts(env);
1813 return Decode<String*>(ts, java_string)->GetLength();
1814 }
1815
1816 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1817 ScopedJniThreadState ts(env);
1818 return Decode<String*>(ts, java_string)->GetUtfLength();
1819 }
1820
Elliott Hughesb465ab02011-08-24 11:21:21 -07001821 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001823 String* s = Decode<String*>(ts, java_string);
1824 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1825 ThrowSIOOBE(ts, start, length, s->GetLength());
1826 } else {
1827 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1828 memcpy(buf, chars + start, length * sizeof(jchar));
1829 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001830 }
1831
Elliott Hughesb465ab02011-08-24 11:21:21 -07001832 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001834 String* s = Decode<String*>(ts, java_string);
1835 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1836 ThrowSIOOBE(ts, start, length, s->GetLength());
1837 } else {
1838 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1839 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1840 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 }
1842
Elliott Hughes75770752011-08-24 17:52:38 -07001843 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001844 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 String* s = Decode<String*>(ts, java_string);
1846 const CharArray* chars = s->GetCharArray();
1847 PinPrimitiveArray(ts, chars);
1848 if (is_copy != NULL) {
1849 *is_copy = JNI_FALSE;
1850 }
1851 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 }
1853
Elliott Hughes75770752011-08-24 17:52:38 -07001854 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001855 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001856 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes75770752011-08-24 17:52:38 -07001859 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001861 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes75770752011-08-24 17:52:38 -07001864 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001866 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001870 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001871 if (java_string == NULL) {
1872 return NULL;
1873 }
1874 if (is_copy != NULL) {
1875 *is_copy = JNI_TRUE;
1876 }
1877 String* s = Decode<String*>(ts, java_string);
1878 size_t byte_count = s->GetUtfLength();
1879 char* bytes = new char[byte_count + 1];
1880 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001881 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001882 return NULL;
1883 }
1884 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1885 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1886 bytes[byte_count] = '\0';
1887 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001891 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001892 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001893 }
1894
Elliott Hughesbd935992011-08-22 11:59:34 -07001895 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001897 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001898 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001899 Array* array = obj->AsArray();
1900 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes814e4032011-08-23 12:07:56 -07001903 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001905 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001906 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
1909 static void SetObjectArrayElement(JNIEnv* env,
1910 jobjectArray java_array, jsize index, jobject java_value) {
1911 ScopedJniThreadState ts(env);
1912 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1913 Object* value = Decode<Object*>(ts, java_value);
1914 array->Set(index, value);
1915 }
1916
1917 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1918 ScopedJniThreadState ts(env);
1919 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1920 }
1921
1922 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1923 ScopedJniThreadState ts(env);
1924 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1925 }
1926
1927 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1928 ScopedJniThreadState ts(env);
1929 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1930 }
1931
1932 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1933 ScopedJniThreadState ts(env);
1934 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1935 }
1936
1937 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1938 ScopedJniThreadState ts(env);
1939 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1940 }
1941
1942 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1943 ScopedJniThreadState ts(env);
1944 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1945 }
1946
1947 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1948 ScopedJniThreadState ts(env);
1949 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1950 }
1951
1952 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1953 ScopedJniThreadState ts(env);
1954 CHECK_GE(length, 0); // TODO: ReportJniError
1955
1956 // Compute the array class corresponding to the given element class.
1957 Class* element_class = Decode<Class*>(ts, element_jclass);
1958 std::string descriptor;
1959 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001960 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001961
1962 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001963 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1964 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 return NULL;
1966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 // Allocate and initialize if necessary.
1969 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 if (initial_element != NULL) {
1972 Object* initial_object = Decode<Object*>(ts, initial_element);
1973 for (jsize i = 0; i < length; ++i) {
1974 result->Set(i, initial_object);
1975 }
1976 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001977 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
1980 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1981 ScopedJniThreadState ts(env);
1982 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002052 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002057 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes75770752011-08-24 17:52:38 -07002060 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002062 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes75770752011-08-24 17:52:38 -07002065 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002067 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes75770752011-08-24 17:52:38 -07002070 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002072 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 Class* c = Decode<Class*>(ts, java_class);
2158
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 const char* name = methods[i].name;
2161 const char* sig = methods[i].signature;
2162
2163 if (*sig == '!') {
2164 // TODO: fast jni. it's too noisy to log all these.
2165 ++sig;
2166 }
2167
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 Method* m = c->FindDirectMethod(name, sig);
2169 if (m == NULL) {
2170 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2176 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002177 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002179 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2183 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002184 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 return JNI_ERR;
2186 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187
Elliott Hughes75770752011-08-24 17:52:38 -07002188 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002189 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002190 }
2191
2192 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194 return JNI_OK;
2195 }
2196
Elliott Hughes5174fe62011-08-23 15:12:35 -07002197 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002199 Class* c = Decode<Class*>(ts, java_class);
2200
Elliott Hughes75770752011-08-24 17:52:38 -07002201 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 LOG(INFO) << "[Unregistering JNI native methods for "
2203 << PrettyDescriptor(c->GetDescriptor()) << "]";
2204 }
2205
2206 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2207 Method* m = c->GetDirectMethod(i);
2208 if (m->IsNative()) {
2209 m->UnregisterNative();
2210 }
2211 }
2212 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2213 Method* m = c->GetVirtualMethod(i);
2214 if (m->IsNative()) {
2215 m->UnregisterNative();
2216 }
2217 }
2218
2219 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes72025e52011-08-23 17:50:30 -07002222 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002224 Decode<Object*>(ts, java_object)->MonitorEnter();
2225 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes72025e52011-08-23 17:50:30 -07002228 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002230 Decode<Object*>(ts, java_object)->MonitorEnter();
2231 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
2234 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2235 ScopedJniThreadState ts(env);
2236 Runtime* runtime = Runtime::Current();
2237 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002238 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 } else {
2240 *vm = NULL;
2241 }
2242 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2243 }
2244
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2246 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247
2248 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002249 CHECK(address != NULL); // TODO: ReportJniError
2250 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
2252 jclass buffer_class = GetDirectByteBufferClass(env);
2253 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2254 if (mid == NULL) {
2255 return NULL;
2256 }
2257
2258 // At the moment, the Java side is limited to 32 bits.
2259 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2260 CHECK_LE(capacity, 0xffffffff);
2261 jint address_arg = reinterpret_cast<jint>(address);
2262 jint capacity_arg = static_cast<jint>(capacity);
2263
2264 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2265 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2271 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2277 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002282
Elliott Hughes75770752011-08-24 17:52:38 -07002283 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284
2285 // Do we definitely know what kind of reference this is?
2286 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2287 IndirectRefKind kind = GetIndirectRefKind(ref);
2288 switch (kind) {
2289 case kLocal:
2290 return JNILocalRefType;
2291 case kGlobal:
2292 return JNIGlobalRefType;
2293 case kWeakGlobal:
2294 return JNIWeakGlobalRefType;
2295 case kSirtOrInvalid:
2296 // Is it in a stack IRT?
2297 if (ts.Self()->SirtContains(java_object)) {
2298 return JNILocalRefType;
2299 }
2300
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002301 if (!ts.Env()->work_around_app_jni_bugs) {
2302 return JNIInvalidRefType;
2303 }
2304
Elliott Hughesb465ab02011-08-24 11:21:21 -07002305 // If we're handing out direct pointers, check whether it's a direct pointer
2306 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002307 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 if (ts.Env()->locals.Contains(java_object)) {
2309 return JNILocalRefType;
2310 }
2311 }
2312
2313 return JNIInvalidRefType;
2314 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002315 }
2316};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002317
Elliott Hughesa2501992011-08-26 19:39:54 -07002318const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002319 NULL, // reserved0.
2320 NULL, // reserved1.
2321 NULL, // reserved2.
2322 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 JNI::GetVersion,
2324 JNI::DefineClass,
2325 JNI::FindClass,
2326 JNI::FromReflectedMethod,
2327 JNI::FromReflectedField,
2328 JNI::ToReflectedMethod,
2329 JNI::GetSuperclass,
2330 JNI::IsAssignableFrom,
2331 JNI::ToReflectedField,
2332 JNI::Throw,
2333 JNI::ThrowNew,
2334 JNI::ExceptionOccurred,
2335 JNI::ExceptionDescribe,
2336 JNI::ExceptionClear,
2337 JNI::FatalError,
2338 JNI::PushLocalFrame,
2339 JNI::PopLocalFrame,
2340 JNI::NewGlobalRef,
2341 JNI::DeleteGlobalRef,
2342 JNI::DeleteLocalRef,
2343 JNI::IsSameObject,
2344 JNI::NewLocalRef,
2345 JNI::EnsureLocalCapacity,
2346 JNI::AllocObject,
2347 JNI::NewObject,
2348 JNI::NewObjectV,
2349 JNI::NewObjectA,
2350 JNI::GetObjectClass,
2351 JNI::IsInstanceOf,
2352 JNI::GetMethodID,
2353 JNI::CallObjectMethod,
2354 JNI::CallObjectMethodV,
2355 JNI::CallObjectMethodA,
2356 JNI::CallBooleanMethod,
2357 JNI::CallBooleanMethodV,
2358 JNI::CallBooleanMethodA,
2359 JNI::CallByteMethod,
2360 JNI::CallByteMethodV,
2361 JNI::CallByteMethodA,
2362 JNI::CallCharMethod,
2363 JNI::CallCharMethodV,
2364 JNI::CallCharMethodA,
2365 JNI::CallShortMethod,
2366 JNI::CallShortMethodV,
2367 JNI::CallShortMethodA,
2368 JNI::CallIntMethod,
2369 JNI::CallIntMethodV,
2370 JNI::CallIntMethodA,
2371 JNI::CallLongMethod,
2372 JNI::CallLongMethodV,
2373 JNI::CallLongMethodA,
2374 JNI::CallFloatMethod,
2375 JNI::CallFloatMethodV,
2376 JNI::CallFloatMethodA,
2377 JNI::CallDoubleMethod,
2378 JNI::CallDoubleMethodV,
2379 JNI::CallDoubleMethodA,
2380 JNI::CallVoidMethod,
2381 JNI::CallVoidMethodV,
2382 JNI::CallVoidMethodA,
2383 JNI::CallNonvirtualObjectMethod,
2384 JNI::CallNonvirtualObjectMethodV,
2385 JNI::CallNonvirtualObjectMethodA,
2386 JNI::CallNonvirtualBooleanMethod,
2387 JNI::CallNonvirtualBooleanMethodV,
2388 JNI::CallNonvirtualBooleanMethodA,
2389 JNI::CallNonvirtualByteMethod,
2390 JNI::CallNonvirtualByteMethodV,
2391 JNI::CallNonvirtualByteMethodA,
2392 JNI::CallNonvirtualCharMethod,
2393 JNI::CallNonvirtualCharMethodV,
2394 JNI::CallNonvirtualCharMethodA,
2395 JNI::CallNonvirtualShortMethod,
2396 JNI::CallNonvirtualShortMethodV,
2397 JNI::CallNonvirtualShortMethodA,
2398 JNI::CallNonvirtualIntMethod,
2399 JNI::CallNonvirtualIntMethodV,
2400 JNI::CallNonvirtualIntMethodA,
2401 JNI::CallNonvirtualLongMethod,
2402 JNI::CallNonvirtualLongMethodV,
2403 JNI::CallNonvirtualLongMethodA,
2404 JNI::CallNonvirtualFloatMethod,
2405 JNI::CallNonvirtualFloatMethodV,
2406 JNI::CallNonvirtualFloatMethodA,
2407 JNI::CallNonvirtualDoubleMethod,
2408 JNI::CallNonvirtualDoubleMethodV,
2409 JNI::CallNonvirtualDoubleMethodA,
2410 JNI::CallNonvirtualVoidMethod,
2411 JNI::CallNonvirtualVoidMethodV,
2412 JNI::CallNonvirtualVoidMethodA,
2413 JNI::GetFieldID,
2414 JNI::GetObjectField,
2415 JNI::GetBooleanField,
2416 JNI::GetByteField,
2417 JNI::GetCharField,
2418 JNI::GetShortField,
2419 JNI::GetIntField,
2420 JNI::GetLongField,
2421 JNI::GetFloatField,
2422 JNI::GetDoubleField,
2423 JNI::SetObjectField,
2424 JNI::SetBooleanField,
2425 JNI::SetByteField,
2426 JNI::SetCharField,
2427 JNI::SetShortField,
2428 JNI::SetIntField,
2429 JNI::SetLongField,
2430 JNI::SetFloatField,
2431 JNI::SetDoubleField,
2432 JNI::GetStaticMethodID,
2433 JNI::CallStaticObjectMethod,
2434 JNI::CallStaticObjectMethodV,
2435 JNI::CallStaticObjectMethodA,
2436 JNI::CallStaticBooleanMethod,
2437 JNI::CallStaticBooleanMethodV,
2438 JNI::CallStaticBooleanMethodA,
2439 JNI::CallStaticByteMethod,
2440 JNI::CallStaticByteMethodV,
2441 JNI::CallStaticByteMethodA,
2442 JNI::CallStaticCharMethod,
2443 JNI::CallStaticCharMethodV,
2444 JNI::CallStaticCharMethodA,
2445 JNI::CallStaticShortMethod,
2446 JNI::CallStaticShortMethodV,
2447 JNI::CallStaticShortMethodA,
2448 JNI::CallStaticIntMethod,
2449 JNI::CallStaticIntMethodV,
2450 JNI::CallStaticIntMethodA,
2451 JNI::CallStaticLongMethod,
2452 JNI::CallStaticLongMethodV,
2453 JNI::CallStaticLongMethodA,
2454 JNI::CallStaticFloatMethod,
2455 JNI::CallStaticFloatMethodV,
2456 JNI::CallStaticFloatMethodA,
2457 JNI::CallStaticDoubleMethod,
2458 JNI::CallStaticDoubleMethodV,
2459 JNI::CallStaticDoubleMethodA,
2460 JNI::CallStaticVoidMethod,
2461 JNI::CallStaticVoidMethodV,
2462 JNI::CallStaticVoidMethodA,
2463 JNI::GetStaticFieldID,
2464 JNI::GetStaticObjectField,
2465 JNI::GetStaticBooleanField,
2466 JNI::GetStaticByteField,
2467 JNI::GetStaticCharField,
2468 JNI::GetStaticShortField,
2469 JNI::GetStaticIntField,
2470 JNI::GetStaticLongField,
2471 JNI::GetStaticFloatField,
2472 JNI::GetStaticDoubleField,
2473 JNI::SetStaticObjectField,
2474 JNI::SetStaticBooleanField,
2475 JNI::SetStaticByteField,
2476 JNI::SetStaticCharField,
2477 JNI::SetStaticShortField,
2478 JNI::SetStaticIntField,
2479 JNI::SetStaticLongField,
2480 JNI::SetStaticFloatField,
2481 JNI::SetStaticDoubleField,
2482 JNI::NewString,
2483 JNI::GetStringLength,
2484 JNI::GetStringChars,
2485 JNI::ReleaseStringChars,
2486 JNI::NewStringUTF,
2487 JNI::GetStringUTFLength,
2488 JNI::GetStringUTFChars,
2489 JNI::ReleaseStringUTFChars,
2490 JNI::GetArrayLength,
2491 JNI::NewObjectArray,
2492 JNI::GetObjectArrayElement,
2493 JNI::SetObjectArrayElement,
2494 JNI::NewBooleanArray,
2495 JNI::NewByteArray,
2496 JNI::NewCharArray,
2497 JNI::NewShortArray,
2498 JNI::NewIntArray,
2499 JNI::NewLongArray,
2500 JNI::NewFloatArray,
2501 JNI::NewDoubleArray,
2502 JNI::GetBooleanArrayElements,
2503 JNI::GetByteArrayElements,
2504 JNI::GetCharArrayElements,
2505 JNI::GetShortArrayElements,
2506 JNI::GetIntArrayElements,
2507 JNI::GetLongArrayElements,
2508 JNI::GetFloatArrayElements,
2509 JNI::GetDoubleArrayElements,
2510 JNI::ReleaseBooleanArrayElements,
2511 JNI::ReleaseByteArrayElements,
2512 JNI::ReleaseCharArrayElements,
2513 JNI::ReleaseShortArrayElements,
2514 JNI::ReleaseIntArrayElements,
2515 JNI::ReleaseLongArrayElements,
2516 JNI::ReleaseFloatArrayElements,
2517 JNI::ReleaseDoubleArrayElements,
2518 JNI::GetBooleanArrayRegion,
2519 JNI::GetByteArrayRegion,
2520 JNI::GetCharArrayRegion,
2521 JNI::GetShortArrayRegion,
2522 JNI::GetIntArrayRegion,
2523 JNI::GetLongArrayRegion,
2524 JNI::GetFloatArrayRegion,
2525 JNI::GetDoubleArrayRegion,
2526 JNI::SetBooleanArrayRegion,
2527 JNI::SetByteArrayRegion,
2528 JNI::SetCharArrayRegion,
2529 JNI::SetShortArrayRegion,
2530 JNI::SetIntArrayRegion,
2531 JNI::SetLongArrayRegion,
2532 JNI::SetFloatArrayRegion,
2533 JNI::SetDoubleArrayRegion,
2534 JNI::RegisterNatives,
2535 JNI::UnregisterNatives,
2536 JNI::MonitorEnter,
2537 JNI::MonitorExit,
2538 JNI::GetJavaVM,
2539 JNI::GetStringRegion,
2540 JNI::GetStringUTFRegion,
2541 JNI::GetPrimitiveArrayCritical,
2542 JNI::ReleasePrimitiveArrayCritical,
2543 JNI::GetStringCritical,
2544 JNI::ReleaseStringCritical,
2545 JNI::NewWeakGlobalRef,
2546 JNI::DeleteWeakGlobalRef,
2547 JNI::ExceptionCheck,
2548 JNI::NewDirectByteBuffer,
2549 JNI::GetDirectBufferAddress,
2550 JNI::GetDirectBufferCapacity,
2551 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002552};
2553
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002554static const size_t kMonitorsInitial = 32; // Arbitrary.
2555static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2556
2557static const size_t kLocalsInitial = 64; // Arbitrary.
2558static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002559
Elliott Hughes75770752011-08-24 17:52:38 -07002560JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002561 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002562 vm(vm),
2563 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002564 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002565 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002566 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2567 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002568 functions = unchecked_functions = &gNativeInterface;
2569 if (check_jni) {
2570 functions = GetCheckJniNativeInterface();
2571 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002572}
2573
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002574JNIEnvExt::~JNIEnvExt() {
2575}
2576
Carl Shapiroea4dca82011-08-01 13:45:38 -07002577// JNI Invocation interface.
2578
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002579extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2580 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2581 if (args->version < JNI_VERSION_1_2) {
2582 return JNI_EVERSION;
2583 }
2584 Runtime::Options options;
2585 for (int i = 0; i < args->nOptions; ++i) {
2586 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002587 options.push_back(std::make_pair(StringPiece(option->optionString),
2588 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 }
2590 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002591 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 if (runtime == NULL) {
2593 return JNI_ERR;
2594 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002595 *p_env = Thread::Current()->GetJniEnv();
2596 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597 return JNI_OK;
2598 }
2599}
2600
Elliott Hughesf2682d52011-08-15 16:37:04 -07002601extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002602 Runtime* runtime = Runtime::Current();
2603 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002604 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002606 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002607 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 }
2609 return JNI_OK;
2610}
2611
2612// Historically unsupported.
2613extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2614 return JNI_ERR;
2615}
2616
Elliott Hughescdf53122011-08-19 15:46:09 -07002617class JII {
2618 public:
2619 static jint DestroyJavaVM(JavaVM* vm) {
2620 if (vm == NULL) {
2621 return JNI_ERR;
2622 } else {
2623 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2624 delete raw_vm->runtime;
2625 return JNI_OK;
2626 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002628
Elliott Hughescdf53122011-08-19 15:46:09 -07002629 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002630 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002631 }
2632
2633 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002634 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002635 }
2636
2637 static jint DetachCurrentThread(JavaVM* vm) {
2638 if (vm == NULL) {
2639 return JNI_ERR;
2640 } else {
2641 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2642 Runtime* runtime = raw_vm->runtime;
2643 runtime->DetachCurrentThread();
2644 return JNI_OK;
2645 }
2646 }
2647
2648 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2649 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2650 return JNI_EVERSION;
2651 }
2652 if (vm == NULL || env == NULL) {
2653 return JNI_ERR;
2654 }
2655 Thread* thread = Thread::Current();
2656 if (thread == NULL) {
2657 *env = NULL;
2658 return JNI_EDETACHED;
2659 }
2660 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661 return JNI_OK;
2662 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002663};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664
Elliott Hughesa2501992011-08-26 19:39:54 -07002665const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002666 NULL, // reserved0
2667 NULL, // reserved1
2668 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002669 JII::DestroyJavaVM,
2670 JII::AttachCurrentThread,
2671 JII::DetachCurrentThread,
2672 JII::GetEnv,
2673 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002674};
2675
Elliott Hughesbbd76712011-08-17 10:25:24 -07002676static const size_t kPinTableInitialSize = 16;
2677static const size_t kPinTableMaxSize = 1024;
2678
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002679static const size_t kGlobalsInitial = 512; // Arbitrary.
2680static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2681
2682static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2683static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2684
Elliott Hughesa0957642011-09-02 14:27:33 -07002685JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002686 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002687 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002688 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002689 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002690 verbose_jni(options->IsVerbose("jni")),
2691 log_third_party_jni(options->IsVerbose("third-party-jni")),
2692 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002693 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002694 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002695 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002696 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002697 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002698 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002699 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2700 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2701 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002702 functions = unchecked_functions = &gInvokeInterface;
2703 if (check_jni) {
2704 functions = GetCheckJniInvokeInterface();
2705 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002706}
2707
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002708JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002709 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002710 delete globals_lock;
2711 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002712 delete libraries_lock;
2713 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002714}
2715
Elliott Hughes75770752011-08-24 17:52:38 -07002716bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2717 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002718
2719 // See if we've already loaded this library. If we have, and the class loader
2720 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002721 // TODO: for better results we should canonicalize the pathname (or even compare
2722 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002723 SharedLibrary* library;
2724 {
2725 // TODO: move the locking (and more of this logic) into Libraries.
2726 MutexLock mu(libraries_lock);
2727 library = libraries->Get(path);
2728 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002729 if (library != NULL) {
2730 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002731 // The library will be associated with class_loader. The JNI
2732 // spec says we can't load the same library into more than one
2733 // class loader.
2734 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2735 "ClassLoader %p; can't open in ClassLoader %p",
2736 path.c_str(), library->GetClassLoader(), class_loader);
2737 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002738 return false;
2739 }
2740 if (verbose_jni) {
2741 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2742 << "ClassLoader " << class_loader << "]";
2743 }
2744 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002745 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2746 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002747 return false;
2748 }
2749 return true;
2750 }
2751
2752 // Open the shared library. Because we're using a full path, the system
2753 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2754 // resolve this library's dependencies though.)
2755
2756 // Failures here are expected when java.library.path has several entries
2757 // and we have to hunt for the lib.
2758
2759 // The current version of the dynamic linker prints detailed information
2760 // about dlopen() failures. Some things to check if the message is
2761 // cryptic:
2762 // - make sure the library exists on the device
2763 // - verify that the right path is being opened (the debug log message
2764 // above can help with that)
2765 // - check to see if the library is valid (e.g. not zero bytes long)
2766 // - check config/prelink-linux-arm.map to ensure that the library
2767 // is listed and is not being overrun by the previous entry (if
2768 // loading suddenly stops working on a prelinked library, this is
2769 // a good one to check)
2770 // - write a trivial app that calls sleep() then dlopen(), attach
2771 // to it with "strace -p <pid>" while it sleeps, and watch for
2772 // attempts to open nonexistent dependent shared libs
2773
2774 // TODO: automate some of these checks!
2775
2776 // This can execute slowly for a large library on a busy system, so we
2777 // want to switch from RUNNING to VMWAIT while it executes. This allows
2778 // the GC to ignore us.
2779 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002780 void* handle = NULL;
2781 {
2782 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2783 handle = dlopen(path.c_str(), RTLD_LAZY);
2784 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002785
2786 if (verbose_jni) {
2787 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2788 }
2789
2790 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002791 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 return false;
2793 }
2794
2795 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002797 // TODO: move the locking (and more of this logic) into Libraries.
2798 MutexLock mu(libraries_lock);
2799 library = libraries->Get(path);
2800 if (library != NULL) {
2801 LOG(INFO) << "WOW: we lost a race to add shared library: "
2802 << "\"" << path << "\" ClassLoader=" << class_loader;
2803 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002805 library = new SharedLibrary(path, handle, class_loader);
2806 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002808
2809 if (verbose_jni) {
2810 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2811 }
2812
2813 bool result = true;
2814 void* sym = dlsym(handle, "JNI_OnLoad");
2815 if (sym == NULL) {
2816 if (verbose_jni) {
2817 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2818 }
2819 } else {
2820 // Call JNI_OnLoad. We have to override the current class
2821 // loader, which will always be "null" since the stuff at the
2822 // top of the stack is around Runtime.loadLibrary(). (See
2823 // the comments in the JNI FindClass function.)
2824 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2825 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002826 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002827 self->SetClassLoaderOverride(class_loader);
2828
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002829 int version = 0;
2830 {
2831 ScopedThreadStateChange tsc(self, Thread::kNative);
2832 if (verbose_jni) {
2833 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2834 }
2835 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002836 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002837
2838 self->SetClassLoaderOverride(old_class_loader);;
2839
2840 if (version != JNI_VERSION_1_2 &&
2841 version != JNI_VERSION_1_4 &&
2842 version != JNI_VERSION_1_6) {
2843 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2844 << "bad version: " << version;
2845 // It's unwise to call dlclose() here, but we can mark it
2846 // as bad and ensure that future load attempts will fail.
2847 // We don't know how far JNI_OnLoad got, so there could
2848 // be some partially-initialized stuff accessible through
2849 // newly-registered native method calls. We could try to
2850 // unregister them, but that doesn't seem worthwhile.
2851 result = false;
2852 } else {
2853 if (verbose_jni) {
2854 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2855 << " from JNI_OnLoad in \"" << path << "\"]";
2856 }
2857 }
2858 }
2859
2860 library->SetResult(result);
2861 return result;
2862}
2863
2864void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2865 CHECK(m->IsNative());
2866
2867 Class* c = m->GetDeclaringClass();
2868
2869 // If this is a static method, it could be called before the class
2870 // has been initialized.
2871 if (m->IsStatic()) {
2872 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2873 return NULL;
2874 }
2875 } else {
2876 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2877 }
2878
2879 MutexLock mu(libraries_lock);
2880 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002881}
2882
Elliott Hughes410c0c82011-09-01 17:58:25 -07002883void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2884 {
2885 MutexLock mu(globals_lock);
2886 globals.VisitRoots(visitor, arg);
2887 }
2888 {
2889 MutexLock mu(pins_lock);
2890 pin_table.VisitRoots(visitor, arg);
2891 }
2892 // The weak_globals table is visited by the GC itself (because it mutates the table).
2893}
2894
Ian Rogersdf20fe02011-07-20 20:34:16 -07002895} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002896
2897std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2898 switch (rhs) {
2899 case JNIInvalidRefType:
2900 os << "JNIInvalidRefType";
2901 return os;
2902 case JNILocalRefType:
2903 os << "JNILocalRefType";
2904 return os;
2905 case JNIGlobalRefType:
2906 os << "JNIGlobalRefType";
2907 return os;
2908 case JNIWeakGlobalRefType:
2909 os << "JNIWeakGlobalRefType";
2910 return os;
2911 }
2912}