blob: 04832c7de0a3ad79ad7e456a13feb92c7d07c96d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070029#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070034#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070037#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070038#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070039#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070059void SetJniGlobalsMax(size_t max) {
60 if (max != 0) {
61 gGlobalsMax = max;
62 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
63 }
64}
Ian Rogersdf20fe02011-07-20 20:34:16 -070065
Elliott Hughesc5f7c912011-08-18 14:00:42 -070066/*
67 * Add a local reference for an object to the current stack frame. When
68 * the native function returns, the reference will be discarded.
69 *
70 * We need to allow the same reference to be added multiple times.
71 *
72 * This will be called on otherwise unreferenced objects. We cannot do
73 * GC allocations here, and it's best if we don't grab a mutex.
74 *
75 * Returns the local reference (currently just the same pointer that was
76 * passed in), or NULL on failure.
77 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070078template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
80 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
81 Object* obj = const_cast<Object*>(const_obj);
82
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 if (obj == NULL) {
84 return NULL;
85 }
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089
Ian Rogers5a7a74a2011-09-26 16:32:29 -070090 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 IndirectRef ref = locals.Add(cookie, obj);
92 if (ref == NULL) {
93 // TODO: just change Add's DCHECK to CHECK and lose this?
94 locals.Dump();
95 LOG(FATAL) << "Failed adding to JNI local reference table "
96 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 }
98
99#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700100 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101 size_t entry_count = locals.Capacity();
102 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700104 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700105 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800106 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700107 }
108 }
109#endif
110
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800111 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 // Hand out direct pointers to support broken old apps.
113 return reinterpret_cast<T>(obj);
114 }
115
116 return reinterpret_cast<T>(ref);
117}
118
Ian Rogers0571d352011-11-03 19:51:38 -0700119size_t NumArgArrayBytes(const char* shorty) {
120 size_t num_bytes = 0;
121 size_t end = strlen(shorty);
122 for (size_t i = 1; i < end; ++i) {
123 char ch = shorty[i];
124 if (ch == 'D' || ch == 'J') {
125 num_bytes += 8;
126 } else if (ch == 'L') {
127 // Argument is a reference or an array. The shorty descriptor
128 // does not distinguish between these types.
129 num_bytes += sizeof(Object*);
130 } else {
131 num_bytes += 4;
132 }
133 }
134 return num_bytes;
135}
136
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137// For external use.
138template<typename T>
139T Decode(JNIEnv* public_env, jobject obj) {
140 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
141 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
142}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800143// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
144Object* DecodeObj(JNIEnv* public_env, jobject obj) {
145 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
146 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
147}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700149template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700150template Class* Decode<Class*>(JNIEnv*, jobject);
151template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
152template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700153template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700154template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700155template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700156template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400157template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700158template String* Decode<String*>(JNIEnv*, jobject);
159template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700160
161namespace {
162
Elliott Hughescdf53122011-08-19 15:46:09 -0700163jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
164 if (obj == NULL) {
165 return NULL;
166 }
Elliott Hughes75770752011-08-24 17:52:38 -0700167 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700168 IndirectReferenceTable& weak_globals = vm->weak_globals;
169 MutexLock mu(vm->weak_globals_lock);
170 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
171 return reinterpret_cast<jweak>(ref);
172}
173
Elliott Hughesbf86d042011-08-31 17:53:14 -0700174// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700175template<typename T>
176T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700177 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700178}
179
Ian Rogers0571d352011-11-03 19:51:38 -0700180static byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700181 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800182 const char* shorty = MethodHelper(method).GetShorty();
183 size_t shorty_len = strlen(shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700184 size_t num_bytes = NumArgArrayBytes(shorty);
185 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
187 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700188 case 'Z':
189 case 'B':
190 case 'C':
191 case 'S':
192 case 'I':
193 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
194 offset += 4;
195 break;
196 case 'F':
197 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
198 offset += 4;
199 break;
200 case 'L': {
Shih-wei Liao24782c62012-01-08 12:46:11 -0800201 Object* obj = DecodeObj(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700202 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
203 offset += sizeof(Object*);
204 break;
205 }
206 case 'D':
207 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
208 offset += 8;
209 break;
210 case 'J':
211 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
212 offset += 8;
213 break;
214 }
215 }
216 return arg_array.release();
217}
218
Ian Rogers0571d352011-11-03 19:51:38 -0700219static byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 const char* shorty = MethodHelper(method).GetShorty();
222 size_t shorty_len = strlen(shorty);
223 size_t num_bytes = NumArgArrayBytes(shorty);
Elliott Hughes90a33692011-08-30 13:27:07 -0700224 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
226 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700227 case 'Z':
228 case 'B':
229 case 'C':
230 case 'S':
231 case 'I':
232 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
233 offset += 4;
234 break;
235 case 'F':
236 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
237 offset += 4;
238 break;
239 case 'L': {
Shih-wei Liao24782c62012-01-08 12:46:11 -0800240 Object* obj = DecodeObj(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700241 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
242 offset += sizeof(Object*);
243 break;
244 }
245 case 'D':
246 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
247 offset += 8;
248 break;
249 case 'J':
250 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
251 offset += 8;
252 break;
253 }
254 }
255 return arg_array.release();
256}
257
Elliott Hughesd07986f2011-12-06 18:27:45 -0800258static byte* CreateArgArray(Method* method, JValue* args) {
259 const char* shorty = MethodHelper(method).GetShorty();
260 size_t shorty_len = strlen(shorty);
261 size_t num_bytes = NumArgArrayBytes(shorty);
262 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
263 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
264 switch (shorty[i]) {
265 case 'Z':
266 case 'B':
267 case 'C':
268 case 'S':
269 case 'I':
270 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
271 offset += 4;
272 break;
273 case 'F':
274 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
275 offset += 4;
276 break;
277 case 'L':
278 *reinterpret_cast<Object**>(&arg_array[offset]) = args[i - 1].l;
279 offset += sizeof(Object*);
280 break;
281 case 'D':
282 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
283 offset += 8;
284 break;
285 case 'J':
286 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
287 offset += 8;
288 break;
289 }
290 }
291 return arg_array.release();
292}
293
Ian Rogers0571d352011-11-03 19:51:38 -0700294static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700296 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700297 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700298 return result;
299}
300
Ian Rogers0571d352011-11-03 19:51:38 -0700301static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700302 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800303 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700304 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700305 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
306 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700307}
308
Ian Rogers0571d352011-11-03 19:51:38 -0700309static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700310 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700311}
312
Ian Rogers0571d352011-11-03 19:51:38 -0700313static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
314 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800316 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700317 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700318 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
319 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700320}
321
Ian Rogers0571d352011-11-03 19:51:38 -0700322static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
323 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700324 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800325 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700326 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700327 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
328 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700329}
330
Elliott Hughes6b436852011-08-12 10:16:44 -0700331// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
332// separated with slashes but aren't wrapped with "L;" like regular descriptors
333// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
334// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
335// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700336static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700337 std::string result;
338 // Add the missing "L;" if necessary.
339 if (name[0] == '[') {
340 result = name;
341 } else {
342 result += 'L';
343 result += name;
344 result += ';';
345 }
346 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700347 if (result.find('.') != std::string::npos) {
348 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
349 << "\"" << name << "\"";
350 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700351 }
352 return result;
353}
354
Ian Rogers0571d352011-11-03 19:51:38 -0700355static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700356 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800357 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700358}
359
Ian Rogers0571d352011-11-03 19:51:38 -0700360static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700361 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700362 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700363 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700364 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700365
366 Method* method = NULL;
367 if (is_static) {
368 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700369 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700370 method = c->FindVirtualMethod(name, sig);
371 if (method == NULL) {
372 // No virtual method matching the signature. Search declared
373 // private methods and constructors.
374 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700375 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700377
Elliott Hughescdf53122011-08-19 15:46:09 -0700378 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700379 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700380 return NULL;
381 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700382
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700383 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700384}
385
Ian Rogers0571d352011-11-03 19:51:38 -0700386static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700387 Frame frame = self->GetTopOfStack();
388 Method* method = frame.GetMethod();
389 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
390 return self->GetClassLoaderOverride();
391 }
392 return method->GetDeclaringClass()->GetClassLoader();
393}
394
Ian Rogers0571d352011-11-03 19:51:38 -0700395static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700397 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700398 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700399 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700400
401 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 Class* field_type;
403 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
404 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700405 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700407 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700409 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410 if (field_type == NULL) {
411 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700412 DCHECK(ts.Self()->IsExceptionPending());
413 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700414 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700415 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800416 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 return NULL;
418 }
419 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800420 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700424 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700425 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700426 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800427 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700428 return NULL;
429 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700430 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700431}
432
Ian Rogers0571d352011-11-03 19:51:38 -0700433static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700434 JavaVMExt* vm = ts.Vm();
435 MutexLock mu(vm->pins_lock);
436 vm->pin_table.Add(array);
437}
438
Ian Rogers0571d352011-11-03 19:51:38 -0700439static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700440 JavaVMExt* vm = ts.Vm();
441 MutexLock mu(vm->pins_lock);
442 vm->pin_table.Remove(array);
443}
444
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700445template<typename JniT, typename ArtT>
446JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
447 CHECK_GE(length, 0); // TODO: ReportJniError
448 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700449 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700450}
451
Elliott Hughes75770752011-08-24 17:52:38 -0700452template <typename ArrayT, typename CArrayT, typename ArtArrayT>
453CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
454 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
455 PinPrimitiveArray(ts, array);
456 if (is_copy != NULL) {
457 *is_copy = JNI_FALSE;
458 }
459 return array->GetData();
460}
461
462template <typename ArrayT>
463void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
464 if (mode != JNI_COMMIT) {
465 Array* array = Decode<Array*>(ts, java_array);
466 UnpinPrimitiveArray(ts, array);
467 }
468}
469
Ian Rogers0571d352011-11-03 19:51:38 -0700470static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700471 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700472 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700473 "%s offset=%d length=%d %s.length=%d",
474 type.c_str(), start, length, identifier, array->GetLength());
475}
Ian Rogers0571d352011-11-03 19:51:38 -0700476
477static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700478 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700479 "offset=%d length=%d string.length()=%d", start, length, array_length);
480}
Elliott Hughes814e4032011-08-23 12:07:56 -0700481
482template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700483void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700484 ArrayT* array = Decode<ArrayT*>(ts, java_array);
485 if (start < 0 || length < 0 || start + length > array->GetLength()) {
486 ThrowAIOOBE(ts, array, start, length, "src");
487 } else {
488 JavaT* data = array->GetData();
489 memcpy(buf, data + start, length * sizeof(JavaT));
490 }
491}
492
493template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700494void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700495 ArrayT* array = Decode<ArrayT*>(ts, java_array);
496 if (start < 0 || length < 0 || start + length > array->GetLength()) {
497 ThrowAIOOBE(ts, array, start, length, "dst");
498 } else {
499 JavaT* data = array->GetData();
500 memcpy(data + start, buf, length * sizeof(JavaT));
501 }
502}
503
Ian Rogers0571d352011-11-03 19:51:38 -0700504static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700505 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
506 CHECK(buffer_class.get() != NULL);
507 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
508}
509
Ian Rogers0571d352011-11-03 19:51:38 -0700510static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700511 static jclass buffer_class = InitDirectByteBufferClass(env);
512 return buffer_class;
513}
514
Ian Rogers0571d352011-11-03 19:51:38 -0700515static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700516 if (vm == NULL || p_env == NULL) {
517 return JNI_ERR;
518 }
519
Elliott Hughescac6cc72011-11-03 20:31:21 -0700520 // Return immediately if we're already one with the VM.
521 Thread* self = Thread::Current();
522 if (self != NULL) {
523 *p_env = self->GetJniEnv();
524 return JNI_OK;
525 }
526
527 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
528
529 // No threads allowed in zygote mode.
530 if (runtime->IsZygote()) {
531 LOG(ERROR) << "Attempt to attach a thread in the zygote";
532 return JNI_ERR;
533 }
534
Elliott Hughes75770752011-08-24 17:52:38 -0700535 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
536 JavaVMAttachArgs args;
537 if (thr_args == NULL) {
538 // Allow the v1.1 calling convention.
539 args.version = JNI_VERSION_1_2;
540 args.name = NULL;
541 args.group = NULL; // TODO: get "main" thread group
542 } else {
543 args.version = in_args->version;
544 args.name = in_args->name;
545 if (in_args->group != NULL) {
546 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
547 args.group = NULL; // TODO: decode in_args->group
548 } else {
549 args.group = NULL; // TODO: get "main" thread group
550 }
551 }
552 CHECK_GE(args.version, JNI_VERSION_1_2);
553
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700554 runtime->AttachCurrentThread(args.name, as_daemon);
555 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700556 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700557}
558
Elliott Hughes79082e32011-08-25 12:07:32 -0700559class SharedLibrary {
560 public:
561 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
562 : path_(path),
563 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700564 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700565 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700566 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700567 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 }
570
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 Object* GetClassLoader() {
572 return class_loader_;
573 }
574
575 std::string GetPath() {
576 return path_;
577 }
578
579 /*
580 * Check the result of an earlier call to JNI_OnLoad on this library. If
581 * the call has not yet finished in another thread, wait for it.
582 */
583 bool CheckOnLoadResult(JavaVMExt* vm) {
584 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700585 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 // Check this so we don't end up waiting for ourselves. We need
587 // to return "true" so the caller can continue.
588 LOG(INFO) << *self << " recursive attempt to load library "
589 << "\"" << path_ << "\"";
590 return true;
591 }
592
593 MutexLock mu(jni_on_load_lock_);
594 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
596 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700597 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700598 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 }
600
601 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
603 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 return okay;
605 }
606
607 void SetResult(bool result) {
608 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700609 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700610
611 // Broadcast a wakeup to anybody sleeping on the condition variable.
612 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700613 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 }
615
616 void* FindSymbol(const std::string& symbol_name) {
617 return dlsym(handle_, symbol_name.c_str());
618 }
619
620 private:
621 enum JNI_OnLoadState {
622 kPending,
623 kFailed,
624 kOkay,
625 };
626
627 // Path to library "/system/lib/libjni.so".
628 std::string path_;
629
630 // The void* returned by dlopen(3).
631 void* handle_;
632
633 // The ClassLoader this library is associated with.
634 Object* class_loader_;
635
636 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700637 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700638 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700639 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700641 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 // Result of earlier JNI_OnLoad call.
643 JNI_OnLoadState jni_on_load_result_;
644};
645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughes79082e32011-08-25 12:07:32 -0700648// This exists mainly to keep implementation details out of the header file.
649class Libraries {
650 public:
651 Libraries() {
652 }
653
654 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700655 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 }
657
658 SharedLibrary* Get(const std::string& path) {
659 return libraries_[path];
660 }
661
662 void Put(const std::string& path, SharedLibrary* library) {
663 libraries_[path] = library;
664 }
665
666 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700667 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700668 std::string jni_short_name(JniShortName(m));
669 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700670 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700671 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
672 SharedLibrary* library = it->second;
673 if (library->GetClassLoader() != declaring_class_loader) {
674 // We only search libraries loaded by the appropriate ClassLoader.
675 continue;
676 }
677 // Try the short name then the long name...
678 void* fn = library->FindSymbol(jni_short_name);
679 if (fn == NULL) {
680 fn = library->FindSymbol(jni_long_name);
681 }
682 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800683 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
684 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700685 return fn;
686 }
687 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700688 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700689 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700690 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700691 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700692 return NULL;
693 }
694
695 private:
696 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
697
698 std::map<std::string, SharedLibrary*> libraries_;
699};
700
Elliott Hughes418d20f2011-09-22 14:00:39 -0700701JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
702 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
703 Object* receiver = Decode<Object*>(env, obj);
704 Method* method = DecodeMethod(mid);
705 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
706 return InvokeWithArgArray(env, receiver, method, arg_array.get());
707}
708
Elliott Hughesd07986f2011-12-06 18:27:45 -0800709JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
710 UniquePtr<byte[]> arg_array(CreateArgArray(m, args));
711 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, arg_array.get());
712}
713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714class JNI {
715 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 static jint GetVersion(JNIEnv* env) {
718 ScopedJniThreadState ts(env);
719 return JNI_VERSION_1_6;
720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
723 ScopedJniThreadState ts(env);
724 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700725 return NULL;
726 }
727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 static jclass FindClass(JNIEnv* env, const char* name) {
729 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700730 Runtime* runtime = Runtime::Current();
731 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700733 Class* c = NULL;
734 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700735 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800736 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700737 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800738 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700739 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700740 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700741 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700742
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
744 ScopedJniThreadState ts(env);
745 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700746 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700747 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700748
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
750 ScopedJniThreadState ts(env);
751 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700752 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700754
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
756 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700757 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700758 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700760
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
762 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700763 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700764 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
768 ScopedJniThreadState ts(env);
769 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700770 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 }
772
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700773 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700775 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700776 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700778
Elliott Hughes37f7a402011-08-22 18:56:01 -0700779 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 Class* c1 = Decode<Class*>(ts, java_class1);
782 Class* c2 = Decode<Class*>(ts, java_class2);
783 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700785
Elliott Hughes37f7a402011-08-22 18:56:01 -0700786 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700788 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700789 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700790 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700791 return JNI_TRUE;
792 } else {
793 Object* obj = Decode<Object*>(ts, jobj);
794 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700795 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700796 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700798
Elliott Hughes37f7a402011-08-22 18:56:01 -0700799 static jint Throw(JNIEnv* env, jthrowable java_exception) {
800 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700801 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700802 if (exception == NULL) {
803 return JNI_ERR;
804 }
805 ts.Self()->SetException(exception);
806 return JNI_OK;
807 }
808
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700809 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700810 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700811 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700812 jmethodID mid = ((msg != NULL)
813 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
814 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700815 if (mid == NULL) {
816 return JNI_ERR;
817 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700819 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700820 return JNI_ERR;
821 }
822
823 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 args[0].l = s.get();
825 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
826 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700827 return JNI_ERR;
828 }
829
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700831
Elliott Hughes37f7a402011-08-22 18:56:01 -0700832 return JNI_OK;
833 }
834
835 static jboolean ExceptionCheck(JNIEnv* env) {
836 ScopedJniThreadState ts(env);
837 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
838 }
839
840 static void ExceptionClear(JNIEnv* env) {
841 ScopedJniThreadState ts(env);
842 ts.Self()->ClearException();
843 }
844
845 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700847
848 Thread* self = ts.Self();
849 Throwable* original_exception = self->GetException();
850 self->ClearException();
851
Elliott Hughesbf86d042011-08-31 17:53:14 -0700852 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
854 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
855 if (mid == NULL) {
856 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700857 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 } else {
859 env->CallVoidMethod(exception.get(), mid);
860 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700861 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700862 << " thrown while calling printStackTrace";
863 self->ClearException();
864 }
865 }
866
867 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700869
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 static jthrowable ExceptionOccurred(JNIEnv* env) {
871 ScopedJniThreadState ts(env);
872 Object* exception = ts.Self()->GetException();
873 if (exception == NULL) {
874 return NULL;
875 } else {
876 // TODO: if adding a local reference failing causes the VM to abort
877 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700878 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 if (localException == NULL) {
880 // We were unable to add a new local reference, and threw a new
881 // exception. We can't return "exception", because it's not a
882 // local reference. So we have to return NULL, indicating that
883 // there was no exception, even though it's pretty much raining
884 // exceptions in here.
885 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
886 }
887 return localException;
888 }
889 }
890
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 static void FatalError(JNIEnv* env, const char* msg) {
892 ScopedJniThreadState ts(env);
893 LOG(FATAL) << "JNI FatalError called: " << msg;
894 }
895
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700896 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700898 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
899 return JNI_ERR;
900 }
901 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 return JNI_OK;
903 }
904
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700905 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700907 Object* survivor = Decode<Object*>(ts, java_survivor);
908 ts.Env()->PopFrame();
909 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700912 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700914 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
915 }
916
917 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
918 // TODO: we should try to expand the table if necessary.
919 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
920 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
921 return JNI_ERR;
922 }
923 // TODO: this isn't quite right, since "capacity" includes holes.
924 size_t capacity = ts.Env()->locals.Capacity();
925 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
926 if (!okay) {
927 ts.Self()->ThrowOutOfMemoryError(caller);
928 }
929 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 }
931
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
933 ScopedJniThreadState ts(env);
934 if (obj == NULL) {
935 return NULL;
936 }
937
Elliott Hughes75770752011-08-24 17:52:38 -0700938 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 IndirectReferenceTable& globals = vm->globals;
940 MutexLock mu(vm->globals_lock);
941 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
942 return reinterpret_cast<jobject>(ref);
943 }
944
945 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
946 ScopedJniThreadState ts(env);
947 if (obj == NULL) {
948 return;
949 }
950
Elliott Hughes75770752011-08-24 17:52:38 -0700951 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 IndirectReferenceTable& globals = vm->globals;
953 MutexLock mu(vm->globals_lock);
954
955 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
956 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
957 << "failed to find entry";
958 }
959 }
960
961 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
962 ScopedJniThreadState ts(env);
963 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
964 }
965
966 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
967 ScopedJniThreadState ts(env);
968 if (obj == NULL) {
969 return;
970 }
971
Elliott Hughes75770752011-08-24 17:52:38 -0700972 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 IndirectReferenceTable& weak_globals = vm->weak_globals;
974 MutexLock mu(vm->weak_globals_lock);
975
976 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
977 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
978 << "failed to find entry";
979 }
980 }
981
982 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
983 ScopedJniThreadState ts(env);
984 if (obj == NULL) {
985 return NULL;
986 }
987
988 IndirectReferenceTable& locals = ts.Env()->locals;
989
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700990 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
992 return reinterpret_cast<jobject>(ref);
993 }
994
995 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
996 ScopedJniThreadState ts(env);
997 if (obj == NULL) {
998 return;
999 }
1000
1001 IndirectReferenceTable& locals = ts.Env()->locals;
1002
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001003 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 if (!locals.Remove(cookie, obj)) {
1005 // Attempting to delete a local reference that is not in the
1006 // topmost local reference frame is a no-op. DeleteLocalRef returns
1007 // void and doesn't throw any exceptions, but we should probably
1008 // complain about it so the user will notice that things aren't
1009 // going quite the way they expect.
1010 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1011 << "failed to find entry";
1012 }
1013 }
1014
1015 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1016 ScopedJniThreadState ts(env);
1017 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1018 ? JNI_TRUE : JNI_FALSE;
1019 }
1020
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001021 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001023 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001024 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001025 return NULL;
1026 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001027 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
1032 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_start(args, mid);
1034 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 va_end(args);
1036 return result;
1037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001041 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001042 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001043 return NULL;
1044 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001045 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001046 if (result == NULL) {
1047 return NULL;
1048 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001049 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001051 if (!ts.Self()->IsExceptionPending()) {
1052 return local_result;
1053 } else {
1054 return NULL;
1055 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001060 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001061 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001062 return NULL;
1063 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001064 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001065 if (result == NULL) {
1066 return NULL;
1067 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001068 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001070 if (!ts.Self()->IsExceptionPending()) {
1071 return local_result;
1072 } else {
1073 return NULL;
1074 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1078 ScopedJniThreadState ts(env);
1079 return FindMethodID(ts, c, name, sig, false);
1080 }
1081
1082 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1083 ScopedJniThreadState ts(env);
1084 return FindMethodID(ts, c, name, sig, true);
1085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001091 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001093 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001098 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001099 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001104 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001105 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_end(ap);
1114 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001119 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
1133 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001138 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_end(ap);
1152 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001162 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_end(ap);
1171 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001176 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_list ap;
1187 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_end(ap);
1190 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001195 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001200 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_list ap;
1206 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001207 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_end(ap);
1209 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001214 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001219 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_list ap;
1225 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_end(ap);
1228 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001233 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_list ap;
1244 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_end(ap);
1247 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001252 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001257 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_list ap;
1263 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001264 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001270 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
1281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001284 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 va_end(ap);
1286 return local_result;
1287 }
1288
1289 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001293 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
1296 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001299 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001300 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
1306 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 va_end(ap);
1310 return result.z;
1311 }
1312
1313 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001316 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
1319 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
1328 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001330 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
1332 return result.b;
1333 }
1334
1335 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001338 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
1341 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001344 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
1350 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001352 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_end(ap);
1354 return result.c;
1355 }
1356
1357 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001360 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
1363 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001364 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001366 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
1369 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
1372 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001374 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 va_end(ap);
1376 return result.s;
1377 }
1378
1379 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001382 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
1385 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001388 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
1393 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001394 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001395 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 va_end(ap);
1397 return result.i;
1398 }
1399
1400 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001401 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001403 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
1406 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001409 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
1412 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 ScopedJniThreadState ts(env);
1415 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001417 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
1419 return result.j;
1420 }
1421
1422 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001423 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001425 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
1428 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001429 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001431 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
1434 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
1437 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001438 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001439 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_end(ap);
1441 return result.f;
1442 }
1443
1444 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001445 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001447 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
1450 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001451 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001453 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
1456 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001457 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 ScopedJniThreadState ts(env);
1459 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001460 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001461 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 va_end(ap);
1463 return result.d;
1464 }
1465
1466 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001467 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001469 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
1472 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001473 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001475 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
1478 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001479 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 ScopedJniThreadState ts(env);
1481 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001482 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001483 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 va_end(ap);
1485 }
1486
1487 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001488 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001490 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
1493 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001494 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001496 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001499 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 ScopedJniThreadState ts(env);
1501 return FindFieldID(ts, c, name, sig, false);
1502 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001503
1504
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001505 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 ScopedJniThreadState ts(env);
1507 return FindFieldID(ts, c, name, sig, true);
1508 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001513 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001514 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001516
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001519 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001520 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 Object* o = Decode<Object*>(ts, java_object);
1526 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001527 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001534 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538#define GET_PRIMITIVE_FIELD(fn, instance) \
1539 ScopedJniThreadState ts(env); \
1540 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001541 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 return f->fn(o)
1543
1544#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1545 ScopedJniThreadState ts(env); \
1546 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001547 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 f->fn(o, value)
1549
1550 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1551 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1555 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1559 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1563 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1567 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1571 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 }
1573
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1575 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1579 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1583 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1587 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1591 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1595 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1599 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1603 GET_PRIMITIVE_FIELD(GetLong, NULL);
1604 }
1605
1606 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1607 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1608 }
1609
1610 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1611 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1612 }
1613
1614 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1615 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1616 }
1617
1618 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1619 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1620 }
1621
1622 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1623 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1624 }
1625
1626 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1627 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1628 }
1629
1630 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1631 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1632 }
1633
1634 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1635 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1636 }
1637
1638 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1639 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1640 }
1641
1642 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1643 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1644 }
1645
1646 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1647 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1648 }
1649
1650 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1651 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1652 }
1653
1654 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1655 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1656 }
1657
1658 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1659 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1660 }
1661
1662 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1663 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1664 }
1665
1666 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1667 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1668 }
1669
1670 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1671 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1672 }
1673
1674 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1675 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
1680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001683 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 va_end(ap);
1685 return local_result;
1686 }
1687
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001691 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001696 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001697 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
1702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 va_end(ap);
1706 return result.z;
1707 }
1708
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
1721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
1725 return result.b;
1726 }
1727
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
1740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_end(ap);
1744 return result.c;
1745 }
1746
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, 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);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_end(ap);
1763 return result.s;
1764 }
1765
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
1778 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001780 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 va_end(ap);
1782 return result.i;
1783 }
1784
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes72025e52011-08-23 17:50:30 -07001795 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
1797 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001798 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001799 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 va_end(ap);
1801 return result.j;
1802 }
1803
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001806 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes418d20f2011-09-22 14:00:39 -07001809 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes72025e52011-08-23 17:50:30 -07001814 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
1816 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001817 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001818 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 va_end(ap);
1820 return result.f;
1821 }
1822
Elliott Hughes418d20f2011-09-22 14:00:39 -07001823 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001825 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes418d20f2011-09-22 14:00:39 -07001828 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes72025e52011-08-23 17:50:30 -07001833 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
1835 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001836 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001837 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 va_end(ap);
1839 return result.d;
1840 }
1841
Elliott Hughes418d20f2011-09-22 14:00:39 -07001842 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001844 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes418d20f2011-09-22 14:00:39 -07001847 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001849 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes72025e52011-08-23 17:50:30 -07001852 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
1854 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001855 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001856 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_end(ap);
1858 }
1859
Elliott Hughes418d20f2011-09-22 14:00:39 -07001860 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001862 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes418d20f2011-09-22 14:00:39 -07001865 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001867 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001873 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
1876 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1877 ScopedJniThreadState ts(env);
1878 if (utf == NULL) {
1879 return NULL;
1880 }
1881 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001882 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes814e4032011-08-23 12:07:56 -07001885 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1886 ScopedJniThreadState ts(env);
1887 return Decode<String*>(ts, java_string)->GetLength();
1888 }
1889
1890 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1891 ScopedJniThreadState ts(env);
1892 return Decode<String*>(ts, java_string)->GetUtfLength();
1893 }
1894
Elliott Hughesb465ab02011-08-24 11:21:21 -07001895 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 String* s = Decode<String*>(ts, java_string);
1898 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1899 ThrowSIOOBE(ts, start, length, s->GetLength());
1900 } else {
1901 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1902 memcpy(buf, chars + start, length * sizeof(jchar));
1903 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001904 }
1905
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001907 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 String* s = Decode<String*>(ts, java_string);
1909 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1910 ThrowSIOOBE(ts, start, length, s->GetLength());
1911 } else {
1912 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1913 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1914 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001918 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001919 String* s = Decode<String*>(ts, java_string);
1920 const CharArray* chars = s->GetCharArray();
1921 PinPrimitiveArray(ts, chars);
1922 if (is_copy != NULL) {
1923 *is_copy = JNI_FALSE;
1924 }
1925 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001929 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001930 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001935 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001945 if (java_string == NULL) {
1946 return NULL;
1947 }
1948 if (is_copy != NULL) {
1949 *is_copy = JNI_TRUE;
1950 }
1951 String* s = Decode<String*>(ts, java_string);
1952 size_t byte_count = s->GetUtfLength();
1953 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001954 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001955 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1956 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1957 bytes[byte_count] = '\0';
1958 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001964 }
1965
Elliott Hughesbd935992011-08-22 11:59:34 -07001966 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001968 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001969 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001970 Array* array = obj->AsArray();
1971 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes814e4032011-08-23 12:07:56 -07001974 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001977 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
1980 static void SetObjectArrayElement(JNIEnv* env,
1981 jobjectArray java_array, jsize index, jobject java_value) {
1982 ScopedJniThreadState ts(env);
1983 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1984 Object* value = Decode<Object*>(ts, java_value);
1985 array->Set(index, value);
1986 }
1987
1988 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1989 ScopedJniThreadState ts(env);
1990 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1991 }
1992
1993 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1994 ScopedJniThreadState ts(env);
1995 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1996 }
1997
1998 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1999 ScopedJniThreadState ts(env);
2000 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
2001 }
2002
2003 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2004 ScopedJniThreadState ts(env);
2005 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2006 }
2007
2008 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2009 ScopedJniThreadState ts(env);
2010 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2011 }
2012
2013 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2014 ScopedJniThreadState ts(env);
2015 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2016 }
2017
2018 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2019 ScopedJniThreadState ts(env);
2020 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2021 }
2022
2023 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2024 ScopedJniThreadState ts(env);
2025 CHECK_GE(length, 0); // TODO: ReportJniError
2026
2027 // Compute the array class corresponding to the given element class.
2028 Class* element_class = Decode<Class*>(ts, element_jclass);
2029 std::string descriptor;
2030 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002031 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002032
2033 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002034 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2035 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 return NULL;
2037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 // Allocate and initialize if necessary.
2040 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 if (initial_element != NULL) {
2043 Object* initial_object = Decode<Object*>(ts, initial_element);
2044 for (jsize i = 0; i < length; ++i) {
2045 result->Set(i, initial_object);
2046 }
2047 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002048 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
2051 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2052 ScopedJniThreadState ts(env);
2053 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2054 }
2055
Ian Rogersa15e67d2012-02-28 13:51:55 -08002056 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002057 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002058 Array* array = Decode<Array*>(ts, java_array);
2059 PinPrimitiveArray(ts, array);
2060 if (is_copy != NULL) {
2061 *is_copy = JNI_FALSE;
2062 }
2063 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002064 }
2065
Elliott Hughes75770752011-08-24 17:52:38 -07002066 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002068 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002069 }
2070
Elliott Hughes75770752011-08-24 17:52:38 -07002071 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002073 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes75770752011-08-24 17:52:38 -07002076 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002078 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes75770752011-08-24 17:52:38 -07002081 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002083 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes75770752011-08-24 17:52:38 -07002086 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002088 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes75770752011-08-24 17:52:38 -07002091 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002093 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes75770752011-08-24 17:52:38 -07002096 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002098 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes75770752011-08-24 17:52:38 -07002101 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002103 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes75770752011-08-24 17:52:38 -07002106 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002108 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes75770752011-08-24 17:52:38 -07002111 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002113 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes75770752011-08-24 17:52:38 -07002116 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002118 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes75770752011-08-24 17:52:38 -07002121 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002123 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes75770752011-08-24 17:52:38 -07002126 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002128 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes75770752011-08-24 17:52:38 -07002131 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002133 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes75770752011-08-24 17:52:38 -07002136 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002138 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes75770752011-08-24 17:52:38 -07002141 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002143 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes75770752011-08-24 17:52:38 -07002146 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002148 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes814e4032011-08-23 12:07:56 -07002181 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Elliott Hughes814e4032011-08-23 12:07:56 -07002186 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes814e4032011-08-23 12:07:56 -07002191 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes814e4032011-08-23 12:07:56 -07002196 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Elliott Hughes814e4032011-08-23 12:07:56 -07002201 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughes814e4032011-08-23 12:07:56 -07002206 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes814e4032011-08-23 12:07:56 -07002211 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002213 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes814e4032011-08-23 12:07:56 -07002216 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002218 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughes814e4032011-08-23 12:07:56 -07002221 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002223 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughes814e4032011-08-23 12:07:56 -07002226 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002228 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes5174fe62011-08-23 15:12:35 -07002231 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002233 Class* c = Decode<Class*>(ts, java_class);
2234
Elliott Hughes5174fe62011-08-23 15:12:35 -07002235 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 const char* name = methods[i].name;
2237 const char* sig = methods[i].signature;
2238
2239 if (*sig == '!') {
2240 // TODO: fast jni. it's too noisy to log all these.
2241 ++sig;
2242 }
2243
Elliott Hughes5174fe62011-08-23 15:12:35 -07002244 Method* m = c->FindDirectMethod(name, sig);
2245 if (m == NULL) {
2246 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002249 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002250 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002252 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002253 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002254 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 return JNI_ERR;
2256 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002257
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002258 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002259
Ian Rogers60db5ab2012-02-20 17:02:00 -08002260 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
2262 return JNI_OK;
2263 }
2264
Elliott Hughes5174fe62011-08-23 15:12:35 -07002265 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002267 Class* c = Decode<Class*>(ts, java_class);
2268
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002269 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002270
2271 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2272 Method* m = c->GetDirectMethod(i);
2273 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002274 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002275 }
2276 }
2277 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2278 Method* m = c->GetVirtualMethod(i);
2279 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002280 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002281 }
2282 }
2283
2284 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Elliott Hughes72025e52011-08-23 17:50:30 -07002287 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002289 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002290 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Elliott Hughes72025e52011-08-23 17:50:30 -07002293 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002295 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002296 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 }
2298
2299 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2300 ScopedJniThreadState ts(env);
2301 Runtime* runtime = Runtime::Current();
2302 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002303 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 } else {
2305 *vm = NULL;
2306 }
2307 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2308 }
2309
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2311 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312
2313 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002314 CHECK(address != NULL); // TODO: ReportJniError
2315 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316
2317 jclass buffer_class = GetDirectByteBufferClass(env);
2318 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2319 if (mid == NULL) {
2320 return NULL;
2321 }
2322
2323 // At the moment, the Java side is limited to 32 bits.
2324 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2325 CHECK_LE(capacity, 0xffffffff);
2326 jint address_arg = reinterpret_cast<jint>(address);
2327 jint capacity_arg = static_cast<jint>(capacity);
2328
2329 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2330 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002335 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2336 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 }
2338
Elliott Hughesb465ab02011-08-24 11:21:21 -07002339 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002341 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2342 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002343 }
2344
Elliott Hughesb465ab02011-08-24 11:21:21 -07002345 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002346 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002347
Elliott Hughes75770752011-08-24 17:52:38 -07002348 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002349
2350 // Do we definitely know what kind of reference this is?
2351 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2352 IndirectRefKind kind = GetIndirectRefKind(ref);
2353 switch (kind) {
2354 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002355 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2356 return JNILocalRefType;
2357 }
2358 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002359 case kGlobal:
2360 return JNIGlobalRefType;
2361 case kWeakGlobal:
2362 return JNIWeakGlobalRefType;
2363 case kSirtOrInvalid:
2364 // Is it in a stack IRT?
2365 if (ts.Self()->SirtContains(java_object)) {
2366 return JNILocalRefType;
2367 }
2368
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002369 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002370 return JNIInvalidRefType;
2371 }
2372
Elliott Hughesb465ab02011-08-24 11:21:21 -07002373 // If we're handing out direct pointers, check whether it's a direct pointer
2374 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002375 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002376 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002377 return JNILocalRefType;
2378 }
2379 }
2380
2381 return JNIInvalidRefType;
2382 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002383 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2384 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 }
2386};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002387
Elliott Hughesa2501992011-08-26 19:39:54 -07002388const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002389 NULL, // reserved0.
2390 NULL, // reserved1.
2391 NULL, // reserved2.
2392 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 JNI::GetVersion,
2394 JNI::DefineClass,
2395 JNI::FindClass,
2396 JNI::FromReflectedMethod,
2397 JNI::FromReflectedField,
2398 JNI::ToReflectedMethod,
2399 JNI::GetSuperclass,
2400 JNI::IsAssignableFrom,
2401 JNI::ToReflectedField,
2402 JNI::Throw,
2403 JNI::ThrowNew,
2404 JNI::ExceptionOccurred,
2405 JNI::ExceptionDescribe,
2406 JNI::ExceptionClear,
2407 JNI::FatalError,
2408 JNI::PushLocalFrame,
2409 JNI::PopLocalFrame,
2410 JNI::NewGlobalRef,
2411 JNI::DeleteGlobalRef,
2412 JNI::DeleteLocalRef,
2413 JNI::IsSameObject,
2414 JNI::NewLocalRef,
2415 JNI::EnsureLocalCapacity,
2416 JNI::AllocObject,
2417 JNI::NewObject,
2418 JNI::NewObjectV,
2419 JNI::NewObjectA,
2420 JNI::GetObjectClass,
2421 JNI::IsInstanceOf,
2422 JNI::GetMethodID,
2423 JNI::CallObjectMethod,
2424 JNI::CallObjectMethodV,
2425 JNI::CallObjectMethodA,
2426 JNI::CallBooleanMethod,
2427 JNI::CallBooleanMethodV,
2428 JNI::CallBooleanMethodA,
2429 JNI::CallByteMethod,
2430 JNI::CallByteMethodV,
2431 JNI::CallByteMethodA,
2432 JNI::CallCharMethod,
2433 JNI::CallCharMethodV,
2434 JNI::CallCharMethodA,
2435 JNI::CallShortMethod,
2436 JNI::CallShortMethodV,
2437 JNI::CallShortMethodA,
2438 JNI::CallIntMethod,
2439 JNI::CallIntMethodV,
2440 JNI::CallIntMethodA,
2441 JNI::CallLongMethod,
2442 JNI::CallLongMethodV,
2443 JNI::CallLongMethodA,
2444 JNI::CallFloatMethod,
2445 JNI::CallFloatMethodV,
2446 JNI::CallFloatMethodA,
2447 JNI::CallDoubleMethod,
2448 JNI::CallDoubleMethodV,
2449 JNI::CallDoubleMethodA,
2450 JNI::CallVoidMethod,
2451 JNI::CallVoidMethodV,
2452 JNI::CallVoidMethodA,
2453 JNI::CallNonvirtualObjectMethod,
2454 JNI::CallNonvirtualObjectMethodV,
2455 JNI::CallNonvirtualObjectMethodA,
2456 JNI::CallNonvirtualBooleanMethod,
2457 JNI::CallNonvirtualBooleanMethodV,
2458 JNI::CallNonvirtualBooleanMethodA,
2459 JNI::CallNonvirtualByteMethod,
2460 JNI::CallNonvirtualByteMethodV,
2461 JNI::CallNonvirtualByteMethodA,
2462 JNI::CallNonvirtualCharMethod,
2463 JNI::CallNonvirtualCharMethodV,
2464 JNI::CallNonvirtualCharMethodA,
2465 JNI::CallNonvirtualShortMethod,
2466 JNI::CallNonvirtualShortMethodV,
2467 JNI::CallNonvirtualShortMethodA,
2468 JNI::CallNonvirtualIntMethod,
2469 JNI::CallNonvirtualIntMethodV,
2470 JNI::CallNonvirtualIntMethodA,
2471 JNI::CallNonvirtualLongMethod,
2472 JNI::CallNonvirtualLongMethodV,
2473 JNI::CallNonvirtualLongMethodA,
2474 JNI::CallNonvirtualFloatMethod,
2475 JNI::CallNonvirtualFloatMethodV,
2476 JNI::CallNonvirtualFloatMethodA,
2477 JNI::CallNonvirtualDoubleMethod,
2478 JNI::CallNonvirtualDoubleMethodV,
2479 JNI::CallNonvirtualDoubleMethodA,
2480 JNI::CallNonvirtualVoidMethod,
2481 JNI::CallNonvirtualVoidMethodV,
2482 JNI::CallNonvirtualVoidMethodA,
2483 JNI::GetFieldID,
2484 JNI::GetObjectField,
2485 JNI::GetBooleanField,
2486 JNI::GetByteField,
2487 JNI::GetCharField,
2488 JNI::GetShortField,
2489 JNI::GetIntField,
2490 JNI::GetLongField,
2491 JNI::GetFloatField,
2492 JNI::GetDoubleField,
2493 JNI::SetObjectField,
2494 JNI::SetBooleanField,
2495 JNI::SetByteField,
2496 JNI::SetCharField,
2497 JNI::SetShortField,
2498 JNI::SetIntField,
2499 JNI::SetLongField,
2500 JNI::SetFloatField,
2501 JNI::SetDoubleField,
2502 JNI::GetStaticMethodID,
2503 JNI::CallStaticObjectMethod,
2504 JNI::CallStaticObjectMethodV,
2505 JNI::CallStaticObjectMethodA,
2506 JNI::CallStaticBooleanMethod,
2507 JNI::CallStaticBooleanMethodV,
2508 JNI::CallStaticBooleanMethodA,
2509 JNI::CallStaticByteMethod,
2510 JNI::CallStaticByteMethodV,
2511 JNI::CallStaticByteMethodA,
2512 JNI::CallStaticCharMethod,
2513 JNI::CallStaticCharMethodV,
2514 JNI::CallStaticCharMethodA,
2515 JNI::CallStaticShortMethod,
2516 JNI::CallStaticShortMethodV,
2517 JNI::CallStaticShortMethodA,
2518 JNI::CallStaticIntMethod,
2519 JNI::CallStaticIntMethodV,
2520 JNI::CallStaticIntMethodA,
2521 JNI::CallStaticLongMethod,
2522 JNI::CallStaticLongMethodV,
2523 JNI::CallStaticLongMethodA,
2524 JNI::CallStaticFloatMethod,
2525 JNI::CallStaticFloatMethodV,
2526 JNI::CallStaticFloatMethodA,
2527 JNI::CallStaticDoubleMethod,
2528 JNI::CallStaticDoubleMethodV,
2529 JNI::CallStaticDoubleMethodA,
2530 JNI::CallStaticVoidMethod,
2531 JNI::CallStaticVoidMethodV,
2532 JNI::CallStaticVoidMethodA,
2533 JNI::GetStaticFieldID,
2534 JNI::GetStaticObjectField,
2535 JNI::GetStaticBooleanField,
2536 JNI::GetStaticByteField,
2537 JNI::GetStaticCharField,
2538 JNI::GetStaticShortField,
2539 JNI::GetStaticIntField,
2540 JNI::GetStaticLongField,
2541 JNI::GetStaticFloatField,
2542 JNI::GetStaticDoubleField,
2543 JNI::SetStaticObjectField,
2544 JNI::SetStaticBooleanField,
2545 JNI::SetStaticByteField,
2546 JNI::SetStaticCharField,
2547 JNI::SetStaticShortField,
2548 JNI::SetStaticIntField,
2549 JNI::SetStaticLongField,
2550 JNI::SetStaticFloatField,
2551 JNI::SetStaticDoubleField,
2552 JNI::NewString,
2553 JNI::GetStringLength,
2554 JNI::GetStringChars,
2555 JNI::ReleaseStringChars,
2556 JNI::NewStringUTF,
2557 JNI::GetStringUTFLength,
2558 JNI::GetStringUTFChars,
2559 JNI::ReleaseStringUTFChars,
2560 JNI::GetArrayLength,
2561 JNI::NewObjectArray,
2562 JNI::GetObjectArrayElement,
2563 JNI::SetObjectArrayElement,
2564 JNI::NewBooleanArray,
2565 JNI::NewByteArray,
2566 JNI::NewCharArray,
2567 JNI::NewShortArray,
2568 JNI::NewIntArray,
2569 JNI::NewLongArray,
2570 JNI::NewFloatArray,
2571 JNI::NewDoubleArray,
2572 JNI::GetBooleanArrayElements,
2573 JNI::GetByteArrayElements,
2574 JNI::GetCharArrayElements,
2575 JNI::GetShortArrayElements,
2576 JNI::GetIntArrayElements,
2577 JNI::GetLongArrayElements,
2578 JNI::GetFloatArrayElements,
2579 JNI::GetDoubleArrayElements,
2580 JNI::ReleaseBooleanArrayElements,
2581 JNI::ReleaseByteArrayElements,
2582 JNI::ReleaseCharArrayElements,
2583 JNI::ReleaseShortArrayElements,
2584 JNI::ReleaseIntArrayElements,
2585 JNI::ReleaseLongArrayElements,
2586 JNI::ReleaseFloatArrayElements,
2587 JNI::ReleaseDoubleArrayElements,
2588 JNI::GetBooleanArrayRegion,
2589 JNI::GetByteArrayRegion,
2590 JNI::GetCharArrayRegion,
2591 JNI::GetShortArrayRegion,
2592 JNI::GetIntArrayRegion,
2593 JNI::GetLongArrayRegion,
2594 JNI::GetFloatArrayRegion,
2595 JNI::GetDoubleArrayRegion,
2596 JNI::SetBooleanArrayRegion,
2597 JNI::SetByteArrayRegion,
2598 JNI::SetCharArrayRegion,
2599 JNI::SetShortArrayRegion,
2600 JNI::SetIntArrayRegion,
2601 JNI::SetLongArrayRegion,
2602 JNI::SetFloatArrayRegion,
2603 JNI::SetDoubleArrayRegion,
2604 JNI::RegisterNatives,
2605 JNI::UnregisterNatives,
2606 JNI::MonitorEnter,
2607 JNI::MonitorExit,
2608 JNI::GetJavaVM,
2609 JNI::GetStringRegion,
2610 JNI::GetStringUTFRegion,
2611 JNI::GetPrimitiveArrayCritical,
2612 JNI::ReleasePrimitiveArrayCritical,
2613 JNI::GetStringCritical,
2614 JNI::ReleaseStringCritical,
2615 JNI::NewWeakGlobalRef,
2616 JNI::DeleteWeakGlobalRef,
2617 JNI::ExceptionCheck,
2618 JNI::NewDirectByteBuffer,
2619 JNI::GetDirectBufferAddress,
2620 JNI::GetDirectBufferCapacity,
2621 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002622};
2623
Elliott Hughes75770752011-08-24 17:52:38 -07002624JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002625 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002626 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002627 local_ref_cookie(IRT_FIRST_SEGMENT),
2628 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002629 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002630 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002631 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002632 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002633 if (vm->check_jni) {
2634 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002635 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002636 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2637 // errors will ensue.
2638 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2639 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002640}
2641
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002642JNIEnvExt::~JNIEnvExt() {
2643}
2644
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002645void JNIEnvExt::EnableCheckJni() {
2646 check_jni = true;
2647 functions = GetCheckJniNativeInterface();
2648}
2649
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002650void JNIEnvExt::DumpReferenceTables() {
2651 locals.Dump();
2652 monitors.Dump();
2653}
2654
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002655void JNIEnvExt::PushFrame(int capacity) {
2656 stacked_local_ref_cookies.push_back(local_ref_cookie);
2657 local_ref_cookie = locals.GetSegmentState();
2658}
2659
2660void JNIEnvExt::PopFrame() {
2661 locals.SetSegmentState(local_ref_cookie);
2662 local_ref_cookie = stacked_local_ref_cookies.back();
2663 stacked_local_ref_cookies.pop_back();
2664}
2665
Carl Shapiroea4dca82011-08-01 13:45:38 -07002666// JNI Invocation interface.
2667
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2669 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2670 if (args->version < JNI_VERSION_1_2) {
2671 return JNI_EVERSION;
2672 }
2673 Runtime::Options options;
2674 for (int i = 0; i < args->nOptions; ++i) {
2675 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002676 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002677 }
2678 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002679 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 if (runtime == NULL) {
2681 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002682 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002683 runtime->Start();
2684 *p_env = Thread::Current()->GetJniEnv();
2685 *p_vm = runtime->GetJavaVM();
2686 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002687}
2688
Elliott Hughesf2682d52011-08-15 16:37:04 -07002689extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002690 Runtime* runtime = Runtime::Current();
2691 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002692 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002693 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002694 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002695 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002696 }
2697 return JNI_OK;
2698}
2699
2700// Historically unsupported.
2701extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2702 return JNI_ERR;
2703}
2704
Elliott Hughescdf53122011-08-19 15:46:09 -07002705class JII {
2706 public:
2707 static jint DestroyJavaVM(JavaVM* vm) {
2708 if (vm == NULL) {
2709 return JNI_ERR;
2710 } else {
2711 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2712 delete raw_vm->runtime;
2713 return JNI_OK;
2714 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002715 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002716
Elliott Hughescdf53122011-08-19 15:46:09 -07002717 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002718 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002719 }
2720
2721 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002722 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 }
2724
2725 static jint DetachCurrentThread(JavaVM* vm) {
2726 if (vm == NULL) {
2727 return JNI_ERR;
2728 } else {
2729 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2730 Runtime* runtime = raw_vm->runtime;
2731 runtime->DetachCurrentThread();
2732 return JNI_OK;
2733 }
2734 }
2735
2736 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2737 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2738 return JNI_EVERSION;
2739 }
2740 if (vm == NULL || env == NULL) {
2741 return JNI_ERR;
2742 }
2743 Thread* thread = Thread::Current();
2744 if (thread == NULL) {
2745 *env = NULL;
2746 return JNI_EDETACHED;
2747 }
2748 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002749 return JNI_OK;
2750 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002751};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002752
Elliott Hughesa2501992011-08-26 19:39:54 -07002753const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002754 NULL, // reserved0
2755 NULL, // reserved1
2756 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 JII::DestroyJavaVM,
2758 JII::AttachCurrentThread,
2759 JII::DetachCurrentThread,
2760 JII::GetEnv,
2761 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002762};
2763
Elliott Hughesa0957642011-09-02 14:27:33 -07002764JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002765 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002766 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002767 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002768 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002769 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002770 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002771 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002772 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002773 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002774 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002775 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002776 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002777 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002778 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002779 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002780 if (options->check_jni_) {
2781 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002782 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002783}
2784
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002785JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002786 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002787}
2788
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002789void JavaVMExt::EnableCheckJni() {
2790 check_jni = true;
2791 functions = GetCheckJniInvokeInterface();
2792}
2793
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002794void JavaVMExt::DumpReferenceTables() {
2795 {
2796 MutexLock mu(globals_lock);
2797 globals.Dump();
2798 }
2799 {
2800 MutexLock mu(weak_globals_lock);
2801 weak_globals.Dump();
2802 }
2803 {
2804 MutexLock mu(pins_lock);
2805 pin_table.Dump();
2806 }
2807}
2808
Elliott Hughes75770752011-08-24 17:52:38 -07002809bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2810 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002811
2812 // See if we've already loaded this library. If we have, and the class loader
2813 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002814 // TODO: for better results we should canonicalize the pathname (or even compare
2815 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002816 SharedLibrary* library;
2817 {
2818 // TODO: move the locking (and more of this logic) into Libraries.
2819 MutexLock mu(libraries_lock);
2820 library = libraries->Get(path);
2821 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002822 if (library != NULL) {
2823 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002824 // The library will be associated with class_loader. The JNI
2825 // spec says we can't load the same library into more than one
2826 // class loader.
2827 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2828 "ClassLoader %p; can't open in ClassLoader %p",
2829 path.c_str(), library->GetClassLoader(), class_loader);
2830 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002831 return false;
2832 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002833 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2834 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002835 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002836 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2837 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002838 return false;
2839 }
2840 return true;
2841 }
2842
2843 // Open the shared library. Because we're using a full path, the system
2844 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2845 // resolve this library's dependencies though.)
2846
2847 // Failures here are expected when java.library.path has several entries
2848 // and we have to hunt for the lib.
2849
2850 // The current version of the dynamic linker prints detailed information
2851 // about dlopen() failures. Some things to check if the message is
2852 // cryptic:
2853 // - make sure the library exists on the device
2854 // - verify that the right path is being opened (the debug log message
2855 // above can help with that)
2856 // - check to see if the library is valid (e.g. not zero bytes long)
2857 // - check config/prelink-linux-arm.map to ensure that the library
2858 // is listed and is not being overrun by the previous entry (if
2859 // loading suddenly stops working on a prelinked library, this is
2860 // a good one to check)
2861 // - write a trivial app that calls sleep() then dlopen(), attach
2862 // to it with "strace -p <pid>" while it sleeps, and watch for
2863 // attempts to open nonexistent dependent shared libs
2864
2865 // TODO: automate some of these checks!
2866
2867 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002868 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002869 // the GC to ignore us.
2870 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002871 void* handle = NULL;
2872 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002873 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002874 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002875 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002876
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002877 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002878
2879 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002880 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002881 return false;
2882 }
2883
2884 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002885 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 // TODO: move the locking (and more of this logic) into Libraries.
2887 MutexLock mu(libraries_lock);
2888 library = libraries->Get(path);
2889 if (library != NULL) {
2890 LOG(INFO) << "WOW: we lost a race to add shared library: "
2891 << "\"" << path << "\" ClassLoader=" << class_loader;
2892 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002893 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002894 library = new SharedLibrary(path, handle, class_loader);
2895 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002896 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002897
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002898 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002899
2900 bool result = true;
2901 void* sym = dlsym(handle, "JNI_OnLoad");
2902 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002903 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002904 } else {
2905 // Call JNI_OnLoad. We have to override the current class
2906 // loader, which will always be "null" since the stuff at the
2907 // top of the stack is around Runtime.loadLibrary(). (See
2908 // the comments in the JNI FindClass function.)
2909 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2910 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002911 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002912 self->SetClassLoaderOverride(class_loader);
2913
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002914 int version = 0;
2915 {
2916 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002917 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002918 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002919 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002920
Brian Carlstromaded5f72011-10-07 17:15:04 -07002921 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002922
2923 if (version != JNI_VERSION_1_2 &&
2924 version != JNI_VERSION_1_4 &&
2925 version != JNI_VERSION_1_6) {
2926 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2927 << "bad version: " << version;
2928 // It's unwise to call dlclose() here, but we can mark it
2929 // as bad and ensure that future load attempts will fail.
2930 // We don't know how far JNI_OnLoad got, so there could
2931 // be some partially-initialized stuff accessible through
2932 // newly-registered native method calls. We could try to
2933 // unregister them, but that doesn't seem worthwhile.
2934 result = false;
2935 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002936 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2937 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002938 }
2939 }
2940
2941 library->SetResult(result);
2942 return result;
2943}
2944
2945void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2946 CHECK(m->IsNative());
2947
2948 Class* c = m->GetDeclaringClass();
2949
2950 // If this is a static method, it could be called before the class
2951 // has been initialized.
2952 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002953 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002954 return NULL;
2955 }
2956 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002957 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002958 }
2959
Brian Carlstrom16192862011-09-12 17:50:06 -07002960 std::string detail;
2961 void* native_method;
2962 {
2963 MutexLock mu(libraries_lock);
2964 native_method = libraries->FindNativeMethod(m, detail);
2965 }
2966 // throwing can cause libraries_lock to be reacquired
2967 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002968 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002969 }
2970 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002971}
2972
Elliott Hughes410c0c82011-09-01 17:58:25 -07002973void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2974 {
2975 MutexLock mu(globals_lock);
2976 globals.VisitRoots(visitor, arg);
2977 }
2978 {
2979 MutexLock mu(pins_lock);
2980 pin_table.VisitRoots(visitor, arg);
2981 }
2982 // The weak_globals table is visited by the GC itself (because it mutates the table).
2983}
2984
Elliott Hughes844f9a02012-01-24 20:19:58 -08002985jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2986 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2987 if (c.get() == NULL) {
2988 return NULL;
2989 }
2990 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2991}
2992
Ian Rogersdf20fe02011-07-20 20:34:16 -07002993} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002994
2995std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2996 switch (rhs) {
2997 case JNIInvalidRefType:
2998 os << "JNIInvalidRefType";
2999 return os;
3000 case JNILocalRefType:
3001 os << "JNILocalRefType";
3002 return os;
3003 case JNIGlobalRefType:
3004 os << "JNIGlobalRefType";
3005 return os;
3006 case JNIWeakGlobalRefType:
3007 os << "JNIWeakGlobalRefType";
3008 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003009 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003010 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003011 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003012 }
3013}