blob: c9e4839e498c71b5e9bdd292128f4503cd41956f [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;
349 if (is_static) {
350 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700351 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700352 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700353 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700354
Elliott Hughescdf53122011-08-19 15:46:09 -0700355 if (field == NULL) {
356 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700357 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700358 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
359 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700360 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700361 return NULL;
362 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700363
Elliott Hughescdf53122011-08-19 15:46:09 -0700364 jweak fid = AddWeakGlobalReference(ts, field);
365 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700366}
367
Elliott Hughes75770752011-08-24 17:52:38 -0700368void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
369 JavaVMExt* vm = ts.Vm();
370 MutexLock mu(vm->pins_lock);
371 vm->pin_table.Add(array);
372}
373
374void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
375 JavaVMExt* vm = ts.Vm();
376 MutexLock mu(vm->pins_lock);
377 vm->pin_table.Remove(array);
378}
379
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700380template<typename JniT, typename ArtT>
381JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
382 CHECK_GE(length, 0); // TODO: ReportJniError
383 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700384 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700385}
386
Elliott Hughes75770752011-08-24 17:52:38 -0700387template <typename ArrayT, typename CArrayT, typename ArtArrayT>
388CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
389 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
390 PinPrimitiveArray(ts, array);
391 if (is_copy != NULL) {
392 *is_copy = JNI_FALSE;
393 }
394 return array->GetData();
395}
396
397template <typename ArrayT>
398void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
399 if (mode != JNI_COMMIT) {
400 Array* array = Decode<Array*>(ts, java_array);
401 UnpinPrimitiveArray(ts, array);
402 }
403}
404
Elliott Hughes814e4032011-08-23 12:07:56 -0700405void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
406 std::string type(PrettyType(array));
407 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
408 "%s offset=%d length=%d %s.length=%d",
409 type.c_str(), start, length, identifier, array->GetLength());
410}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700411void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
412 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
413 "offset=%d length=%d string.length()=%d", start, length, array_length);
414}
Elliott Hughes814e4032011-08-23 12:07:56 -0700415
416template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700417void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700418 ArrayT* array = Decode<ArrayT*>(ts, java_array);
419 if (start < 0 || length < 0 || start + length > array->GetLength()) {
420 ThrowAIOOBE(ts, array, start, length, "src");
421 } else {
422 JavaT* data = array->GetData();
423 memcpy(buf, data + start, length * sizeof(JavaT));
424 }
425}
426
427template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700428void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700429 ArrayT* array = Decode<ArrayT*>(ts, java_array);
430 if (start < 0 || length < 0 || start + length > array->GetLength()) {
431 ThrowAIOOBE(ts, array, start, length, "dst");
432 } else {
433 JavaT* data = array->GetData();
434 memcpy(data + start, buf, length * sizeof(JavaT));
435 }
436}
437
Elliott Hughes75770752011-08-24 17:52:38 -0700438jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700439 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
440 CHECK(buffer_class.get() != NULL);
441 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
442}
443
Elliott Hughes75770752011-08-24 17:52:38 -0700444jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700445 static jclass buffer_class = InitDirectByteBufferClass(env);
446 return buffer_class;
447}
448
Elliott Hughes75770752011-08-24 17:52:38 -0700449jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
450 if (vm == NULL || p_env == NULL) {
451 return JNI_ERR;
452 }
453
454 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
455 JavaVMAttachArgs args;
456 if (thr_args == NULL) {
457 // Allow the v1.1 calling convention.
458 args.version = JNI_VERSION_1_2;
459 args.name = NULL;
460 args.group = NULL; // TODO: get "main" thread group
461 } else {
462 args.version = in_args->version;
463 args.name = in_args->name;
464 if (in_args->group != NULL) {
465 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
466 args.group = NULL; // TODO: decode in_args->group
467 } else {
468 args.group = NULL; // TODO: get "main" thread group
469 }
470 }
471 CHECK_GE(args.version, JNI_VERSION_1_2);
472
473 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
474 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
475}
476
Elliott Hughes79082e32011-08-25 12:07:32 -0700477class SharedLibrary {
478 public:
479 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
480 : path_(path),
481 handle_(handle),
482 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
483 jni_on_load_tid_(Thread::Current()->GetId()),
484 jni_on_load_result_(kPending) {
485 pthread_cond_init(&jni_on_load_cond_, NULL);
486 }
487
488 ~SharedLibrary() {
489 delete jni_on_load_lock_;
490 }
491
492 Object* GetClassLoader() {
493 return class_loader_;
494 }
495
496 std::string GetPath() {
497 return path_;
498 }
499
500 /*
501 * Check the result of an earlier call to JNI_OnLoad on this library. If
502 * the call has not yet finished in another thread, wait for it.
503 */
504 bool CheckOnLoadResult(JavaVMExt* vm) {
505 Thread* self = Thread::Current();
506 if (jni_on_load_tid_ == self->GetId()) {
507 // Check this so we don't end up waiting for ourselves. We need
508 // to return "true" so the caller can continue.
509 LOG(INFO) << *self << " recursive attempt to load library "
510 << "\"" << path_ << "\"";
511 return true;
512 }
513
514 MutexLock mu(jni_on_load_lock_);
515 while (jni_on_load_result_ == kPending) {
516 if (vm->verbose_jni) {
517 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
518 << "JNI_OnLoad...]";
519 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700520 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700522 }
523
524 bool okay = (jni_on_load_result_ == kOkay);
525 if (vm->verbose_jni) {
526 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
527 << (okay ? "succeeded" : "failed") << "]";
528 }
529 return okay;
530 }
531
532 void SetResult(bool result) {
533 jni_on_load_result_ = result ? kOkay : kFailed;
534 jni_on_load_tid_ = 0;
535
536 // Broadcast a wakeup to anybody sleeping on the condition variable.
537 MutexLock mu(jni_on_load_lock_);
538 pthread_cond_broadcast(&jni_on_load_cond_);
539 }
540
541 void* FindSymbol(const std::string& symbol_name) {
542 return dlsym(handle_, symbol_name.c_str());
543 }
544
545 private:
546 enum JNI_OnLoadState {
547 kPending,
548 kFailed,
549 kOkay,
550 };
551
552 // Path to library "/system/lib/libjni.so".
553 std::string path_;
554
555 // The void* returned by dlopen(3).
556 void* handle_;
557
558 // The ClassLoader this library is associated with.
559 Object* class_loader_;
560
561 // Guards remaining items.
562 Mutex* jni_on_load_lock_;
563 // Wait for JNI_OnLoad in other thread.
564 pthread_cond_t jni_on_load_cond_;
565 // Recursive invocation guard.
566 uint32_t jni_on_load_tid_;
567 // Result of earlier JNI_OnLoad call.
568 JNI_OnLoadState jni_on_load_result_;
569};
570
Elliott Hughescdf53122011-08-19 15:46:09 -0700571} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700572
Elliott Hughes79082e32011-08-25 12:07:32 -0700573// This exists mainly to keep implementation details out of the header file.
574class Libraries {
575 public:
576 Libraries() {
577 }
578
579 ~Libraries() {
580 // Delete our map values. (The keys will be cleaned up by the map itself.)
581 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
582 delete it->second;
583 }
584 }
585
586 SharedLibrary* Get(const std::string& path) {
587 return libraries_[path];
588 }
589
590 void Put(const std::string& path, SharedLibrary* library) {
591 libraries_[path] = library;
592 }
593
594 // See section 11.3 "Linking Native Methods" of the JNI spec.
595 void* FindNativeMethod(const Method* m) {
596 std::string jni_short_name(JniShortName(m));
597 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700598 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
600 SharedLibrary* library = it->second;
601 if (library->GetClassLoader() != declaring_class_loader) {
602 // We only search libraries loaded by the appropriate ClassLoader.
603 continue;
604 }
605 // Try the short name then the long name...
606 void* fn = library->FindSymbol(jni_short_name);
607 if (fn == NULL) {
608 fn = library->FindSymbol(jni_long_name);
609 }
610 if (fn != NULL) {
611 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700612 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 << " in \"" << library->GetPath() << "\"]";
614 }
615 return fn;
616 }
617 }
618 std::string detail;
619 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700620 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700621 LOG(ERROR) << detail;
622 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
623 "%s", detail.c_str());
624 return NULL;
625 }
626
627 private:
628 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
629
630 std::map<std::string, SharedLibrary*> libraries_;
631};
632
Elliott Hughescdf53122011-08-19 15:46:09 -0700633class JNI {
634 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 static jint GetVersion(JNIEnv* env) {
637 ScopedJniThreadState ts(env);
638 return JNI_VERSION_1_6;
639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
642 ScopedJniThreadState ts(env);
643 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700644 return NULL;
645 }
646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jclass FindClass(JNIEnv* env, const char* name) {
648 ScopedJniThreadState ts(env);
649 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
650 std::string descriptor(NormalizeJniClassDescriptor(name));
651 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700652 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700653 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700654 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
658 ScopedJniThreadState ts(env);
659 Method* method = Decode<Method*>(ts, java_method);
660 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700661 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
664 ScopedJniThreadState ts(env);
665 Field* field = Decode<Field*>(ts, java_field);
666 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
670 ScopedJniThreadState ts(env);
671 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700672 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
676 ScopedJniThreadState ts(env);
677 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700678 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
682 ScopedJniThreadState ts(env);
683 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700684 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 }
686
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700687 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700689 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700690 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 Class* c1 = Decode<Class*>(ts, java_class1);
696 Class* c2 = Decode<Class*>(ts, java_class2);
697 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700699
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700702 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 if (jobj == NULL) {
704 // NB. JNI is different from regular Java instanceof in this respect
705 return JNI_TRUE;
706 } else {
707 Object* obj = Decode<Object*>(ts, jobj);
708 Class* klass = Decode<Class*>(ts, clazz);
709 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
710 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700712
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 static jint Throw(JNIEnv* env, jthrowable java_exception) {
714 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 if (exception == NULL) {
717 return JNI_ERR;
718 }
719 ts.Self()->SetException(exception);
720 return JNI_OK;
721 }
722
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700723 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700725 // TODO: check for a pending exception to decide what constructor to call.
726 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
727 if (mid == NULL) {
728 return JNI_ERR;
729 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
731 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732 return JNI_ERR;
733 }
734
735 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 args[0].l = s.get();
737 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
738 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700739 return JNI_ERR;
740 }
741
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700743 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 return JNI_OK;
747 }
748
749 static jboolean ExceptionCheck(JNIEnv* env) {
750 ScopedJniThreadState ts(env);
751 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
752 }
753
754 static void ExceptionClear(JNIEnv* env) {
755 ScopedJniThreadState ts(env);
756 ts.Self()->ClearException();
757 }
758
759 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700761
762 Thread* self = ts.Self();
763 Throwable* original_exception = self->GetException();
764 self->ClearException();
765
Elliott Hughesbf86d042011-08-31 17:53:14 -0700766 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
768 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
769 if (mid == NULL) {
770 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
771 << PrettyType(original_exception);
772 } else {
773 env->CallVoidMethod(exception.get(), mid);
774 if (self->IsExceptionPending()) {
775 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
776 << " thrown while calling printStackTrace";
777 self->ClearException();
778 }
779 }
780
781 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700783
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 static jthrowable ExceptionOccurred(JNIEnv* env) {
785 ScopedJniThreadState ts(env);
786 Object* exception = ts.Self()->GetException();
787 if (exception == NULL) {
788 return NULL;
789 } else {
790 // TODO: if adding a local reference failing causes the VM to abort
791 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700792 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 if (localException == NULL) {
794 // We were unable to add a new local reference, and threw a new
795 // exception. We can't return "exception", because it's not a
796 // local reference. So we have to return NULL, indicating that
797 // there was no exception, even though it's pretty much raining
798 // exceptions in here.
799 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
800 }
801 return localException;
802 }
803 }
804
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 static void FatalError(JNIEnv* env, const char* msg) {
806 ScopedJniThreadState ts(env);
807 LOG(FATAL) << "JNI FatalError called: " << msg;
808 }
809
810 static jint PushLocalFrame(JNIEnv* env, jint cap) {
811 ScopedJniThreadState ts(env);
812 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
813 return JNI_OK;
814 }
815
816 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
817 ScopedJniThreadState ts(env);
818 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
819 return res;
820 }
821
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
823 ScopedJniThreadState ts(env);
824 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
825 return 0;
826 }
827
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
829 ScopedJniThreadState ts(env);
830 if (obj == NULL) {
831 return NULL;
832 }
833
Elliott Hughes75770752011-08-24 17:52:38 -0700834 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 IndirectReferenceTable& globals = vm->globals;
836 MutexLock mu(vm->globals_lock);
837 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
838 return reinterpret_cast<jobject>(ref);
839 }
840
841 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
842 ScopedJniThreadState ts(env);
843 if (obj == NULL) {
844 return;
845 }
846
Elliott Hughes75770752011-08-24 17:52:38 -0700847 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 IndirectReferenceTable& globals = vm->globals;
849 MutexLock mu(vm->globals_lock);
850
851 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
852 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
853 << "failed to find entry";
854 }
855 }
856
857 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
858 ScopedJniThreadState ts(env);
859 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
860 }
861
862 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
863 ScopedJniThreadState ts(env);
864 if (obj == NULL) {
865 return;
866 }
867
Elliott Hughes75770752011-08-24 17:52:38 -0700868 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 IndirectReferenceTable& weak_globals = vm->weak_globals;
870 MutexLock mu(vm->weak_globals_lock);
871
872 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
873 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
874 << "failed to find entry";
875 }
876 }
877
878 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return NULL;
882 }
883
884 IndirectReferenceTable& locals = ts.Env()->locals;
885
886 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
887 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
888 return reinterpret_cast<jobject>(ref);
889 }
890
891 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
892 ScopedJniThreadState ts(env);
893 if (obj == NULL) {
894 return;
895 }
896
897 IndirectReferenceTable& locals = ts.Env()->locals;
898
899 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
900 if (!locals.Remove(cookie, obj)) {
901 // Attempting to delete a local reference that is not in the
902 // topmost local reference frame is a no-op. DeleteLocalRef returns
903 // void and doesn't throw any exceptions, but we should probably
904 // complain about it so the user will notice that things aren't
905 // going quite the way they expect.
906 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
907 << "failed to find entry";
908 }
909 }
910
911 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
912 ScopedJniThreadState ts(env);
913 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
914 ? JNI_TRUE : JNI_FALSE;
915 }
916
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700917 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 Class* c = Decode<Class*>(ts, java_class);
920 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
921 return NULL;
922 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700923 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 ScopedJniThreadState ts(env);
928 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 va_start(args, mid);
930 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 va_end(args);
932 return result;
933 }
934
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700937 Class* c = Decode<Class*>(ts, java_class);
938 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
939 return NULL;
940 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700941 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700942 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 return local_result;
945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700949 Class* c = Decode<Class*>(ts, java_class);
950 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
951 return NULL;
952 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700953 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700954 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 return local_result;
957 }
958
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
960 ScopedJniThreadState ts(env);
961 return FindMethodID(ts, c, name, sig, false);
962 }
963
964 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
965 ScopedJniThreadState ts(env);
966 return FindMethodID(ts, c, name, sig, true);
967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700975 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700980 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700981 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700986 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700987 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_list ap;
993 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700994 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_end(ap);
996 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001001 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001006 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001013 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
1015 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001020 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001025 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001032 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_end(ap);
1034 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001039 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001044 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001051 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_end(ap);
1053 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001058 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001063 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001070 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
1072 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001077 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001082 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001089 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_end(ap);
1091 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001096 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001101 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001108 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
1110 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001115 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001120 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_list ap;
1126 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001127 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
1129 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001134 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001139 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_list ap;
1145 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001146 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001152 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001157 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
1160 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
1163 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_start(ap, mid);
1165 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001166 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 va_end(ap);
1168 return local_result;
1169 }
1170
1171 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001175 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
1178 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001182 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
1185 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
1188 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_start(ap, mid);
1190 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 va_end(ap);
1192 return result.z;
1193 }
1194
1195 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
1210 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_start(ap, mid);
1212 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_end(ap);
1214 return result.b;
1215 }
1216
1217 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
1223 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
1229 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 ScopedJniThreadState ts(env);
1232 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_start(ap, mid);
1234 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_end(ap);
1236 return result.c;
1237 }
1238
1239 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
1251 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
1254 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_start(ap, mid);
1256 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_end(ap);
1258 return result.s;
1259 }
1260
1261 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
1273 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
1276 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 va_start(ap, mid);
1278 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
1280 return result.i;
1281 }
1282
1283 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
1298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
1300 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_end(ap);
1302 return result.j;
1303 }
1304
1305 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
1317 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
1320 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 va_start(ap, mid);
1322 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 va_end(ap);
1324 return result.f;
1325 }
1326
1327 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
1339 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 ScopedJniThreadState ts(env);
1342 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 va_start(ap, mid);
1344 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 va_end(ap);
1346 return result.d;
1347 }
1348
1349 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
1355 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
1361 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
1364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
1366 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_end(ap);
1368 }
1369
1370 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001371 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
1376 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001379 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
1382 static jfieldID GetFieldID(JNIEnv* env,
1383 jclass c, const char* name, const char* sig) {
1384 ScopedJniThreadState ts(env);
1385 return FindFieldID(ts, c, name, sig, false);
1386 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001387
1388
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 static jfieldID GetStaticFieldID(JNIEnv* env,
1390 jclass c, const char* name, const char* sig) {
1391 ScopedJniThreadState ts(env);
1392 return FindFieldID(ts, c, name, sig, true);
1393 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001394
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 Object* o = Decode<Object*>(ts, obj);
1398 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001399 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001405 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 Object* o = Decode<Object*>(ts, java_object);
1411 Object* v = Decode<Object*>(ts, java_value);
1412 Field* f = DecodeField(ts, fid);
1413 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 Object* v = Decode<Object*>(ts, java_value);
1419 Field* f = DecodeField(ts, fid);
1420 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423#define GET_PRIMITIVE_FIELD(fn, instance) \
1424 ScopedJniThreadState ts(env); \
1425 Object* o = Decode<Object*>(ts, instance); \
1426 Field* f = DecodeField(ts, fid); \
1427 return f->fn(o)
1428
1429#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1430 ScopedJniThreadState ts(env); \
1431 Object* o = Decode<Object*>(ts, instance); \
1432 Field* f = DecodeField(ts, fid); \
1433 f->fn(o, value)
1434
1435 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetLong, NULL);
1489 }
1490
1491 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1493 }
1494
1495 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1497 }
1498
1499 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1500 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1501 }
1502
1503 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1504 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1505 }
1506
1507 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1508 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1509 }
1510
1511 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1512 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1513 }
1514
1515 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1516 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1517 }
1518
1519 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1520 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1521 }
1522
1523 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1524 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1525 }
1526
1527 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1528 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1529 }
1530
1531 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1532 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1533 }
1534
1535 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1536 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1537 }
1538
1539 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1540 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1541 }
1542
1543 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1544 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1545 }
1546
1547 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1548 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1549 }
1550
1551 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1552 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1553 }
1554
1555 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1556 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1557 }
1558
1559 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1560 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
1563 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 ScopedJniThreadState ts(env);
1566 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 va_start(ap, mid);
1568 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001569 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 va_end(ap);
1571 return local_result;
1572 }
1573
1574 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001578 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
1581 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001585 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
1588 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
1591 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 va_start(ap, mid);
1593 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 va_end(ap);
1595 return result.z;
1596 }
1597
1598 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
1604 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
1612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
1614 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_end(ap);
1616 return result.b;
1617 }
1618
1619 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
1625 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
1633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
1635 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_end(ap);
1637 return result.c;
1638 }
1639
1640 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
1646 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 }
1651
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 ScopedJniThreadState ts(env);
1654 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 va_start(ap, mid);
1656 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 va_end(ap);
1658 return result.s;
1659 }
1660
1661 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
1667 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
1675 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 va_start(ap, mid);
1677 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 va_end(ap);
1679 return result.i;
1680 }
1681
1682 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
1688 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
1696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
1698 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 return result.j;
1701 }
1702
1703 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
1709 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
1717 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 va_start(ap, mid);
1719 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_end(ap);
1721 return result.f;
1722 }
1723
1724 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
1730 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
1738 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 va_start(ap, mid);
1740 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
1742 return result.d;
1743 }
1744
1745 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
1751 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
1759 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 va_start(ap, mid);
1761 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_end(ap);
1763 }
1764
1765 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001768 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
1771 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes814e4032011-08-23 12:07:56 -07001777 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 if (chars == NULL && char_count == 0) {
1780 return NULL;
1781 }
1782 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001783 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
1786 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1787 ScopedJniThreadState ts(env);
1788 if (utf == NULL) {
1789 return NULL;
1790 }
1791 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001792 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes814e4032011-08-23 12:07:56 -07001795 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1796 ScopedJniThreadState ts(env);
1797 return Decode<String*>(ts, java_string)->GetLength();
1798 }
1799
1800 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1801 ScopedJniThreadState ts(env);
1802 return Decode<String*>(ts, java_string)->GetUtfLength();
1803 }
1804
Elliott Hughesb465ab02011-08-24 11:21:21 -07001805 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001807 String* s = Decode<String*>(ts, java_string);
1808 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1809 ThrowSIOOBE(ts, start, length, s->GetLength());
1810 } else {
1811 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1812 memcpy(buf, chars + start, length * sizeof(jchar));
1813 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 }
1815
Elliott Hughesb465ab02011-08-24 11:21:21 -07001816 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001817 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001818 String* s = Decode<String*>(ts, java_string);
1819 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1820 ThrowSIOOBE(ts, start, length, s->GetLength());
1821 } else {
1822 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1823 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1824 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001825 }
1826
Elliott Hughes75770752011-08-24 17:52:38 -07001827 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001829 String* s = Decode<String*>(ts, java_string);
1830 const CharArray* chars = s->GetCharArray();
1831 PinPrimitiveArray(ts, chars);
1832 if (is_copy != NULL) {
1833 *is_copy = JNI_FALSE;
1834 }
1835 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001840 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes75770752011-08-24 17:52:38 -07001843 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes75770752011-08-24 17:52:38 -07001848 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001850 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes75770752011-08-24 17:52:38 -07001853 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001855 if (java_string == NULL) {
1856 return NULL;
1857 }
1858 if (is_copy != NULL) {
1859 *is_copy = JNI_TRUE;
1860 }
1861 String* s = Decode<String*>(ts, java_string);
1862 size_t byte_count = s->GetUtfLength();
1863 char* bytes = new char[byte_count + 1];
1864 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001865 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001866 return NULL;
1867 }
1868 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1869 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1870 bytes[byte_count] = '\0';
1871 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001877 }
1878
Elliott Hughesbd935992011-08-22 11:59:34 -07001879 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001881 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001882 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001883 Array* array = obj->AsArray();
1884 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001890 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
1893 static void SetObjectArrayElement(JNIEnv* env,
1894 jobjectArray java_array, jsize index, jobject java_value) {
1895 ScopedJniThreadState ts(env);
1896 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1897 Object* value = Decode<Object*>(ts, java_value);
1898 array->Set(index, value);
1899 }
1900
1901 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1902 ScopedJniThreadState ts(env);
1903 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1904 }
1905
1906 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1907 ScopedJniThreadState ts(env);
1908 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1909 }
1910
1911 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1912 ScopedJniThreadState ts(env);
1913 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1914 }
1915
1916 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1917 ScopedJniThreadState ts(env);
1918 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1919 }
1920
1921 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1922 ScopedJniThreadState ts(env);
1923 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1924 }
1925
1926 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1927 ScopedJniThreadState ts(env);
1928 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1929 }
1930
1931 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1932 ScopedJniThreadState ts(env);
1933 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1934 }
1935
1936 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1937 ScopedJniThreadState ts(env);
1938 CHECK_GE(length, 0); // TODO: ReportJniError
1939
1940 // Compute the array class corresponding to the given element class.
1941 Class* element_class = Decode<Class*>(ts, element_jclass);
1942 std::string descriptor;
1943 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001944 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001945
1946 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001947 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1948 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 return NULL;
1950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 // Allocate and initialize if necessary.
1953 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 if (initial_element != NULL) {
1956 Object* initial_object = Decode<Object*>(ts, initial_element);
1957 for (jsize i = 0; i < length; ++i) {
1958 result->Set(i, initial_object);
1959 }
1960 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001961 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
1964 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1965 ScopedJniThreadState ts(env);
1966 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002041 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002046 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes75770752011-08-24 17:52:38 -07002054 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 Class* c = Decode<Class*>(ts, java_class);
2142
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 const char* name = methods[i].name;
2145 const char* sig = methods[i].signature;
2146
2147 if (*sig == '!') {
2148 // TODO: fast jni. it's too noisy to log all these.
2149 ++sig;
2150 }
2151
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152 Method* m = c->FindDirectMethod(name, sig);
2153 if (m == NULL) {
2154 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2160 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002161 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2167 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002168 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 return JNI_ERR;
2170 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171
Elliott Hughes75770752011-08-24 17:52:38 -07002172 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002173 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 }
2175
2176 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178 return JNI_OK;
2179 }
2180
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 Class* c = Decode<Class*>(ts, java_class);
2184
Elliott Hughes75770752011-08-24 17:52:38 -07002185 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002186 LOG(INFO) << "[Unregistering JNI native methods for "
2187 << PrettyDescriptor(c->GetDescriptor()) << "]";
2188 }
2189
2190 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2191 Method* m = c->GetDirectMethod(i);
2192 if (m->IsNative()) {
2193 m->UnregisterNative();
2194 }
2195 }
2196 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2197 Method* m = c->GetVirtualMethod(i);
2198 if (m->IsNative()) {
2199 m->UnregisterNative();
2200 }
2201 }
2202
2203 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughes72025e52011-08-23 17:50:30 -07002206 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002208 Decode<Object*>(ts, java_object)->MonitorEnter();
2209 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes72025e52011-08-23 17:50:30 -07002212 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002214 Decode<Object*>(ts, java_object)->MonitorEnter();
2215 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
2218 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2219 ScopedJniThreadState ts(env);
2220 Runtime* runtime = Runtime::Current();
2221 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002222 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 } else {
2224 *vm = NULL;
2225 }
2226 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2227 }
2228
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2230 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231
2232 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002233 CHECK(address != NULL); // TODO: ReportJniError
2234 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235
2236 jclass buffer_class = GetDirectByteBufferClass(env);
2237 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2238 if (mid == NULL) {
2239 return NULL;
2240 }
2241
2242 // At the moment, the Java side is limited to 32 bits.
2243 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2244 CHECK_LE(capacity, 0xffffffff);
2245 jint address_arg = reinterpret_cast<jint>(address);
2246 jint capacity_arg = static_cast<jint>(capacity);
2247
2248 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2249 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2255 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002260 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2261 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002266
Elliott Hughes75770752011-08-24 17:52:38 -07002267 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268
2269 // Do we definitely know what kind of reference this is?
2270 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2271 IndirectRefKind kind = GetIndirectRefKind(ref);
2272 switch (kind) {
2273 case kLocal:
2274 return JNILocalRefType;
2275 case kGlobal:
2276 return JNIGlobalRefType;
2277 case kWeakGlobal:
2278 return JNIWeakGlobalRefType;
2279 case kSirtOrInvalid:
2280 // Is it in a stack IRT?
2281 if (ts.Self()->SirtContains(java_object)) {
2282 return JNILocalRefType;
2283 }
2284
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002285 if (!ts.Env()->work_around_app_jni_bugs) {
2286 return JNIInvalidRefType;
2287 }
2288
Elliott Hughesb465ab02011-08-24 11:21:21 -07002289 // If we're handing out direct pointers, check whether it's a direct pointer
2290 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002291 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 if (ts.Env()->locals.Contains(java_object)) {
2293 return JNILocalRefType;
2294 }
2295 }
2296
2297 return JNIInvalidRefType;
2298 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002301
Elliott Hughesa2501992011-08-26 19:39:54 -07002302const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002303 NULL, // reserved0.
2304 NULL, // reserved1.
2305 NULL, // reserved2.
2306 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 JNI::GetVersion,
2308 JNI::DefineClass,
2309 JNI::FindClass,
2310 JNI::FromReflectedMethod,
2311 JNI::FromReflectedField,
2312 JNI::ToReflectedMethod,
2313 JNI::GetSuperclass,
2314 JNI::IsAssignableFrom,
2315 JNI::ToReflectedField,
2316 JNI::Throw,
2317 JNI::ThrowNew,
2318 JNI::ExceptionOccurred,
2319 JNI::ExceptionDescribe,
2320 JNI::ExceptionClear,
2321 JNI::FatalError,
2322 JNI::PushLocalFrame,
2323 JNI::PopLocalFrame,
2324 JNI::NewGlobalRef,
2325 JNI::DeleteGlobalRef,
2326 JNI::DeleteLocalRef,
2327 JNI::IsSameObject,
2328 JNI::NewLocalRef,
2329 JNI::EnsureLocalCapacity,
2330 JNI::AllocObject,
2331 JNI::NewObject,
2332 JNI::NewObjectV,
2333 JNI::NewObjectA,
2334 JNI::GetObjectClass,
2335 JNI::IsInstanceOf,
2336 JNI::GetMethodID,
2337 JNI::CallObjectMethod,
2338 JNI::CallObjectMethodV,
2339 JNI::CallObjectMethodA,
2340 JNI::CallBooleanMethod,
2341 JNI::CallBooleanMethodV,
2342 JNI::CallBooleanMethodA,
2343 JNI::CallByteMethod,
2344 JNI::CallByteMethodV,
2345 JNI::CallByteMethodA,
2346 JNI::CallCharMethod,
2347 JNI::CallCharMethodV,
2348 JNI::CallCharMethodA,
2349 JNI::CallShortMethod,
2350 JNI::CallShortMethodV,
2351 JNI::CallShortMethodA,
2352 JNI::CallIntMethod,
2353 JNI::CallIntMethodV,
2354 JNI::CallIntMethodA,
2355 JNI::CallLongMethod,
2356 JNI::CallLongMethodV,
2357 JNI::CallLongMethodA,
2358 JNI::CallFloatMethod,
2359 JNI::CallFloatMethodV,
2360 JNI::CallFloatMethodA,
2361 JNI::CallDoubleMethod,
2362 JNI::CallDoubleMethodV,
2363 JNI::CallDoubleMethodA,
2364 JNI::CallVoidMethod,
2365 JNI::CallVoidMethodV,
2366 JNI::CallVoidMethodA,
2367 JNI::CallNonvirtualObjectMethod,
2368 JNI::CallNonvirtualObjectMethodV,
2369 JNI::CallNonvirtualObjectMethodA,
2370 JNI::CallNonvirtualBooleanMethod,
2371 JNI::CallNonvirtualBooleanMethodV,
2372 JNI::CallNonvirtualBooleanMethodA,
2373 JNI::CallNonvirtualByteMethod,
2374 JNI::CallNonvirtualByteMethodV,
2375 JNI::CallNonvirtualByteMethodA,
2376 JNI::CallNonvirtualCharMethod,
2377 JNI::CallNonvirtualCharMethodV,
2378 JNI::CallNonvirtualCharMethodA,
2379 JNI::CallNonvirtualShortMethod,
2380 JNI::CallNonvirtualShortMethodV,
2381 JNI::CallNonvirtualShortMethodA,
2382 JNI::CallNonvirtualIntMethod,
2383 JNI::CallNonvirtualIntMethodV,
2384 JNI::CallNonvirtualIntMethodA,
2385 JNI::CallNonvirtualLongMethod,
2386 JNI::CallNonvirtualLongMethodV,
2387 JNI::CallNonvirtualLongMethodA,
2388 JNI::CallNonvirtualFloatMethod,
2389 JNI::CallNonvirtualFloatMethodV,
2390 JNI::CallNonvirtualFloatMethodA,
2391 JNI::CallNonvirtualDoubleMethod,
2392 JNI::CallNonvirtualDoubleMethodV,
2393 JNI::CallNonvirtualDoubleMethodA,
2394 JNI::CallNonvirtualVoidMethod,
2395 JNI::CallNonvirtualVoidMethodV,
2396 JNI::CallNonvirtualVoidMethodA,
2397 JNI::GetFieldID,
2398 JNI::GetObjectField,
2399 JNI::GetBooleanField,
2400 JNI::GetByteField,
2401 JNI::GetCharField,
2402 JNI::GetShortField,
2403 JNI::GetIntField,
2404 JNI::GetLongField,
2405 JNI::GetFloatField,
2406 JNI::GetDoubleField,
2407 JNI::SetObjectField,
2408 JNI::SetBooleanField,
2409 JNI::SetByteField,
2410 JNI::SetCharField,
2411 JNI::SetShortField,
2412 JNI::SetIntField,
2413 JNI::SetLongField,
2414 JNI::SetFloatField,
2415 JNI::SetDoubleField,
2416 JNI::GetStaticMethodID,
2417 JNI::CallStaticObjectMethod,
2418 JNI::CallStaticObjectMethodV,
2419 JNI::CallStaticObjectMethodA,
2420 JNI::CallStaticBooleanMethod,
2421 JNI::CallStaticBooleanMethodV,
2422 JNI::CallStaticBooleanMethodA,
2423 JNI::CallStaticByteMethod,
2424 JNI::CallStaticByteMethodV,
2425 JNI::CallStaticByteMethodA,
2426 JNI::CallStaticCharMethod,
2427 JNI::CallStaticCharMethodV,
2428 JNI::CallStaticCharMethodA,
2429 JNI::CallStaticShortMethod,
2430 JNI::CallStaticShortMethodV,
2431 JNI::CallStaticShortMethodA,
2432 JNI::CallStaticIntMethod,
2433 JNI::CallStaticIntMethodV,
2434 JNI::CallStaticIntMethodA,
2435 JNI::CallStaticLongMethod,
2436 JNI::CallStaticLongMethodV,
2437 JNI::CallStaticLongMethodA,
2438 JNI::CallStaticFloatMethod,
2439 JNI::CallStaticFloatMethodV,
2440 JNI::CallStaticFloatMethodA,
2441 JNI::CallStaticDoubleMethod,
2442 JNI::CallStaticDoubleMethodV,
2443 JNI::CallStaticDoubleMethodA,
2444 JNI::CallStaticVoidMethod,
2445 JNI::CallStaticVoidMethodV,
2446 JNI::CallStaticVoidMethodA,
2447 JNI::GetStaticFieldID,
2448 JNI::GetStaticObjectField,
2449 JNI::GetStaticBooleanField,
2450 JNI::GetStaticByteField,
2451 JNI::GetStaticCharField,
2452 JNI::GetStaticShortField,
2453 JNI::GetStaticIntField,
2454 JNI::GetStaticLongField,
2455 JNI::GetStaticFloatField,
2456 JNI::GetStaticDoubleField,
2457 JNI::SetStaticObjectField,
2458 JNI::SetStaticBooleanField,
2459 JNI::SetStaticByteField,
2460 JNI::SetStaticCharField,
2461 JNI::SetStaticShortField,
2462 JNI::SetStaticIntField,
2463 JNI::SetStaticLongField,
2464 JNI::SetStaticFloatField,
2465 JNI::SetStaticDoubleField,
2466 JNI::NewString,
2467 JNI::GetStringLength,
2468 JNI::GetStringChars,
2469 JNI::ReleaseStringChars,
2470 JNI::NewStringUTF,
2471 JNI::GetStringUTFLength,
2472 JNI::GetStringUTFChars,
2473 JNI::ReleaseStringUTFChars,
2474 JNI::GetArrayLength,
2475 JNI::NewObjectArray,
2476 JNI::GetObjectArrayElement,
2477 JNI::SetObjectArrayElement,
2478 JNI::NewBooleanArray,
2479 JNI::NewByteArray,
2480 JNI::NewCharArray,
2481 JNI::NewShortArray,
2482 JNI::NewIntArray,
2483 JNI::NewLongArray,
2484 JNI::NewFloatArray,
2485 JNI::NewDoubleArray,
2486 JNI::GetBooleanArrayElements,
2487 JNI::GetByteArrayElements,
2488 JNI::GetCharArrayElements,
2489 JNI::GetShortArrayElements,
2490 JNI::GetIntArrayElements,
2491 JNI::GetLongArrayElements,
2492 JNI::GetFloatArrayElements,
2493 JNI::GetDoubleArrayElements,
2494 JNI::ReleaseBooleanArrayElements,
2495 JNI::ReleaseByteArrayElements,
2496 JNI::ReleaseCharArrayElements,
2497 JNI::ReleaseShortArrayElements,
2498 JNI::ReleaseIntArrayElements,
2499 JNI::ReleaseLongArrayElements,
2500 JNI::ReleaseFloatArrayElements,
2501 JNI::ReleaseDoubleArrayElements,
2502 JNI::GetBooleanArrayRegion,
2503 JNI::GetByteArrayRegion,
2504 JNI::GetCharArrayRegion,
2505 JNI::GetShortArrayRegion,
2506 JNI::GetIntArrayRegion,
2507 JNI::GetLongArrayRegion,
2508 JNI::GetFloatArrayRegion,
2509 JNI::GetDoubleArrayRegion,
2510 JNI::SetBooleanArrayRegion,
2511 JNI::SetByteArrayRegion,
2512 JNI::SetCharArrayRegion,
2513 JNI::SetShortArrayRegion,
2514 JNI::SetIntArrayRegion,
2515 JNI::SetLongArrayRegion,
2516 JNI::SetFloatArrayRegion,
2517 JNI::SetDoubleArrayRegion,
2518 JNI::RegisterNatives,
2519 JNI::UnregisterNatives,
2520 JNI::MonitorEnter,
2521 JNI::MonitorExit,
2522 JNI::GetJavaVM,
2523 JNI::GetStringRegion,
2524 JNI::GetStringUTFRegion,
2525 JNI::GetPrimitiveArrayCritical,
2526 JNI::ReleasePrimitiveArrayCritical,
2527 JNI::GetStringCritical,
2528 JNI::ReleaseStringCritical,
2529 JNI::NewWeakGlobalRef,
2530 JNI::DeleteWeakGlobalRef,
2531 JNI::ExceptionCheck,
2532 JNI::NewDirectByteBuffer,
2533 JNI::GetDirectBufferAddress,
2534 JNI::GetDirectBufferCapacity,
2535 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002536};
2537
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002538static const size_t kMonitorsInitial = 32; // Arbitrary.
2539static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2540
2541static const size_t kLocalsInitial = 64; // Arbitrary.
2542static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002543
Elliott Hughes75770752011-08-24 17:52:38 -07002544JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002545 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002546 vm(vm),
2547 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002548 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002549 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002550 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2551 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002552 functions = unchecked_functions = &gNativeInterface;
2553 if (check_jni) {
2554 functions = GetCheckJniNativeInterface();
2555 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002556}
2557
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002558JNIEnvExt::~JNIEnvExt() {
2559}
2560
Carl Shapiroea4dca82011-08-01 13:45:38 -07002561// JNI Invocation interface.
2562
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2564 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2565 if (args->version < JNI_VERSION_1_2) {
2566 return JNI_EVERSION;
2567 }
2568 Runtime::Options options;
2569 for (int i = 0; i < args->nOptions; ++i) {
2570 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002571 options.push_back(std::make_pair(StringPiece(option->optionString),
2572 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 }
2574 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002575 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 if (runtime == NULL) {
2577 return JNI_ERR;
2578 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002579 *p_env = Thread::Current()->GetJniEnv();
2580 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581 return JNI_OK;
2582 }
2583}
2584
Elliott Hughesf2682d52011-08-15 16:37:04 -07002585extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 Runtime* runtime = Runtime::Current();
2587 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002588 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002590 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002591 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 }
2593 return JNI_OK;
2594}
2595
2596// Historically unsupported.
2597extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2598 return JNI_ERR;
2599}
2600
Elliott Hughescdf53122011-08-19 15:46:09 -07002601class JII {
2602 public:
2603 static jint DestroyJavaVM(JavaVM* vm) {
2604 if (vm == NULL) {
2605 return JNI_ERR;
2606 } else {
2607 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2608 delete raw_vm->runtime;
2609 return JNI_OK;
2610 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612
Elliott Hughescdf53122011-08-19 15:46:09 -07002613 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002614 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002615 }
2616
2617 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002618 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002619 }
2620
2621 static jint DetachCurrentThread(JavaVM* vm) {
2622 if (vm == NULL) {
2623 return JNI_ERR;
2624 } else {
2625 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2626 Runtime* runtime = raw_vm->runtime;
2627 runtime->DetachCurrentThread();
2628 return JNI_OK;
2629 }
2630 }
2631
2632 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2633 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2634 return JNI_EVERSION;
2635 }
2636 if (vm == NULL || env == NULL) {
2637 return JNI_ERR;
2638 }
2639 Thread* thread = Thread::Current();
2640 if (thread == NULL) {
2641 *env = NULL;
2642 return JNI_EDETACHED;
2643 }
2644 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 return JNI_OK;
2646 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002647};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002648
Elliott Hughesa2501992011-08-26 19:39:54 -07002649const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002650 NULL, // reserved0
2651 NULL, // reserved1
2652 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002653 JII::DestroyJavaVM,
2654 JII::AttachCurrentThread,
2655 JII::DetachCurrentThread,
2656 JII::GetEnv,
2657 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002658};
2659
Elliott Hughesbbd76712011-08-17 10:25:24 -07002660static const size_t kPinTableInitialSize = 16;
2661static const size_t kPinTableMaxSize = 1024;
2662
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002663static const size_t kGlobalsInitial = 512; // Arbitrary.
2664static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2665
2666static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2667static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2668
Elliott Hughes0af55432011-08-17 18:37:28 -07002669JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002670 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002671 check_jni_abort_hook(NULL),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002672 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002673 verbose_jni(verbose_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002674 force_copy(false), // TODO: add a way to enable this
2675 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002676 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002677 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002678 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002679 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002680 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002681 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2682 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2683 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002684 functions = unchecked_functions = &gInvokeInterface;
2685 if (check_jni) {
2686 functions = GetCheckJniInvokeInterface();
2687 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002688}
2689
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002690JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002691 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002692 delete globals_lock;
2693 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002694 delete libraries_lock;
2695 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002696}
2697
Elliott Hughes75770752011-08-24 17:52:38 -07002698bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2699 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002700
2701 // See if we've already loaded this library. If we have, and the class loader
2702 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002703 // TODO: for better results we should canonicalize the pathname (or even compare
2704 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002705 SharedLibrary* library;
2706 {
2707 // TODO: move the locking (and more of this logic) into Libraries.
2708 MutexLock mu(libraries_lock);
2709 library = libraries->Get(path);
2710 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 if (library != NULL) {
2712 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002713 // The library will be associated with class_loader. The JNI
2714 // spec says we can't load the same library into more than one
2715 // class loader.
2716 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2717 "ClassLoader %p; can't open in ClassLoader %p",
2718 path.c_str(), library->GetClassLoader(), class_loader);
2719 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002720 return false;
2721 }
2722 if (verbose_jni) {
2723 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2724 << "ClassLoader " << class_loader << "]";
2725 }
2726 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002727 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2728 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002729 return false;
2730 }
2731 return true;
2732 }
2733
2734 // Open the shared library. Because we're using a full path, the system
2735 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2736 // resolve this library's dependencies though.)
2737
2738 // Failures here are expected when java.library.path has several entries
2739 // and we have to hunt for the lib.
2740
2741 // The current version of the dynamic linker prints detailed information
2742 // about dlopen() failures. Some things to check if the message is
2743 // cryptic:
2744 // - make sure the library exists on the device
2745 // - verify that the right path is being opened (the debug log message
2746 // above can help with that)
2747 // - check to see if the library is valid (e.g. not zero bytes long)
2748 // - check config/prelink-linux-arm.map to ensure that the library
2749 // is listed and is not being overrun by the previous entry (if
2750 // loading suddenly stops working on a prelinked library, this is
2751 // a good one to check)
2752 // - write a trivial app that calls sleep() then dlopen(), attach
2753 // to it with "strace -p <pid>" while it sleeps, and watch for
2754 // attempts to open nonexistent dependent shared libs
2755
2756 // TODO: automate some of these checks!
2757
2758 // This can execute slowly for a large library on a busy system, so we
2759 // want to switch from RUNNING to VMWAIT while it executes. This allows
2760 // the GC to ignore us.
2761 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002762 void* handle = NULL;
2763 {
2764 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2765 handle = dlopen(path.c_str(), RTLD_LAZY);
2766 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002767
2768 if (verbose_jni) {
2769 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2770 }
2771
2772 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002773 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002774 return false;
2775 }
2776
2777 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002778 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002779 // TODO: move the locking (and more of this logic) into Libraries.
2780 MutexLock mu(libraries_lock);
2781 library = libraries->Get(path);
2782 if (library != NULL) {
2783 LOG(INFO) << "WOW: we lost a race to add shared library: "
2784 << "\"" << path << "\" ClassLoader=" << class_loader;
2785 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002786 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002787 library = new SharedLibrary(path, handle, class_loader);
2788 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002789 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002790
2791 if (verbose_jni) {
2792 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2793 }
2794
2795 bool result = true;
2796 void* sym = dlsym(handle, "JNI_OnLoad");
2797 if (sym == NULL) {
2798 if (verbose_jni) {
2799 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2800 }
2801 } else {
2802 // Call JNI_OnLoad. We have to override the current class
2803 // loader, which will always be "null" since the stuff at the
2804 // top of the stack is around Runtime.loadLibrary(). (See
2805 // the comments in the JNI FindClass function.)
2806 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2807 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002808 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 self->SetClassLoaderOverride(class_loader);
2810
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002811 int version = 0;
2812 {
2813 ScopedThreadStateChange tsc(self, Thread::kNative);
2814 if (verbose_jni) {
2815 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2816 }
2817 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002818 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002819
2820 self->SetClassLoaderOverride(old_class_loader);;
2821
2822 if (version != JNI_VERSION_1_2 &&
2823 version != JNI_VERSION_1_4 &&
2824 version != JNI_VERSION_1_6) {
2825 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2826 << "bad version: " << version;
2827 // It's unwise to call dlclose() here, but we can mark it
2828 // as bad and ensure that future load attempts will fail.
2829 // We don't know how far JNI_OnLoad got, so there could
2830 // be some partially-initialized stuff accessible through
2831 // newly-registered native method calls. We could try to
2832 // unregister them, but that doesn't seem worthwhile.
2833 result = false;
2834 } else {
2835 if (verbose_jni) {
2836 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2837 << " from JNI_OnLoad in \"" << path << "\"]";
2838 }
2839 }
2840 }
2841
2842 library->SetResult(result);
2843 return result;
2844}
2845
2846void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2847 CHECK(m->IsNative());
2848
2849 Class* c = m->GetDeclaringClass();
2850
2851 // If this is a static method, it could be called before the class
2852 // has been initialized.
2853 if (m->IsStatic()) {
2854 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2855 return NULL;
2856 }
2857 } else {
2858 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2859 }
2860
2861 MutexLock mu(libraries_lock);
2862 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002863}
2864
Elliott Hughes410c0c82011-09-01 17:58:25 -07002865void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2866 {
2867 MutexLock mu(globals_lock);
2868 globals.VisitRoots(visitor, arg);
2869 }
2870 {
2871 MutexLock mu(pins_lock);
2872 pin_table.VisitRoots(visitor, arg);
2873 }
2874 // The weak_globals table is visited by the GC itself (because it mutates the table).
2875}
2876
Ian Rogersdf20fe02011-07-20 20:34:16 -07002877} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002878
2879std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2880 switch (rhs) {
2881 case JNIInvalidRefType:
2882 os << "JNIInvalidRefType";
2883 return os;
2884 case JNILocalRefType:
2885 os << "JNILocalRefType";
2886 return os;
2887 case JNIGlobalRefType:
2888 os << "JNIGlobalRefType";
2889 return os;
2890 case JNIWeakGlobalRefType:
2891 os << "JNIWeakGlobalRefType";
2892 return os;
2893 }
2894}