blob: c447de88a831517acfc187a92859a5732c6dfa5d [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
Elliott Hughesbf86d042011-08-31 17:53:14 -0700119// For external use.
120template<typename T>
121T Decode(JNIEnv* public_env, jobject obj) {
122 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
123 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
124}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800125// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
126Object* DecodeObj(JNIEnv* public_env, jobject obj) {
127 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
128 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
129}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700131template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132template Class* Decode<Class*>(JNIEnv*, jobject);
133template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
134template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700135template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700136template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700137template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700138template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400139template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700140template String* Decode<String*>(JNIEnv*, jobject);
141template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142
Ian Rogers45619fc2012-02-29 11:15:25 -0800143size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
144 size_t num_bytes = 0;
145 for (size_t i = 1; i < shorty_len; ++i) {
146 char ch = shorty[i];
147 if (ch == 'D' || ch == 'J') {
148 num_bytes += 8;
149 } else if (ch == 'L') {
150 // Argument is a reference or an array. The shorty descriptor
151 // does not distinguish between these types.
152 num_bytes += sizeof(Object*);
153 } else {
154 num_bytes += 4;
155 }
156 }
157 return num_bytes;
158}
159
160class ArgArray {
161 public:
162 explicit ArgArray(Method* method) {
163 MethodHelper mh(method);
164 shorty_ = mh.GetShorty();
165 shorty_len_ = mh.GetShortyLength();
166 size_t num_bytes = NumArgArrayBytes(shorty_, shorty_len_);
167 if (num_bytes < kSmallArgArraySizeInBytes) {
168 arg_array_ = small_arg_array_;
169 } else {
170 large_arg_array_.reset(new byte[num_bytes]);
171 arg_array_ = large_arg_array_.get();
172 }
173 }
174
175 byte* get() {
176 return arg_array_;
177 }
178
179 void BuildArgArray(JNIEnv* public_env, va_list ap) {
180 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
181 for (size_t i = 1, offset = 0; i < shorty_len_; ++i) {
182 switch (shorty_[i]) {
183 case 'Z':
184 case 'B':
185 case 'C':
186 case 'S':
187 case 'I':
188 *reinterpret_cast<int32_t*>(&arg_array_[offset]) = va_arg(ap, jint);
189 offset += 4;
190 break;
191 case 'F':
192 *reinterpret_cast<float*>(&arg_array_[offset]) = va_arg(ap, jdouble);
193 offset += 4;
194 break;
195 case 'L': {
196 Object* obj = DecodeObj(env, va_arg(ap, jobject));
197 *reinterpret_cast<Object**>(&arg_array_[offset]) = obj;
198 offset += sizeof(Object*);
199 break;
200 }
201 case 'D':
202 *reinterpret_cast<double*>(&arg_array_[offset]) = va_arg(ap, jdouble);
203 offset += 8;
204 break;
205 case 'J':
206 *reinterpret_cast<int64_t*>(&arg_array_[offset]) = va_arg(ap, jlong);
207 offset += 8;
208 break;
209 }
210 }
211 }
212
213 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
214 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
215 for (size_t i = 1, offset = 0; i < shorty_len_; ++i) {
216 switch (shorty_[i]) {
217 case 'Z':
218 case 'B':
219 case 'C':
220 case 'S':
221 case 'I':
222 *reinterpret_cast<uint32_t*>(&arg_array_[offset]) = args[i - 1].i;
223 offset += 4;
224 break;
225 case 'F':
226 *reinterpret_cast<float*>(&arg_array_[offset]) = args[i - 1].f;
227 offset += 4;
228 break;
229 case 'L': {
230 Object* obj = DecodeObj(env, args[i - 1].l);
231 *reinterpret_cast<Object**>(&arg_array_[offset]) = obj;
232 offset += sizeof(Object*);
233 break;
234 }
235 case 'D':
236 *reinterpret_cast<double*>(&arg_array_[offset]) = args[i - 1].d;
237 offset += 8;
238 break;
239 case 'J':
240 *reinterpret_cast<uint64_t*>(&arg_array_[offset]) = args[i - 1].j;
241 offset += 8;
242 break;
243 }
244 }
245 }
246
247 void BuildArgArray(JValue* args) {
248 for (size_t i = 1, offset = 0; i < shorty_len_; ++i) {
249 switch (shorty_[i]) {
250 case 'Z':
251 case 'B':
252 case 'C':
253 case 'S':
254 case 'I':
255 *reinterpret_cast<uint32_t*>(&arg_array_[offset]) = args[i - 1].i;
256 offset += 4;
257 break;
258 case 'F':
259 *reinterpret_cast<float*>(&arg_array_[offset]) = args[i - 1].f;
260 offset += 4;
261 break;
262 case 'L':
263 *reinterpret_cast<Object**>(&arg_array_[offset]) = args[i - 1].l;
264 offset += sizeof(Object*);
265 break;
266 case 'D':
267 *reinterpret_cast<double*>(&arg_array_[offset]) = args[i - 1].d;
268 offset += 8;
269 break;
270 case 'J':
271 *reinterpret_cast<uint64_t*>(&arg_array_[offset]) = args[i - 1].j;
272 offset += 8;
273 break;
274 }
275 }
276 }
277
278 private:
279 enum { kSmallArgArraySizeInBytes = 48 };
280 const char* shorty_;
281 uint32_t shorty_len_;
282 byte* arg_array_;
283 byte small_arg_array_[kSmallArgArraySizeInBytes];
284 UniquePtr<byte[]> large_arg_array_;
285};
286
Elliott Hughesbf86d042011-08-31 17:53:14 -0700287namespace {
288
Elliott Hughescdf53122011-08-19 15:46:09 -0700289jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
290 if (obj == NULL) {
291 return NULL;
292 }
Elliott Hughes75770752011-08-24 17:52:38 -0700293 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700294 IndirectReferenceTable& weak_globals = vm->weak_globals;
295 MutexLock mu(vm->weak_globals_lock);
296 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
297 return reinterpret_cast<jweak>(ref);
298}
299
Elliott Hughesbf86d042011-08-31 17:53:14 -0700300// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700301template<typename T>
302T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700303 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700304}
305
Ian Rogers0571d352011-11-03 19:51:38 -0700306static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700307 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700308 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700309 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700310 return result;
311}
312
Ian Rogers0571d352011-11-03 19:51:38 -0700313static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800315 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700316 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800317 ArgArray arg_array(method);
318 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 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 Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700323 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700324}
325
Ian Rogers0571d352011-11-03 19:51:38 -0700326static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
327 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700328 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800329 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700330 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800331 ArgArray arg_array(method);
332 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700333 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700334}
335
Ian Rogers0571d352011-11-03 19:51:38 -0700336static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
337 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800339 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700340 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800341 ArgArray arg_array(method);
342 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700343 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700344}
345
Elliott Hughes6b436852011-08-12 10:16:44 -0700346// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
347// separated with slashes but aren't wrapped with "L;" like regular descriptors
348// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
349// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
350// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700351static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700352 std::string result;
353 // Add the missing "L;" if necessary.
354 if (name[0] == '[') {
355 result = name;
356 } else {
357 result += 'L';
358 result += name;
359 result += ';';
360 }
361 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700362 if (result.find('.') != std::string::npos) {
363 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
364 << "\"" << name << "\"";
365 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700366 }
367 return result;
368}
369
Ian Rogers0571d352011-11-03 19:51:38 -0700370static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700371 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800372 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700373}
374
Ian Rogers0571d352011-11-03 19:51:38 -0700375static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700377 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700378 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700379 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700380
381 Method* method = NULL;
382 if (is_static) {
383 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700384 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700385 method = c->FindVirtualMethod(name, sig);
386 if (method == NULL) {
387 // No virtual method matching the signature. Search declared
388 // private methods and constructors.
389 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700390 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700391 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700392
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700394 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 return NULL;
396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700397
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700398 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700399}
400
Ian Rogers0571d352011-11-03 19:51:38 -0700401static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700402 Frame frame = self->GetTopOfStack();
403 Method* method = frame.GetMethod();
404 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
405 return self->GetClassLoaderOverride();
406 }
407 return method->GetDeclaringClass()->GetClassLoader();
408}
409
Ian Rogers0571d352011-11-03 19:51:38 -0700410static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700411 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700412 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700413 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700414 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700415
416 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 Class* field_type;
418 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
419 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700420 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700422 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700424 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 if (field_type == NULL) {
426 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700427 DCHECK(ts.Self()->IsExceptionPending());
428 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700429 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700430 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800431 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 return NULL;
433 }
434 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700436 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700438 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700439 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700440 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700441 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800442 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700443 return NULL;
444 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700445 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700446}
447
Ian Rogers0571d352011-11-03 19:51:38 -0700448static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700449 JavaVMExt* vm = ts.Vm();
450 MutexLock mu(vm->pins_lock);
451 vm->pin_table.Add(array);
452}
453
Ian Rogers0571d352011-11-03 19:51:38 -0700454static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700455 JavaVMExt* vm = ts.Vm();
456 MutexLock mu(vm->pins_lock);
457 vm->pin_table.Remove(array);
458}
459
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700460template<typename JniT, typename ArtT>
461JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
462 CHECK_GE(length, 0); // TODO: ReportJniError
463 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700464 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700465}
466
Elliott Hughes75770752011-08-24 17:52:38 -0700467template <typename ArrayT, typename CArrayT, typename ArtArrayT>
468CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
469 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
470 PinPrimitiveArray(ts, array);
471 if (is_copy != NULL) {
472 *is_copy = JNI_FALSE;
473 }
474 return array->GetData();
475}
476
477template <typename ArrayT>
478void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
479 if (mode != JNI_COMMIT) {
480 Array* array = Decode<Array*>(ts, java_array);
481 UnpinPrimitiveArray(ts, array);
482 }
483}
484
Ian Rogers0571d352011-11-03 19:51:38 -0700485static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700486 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700487 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700488 "%s offset=%d length=%d %s.length=%d",
489 type.c_str(), start, length, identifier, array->GetLength());
490}
Ian Rogers0571d352011-11-03 19:51:38 -0700491
492static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700493 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700494 "offset=%d length=%d string.length()=%d", start, length, array_length);
495}
Elliott Hughes814e4032011-08-23 12:07:56 -0700496
497template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700498void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700499 ArrayT* array = Decode<ArrayT*>(ts, java_array);
500 if (start < 0 || length < 0 || start + length > array->GetLength()) {
501 ThrowAIOOBE(ts, array, start, length, "src");
502 } else {
503 JavaT* data = array->GetData();
504 memcpy(buf, data + start, length * sizeof(JavaT));
505 }
506}
507
508template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700509void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700510 ArrayT* array = Decode<ArrayT*>(ts, java_array);
511 if (start < 0 || length < 0 || start + length > array->GetLength()) {
512 ThrowAIOOBE(ts, array, start, length, "dst");
513 } else {
514 JavaT* data = array->GetData();
515 memcpy(data + start, buf, length * sizeof(JavaT));
516 }
517}
518
Ian Rogers0571d352011-11-03 19:51:38 -0700519static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700520 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
521 CHECK(buffer_class.get() != NULL);
522 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
523}
524
Ian Rogers0571d352011-11-03 19:51:38 -0700525static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700526 static jclass buffer_class = InitDirectByteBufferClass(env);
527 return buffer_class;
528}
529
Ian Rogers0571d352011-11-03 19:51:38 -0700530static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700531 if (vm == NULL || p_env == NULL) {
532 return JNI_ERR;
533 }
534
Elliott Hughescac6cc72011-11-03 20:31:21 -0700535 // Return immediately if we're already one with the VM.
536 Thread* self = Thread::Current();
537 if (self != NULL) {
538 *p_env = self->GetJniEnv();
539 return JNI_OK;
540 }
541
542 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
543
544 // No threads allowed in zygote mode.
545 if (runtime->IsZygote()) {
546 LOG(ERROR) << "Attempt to attach a thread in the zygote";
547 return JNI_ERR;
548 }
549
Elliott Hughes75770752011-08-24 17:52:38 -0700550 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
551 JavaVMAttachArgs args;
552 if (thr_args == NULL) {
553 // Allow the v1.1 calling convention.
554 args.version = JNI_VERSION_1_2;
555 args.name = NULL;
556 args.group = NULL; // TODO: get "main" thread group
557 } else {
558 args.version = in_args->version;
559 args.name = in_args->name;
560 if (in_args->group != NULL) {
561 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
562 args.group = NULL; // TODO: decode in_args->group
563 } else {
564 args.group = NULL; // TODO: get "main" thread group
565 }
566 }
567 CHECK_GE(args.version, JNI_VERSION_1_2);
568
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700569 runtime->AttachCurrentThread(args.name, as_daemon);
570 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700571 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700572}
573
Elliott Hughes79082e32011-08-25 12:07:32 -0700574class SharedLibrary {
575 public:
576 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
577 : path_(path),
578 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700579 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700580 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700581 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700582 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 }
585
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 Object* GetClassLoader() {
587 return class_loader_;
588 }
589
590 std::string GetPath() {
591 return path_;
592 }
593
594 /*
595 * Check the result of an earlier call to JNI_OnLoad on this library. If
596 * the call has not yet finished in another thread, wait for it.
597 */
598 bool CheckOnLoadResult(JavaVMExt* vm) {
599 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700600 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 // Check this so we don't end up waiting for ourselves. We need
602 // to return "true" so the caller can continue.
603 LOG(INFO) << *self << " recursive attempt to load library "
604 << "\"" << path_ << "\"";
605 return true;
606 }
607
608 MutexLock mu(jni_on_load_lock_);
609 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
611 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700612 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700613 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 }
615
616 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800617 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
618 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 return okay;
620 }
621
622 void SetResult(bool result) {
623 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700624 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700625
626 // Broadcast a wakeup to anybody sleeping on the condition variable.
627 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700628 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700629 }
630
631 void* FindSymbol(const std::string& symbol_name) {
632 return dlsym(handle_, symbol_name.c_str());
633 }
634
635 private:
636 enum JNI_OnLoadState {
637 kPending,
638 kFailed,
639 kOkay,
640 };
641
642 // Path to library "/system/lib/libjni.so".
643 std::string path_;
644
645 // The void* returned by dlopen(3).
646 void* handle_;
647
648 // The ClassLoader this library is associated with.
649 Object* class_loader_;
650
651 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700652 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700653 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700654 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700655 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700656 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700657 // Result of earlier JNI_OnLoad call.
658 JNI_OnLoadState jni_on_load_result_;
659};
660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughes79082e32011-08-25 12:07:32 -0700663// This exists mainly to keep implementation details out of the header file.
664class Libraries {
665 public:
666 Libraries() {
667 }
668
669 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700670 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700671 }
672
673 SharedLibrary* Get(const std::string& path) {
674 return libraries_[path];
675 }
676
677 void Put(const std::string& path, SharedLibrary* library) {
678 libraries_[path] = library;
679 }
680
681 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700682 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700683 std::string jni_short_name(JniShortName(m));
684 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700685 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700686 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
687 SharedLibrary* library = it->second;
688 if (library->GetClassLoader() != declaring_class_loader) {
689 // We only search libraries loaded by the appropriate ClassLoader.
690 continue;
691 }
692 // Try the short name then the long name...
693 void* fn = library->FindSymbol(jni_short_name);
694 if (fn == NULL) {
695 fn = library->FindSymbol(jni_long_name);
696 }
697 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800698 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
699 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700700 return fn;
701 }
702 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700703 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700704 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700705 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700706 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700707 return NULL;
708 }
709
710 private:
711 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
712
713 std::map<std::string, SharedLibrary*> libraries_;
714};
715
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
717 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
718 Object* receiver = Decode<Object*>(env, obj);
719 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800720 ArgArray arg_array(method);
721 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700722 return InvokeWithArgArray(env, receiver, method, arg_array.get());
723}
724
Elliott Hughesd07986f2011-12-06 18:27:45 -0800725JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800726 ArgArray arg_array(m);
727 arg_array.BuildArgArray(args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800728 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, arg_array.get());
729}
730
Elliott Hughescdf53122011-08-19 15:46:09 -0700731class JNI {
732 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700733
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 static jint GetVersion(JNIEnv* env) {
735 ScopedJniThreadState ts(env);
736 return JNI_VERSION_1_6;
737 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700738
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
740 ScopedJniThreadState ts(env);
741 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700742 return NULL;
743 }
744
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 static jclass FindClass(JNIEnv* env, const char* name) {
746 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700747 Runtime* runtime = Runtime::Current();
748 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700750 Class* c = NULL;
751 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700752 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800753 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700754 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800755 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700756 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700757 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700758 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700759
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
761 ScopedJniThreadState ts(env);
762 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700763 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
767 ScopedJniThreadState ts(env);
768 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700769 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700771
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
773 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700774 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700775 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700777
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
779 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700780 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700781 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700783
Elliott Hughes37f7a402011-08-22 18:56:01 -0700784 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
785 ScopedJniThreadState ts(env);
786 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700787 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700788 }
789
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700790 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700792 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700793 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700795
Elliott Hughes37f7a402011-08-22 18:56:01 -0700796 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700798 Class* c1 = Decode<Class*>(ts, java_class1);
799 Class* c2 = Decode<Class*>(ts, java_class2);
800 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700802
Elliott Hughes37f7a402011-08-22 18:56:01 -0700803 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700805 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700806 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700807 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700808 return JNI_TRUE;
809 } else {
810 Object* obj = Decode<Object*>(ts, jobj);
811 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700812 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700813 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700815
Elliott Hughes37f7a402011-08-22 18:56:01 -0700816 static jint Throw(JNIEnv* env, jthrowable java_exception) {
817 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700818 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700819 if (exception == NULL) {
820 return JNI_ERR;
821 }
822 ts.Self()->SetException(exception);
823 return JNI_OK;
824 }
825
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700826 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700827 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700828 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700829 jmethodID mid = ((msg != NULL)
830 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
831 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700832 if (mid == NULL) {
833 return JNI_ERR;
834 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700835 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700836 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700837 return JNI_ERR;
838 }
839
840 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 args[0].l = s.get();
842 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
843 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700844 return JNI_ERR;
845 }
846
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700848
Elliott Hughes37f7a402011-08-22 18:56:01 -0700849 return JNI_OK;
850 }
851
852 static jboolean ExceptionCheck(JNIEnv* env) {
853 ScopedJniThreadState ts(env);
854 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
855 }
856
857 static void ExceptionClear(JNIEnv* env) {
858 ScopedJniThreadState ts(env);
859 ts.Self()->ClearException();
860 }
861
862 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700864
865 Thread* self = ts.Self();
866 Throwable* original_exception = self->GetException();
867 self->ClearException();
868
Elliott Hughesbf86d042011-08-31 17:53:14 -0700869 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
871 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
872 if (mid == NULL) {
873 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700874 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 } else {
876 env->CallVoidMethod(exception.get(), mid);
877 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700878 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 << " thrown while calling printStackTrace";
880 self->ClearException();
881 }
882 }
883
884 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700886
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 static jthrowable ExceptionOccurred(JNIEnv* env) {
888 ScopedJniThreadState ts(env);
889 Object* exception = ts.Self()->GetException();
890 if (exception == NULL) {
891 return NULL;
892 } else {
893 // TODO: if adding a local reference failing causes the VM to abort
894 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700895 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 if (localException == NULL) {
897 // We were unable to add a new local reference, and threw a new
898 // exception. We can't return "exception", because it's not a
899 // local reference. So we have to return NULL, indicating that
900 // there was no exception, even though it's pretty much raining
901 // exceptions in here.
902 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
903 }
904 return localException;
905 }
906 }
907
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 static void FatalError(JNIEnv* env, const char* msg) {
909 ScopedJniThreadState ts(env);
910 LOG(FATAL) << "JNI FatalError called: " << msg;
911 }
912
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700913 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700915 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
916 return JNI_ERR;
917 }
918 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 return JNI_OK;
920 }
921
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700922 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700924 Object* survivor = Decode<Object*>(ts, java_survivor);
925 ts.Env()->PopFrame();
926 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700929 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700931 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
932 }
933
934 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
935 // TODO: we should try to expand the table if necessary.
936 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
937 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
938 return JNI_ERR;
939 }
940 // TODO: this isn't quite right, since "capacity" includes holes.
941 size_t capacity = ts.Env()->locals.Capacity();
942 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
943 if (!okay) {
944 ts.Self()->ThrowOutOfMemoryError(caller);
945 }
946 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 }
948
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
950 ScopedJniThreadState ts(env);
951 if (obj == NULL) {
952 return NULL;
953 }
954
Elliott Hughes75770752011-08-24 17:52:38 -0700955 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 IndirectReferenceTable& globals = vm->globals;
957 MutexLock mu(vm->globals_lock);
958 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
959 return reinterpret_cast<jobject>(ref);
960 }
961
962 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
963 ScopedJniThreadState ts(env);
964 if (obj == NULL) {
965 return;
966 }
967
Elliott Hughes75770752011-08-24 17:52:38 -0700968 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 IndirectReferenceTable& globals = vm->globals;
970 MutexLock mu(vm->globals_lock);
971
972 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
973 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
974 << "failed to find entry";
975 }
976 }
977
978 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
979 ScopedJniThreadState ts(env);
980 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
981 }
982
983 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
984 ScopedJniThreadState ts(env);
985 if (obj == NULL) {
986 return;
987 }
988
Elliott Hughes75770752011-08-24 17:52:38 -0700989 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 IndirectReferenceTable& weak_globals = vm->weak_globals;
991 MutexLock mu(vm->weak_globals_lock);
992
993 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
994 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
995 << "failed to find entry";
996 }
997 }
998
999 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
1000 ScopedJniThreadState ts(env);
1001 if (obj == NULL) {
1002 return NULL;
1003 }
1004
1005 IndirectReferenceTable& locals = ts.Env()->locals;
1006
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001007 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
1009 return reinterpret_cast<jobject>(ref);
1010 }
1011
1012 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1013 ScopedJniThreadState ts(env);
1014 if (obj == NULL) {
1015 return;
1016 }
1017
1018 IndirectReferenceTable& locals = ts.Env()->locals;
1019
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001020 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 if (!locals.Remove(cookie, obj)) {
1022 // Attempting to delete a local reference that is not in the
1023 // topmost local reference frame is a no-op. DeleteLocalRef returns
1024 // void and doesn't throw any exceptions, but we should probably
1025 // complain about it so the user will notice that things aren't
1026 // going quite the way they expect.
1027 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1028 << "failed to find entry";
1029 }
1030 }
1031
1032 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1033 ScopedJniThreadState ts(env);
1034 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1035 ? JNI_TRUE : JNI_FALSE;
1036 }
1037
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001038 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001040 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001041 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001042 return NULL;
1043 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001044 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
1049 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_start(args, mid);
1051 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 va_end(args);
1053 return result;
1054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001058 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001059 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001060 return NULL;
1061 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001062 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001063 if (result == NULL) {
1064 return NULL;
1065 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001066 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001068 if (!ts.Self()->IsExceptionPending()) {
1069 return local_result;
1070 } else {
1071 return NULL;
1072 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001077 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001078 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001079 return NULL;
1080 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001081 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001082 if (result == NULL) {
1083 return NULL;
1084 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001085 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001087 if (!ts.Self()->IsExceptionPending()) {
1088 return local_result;
1089 } else {
1090 return NULL;
1091 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1095 ScopedJniThreadState ts(env);
1096 return FindMethodID(ts, c, name, sig, false);
1097 }
1098
1099 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1100 ScopedJniThreadState ts(env);
1101 return FindMethodID(ts, c, name, sig, true);
1102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001110 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001116 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001121 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001122 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001129 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_end(ap);
1131 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_list ap;
1147 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001148 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
1150 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001155 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001160 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_list ap;
1166 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001167 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_end(ap);
1169 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001174 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_list ap;
1185 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001186 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 va_end(ap);
1188 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001198 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_list ap;
1204 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001205 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_end(ap);
1207 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001212 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001217 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_list ap;
1223 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001224 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_end(ap);
1226 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001236 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_list ap;
1242 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 va_end(ap);
1245 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001250 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001255 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_list ap;
1261 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_end(ap);
1264 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001274 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 va_list ap;
1280 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001287 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
1298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001300 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001301 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_end(ap);
1303 return local_result;
1304 }
1305
1306 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001309 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001310 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
1313 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001316 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001317 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
1320 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
1323 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001325 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_end(ap);
1327 return result.z;
1328 }
1329
1330 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001339 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
1342 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
1345 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_end(ap);
1349 return result.b;
1350 }
1351
1352 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001361 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
1364 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
1367 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001369 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 va_end(ap);
1371 return result.c;
1372 }
1373
1374 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001383 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
1386 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
1389 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 va_end(ap);
1393 return result.s;
1394 }
1395
1396 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001399 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
1402 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001405 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes418d20f2011-09-22 14:00:39 -07001408 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
1410 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001411 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_end(ap);
1414 return result.i;
1415 }
1416
1417 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001418 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001420 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
1423 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001424 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001426 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
1429 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001430 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 ScopedJniThreadState ts(env);
1432 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001433 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001434 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 va_end(ap);
1436 return result.j;
1437 }
1438
1439 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001440 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001442 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
1445 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001446 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001448 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
1451 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001452 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 ScopedJniThreadState ts(env);
1454 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001455 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001456 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 va_end(ap);
1458 return result.f;
1459 }
1460
1461 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001464 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
1467 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001468 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001470 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 }
1472
1473 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001474 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 ScopedJniThreadState ts(env);
1476 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001477 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001478 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 va_end(ap);
1480 return result.d;
1481 }
1482
1483 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001484 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001486 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
1489 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001492 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
1495 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001496 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 ScopedJniThreadState ts(env);
1498 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001499 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001500 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 va_end(ap);
1502 }
1503
1504 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001505 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001507 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
1510 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001511 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001513 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001516 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 ScopedJniThreadState ts(env);
1518 return FindFieldID(ts, c, name, sig, false);
1519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
1521
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001522 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 ScopedJniThreadState ts(env);
1524 return FindFieldID(ts, c, name, sig, true);
1525 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001530 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001531 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001536 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001537 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 Object* o = Decode<Object*>(ts, java_object);
1543 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001544 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001551 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555#define GET_PRIMITIVE_FIELD(fn, instance) \
1556 ScopedJniThreadState ts(env); \
1557 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001558 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 return f->fn(o)
1560
1561#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1562 ScopedJniThreadState ts(env); \
1563 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001564 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 f->fn(o, value)
1566
1567 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001575 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1580 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1584 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001587 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1588 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001591 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1592 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001595 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1596 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001599 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1600 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001603 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1604 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001607 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1608 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001611 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1612 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001615 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1616 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 }
1618
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001619 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1620 GET_PRIMITIVE_FIELD(GetLong, NULL);
1621 }
1622
1623 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1624 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1625 }
1626
1627 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1628 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1629 }
1630
1631 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1632 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1633 }
1634
1635 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1636 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1637 }
1638
1639 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1640 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1641 }
1642
1643 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1644 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1645 }
1646
1647 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1648 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1649 }
1650
1651 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1652 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1653 }
1654
1655 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1656 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1657 }
1658
1659 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1660 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1661 }
1662
1663 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1664 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1665 }
1666
1667 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1668 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1669 }
1670
1671 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1672 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1673 }
1674
1675 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1676 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1677 }
1678
1679 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1680 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1681 }
1682
1683 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1684 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1685 }
1686
1687 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1688 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1689 }
1690
1691 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1692 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
1697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001700 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
1702 return local_result;
1703 }
1704
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001708 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001714 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
1719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_end(ap);
1723 return result.z;
1724 }
1725
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
1738 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
1742 return result.b;
1743 }
1744
Elliott Hughes418d20f2011-09-22 14:00:39 -07001745 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
1757 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_end(ap);
1761 return result.c;
1762 }
1763
Elliott Hughes418d20f2011-09-22 14:00:39 -07001764 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 ScopedJniThreadState ts(env);
1776 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001777 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_end(ap);
1780 return result.s;
1781 }
1782
Elliott Hughes418d20f2011-09-22 14:00:39 -07001783 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes418d20f2011-09-22 14:00:39 -07001788 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes72025e52011-08-23 17:50:30 -07001793 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 ScopedJniThreadState ts(env);
1795 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001796 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001797 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 va_end(ap);
1799 return result.i;
1800 }
1801
Elliott Hughes418d20f2011-09-22 14:00:39 -07001802 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes418d20f2011-09-22 14:00:39 -07001807 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001809 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes72025e52011-08-23 17:50:30 -07001812 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 ScopedJniThreadState ts(env);
1814 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001815 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001816 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 va_end(ap);
1818 return result.j;
1819 }
1820
Elliott Hughes418d20f2011-09-22 14:00:39 -07001821 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001823 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes418d20f2011-09-22 14:00:39 -07001826 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001828 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes72025e52011-08-23 17:50:30 -07001831 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 ScopedJniThreadState ts(env);
1833 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001834 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001835 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 va_end(ap);
1837 return result.f;
1838 }
1839
Elliott Hughes418d20f2011-09-22 14:00:39 -07001840 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001842 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes418d20f2011-09-22 14:00:39 -07001845 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001847 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes72025e52011-08-23 17:50:30 -07001850 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 ScopedJniThreadState ts(env);
1852 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001853 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001854 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 va_end(ap);
1856 return result.d;
1857 }
1858
Elliott Hughes418d20f2011-09-22 14:00:39 -07001859 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001861 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes418d20f2011-09-22 14:00:39 -07001864 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001866 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes72025e52011-08-23 17:50:30 -07001869 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 ScopedJniThreadState ts(env);
1871 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001872 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001873 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 va_end(ap);
1875 }
1876
Elliott Hughes418d20f2011-09-22 14:00:39 -07001877 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001879 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes418d20f2011-09-22 14:00:39 -07001882 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001884 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001890 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
1893 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1894 ScopedJniThreadState ts(env);
1895 if (utf == NULL) {
1896 return NULL;
1897 }
1898 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001899 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
Elliott Hughes814e4032011-08-23 12:07:56 -07001902 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1903 ScopedJniThreadState ts(env);
1904 return Decode<String*>(ts, java_string)->GetLength();
1905 }
1906
1907 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1908 ScopedJniThreadState ts(env);
1909 return Decode<String*>(ts, java_string)->GetUtfLength();
1910 }
1911
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 String* s = Decode<String*>(ts, java_string);
1915 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1916 ThrowSIOOBE(ts, start, length, s->GetLength());
1917 } else {
1918 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1919 memcpy(buf, chars + start, length * sizeof(jchar));
1920 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001921 }
1922
Elliott Hughesb465ab02011-08-24 11:21:21 -07001923 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001924 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 String* s = Decode<String*>(ts, java_string);
1926 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1927 ThrowSIOOBE(ts, start, length, s->GetLength());
1928 } else {
1929 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1930 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1931 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001935 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001936 String* s = Decode<String*>(ts, java_string);
1937 const CharArray* chars = s->GetCharArray();
1938 PinPrimitiveArray(ts, chars);
1939 if (is_copy != NULL) {
1940 *is_copy = JNI_FALSE;
1941 }
1942 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001946 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001947 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001952 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001957 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 if (java_string == NULL) {
1963 return NULL;
1964 }
1965 if (is_copy != NULL) {
1966 *is_copy = JNI_TRUE;
1967 }
1968 String* s = Decode<String*>(ts, java_string);
1969 size_t byte_count = s->GetUtfLength();
1970 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001971 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001972 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1973 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1974 bytes[byte_count] = '\0';
1975 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001981 }
1982
Elliott Hughesbd935992011-08-22 11:59:34 -07001983 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001985 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001986 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001987 Array* array = obj->AsArray();
1988 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001993 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001994 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
1997 static void SetObjectArrayElement(JNIEnv* env,
1998 jobjectArray java_array, jsize index, jobject java_value) {
1999 ScopedJniThreadState ts(env);
2000 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
2001 Object* value = Decode<Object*>(ts, java_value);
2002 array->Set(index, value);
2003 }
2004
2005 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2006 ScopedJniThreadState ts(env);
2007 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
2008 }
2009
2010 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2011 ScopedJniThreadState ts(env);
2012 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
2013 }
2014
2015 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2016 ScopedJniThreadState ts(env);
2017 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
2018 }
2019
2020 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2021 ScopedJniThreadState ts(env);
2022 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2023 }
2024
2025 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2026 ScopedJniThreadState ts(env);
2027 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2028 }
2029
2030 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2031 ScopedJniThreadState ts(env);
2032 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2033 }
2034
2035 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2036 ScopedJniThreadState ts(env);
2037 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2038 }
2039
2040 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2041 ScopedJniThreadState ts(env);
2042 CHECK_GE(length, 0); // TODO: ReportJniError
2043
2044 // Compute the array class corresponding to the given element class.
2045 Class* element_class = Decode<Class*>(ts, element_jclass);
2046 std::string descriptor;
2047 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002048 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002049
2050 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2052 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 return NULL;
2054 }
2055
Elliott Hughes75770752011-08-24 17:52:38 -07002056 // Allocate and initialize if necessary.
2057 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 if (initial_element != NULL) {
2060 Object* initial_object = Decode<Object*>(ts, initial_element);
2061 for (jsize i = 0; i < length; ++i) {
2062 result->Set(i, initial_object);
2063 }
2064 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002065 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
2068 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2069 ScopedJniThreadState ts(env);
2070 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2071 }
2072
Ian Rogersa15e67d2012-02-28 13:51:55 -08002073 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002074 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002075 Array* array = Decode<Array*>(ts, java_array);
2076 PinPrimitiveArray(ts, array);
2077 if (is_copy != NULL) {
2078 *is_copy = JNI_FALSE;
2079 }
2080 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes75770752011-08-24 17:52:38 -07002093 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes75770752011-08-24 17:52:38 -07002098 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002100 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes75770752011-08-24 17:52:38 -07002103 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002105 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002110 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes75770752011-08-24 17:52:38 -07002113 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002115 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes75770752011-08-24 17:52:38 -07002118 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002120 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes75770752011-08-24 17:52:38 -07002123 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002125 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002130 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes75770752011-08-24 17:52:38 -07002133 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002135 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes75770752011-08-24 17:52:38 -07002138 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002140 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes75770752011-08-24 17:52:38 -07002143 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002145 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes75770752011-08-24 17:52:38 -07002148 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002150 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes75770752011-08-24 17:52:38 -07002153 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002155 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes75770752011-08-24 17:52:38 -07002158 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002160 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes75770752011-08-24 17:52:38 -07002163 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002165 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002200 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002205 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes814e4032011-08-23 12:07:56 -07002213 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002215 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes814e4032011-08-23 12:07:56 -07002218 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002220 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Elliott Hughes814e4032011-08-23 12:07:56 -07002223 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002225 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes814e4032011-08-23 12:07:56 -07002228 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002230 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughes814e4032011-08-23 12:07:56 -07002233 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002235 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughes814e4032011-08-23 12:07:56 -07002238 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002240 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughes814e4032011-08-23 12:07:56 -07002243 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002245 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002250 Class* c = Decode<Class*>(ts, java_class);
2251
Elliott Hughes5174fe62011-08-23 15:12:35 -07002252 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 const char* name = methods[i].name;
2254 const char* sig = methods[i].signature;
2255
2256 if (*sig == '!') {
2257 // TODO: fast jni. it's too noisy to log all these.
2258 ++sig;
2259 }
2260
Elliott Hughes5174fe62011-08-23 15:12:35 -07002261 Method* m = c->FindDirectMethod(name, sig);
2262 if (m == NULL) {
2263 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002265 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002266 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002267 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002269 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002270 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002271 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 return JNI_ERR;
2273 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002274
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002275 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002276
Ian Rogers60db5ab2012-02-20 17:02:00 -08002277 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279 return JNI_OK;
2280 }
2281
Elliott Hughes5174fe62011-08-23 15:12:35 -07002282 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002284 Class* c = Decode<Class*>(ts, java_class);
2285
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002286 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002287
2288 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2289 Method* m = c->GetDirectMethod(i);
2290 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002291 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002292 }
2293 }
2294 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2295 Method* m = c->GetVirtualMethod(i);
2296 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002297 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002298 }
2299 }
2300
2301 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Elliott Hughes72025e52011-08-23 17:50:30 -07002304 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002306 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002307 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Elliott Hughes72025e52011-08-23 17:50:30 -07002310 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002312 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002313 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
2316 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2317 ScopedJniThreadState ts(env);
2318 Runtime* runtime = Runtime::Current();
2319 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002320 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002321 } else {
2322 *vm = NULL;
2323 }
2324 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2325 }
2326
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2328 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002329
2330 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002331 CHECK(address != NULL); // TODO: ReportJniError
2332 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333
2334 jclass buffer_class = GetDirectByteBufferClass(env);
2335 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2336 if (mid == NULL) {
2337 return NULL;
2338 }
2339
2340 // At the moment, the Java side is limited to 32 bits.
2341 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2342 CHECK_LE(capacity, 0xffffffff);
2343 jint address_arg = reinterpret_cast<jint>(address);
2344 jint capacity_arg = static_cast<jint>(capacity);
2345
2346 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2347 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002348 }
2349
Elliott Hughesb465ab02011-08-24 11:21:21 -07002350 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002352 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2353 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002354 }
2355
Elliott Hughesb465ab02011-08-24 11:21:21 -07002356 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002358 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2359 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002360 }
2361
Elliott Hughesb465ab02011-08-24 11:21:21 -07002362 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002364
Elliott Hughes75770752011-08-24 17:52:38 -07002365 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002366
2367 // Do we definitely know what kind of reference this is?
2368 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2369 IndirectRefKind kind = GetIndirectRefKind(ref);
2370 switch (kind) {
2371 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002372 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2373 return JNILocalRefType;
2374 }
2375 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002376 case kGlobal:
2377 return JNIGlobalRefType;
2378 case kWeakGlobal:
2379 return JNIWeakGlobalRefType;
2380 case kSirtOrInvalid:
2381 // Is it in a stack IRT?
2382 if (ts.Self()->SirtContains(java_object)) {
2383 return JNILocalRefType;
2384 }
2385
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002386 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002387 return JNIInvalidRefType;
2388 }
2389
Elliott Hughesb465ab02011-08-24 11:21:21 -07002390 // If we're handing out direct pointers, check whether it's a direct pointer
2391 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002392 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002393 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002394 return JNILocalRefType;
2395 }
2396 }
2397
2398 return JNIInvalidRefType;
2399 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002400 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2401 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002402 }
2403};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002404
Elliott Hughesa2501992011-08-26 19:39:54 -07002405const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002406 NULL, // reserved0.
2407 NULL, // reserved1.
2408 NULL, // reserved2.
2409 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 JNI::GetVersion,
2411 JNI::DefineClass,
2412 JNI::FindClass,
2413 JNI::FromReflectedMethod,
2414 JNI::FromReflectedField,
2415 JNI::ToReflectedMethod,
2416 JNI::GetSuperclass,
2417 JNI::IsAssignableFrom,
2418 JNI::ToReflectedField,
2419 JNI::Throw,
2420 JNI::ThrowNew,
2421 JNI::ExceptionOccurred,
2422 JNI::ExceptionDescribe,
2423 JNI::ExceptionClear,
2424 JNI::FatalError,
2425 JNI::PushLocalFrame,
2426 JNI::PopLocalFrame,
2427 JNI::NewGlobalRef,
2428 JNI::DeleteGlobalRef,
2429 JNI::DeleteLocalRef,
2430 JNI::IsSameObject,
2431 JNI::NewLocalRef,
2432 JNI::EnsureLocalCapacity,
2433 JNI::AllocObject,
2434 JNI::NewObject,
2435 JNI::NewObjectV,
2436 JNI::NewObjectA,
2437 JNI::GetObjectClass,
2438 JNI::IsInstanceOf,
2439 JNI::GetMethodID,
2440 JNI::CallObjectMethod,
2441 JNI::CallObjectMethodV,
2442 JNI::CallObjectMethodA,
2443 JNI::CallBooleanMethod,
2444 JNI::CallBooleanMethodV,
2445 JNI::CallBooleanMethodA,
2446 JNI::CallByteMethod,
2447 JNI::CallByteMethodV,
2448 JNI::CallByteMethodA,
2449 JNI::CallCharMethod,
2450 JNI::CallCharMethodV,
2451 JNI::CallCharMethodA,
2452 JNI::CallShortMethod,
2453 JNI::CallShortMethodV,
2454 JNI::CallShortMethodA,
2455 JNI::CallIntMethod,
2456 JNI::CallIntMethodV,
2457 JNI::CallIntMethodA,
2458 JNI::CallLongMethod,
2459 JNI::CallLongMethodV,
2460 JNI::CallLongMethodA,
2461 JNI::CallFloatMethod,
2462 JNI::CallFloatMethodV,
2463 JNI::CallFloatMethodA,
2464 JNI::CallDoubleMethod,
2465 JNI::CallDoubleMethodV,
2466 JNI::CallDoubleMethodA,
2467 JNI::CallVoidMethod,
2468 JNI::CallVoidMethodV,
2469 JNI::CallVoidMethodA,
2470 JNI::CallNonvirtualObjectMethod,
2471 JNI::CallNonvirtualObjectMethodV,
2472 JNI::CallNonvirtualObjectMethodA,
2473 JNI::CallNonvirtualBooleanMethod,
2474 JNI::CallNonvirtualBooleanMethodV,
2475 JNI::CallNonvirtualBooleanMethodA,
2476 JNI::CallNonvirtualByteMethod,
2477 JNI::CallNonvirtualByteMethodV,
2478 JNI::CallNonvirtualByteMethodA,
2479 JNI::CallNonvirtualCharMethod,
2480 JNI::CallNonvirtualCharMethodV,
2481 JNI::CallNonvirtualCharMethodA,
2482 JNI::CallNonvirtualShortMethod,
2483 JNI::CallNonvirtualShortMethodV,
2484 JNI::CallNonvirtualShortMethodA,
2485 JNI::CallNonvirtualIntMethod,
2486 JNI::CallNonvirtualIntMethodV,
2487 JNI::CallNonvirtualIntMethodA,
2488 JNI::CallNonvirtualLongMethod,
2489 JNI::CallNonvirtualLongMethodV,
2490 JNI::CallNonvirtualLongMethodA,
2491 JNI::CallNonvirtualFloatMethod,
2492 JNI::CallNonvirtualFloatMethodV,
2493 JNI::CallNonvirtualFloatMethodA,
2494 JNI::CallNonvirtualDoubleMethod,
2495 JNI::CallNonvirtualDoubleMethodV,
2496 JNI::CallNonvirtualDoubleMethodA,
2497 JNI::CallNonvirtualVoidMethod,
2498 JNI::CallNonvirtualVoidMethodV,
2499 JNI::CallNonvirtualVoidMethodA,
2500 JNI::GetFieldID,
2501 JNI::GetObjectField,
2502 JNI::GetBooleanField,
2503 JNI::GetByteField,
2504 JNI::GetCharField,
2505 JNI::GetShortField,
2506 JNI::GetIntField,
2507 JNI::GetLongField,
2508 JNI::GetFloatField,
2509 JNI::GetDoubleField,
2510 JNI::SetObjectField,
2511 JNI::SetBooleanField,
2512 JNI::SetByteField,
2513 JNI::SetCharField,
2514 JNI::SetShortField,
2515 JNI::SetIntField,
2516 JNI::SetLongField,
2517 JNI::SetFloatField,
2518 JNI::SetDoubleField,
2519 JNI::GetStaticMethodID,
2520 JNI::CallStaticObjectMethod,
2521 JNI::CallStaticObjectMethodV,
2522 JNI::CallStaticObjectMethodA,
2523 JNI::CallStaticBooleanMethod,
2524 JNI::CallStaticBooleanMethodV,
2525 JNI::CallStaticBooleanMethodA,
2526 JNI::CallStaticByteMethod,
2527 JNI::CallStaticByteMethodV,
2528 JNI::CallStaticByteMethodA,
2529 JNI::CallStaticCharMethod,
2530 JNI::CallStaticCharMethodV,
2531 JNI::CallStaticCharMethodA,
2532 JNI::CallStaticShortMethod,
2533 JNI::CallStaticShortMethodV,
2534 JNI::CallStaticShortMethodA,
2535 JNI::CallStaticIntMethod,
2536 JNI::CallStaticIntMethodV,
2537 JNI::CallStaticIntMethodA,
2538 JNI::CallStaticLongMethod,
2539 JNI::CallStaticLongMethodV,
2540 JNI::CallStaticLongMethodA,
2541 JNI::CallStaticFloatMethod,
2542 JNI::CallStaticFloatMethodV,
2543 JNI::CallStaticFloatMethodA,
2544 JNI::CallStaticDoubleMethod,
2545 JNI::CallStaticDoubleMethodV,
2546 JNI::CallStaticDoubleMethodA,
2547 JNI::CallStaticVoidMethod,
2548 JNI::CallStaticVoidMethodV,
2549 JNI::CallStaticVoidMethodA,
2550 JNI::GetStaticFieldID,
2551 JNI::GetStaticObjectField,
2552 JNI::GetStaticBooleanField,
2553 JNI::GetStaticByteField,
2554 JNI::GetStaticCharField,
2555 JNI::GetStaticShortField,
2556 JNI::GetStaticIntField,
2557 JNI::GetStaticLongField,
2558 JNI::GetStaticFloatField,
2559 JNI::GetStaticDoubleField,
2560 JNI::SetStaticObjectField,
2561 JNI::SetStaticBooleanField,
2562 JNI::SetStaticByteField,
2563 JNI::SetStaticCharField,
2564 JNI::SetStaticShortField,
2565 JNI::SetStaticIntField,
2566 JNI::SetStaticLongField,
2567 JNI::SetStaticFloatField,
2568 JNI::SetStaticDoubleField,
2569 JNI::NewString,
2570 JNI::GetStringLength,
2571 JNI::GetStringChars,
2572 JNI::ReleaseStringChars,
2573 JNI::NewStringUTF,
2574 JNI::GetStringUTFLength,
2575 JNI::GetStringUTFChars,
2576 JNI::ReleaseStringUTFChars,
2577 JNI::GetArrayLength,
2578 JNI::NewObjectArray,
2579 JNI::GetObjectArrayElement,
2580 JNI::SetObjectArrayElement,
2581 JNI::NewBooleanArray,
2582 JNI::NewByteArray,
2583 JNI::NewCharArray,
2584 JNI::NewShortArray,
2585 JNI::NewIntArray,
2586 JNI::NewLongArray,
2587 JNI::NewFloatArray,
2588 JNI::NewDoubleArray,
2589 JNI::GetBooleanArrayElements,
2590 JNI::GetByteArrayElements,
2591 JNI::GetCharArrayElements,
2592 JNI::GetShortArrayElements,
2593 JNI::GetIntArrayElements,
2594 JNI::GetLongArrayElements,
2595 JNI::GetFloatArrayElements,
2596 JNI::GetDoubleArrayElements,
2597 JNI::ReleaseBooleanArrayElements,
2598 JNI::ReleaseByteArrayElements,
2599 JNI::ReleaseCharArrayElements,
2600 JNI::ReleaseShortArrayElements,
2601 JNI::ReleaseIntArrayElements,
2602 JNI::ReleaseLongArrayElements,
2603 JNI::ReleaseFloatArrayElements,
2604 JNI::ReleaseDoubleArrayElements,
2605 JNI::GetBooleanArrayRegion,
2606 JNI::GetByteArrayRegion,
2607 JNI::GetCharArrayRegion,
2608 JNI::GetShortArrayRegion,
2609 JNI::GetIntArrayRegion,
2610 JNI::GetLongArrayRegion,
2611 JNI::GetFloatArrayRegion,
2612 JNI::GetDoubleArrayRegion,
2613 JNI::SetBooleanArrayRegion,
2614 JNI::SetByteArrayRegion,
2615 JNI::SetCharArrayRegion,
2616 JNI::SetShortArrayRegion,
2617 JNI::SetIntArrayRegion,
2618 JNI::SetLongArrayRegion,
2619 JNI::SetFloatArrayRegion,
2620 JNI::SetDoubleArrayRegion,
2621 JNI::RegisterNatives,
2622 JNI::UnregisterNatives,
2623 JNI::MonitorEnter,
2624 JNI::MonitorExit,
2625 JNI::GetJavaVM,
2626 JNI::GetStringRegion,
2627 JNI::GetStringUTFRegion,
2628 JNI::GetPrimitiveArrayCritical,
2629 JNI::ReleasePrimitiveArrayCritical,
2630 JNI::GetStringCritical,
2631 JNI::ReleaseStringCritical,
2632 JNI::NewWeakGlobalRef,
2633 JNI::DeleteWeakGlobalRef,
2634 JNI::ExceptionCheck,
2635 JNI::NewDirectByteBuffer,
2636 JNI::GetDirectBufferAddress,
2637 JNI::GetDirectBufferCapacity,
2638 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002639};
2640
Elliott Hughes75770752011-08-24 17:52:38 -07002641JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002642 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002643 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002644 local_ref_cookie(IRT_FIRST_SEGMENT),
2645 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002646 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002647 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002648 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002649 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002650 if (vm->check_jni) {
2651 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002652 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002653 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2654 // errors will ensue.
2655 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2656 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002657}
2658
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002659JNIEnvExt::~JNIEnvExt() {
2660}
2661
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002662void JNIEnvExt::EnableCheckJni() {
2663 check_jni = true;
2664 functions = GetCheckJniNativeInterface();
2665}
2666
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002667void JNIEnvExt::DumpReferenceTables() {
2668 locals.Dump();
2669 monitors.Dump();
2670}
2671
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002672void JNIEnvExt::PushFrame(int capacity) {
2673 stacked_local_ref_cookies.push_back(local_ref_cookie);
2674 local_ref_cookie = locals.GetSegmentState();
2675}
2676
2677void JNIEnvExt::PopFrame() {
2678 locals.SetSegmentState(local_ref_cookie);
2679 local_ref_cookie = stacked_local_ref_cookies.back();
2680 stacked_local_ref_cookies.pop_back();
2681}
2682
Carl Shapiroea4dca82011-08-01 13:45:38 -07002683// JNI Invocation interface.
2684
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002685extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2686 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2687 if (args->version < JNI_VERSION_1_2) {
2688 return JNI_EVERSION;
2689 }
2690 Runtime::Options options;
2691 for (int i = 0; i < args->nOptions; ++i) {
2692 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002693 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002694 }
2695 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002696 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697 if (runtime == NULL) {
2698 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002699 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002700 runtime->Start();
2701 *p_env = Thread::Current()->GetJniEnv();
2702 *p_vm = runtime->GetJavaVM();
2703 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002704}
2705
Elliott Hughesf2682d52011-08-15 16:37:04 -07002706extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002707 Runtime* runtime = Runtime::Current();
2708 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002709 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002711 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002712 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002713 }
2714 return JNI_OK;
2715}
2716
2717// Historically unsupported.
2718extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2719 return JNI_ERR;
2720}
2721
Elliott Hughescdf53122011-08-19 15:46:09 -07002722class JII {
2723 public:
2724 static jint DestroyJavaVM(JavaVM* vm) {
2725 if (vm == NULL) {
2726 return JNI_ERR;
2727 } else {
2728 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2729 delete raw_vm->runtime;
2730 return JNI_OK;
2731 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002732 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002733
Elliott Hughescdf53122011-08-19 15:46:09 -07002734 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002736 }
2737
2738 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002739 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002740 }
2741
2742 static jint DetachCurrentThread(JavaVM* vm) {
2743 if (vm == NULL) {
2744 return JNI_ERR;
2745 } else {
2746 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2747 Runtime* runtime = raw_vm->runtime;
2748 runtime->DetachCurrentThread();
2749 return JNI_OK;
2750 }
2751 }
2752
2753 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2754 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2755 return JNI_EVERSION;
2756 }
2757 if (vm == NULL || env == NULL) {
2758 return JNI_ERR;
2759 }
2760 Thread* thread = Thread::Current();
2761 if (thread == NULL) {
2762 *env = NULL;
2763 return JNI_EDETACHED;
2764 }
2765 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002766 return JNI_OK;
2767 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002768};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002769
Elliott Hughesa2501992011-08-26 19:39:54 -07002770const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002771 NULL, // reserved0
2772 NULL, // reserved1
2773 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002774 JII::DestroyJavaVM,
2775 JII::AttachCurrentThread,
2776 JII::DetachCurrentThread,
2777 JII::GetEnv,
2778 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002779};
2780
Elliott Hughesa0957642011-09-02 14:27:33 -07002781JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002782 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002783 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002784 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002785 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002786 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002787 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002788 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002789 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002790 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002791 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002792 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002794 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002795 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002796 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002797 if (options->check_jni_) {
2798 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002799 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002800}
2801
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002802JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002803 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002804}
2805
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002806void JavaVMExt::EnableCheckJni() {
2807 check_jni = true;
2808 functions = GetCheckJniInvokeInterface();
2809}
2810
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002811void JavaVMExt::DumpReferenceTables() {
2812 {
2813 MutexLock mu(globals_lock);
2814 globals.Dump();
2815 }
2816 {
2817 MutexLock mu(weak_globals_lock);
2818 weak_globals.Dump();
2819 }
2820 {
2821 MutexLock mu(pins_lock);
2822 pin_table.Dump();
2823 }
2824}
2825
Elliott Hughes75770752011-08-24 17:52:38 -07002826bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2827 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002828
2829 // See if we've already loaded this library. If we have, and the class loader
2830 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002831 // TODO: for better results we should canonicalize the pathname (or even compare
2832 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002833 SharedLibrary* library;
2834 {
2835 // TODO: move the locking (and more of this logic) into Libraries.
2836 MutexLock mu(libraries_lock);
2837 library = libraries->Get(path);
2838 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002839 if (library != NULL) {
2840 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002841 // The library will be associated with class_loader. The JNI
2842 // spec says we can't load the same library into more than one
2843 // class loader.
2844 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2845 "ClassLoader %p; can't open in ClassLoader %p",
2846 path.c_str(), library->GetClassLoader(), class_loader);
2847 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002848 return false;
2849 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002850 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2851 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002852 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002853 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2854 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002855 return false;
2856 }
2857 return true;
2858 }
2859
2860 // Open the shared library. Because we're using a full path, the system
2861 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2862 // resolve this library's dependencies though.)
2863
2864 // Failures here are expected when java.library.path has several entries
2865 // and we have to hunt for the lib.
2866
2867 // The current version of the dynamic linker prints detailed information
2868 // about dlopen() failures. Some things to check if the message is
2869 // cryptic:
2870 // - make sure the library exists on the device
2871 // - verify that the right path is being opened (the debug log message
2872 // above can help with that)
2873 // - check to see if the library is valid (e.g. not zero bytes long)
2874 // - check config/prelink-linux-arm.map to ensure that the library
2875 // is listed and is not being overrun by the previous entry (if
2876 // loading suddenly stops working on a prelinked library, this is
2877 // a good one to check)
2878 // - write a trivial app that calls sleep() then dlopen(), attach
2879 // to it with "strace -p <pid>" while it sleeps, and watch for
2880 // attempts to open nonexistent dependent shared libs
2881
2882 // TODO: automate some of these checks!
2883
2884 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002885 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002886 // the GC to ignore us.
2887 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002888 void* handle = NULL;
2889 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002890 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002891 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002892 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002893
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002894 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002895
2896 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002897 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002898 return false;
2899 }
2900
2901 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002902 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 // TODO: move the locking (and more of this logic) into Libraries.
2904 MutexLock mu(libraries_lock);
2905 library = libraries->Get(path);
2906 if (library != NULL) {
2907 LOG(INFO) << "WOW: we lost a race to add shared library: "
2908 << "\"" << path << "\" ClassLoader=" << class_loader;
2909 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002910 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002911 library = new SharedLibrary(path, handle, class_loader);
2912 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002913 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002914
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002915 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002916
2917 bool result = true;
2918 void* sym = dlsym(handle, "JNI_OnLoad");
2919 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002920 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002921 } else {
2922 // Call JNI_OnLoad. We have to override the current class
2923 // loader, which will always be "null" since the stuff at the
2924 // top of the stack is around Runtime.loadLibrary(). (See
2925 // the comments in the JNI FindClass function.)
2926 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2927 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002928 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002929 self->SetClassLoaderOverride(class_loader);
2930
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002931 int version = 0;
2932 {
2933 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002934 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002935 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002937
Brian Carlstromaded5f72011-10-07 17:15:04 -07002938 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002939
2940 if (version != JNI_VERSION_1_2 &&
2941 version != JNI_VERSION_1_4 &&
2942 version != JNI_VERSION_1_6) {
2943 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2944 << "bad version: " << version;
2945 // It's unwise to call dlclose() here, but we can mark it
2946 // as bad and ensure that future load attempts will fail.
2947 // We don't know how far JNI_OnLoad got, so there could
2948 // be some partially-initialized stuff accessible through
2949 // newly-registered native method calls. We could try to
2950 // unregister them, but that doesn't seem worthwhile.
2951 result = false;
2952 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002953 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2954 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002955 }
2956 }
2957
2958 library->SetResult(result);
2959 return result;
2960}
2961
2962void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2963 CHECK(m->IsNative());
2964
2965 Class* c = m->GetDeclaringClass();
2966
2967 // If this is a static method, it could be called before the class
2968 // has been initialized.
2969 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002970 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002971 return NULL;
2972 }
2973 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002974 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002975 }
2976
Brian Carlstrom16192862011-09-12 17:50:06 -07002977 std::string detail;
2978 void* native_method;
2979 {
2980 MutexLock mu(libraries_lock);
2981 native_method = libraries->FindNativeMethod(m, detail);
2982 }
2983 // throwing can cause libraries_lock to be reacquired
2984 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002985 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002986 }
2987 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002988}
2989
Elliott Hughes410c0c82011-09-01 17:58:25 -07002990void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2991 {
2992 MutexLock mu(globals_lock);
2993 globals.VisitRoots(visitor, arg);
2994 }
2995 {
2996 MutexLock mu(pins_lock);
2997 pin_table.VisitRoots(visitor, arg);
2998 }
2999 // The weak_globals table is visited by the GC itself (because it mutates the table).
3000}
3001
Elliott Hughes844f9a02012-01-24 20:19:58 -08003002jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
3003 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3004 if (c.get() == NULL) {
3005 return NULL;
3006 }
3007 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
3008}
3009
Ian Rogersdf20fe02011-07-20 20:34:16 -07003010} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003011
3012std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3013 switch (rhs) {
3014 case JNIInvalidRefType:
3015 os << "JNIInvalidRefType";
3016 return os;
3017 case JNILocalRefType:
3018 os << "JNILocalRefType";
3019 return os;
3020 case JNIGlobalRefType:
3021 os << "JNIGlobalRefType";
3022 return os;
3023 case JNIWeakGlobalRefType:
3024 os << "JNIWeakGlobalRefType";
3025 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003026 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003027 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003028 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003029 }
3030}