blob: 74b740a2292097049b5e6063d73e49fe5d68356b [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>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070027#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070029#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070031#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070033#include "scoped_jni_thread_state.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070035#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070036#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070037#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070038#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041namespace art {
42
Elliott Hughes2ced6a52011-10-16 18:44:48 -070043static const size_t kMonitorsInitial = 32; // Arbitrary.
44static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
45
46static const size_t kLocalsInitial = 64; // Arbitrary.
47static const size_t kLocalsMax = 512; // Arbitrary sanity check.
48
49static const size_t kPinTableInitial = 16; // Arbitrary.
50static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052static size_t gGlobalsInitial = 512; // Arbitrary.
53static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054
55static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
56static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
57
Elliott Hugheseac76672012-05-24 21:56:51 -070058void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, size_t method_count) {
59 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
60 if (c.get() == NULL) {
61 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
62 }
63 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
64 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
65 }
66}
67
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070068void SetJniGlobalsMax(size_t max) {
69 if (max != 0) {
70 gGlobalsMax = max;
71 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
72 }
73}
Ian Rogersdf20fe02011-07-20 20:34:16 -070074
Ian Rogers45619fc2012-02-29 11:15:25 -080075size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
76 size_t num_bytes = 0;
77 for (size_t i = 1; i < shorty_len; ++i) {
78 char ch = shorty[i];
79 if (ch == 'D' || ch == 'J') {
80 num_bytes += 8;
81 } else if (ch == 'L') {
82 // Argument is a reference or an array. The shorty descriptor
83 // does not distinguish between these types.
84 num_bytes += sizeof(Object*);
85 } else {
86 num_bytes += 4;
87 }
88 }
89 return num_bytes;
90}
91
92class ArgArray {
93 public:
94 explicit ArgArray(Method* method) {
95 MethodHelper mh(method);
96 shorty_ = mh.GetShorty();
97 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -070098 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -080099 arg_array_ = small_arg_array_;
100 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700101 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800102 arg_array_ = large_arg_array_.get();
103 }
104 }
105
Elliott Hughes77405792012-03-15 15:22:12 -0700106 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800107 return arg_array_;
108 }
109
Ian Rogers365c1022012-06-22 15:05:28 -0700110 void BuildArgArray(const ScopedJniThreadState& ts, va_list ap) {
Elliott Hughes77405792012-03-15 15:22:12 -0700111 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800112 switch (shorty_[i]) {
113 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700114 arg_array_[offset].SetZ(va_arg(ap, jint));
115 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800116 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700117 arg_array_[offset].SetB(va_arg(ap, jint));
118 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800119 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700120 arg_array_[offset].SetC(va_arg(ap, jint));
121 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800122 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700123 arg_array_[offset].SetS(va_arg(ap, jint));
124 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800125 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800127 break;
128 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800130 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700131 case 'L':
Ian Rogers365c1022012-06-22 15:05:28 -0700132 arg_array_[offset].SetL(ts.Decode<Object*>(va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800133 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800134 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700135 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800136 break;
137 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800139 break;
140 }
141 }
142 }
143
Ian Rogers365c1022012-06-22 15:05:28 -0700144 void BuildArgArray(const ScopedJniThreadState& ts, jvalue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700145 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800146 switch (shorty_[i]) {
147 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700148 arg_array_[offset].SetZ(args[offset].z);
149 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800150 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700151 arg_array_[offset].SetB(args[offset].b);
152 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800153 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700154 arg_array_[offset].SetC(args[offset].c);
155 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800156 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700157 arg_array_[offset].SetS(args[offset].s);
158 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800159 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700160 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800161 break;
162 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700163 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800164 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700165 case 'L':
Ian Rogers365c1022012-06-22 15:05:28 -0700166 arg_array_[offset].SetL(ts.Decode<Object*>(args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800167 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800168 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700169 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800170 break;
171 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700172 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800173 break;
174 }
175 }
176 }
177
Ian Rogers45619fc2012-02-29 11:15:25 -0800178 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700179 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800180 const char* shorty_;
181 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700182 JValue* arg_array_;
183 JValue small_arg_array_[kSmallArgArraySize];
184 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800185};
186
Elliott Hughes0512f022012-03-15 22:10:52 -0700187static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700188 if (obj == NULL) {
189 return NULL;
190 }
Elliott Hughes75770752011-08-24 17:52:38 -0700191 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700192 IndirectReferenceTable& weak_globals = vm->weak_globals;
193 MutexLock mu(vm->weak_globals_lock);
194 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
195 return reinterpret_cast<jweak>(ref);
196}
197
Elliott Hughesb264f082012-04-06 17:10:10 -0700198static void CheckMethodArguments(Method* m, JValue* args) {
199 MethodHelper mh(m);
200 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
201 CHECK(parameter_types != NULL);
202 size_t error_count = 0;
203 for (int i = 0; i < parameter_types->GetLength(); ++i) {
204 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700205 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700206 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700207 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700208 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
209 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
210 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
211 ++error_count;
212 }
213 }
214 }
215 if (error_count > 0) {
216 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
217 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700218 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700219 }
220}
Elliott Hughesb264f082012-04-06 17:10:10 -0700221
Ian Rogers365c1022012-06-22 15:05:28 -0700222static JValue InvokeWithArgArray(const ScopedJniThreadState& ts, Object* receiver, Method* method,
223 JValue* args) {
224 if (UNLIKELY(ts.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700225 CheckMethodArguments(method, args);
226 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700227 JValue result;
Ian Rogers365c1022012-06-22 15:05:28 -0700228 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700229 return result;
230}
231
Ian Rogers365c1022012-06-22 15:05:28 -0700232static JValue InvokeWithVarArgs(const ScopedJniThreadState& ts, jobject obj, jmethodID mid,
233 va_list args) {
234 Object* receiver = ts.Decode<Object*>(obj);
235 Method* method = ts.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800236 ArgArray arg_array(method);
Ian Rogers365c1022012-06-22 15:05:28 -0700237 arg_array.BuildArgArray(ts, args);
238 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700239}
240
Ian Rogers0571d352011-11-03 19:51:38 -0700241static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700242 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700243}
244
Ian Rogers365c1022012-06-22 15:05:28 -0700245static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedJniThreadState& ts, jobject obj,
246 jmethodID mid, jvalue* args) {
247 Object* receiver = ts.Decode<Object*>(obj);
248 Method* method = FindVirtualMethod(receiver, ts.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800249 ArgArray arg_array(method);
Ian Rogers365c1022012-06-22 15:05:28 -0700250 arg_array.BuildArgArray(ts, args);
251 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700252}
253
Ian Rogers365c1022012-06-22 15:05:28 -0700254static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedJniThreadState& ts, jobject obj,
255 jmethodID mid, va_list args) {
256 Object* receiver = ts.Decode<Object*>(obj);
257 Method* method = FindVirtualMethod(receiver, ts.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800258 ArgArray arg_array(method);
Ian Rogers365c1022012-06-22 15:05:28 -0700259 arg_array.BuildArgArray(ts, args);
260 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700261}
262
Elliott Hughes6b436852011-08-12 10:16:44 -0700263// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
264// separated with slashes but aren't wrapped with "L;" like regular descriptors
265// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
266// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
267// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700268static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700269 std::string result;
270 // Add the missing "L;" if necessary.
271 if (name[0] == '[') {
272 result = name;
273 } else {
274 result += 'L';
275 result += name;
276 result += ';';
277 }
278 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700279 if (result.find('.') != std::string::npos) {
280 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
281 << "\"" << name << "\"";
282 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700283 }
284 return result;
285}
286
Ian Rogers0571d352011-11-03 19:51:38 -0700287static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700288 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800289 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700290}
291
Ian Rogers0571d352011-11-03 19:51:38 -0700292static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Ian Rogers365c1022012-06-22 15:05:28 -0700293 Class* c = ts.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700294 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700295 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700296 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700297
298 Method* method = NULL;
299 if (is_static) {
300 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700301 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700302 method = c->FindVirtualMethod(name, sig);
303 if (method == NULL) {
304 // No virtual method matching the signature. Search declared
305 // private methods and constructors.
306 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700307 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700308 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700309
Elliott Hughescdf53122011-08-19 15:46:09 -0700310 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700311 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700312 return NULL;
313 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700314
Ian Rogers365c1022012-06-22 15:05:28 -0700315 return ts.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700316}
317
Ian Rogers365c1022012-06-22 15:05:28 -0700318static ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700319 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700320 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
321 return self->GetClassLoaderOverride();
322 }
323 return method->GetDeclaringClass()->GetClassLoader();
324}
325
Ian Rogers365c1022012-06-22 15:05:28 -0700326static jfieldID FindFieldID(const ScopedJniThreadState& ts, jclass jni_class, const char* name,
327 const char* sig, bool is_static) {
328 Class* c = ts.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700329 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700330 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700331 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700332
333 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 Class* field_type;
335 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
336 if (sig[1] != '\0') {
Ian Rogers365c1022012-06-22 15:05:28 -0700337 ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700339 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700340 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700341 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 if (field_type == NULL) {
343 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700344 DCHECK(ts.Self()->IsExceptionPending());
345 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700346 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700347 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800348 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 return NULL;
350 }
351 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800352 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800354 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700357 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700358 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800359 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700360 return NULL;
361 }
Ian Rogers365c1022012-06-22 15:05:28 -0700362 return ts.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700363}
364
Ian Rogers365c1022012-06-22 15:05:28 -0700365static void PinPrimitiveArray(const ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700366 JavaVMExt* vm = ts.Vm();
367 MutexLock mu(vm->pins_lock);
368 vm->pin_table.Add(array);
369}
370
Ian Rogers365c1022012-06-22 15:05:28 -0700371static void UnpinPrimitiveArray(const ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700372 JavaVMExt* vm = ts.Vm();
373 MutexLock mu(vm->pins_lock);
374 vm->pin_table.Remove(array);
375}
376
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700377template<typename JniT, typename ArtT>
Ian Rogers365c1022012-06-22 15:05:28 -0700378static JniT NewPrimitiveArray(const ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700379 CHECK_GE(length, 0); // TODO: ReportJniError
380 ArtT* result = ArtT::Alloc(length);
Ian Rogers365c1022012-06-22 15:05:28 -0700381 return ts.AddLocalReference<JniT>(result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700382}
383
Elliott Hughes75770752011-08-24 17:52:38 -0700384template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700385static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Ian Rogers365c1022012-06-22 15:05:28 -0700386 ArtArrayT* array = ts.Decode<ArtArrayT*>(java_array);
Elliott Hughes75770752011-08-24 17:52:38 -0700387 PinPrimitiveArray(ts, array);
388 if (is_copy != NULL) {
389 *is_copy = JNI_FALSE;
390 }
391 return array->GetData();
392}
393
394template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700395static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700396 if (mode != JNI_COMMIT) {
Ian Rogers365c1022012-06-22 15:05:28 -0700397 Array* array = ts.Decode<Array*>(java_array);
Elliott Hughes75770752011-08-24 17:52:38 -0700398 UnpinPrimitiveArray(ts, array);
399 }
400}
401
Ian Rogers0571d352011-11-03 19:51:38 -0700402static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700403 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700404 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700405 "%s offset=%d length=%d %s.length=%d",
406 type.c_str(), start, length, identifier, array->GetLength());
407}
Ian Rogers0571d352011-11-03 19:51:38 -0700408
409static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700410 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700411 "offset=%d length=%d string.length()=%d", start, length, array_length);
412}
Elliott Hughes814e4032011-08-23 12:07:56 -0700413
414template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700415static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Ian Rogers365c1022012-06-22 15:05:28 -0700416 ArrayT* array = ts.Decode<ArrayT*>(java_array);
Elliott Hughes814e4032011-08-23 12:07:56 -0700417 if (start < 0 || length < 0 || start + length > array->GetLength()) {
418 ThrowAIOOBE(ts, array, start, length, "src");
419 } else {
420 JavaT* data = array->GetData();
421 memcpy(buf, data + start, length * sizeof(JavaT));
422 }
423}
424
425template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700426static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Ian Rogers365c1022012-06-22 15:05:28 -0700427 ArrayT* array = ts.Decode<ArrayT*>(java_array);
Elliott Hughes814e4032011-08-23 12:07:56 -0700428 if (start < 0 || length < 0 || start + length > array->GetLength()) {
429 ThrowAIOOBE(ts, array, start, length, "dst");
430 } else {
431 JavaT* data = array->GetData();
432 memcpy(data + start, buf, length * sizeof(JavaT));
433 }
434}
435
Elliott Hughesa4f94742012-05-29 16:28:38 -0700436int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause) {
437 ScopedJniThreadState ts(env);
438
439 // Turn the const char* into a java.lang.String.
440 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
441 if (msg != NULL && s.get() == NULL) {
442 return JNI_ERR;
443 }
444
445 // Choose an appropriate constructor and set up the arguments.
446 jvalue args[2];
447 const char* signature;
448 if (msg == NULL && cause == NULL) {
449 signature = "()V";
450 } else if (msg != NULL && cause == NULL) {
451 signature = "(Ljava/lang/String;)V";
452 args[0].l = s.get();
453 } else if (msg == NULL && cause != NULL) {
454 signature = "(Ljava/lang/Throwable;)V";
455 args[0].l = cause;
456 } else {
457 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
458 args[0].l = s.get();
459 args[1].l = cause;
460 }
461 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
462 if (mid == NULL) {
Ian Rogers365c1022012-06-22 15:05:28 -0700463 LOG(ERROR) << "No <init>" << signature << " in "
464 << PrettyClass(ts.Decode<Class*>(exception_class));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700465 return JNI_ERR;
466 }
467
468 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
469 if (exception.get() == NULL) {
470 return JNI_ERR;
471 }
472
Ian Rogers365c1022012-06-22 15:05:28 -0700473 ts.Self()->SetException(ts.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700474
475 return JNI_OK;
476}
477
Elliott Hughes462c9442012-03-23 18:47:50 -0700478static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700479 if (vm == NULL || p_env == NULL) {
480 return JNI_ERR;
481 }
482
Elliott Hughes462c9442012-03-23 18:47:50 -0700483 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700484 Thread* self = Thread::Current();
485 if (self != NULL) {
486 *p_env = self->GetJniEnv();
487 return JNI_OK;
488 }
489
490 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
491
492 // No threads allowed in zygote mode.
493 if (runtime->IsZygote()) {
494 LOG(ERROR) << "Attempt to attach a thread in the zygote";
495 return JNI_ERR;
496 }
497
Elliott Hughes462c9442012-03-23 18:47:50 -0700498 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
499 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700500 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700501 if (args != NULL) {
502 CHECK_GE(args->version, JNI_VERSION_1_2);
503 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700504 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700505 }
Elliott Hughes75770752011-08-24 17:52:38 -0700506
Elliott Hughes462c9442012-03-23 18:47:50 -0700507 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700508 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700509 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700510}
511
Elliott Hughes79082e32011-08-25 12:07:32 -0700512class SharedLibrary {
513 public:
514 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
515 : path_(path),
516 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700517 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700518 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700519 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700520 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700522 }
523
Elliott Hughes79082e32011-08-25 12:07:32 -0700524 Object* GetClassLoader() {
525 return class_loader_;
526 }
527
528 std::string GetPath() {
529 return path_;
530 }
531
532 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700533 * Check the result of an earlier call to JNI_OnLoad on this library.
534 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700536 bool CheckOnLoadResult() {
Elliott Hughesf8349362012-06-18 15:00:06 -0700537 MutexLock mu(jni_on_load_lock_);
538
Elliott Hughes79082e32011-08-25 12:07:32 -0700539 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700540 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 // Check this so we don't end up waiting for ourselves. We need
542 // to return "true" so the caller can continue.
543 LOG(INFO) << *self << " recursive attempt to load library "
544 << "\"" << path_ << "\"";
545 return true;
546 }
547
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800549 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
550 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700551 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700552 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 }
554
555 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800556 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
557 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 return okay;
559 }
560
561 void SetResult(bool result) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700562 MutexLock mu(jni_on_load_lock_);
563
Elliott Hughes79082e32011-08-25 12:07:32 -0700564 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700565 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700566
567 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -0700568 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 }
570
571 void* FindSymbol(const std::string& symbol_name) {
572 return dlsym(handle_, symbol_name.c_str());
573 }
574
575 private:
576 enum JNI_OnLoadState {
577 kPending,
578 kFailed,
579 kOkay,
580 };
581
582 // Path to library "/system/lib/libjni.so".
583 std::string path_;
584
585 // The void* returned by dlopen(3).
586 void* handle_;
587
588 // The ClassLoader this library is associated with.
589 Object* class_loader_;
590
591 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700592 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700594 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700596 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700598 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700599};
600
Elliott Hughes79082e32011-08-25 12:07:32 -0700601// This exists mainly to keep implementation details out of the header file.
602class Libraries {
603 public:
604 Libraries() {
605 }
606
607 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700608 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 }
610
Elliott Hughesae80b492012-04-24 10:43:17 -0700611 void Dump(std::ostream& os) const {
612 bool first = true;
613 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
614 if (!first) {
615 os << ' ';
616 }
617 first = false;
618 os << it->first;
619 }
620 }
621
622 size_t size() const {
623 return libraries_.size();
624 }
625
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700627 It it = libraries_.find(path);
628 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700629 }
630
631 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700632 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700633 }
634
635 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700636 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700637 std::string jni_short_name(JniShortName(m));
638 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700639 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
641 SharedLibrary* library = it->second;
642 if (library->GetClassLoader() != declaring_class_loader) {
643 // We only search libraries loaded by the appropriate ClassLoader.
644 continue;
645 }
646 // Try the short name then the long name...
647 void* fn = library->FindSymbol(jni_short_name);
648 if (fn == NULL) {
649 fn = library->FindSymbol(jni_long_name);
650 }
651 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800652 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
653 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700654 return fn;
655 }
656 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700657 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700658 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700659 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700660 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700661 return NULL;
662 }
663
664 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700665 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700666
Elliott Hughesa0e18062012-04-13 15:59:59 -0700667 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700668};
669
Ian Rogers365c1022012-06-22 15:05:28 -0700670JValue InvokeWithJValues(const ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
671 Object* receiver = ts.Decode<Object*>(obj);
672 Method* method = ts.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800673 ArgArray arg_array(method);
Ian Rogers365c1022012-06-22 15:05:28 -0700674 arg_array.BuildArgArray(ts, args);
675 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700676}
677
Ian Rogers365c1022012-06-22 15:05:28 -0700678JValue InvokeWithJValues(const ScopedJniThreadState& ts, Object* receiver, Method* m, JValue* args) {
679 return InvokeWithArgArray(ts, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800680}
681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682class JNI {
683 public:
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 static jint GetVersion(JNIEnv* env) {
685 ScopedJniThreadState ts(env);
686 return JNI_VERSION_1_6;
687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
690 ScopedJniThreadState ts(env);
691 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700692 return NULL;
693 }
694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jclass FindClass(JNIEnv* env, const char* name) {
696 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700697 Runtime* runtime = Runtime::Current();
698 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700700 Class* c = NULL;
701 if (runtime->IsStarted()) {
Ian Rogers365c1022012-06-22 15:05:28 -0700702 ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800703 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800705 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700706 }
Ian Rogers365c1022012-06-22 15:05:28 -0700707 return ts.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
711 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700712 Method* method = ts.Decode<Method*>(java_method);
713 return ts.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
717 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700718 Field* field = ts.Decode<Field*>(java_field);
719 return ts.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
723 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700724 Method* method = ts.DecodeMethod(mid);
725 return ts.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
729 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700730 Field* field = ts.DecodeField(fid);
731 return ts.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700733
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
735 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700736 Object* o = ts.Decode<Object*>(java_object);
737 return ts.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 }
739
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700740 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700742 Class* c = ts.Decode<Class*>(java_class);
743 return ts.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700748 Class* c1 = ts.Decode<Class*>(java_class1);
749 Class* c2 = ts.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700752
Elliott Hughese84278b2012-03-22 10:06:53 -0700753 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700755 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700757 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 return JNI_TRUE;
759 } else {
Ian Rogers365c1022012-06-22 15:05:28 -0700760 Object* obj = ts.Decode<Object*>(jobj);
761 Class* c = ts.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700762 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 static jint Throw(JNIEnv* env, jthrowable java_exception) {
767 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700768 Throwable* exception = ts.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 if (exception == NULL) {
770 return JNI_ERR;
771 }
772 ts.Self()->SetException(exception);
773 return JNI_OK;
774 }
775
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700777 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700778 }
779
780 static jboolean ExceptionCheck(JNIEnv* env) {
781 ScopedJniThreadState ts(env);
782 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
783 }
784
785 static void ExceptionClear(JNIEnv* env) {
786 ScopedJniThreadState ts(env);
787 ts.Self()->ClearException();
788 }
789
790 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700792
793 Thread* self = ts.Self();
794 Throwable* original_exception = self->GetException();
795 self->ClearException();
796
Ian Rogers365c1022012-06-22 15:05:28 -0700797 ScopedLocalRef<jthrowable> exception(env, ts.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
799 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
800 if (mid == NULL) {
801 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700802 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700803 } else {
804 env->CallVoidMethod(exception.get(), mid);
805 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700806 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 << " thrown while calling printStackTrace";
808 self->ClearException();
809 }
810 }
811
812 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700814
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 static jthrowable ExceptionOccurred(JNIEnv* env) {
816 ScopedJniThreadState ts(env);
817 Object* exception = ts.Self()->GetException();
Ian Rogers365c1022012-06-22 15:05:28 -0700818 return ts.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
820
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 static void FatalError(JNIEnv* env, const char* msg) {
822 ScopedJniThreadState ts(env);
823 LOG(FATAL) << "JNI FatalError called: " << msg;
824 }
825
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700826 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700828 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
829 return JNI_ERR;
830 }
831 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 return JNI_OK;
833 }
834
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700835 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700837 Object* survivor = ts.Decode<Object*>(java_survivor);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700838 ts.Env()->PopFrame();
Ian Rogers365c1022012-06-22 15:05:28 -0700839 return ts.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700842 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700844 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
845 }
846
Ian Rogers365c1022012-06-22 15:05:28 -0700847 static jint EnsureLocalCapacity(const ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700848 // TODO: we should try to expand the table if necessary.
849 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
850 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
851 return JNI_ERR;
852 }
853 // TODO: this isn't quite right, since "capacity" includes holes.
854 size_t capacity = ts.Env()->locals.Capacity();
855 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
856 if (!okay) {
857 ts.Self()->ThrowOutOfMemoryError(caller);
858 }
859 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 }
861
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
863 ScopedJniThreadState ts(env);
864 if (obj == NULL) {
865 return NULL;
866 }
867
Elliott Hughes75770752011-08-24 17:52:38 -0700868 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 IndirectReferenceTable& globals = vm->globals;
870 MutexLock mu(vm->globals_lock);
Ian Rogers365c1022012-06-22 15:05:28 -0700871 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, ts.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 return reinterpret_cast<jobject>(ref);
873 }
874
875 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
876 ScopedJniThreadState ts(env);
877 if (obj == NULL) {
878 return;
879 }
880
Elliott Hughes75770752011-08-24 17:52:38 -0700881 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 IndirectReferenceTable& globals = vm->globals;
883 MutexLock mu(vm->globals_lock);
884
885 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
886 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
887 << "failed to find entry";
888 }
889 }
890
891 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
892 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700893 return AddWeakGlobalReference(ts, ts.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
896 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
897 ScopedJniThreadState ts(env);
898 if (obj == NULL) {
899 return;
900 }
901
Elliott Hughes75770752011-08-24 17:52:38 -0700902 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 IndirectReferenceTable& weak_globals = vm->weak_globals;
904 MutexLock mu(vm->weak_globals_lock);
905
906 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
907 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
908 << "failed to find entry";
909 }
910 }
911
912 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
913 ScopedJniThreadState ts(env);
914 if (obj == NULL) {
915 return NULL;
916 }
917
918 IndirectReferenceTable& locals = ts.Env()->locals;
919
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700920 uint32_t cookie = ts.Env()->local_ref_cookie;
Ian Rogers365c1022012-06-22 15:05:28 -0700921 IndirectRef ref = locals.Add(cookie, ts.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 return reinterpret_cast<jobject>(ref);
923 }
924
925 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
926 ScopedJniThreadState ts(env);
927 if (obj == NULL) {
928 return;
929 }
930
931 IndirectReferenceTable& locals = ts.Env()->locals;
932
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700933 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 if (!locals.Remove(cookie, obj)) {
935 // Attempting to delete a local reference that is not in the
936 // topmost local reference frame is a no-op. DeleteLocalRef returns
937 // void and doesn't throw any exceptions, but we should probably
938 // complain about it so the user will notice that things aren't
939 // going quite the way they expect.
940 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
941 << "failed to find entry";
942 }
943 }
944
945 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
946 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700947 return (ts.Decode<Object*>(obj1) == ts.Decode<Object*>(obj2))
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 ? JNI_TRUE : JNI_FALSE;
949 }
950
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700951 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700953 Class* c = ts.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700954 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700955 return NULL;
956 }
Ian Rogers365c1022012-06-22 15:05:28 -0700957 return ts.AddLocalReference<jobject>(c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughese84278b2012-03-22 10:06:53 -0700960 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 ScopedJniThreadState ts(env);
962 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700964 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 va_end(args);
966 return result;
967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700971 Class* c = ts.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700972 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700973 return NULL;
974 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700975 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700976 if (result == NULL) {
977 return NULL;
978 }
Ian Rogers365c1022012-06-22 15:05:28 -0700979 jobject local_result = ts.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700981 if (!ts.Self()->IsExceptionPending()) {
982 return local_result;
983 } else {
984 return NULL;
985 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700990 Class* c = ts.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700991 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700992 return NULL;
993 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700994 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700995 if (result == NULL) {
996 return NULL;
997 }
Ian Rogers365c1022012-06-22 15:05:28 -0700998 jobject local_result = ts.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001000 if (!ts.Self()->IsExceptionPending()) {
1001 return local_result;
1002 } else {
1003 return NULL;
1004 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1008 ScopedJniThreadState ts(env);
1009 return FindMethodID(ts, c, name, sig, false);
1010 }
1011
1012 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1013 ScopedJniThreadState ts(env);
1014 return FindMethodID(ts, c, name, sig, true);
1015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001021 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
Ian Rogers365c1022012-06-22 15:05:28 -07001023 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001028 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args));
1029 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001034 JValue result(InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args));
1035 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_list ap;
1041 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001042 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001044 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001049 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001054 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_list ap;
1060 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001061 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001063 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001068 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001073 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_list ap;
1079 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001080 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001082 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001087 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001092 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001099 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001101 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001106 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001111 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001118 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001120 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001125 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001130 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001137 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001139 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001144 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001149 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_list ap;
1155 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001156 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001158 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001163 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001168 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_list ap;
1174 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001175 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001182 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001187 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_list ap;
1193 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001194 JValue result(InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001200 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001205 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
1210 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001212 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
1213 jobject local_result = ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
1215 return local_result;
1216 }
1217
1218 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001219 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001221 JValue result(InvokeWithVarArgs(ts, obj, mid, args));
1222 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
1225 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001226 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001228 JValue result(InvokeWithJValues(ts, obj, mid, args));
1229 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
1232 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001233 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
1235 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001237 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001239 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001243 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001245 return InvokeWithVarArgs(ts, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001251 return InvokeWithJValues(ts, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001254 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
1256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001258 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001260 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
1263 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001264 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001266 return InvokeWithVarArgs(ts, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
1269 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001270 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001272 return InvokeWithJValues(ts, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
1277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001279 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001281 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001287 return InvokeWithVarArgs(ts, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001291 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001293 return InvokeWithJValues(ts, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001296 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, 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);
Ian Rogers365c1022012-06-22 15:05:28 -07001300 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001302 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001308 return InvokeWithVarArgs(ts, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001314 return InvokeWithJValues(ts, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001317 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
1319 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001321 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001323 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
1326 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001327 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001329 return InvokeWithVarArgs(ts, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
1332 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001335 return InvokeWithJValues(ts, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
1340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001342 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001344 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001348 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001350 return InvokeWithVarArgs(ts, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001354 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001356 return InvokeWithJValues(ts, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001359 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
1361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001363 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001365 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
1368 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001369 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001371 return InvokeWithVarArgs(ts, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001375 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001377 return InvokeWithJValues(ts, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001380 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
1382 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001383 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001384 JValue result(InvokeWithVarArgs(ts, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001386 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
1389 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001390 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001392 return InvokeWithVarArgs(ts, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
1395 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001396 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001398 return InvokeWithJValues(ts, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001401 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
1403 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001405 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_end(ap);
1407 }
1408
1409 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001412 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001418 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001421 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 ScopedJniThreadState ts(env);
1423 return FindFieldID(ts, c, name, sig, false);
1424 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001425
1426
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001427 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 ScopedJniThreadState ts(env);
1429 return FindFieldID(ts, c, name, sig, true);
1430 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001434 Object* o = ts.Decode<Object*>(obj);
1435 Field* f = ts.DecodeField(fid);
1436 return ts.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001441 Field* f = ts.DecodeField(fid);
1442 return ts.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001447 Object* o = ts.Decode<Object*>(java_object);
1448 Object* v = ts.Decode<Object*>(java_value);
1449 Field* f = ts.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001455 Object* v = ts.Decode<Object*>(java_value);
1456 Field* f = ts.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460#define GET_PRIMITIVE_FIELD(fn, instance) \
1461 ScopedJniThreadState ts(env); \
Ian Rogers365c1022012-06-22 15:05:28 -07001462 Object* o = ts.Decode<Object*>(instance); \
1463 Field* f = ts.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 return f->fn(o)
1465
1466#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1467 ScopedJniThreadState ts(env); \
Ian Rogers365c1022012-06-22 15:05:28 -07001468 Object* o = ts.Decode<Object*>(instance); \
1469 Field* f = ts.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 f->fn(o, value)
1471
1472 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001504 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001512 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001513 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 GET_PRIMITIVE_FIELD(GetLong, NULL);
1526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1534 }
1535
1536 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1537 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1538 }
1539
1540 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1541 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1542 }
1543
1544 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1545 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1546 }
1547
1548 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1549 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1550 }
1551
1552 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1553 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1554 }
1555
1556 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1557 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1558 }
1559
1560 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1561 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1562 }
1563
1564 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1565 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1566 }
1567
1568 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1569 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1570 }
1571
1572 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1573 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1574 }
1575
1576 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1577 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1578 }
1579
1580 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1581 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1582 }
1583
1584 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1585 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1586 }
1587
1588 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1589 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1590 }
1591
1592 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1593 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1594 }
1595
1596 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1597 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 ScopedJniThreadState ts(env);
1602 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001604 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
1605 jobject local_result = ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 va_end(ap);
1607 return local_result;
1608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001612 JValue result(InvokeWithVarArgs(ts, NULL, mid, args));
1613 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001618 JValue result(InvokeWithJValues(ts, NULL, mid, args));
1619 return ts.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
1624 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001626 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001628 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001633 return InvokeWithVarArgs(ts, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001638 return InvokeWithJValues(ts, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
1643 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001645 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001647 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001652 return InvokeWithVarArgs(ts, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001657 return InvokeWithJValues(ts, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
1662 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001664 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001666 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001671 return InvokeWithVarArgs(ts, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001676 return InvokeWithJValues(ts, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
1681 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001683 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001685 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001690 return InvokeWithVarArgs(ts, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001695 return InvokeWithJValues(ts, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
1700 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 va_start(ap, mid);
Ian Rogers365c1022012-06-22 15:05:28 -07001702 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001704 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001709 return InvokeWithVarArgs(ts, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001714 return InvokeWithJValues(ts, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jlong CallStaticLongMethod(JNIEnv* env, jclass, 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);
Ian Rogers365c1022012-06-22 15:05:28 -07001721 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001723 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001728 return InvokeWithVarArgs(ts, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001733 return InvokeWithJValues(ts, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, 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);
Ian Rogers365c1022012-06-22 15:05:28 -07001740 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001742 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001747 return InvokeWithVarArgs(ts, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001752 return InvokeWithJValues(ts, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, 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);
Ian Rogers365c1022012-06-22 15:05:28 -07001759 JValue result(InvokeWithVarArgs(ts, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001761 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001766 return InvokeWithVarArgs(ts, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001769 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001771 return InvokeWithJValues(ts, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001774 static void CallStaticVoidMethod(JNIEnv* env, jclass, 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);
Ian Rogers365c1022012-06-22 15:05:28 -07001778 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_end(ap);
1780 }
1781
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001782 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001784 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001789 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes814e4032011-08-23 12:07:56 -07001792 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001794 String* result = String::AllocFromUtf16(char_count, chars);
Ian Rogers365c1022012-06-22 15:05:28 -07001795 return ts.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
1798 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 if (utf == NULL) {
1800 return NULL;
1801 }
Ian Rogers365c1022012-06-22 15:05:28 -07001802 ScopedJniThreadState ts(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 String* result = String::AllocFromModifiedUtf8(utf);
Ian Rogers365c1022012-06-22 15:05:28 -07001804 return ts.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes814e4032011-08-23 12:07:56 -07001807 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1808 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001809 return ts.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001810 }
1811
1812 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1813 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001814 return ts.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001815 }
1816
Elliott Hughesb465ab02011-08-24 11:21:21 -07001817 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001819 String* s = ts.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001820 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1821 ThrowSIOOBE(ts, start, length, s->GetLength());
1822 } else {
1823 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1824 memcpy(buf, chars + start, length * sizeof(jchar));
1825 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001826 }
1827
Elliott Hughesb465ab02011-08-24 11:21:21 -07001828 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001829 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001830 String* s = ts.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001831 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1832 ThrowSIOOBE(ts, start, length, s->GetLength());
1833 } else {
1834 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1835 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1836 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 }
1838
Elliott Hughes75770752011-08-24 17:52:38 -07001839 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001841 String* s = ts.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 const CharArray* chars = s->GetCharArray();
1843 PinPrimitiveArray(ts, chars);
1844 if (is_copy != NULL) {
1845 *is_copy = JNI_FALSE;
1846 }
1847 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001852 UnpinPrimitiveArray(ts, ts.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001857 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes75770752011-08-24 17:52:38 -07001860 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001862 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 if (java_string == NULL) {
1868 return NULL;
1869 }
1870 if (is_copy != NULL) {
1871 *is_copy = JNI_TRUE;
1872 }
Ian Rogers365c1022012-06-22 15:05:28 -07001873 String* s = ts.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001874 size_t byte_count = s->GetUtfLength();
1875 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001876 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001877 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1878 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1879 bytes[byte_count] = '\0';
1880 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001881 }
1882
Elliott Hughes75770752011-08-24 17:52:38 -07001883 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001885 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001886 }
1887
Elliott Hughesbd935992011-08-22 11:59:34 -07001888 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001890 Object* obj = ts.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001891 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001892 Array* array = obj->AsArray();
1893 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001898 ObjectArray<Object>* array = ts.Decode<ObjectArray<Object>*>(java_array);
1899 return ts.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
1902 static void SetObjectArrayElement(JNIEnv* env,
1903 jobjectArray java_array, jsize index, jobject java_value) {
1904 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001905 ObjectArray<Object>* array = ts.Decode<ObjectArray<Object>*>(java_array);
1906 Object* value = ts.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 array->Set(index, value);
1908 }
1909
1910 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1913 }
1914
1915 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1918 }
1919
1920 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1921 ScopedJniThreadState ts(env);
1922 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1923 }
1924
1925 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1926 ScopedJniThreadState ts(env);
1927 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1928 }
1929
1930 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1931 ScopedJniThreadState ts(env);
1932 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1933 }
1934
1935 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1936 ScopedJniThreadState ts(env);
1937 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1938 }
1939
1940 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1941 ScopedJniThreadState ts(env);
1942 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1943 }
1944
1945 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1946 ScopedJniThreadState ts(env);
1947 CHECK_GE(length, 0); // TODO: ReportJniError
1948
1949 // Compute the array class corresponding to the given element class.
Ian Rogers365c1022012-06-22 15:05:28 -07001950 Class* element_class = ts.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 std::string descriptor;
1952 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001953 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001954
1955 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001956 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1957 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 return NULL;
1959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 // Allocate and initialize if necessary.
Ian Rogers365c1022012-06-22 15:05:28 -07001962 Class* array_class = ts.Decode<Class*>(java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001964 if (initial_element != NULL) {
Ian Rogers365c1022012-06-22 15:05:28 -07001965 Object* initial_object = ts.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 for (jsize i = 0; i < length; ++i) {
1967 result->Set(i, initial_object);
1968 }
1969 }
Ian Rogers365c1022012-06-22 15:05:28 -07001970 return ts.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
1973 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1974 ScopedJniThreadState ts(env);
1975 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1976 }
1977
Ian Rogersa15e67d2012-02-28 13:51:55 -08001978 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07001980 Array* array = ts.Decode<Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001981 PinPrimitiveArray(ts, array);
1982 if (is_copy != NULL) {
1983 *is_copy = JNI_FALSE;
1984 }
1985 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001986 }
1987
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001988 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002033 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002038 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002043 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002048 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002053 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002058 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002063 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002068 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07002155 Class* c = ts.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 const char* name = methods[i].name;
2159 const char* sig = methods[i].signature;
2160
2161 if (*sig == '!') {
2162 // TODO: fast jni. it's too noisy to log all these.
2163 ++sig;
2164 }
2165
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 Method* m = c->FindDirectMethod(name, sig);
2167 if (m == NULL) {
2168 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002171 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002172 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002175 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002176 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 return JNI_ERR;
2178 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002179
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002180 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181
Ian Rogers60db5ab2012-02-20 17:02:00 -08002182 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184 return JNI_OK;
2185 }
2186
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07002189 Class* c = ts.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002190
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002191 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002192
2193 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2194 Method* m = c->GetDirectMethod(i);
2195 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002196 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002197 }
2198 }
2199 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2200 Method* m = c->GetVirtualMethod(i);
2201 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002202 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002203 }
2204 }
2205
2206 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes72025e52011-08-23 17:50:30 -07002209 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07002211 Object* o = ts.Decode<Object*>(java_object);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002212 o->MonitorEnter(ts.Self());
2213 if (ts.Self()->IsExceptionPending()) {
2214 return JNI_ERR;
2215 }
2216 ts.Env()->monitors.Add(o);
2217 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughes72025e52011-08-23 17:50:30 -07002220 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -07002222 Object* o = ts.Decode<Object*>(java_object);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002223 o->MonitorExit(ts.Self());
2224 if (ts.Self()->IsExceptionPending()) {
2225 return JNI_ERR;
2226 }
2227 ts.Env()->monitors.Remove(o);
2228 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
2231 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2232 ScopedJniThreadState ts(env);
2233 Runtime* runtime = Runtime::Current();
2234 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002235 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 } else {
2237 *vm = NULL;
2238 }
2239 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2240 }
2241
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2243 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244
2245 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002246 CHECK(address != NULL); // TODO: ReportJniError
2247 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 // At the moment, the Java side is limited to 32 bits.
2250 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2251 CHECK_LE(capacity, 0xffffffff);
2252 jint address_arg = reinterpret_cast<jint>(address);
2253 jint capacity_arg = static_cast<jint>(capacity);
2254
Elliott Hugheseac76672012-05-24 21:56:51 -07002255 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2256 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2257 address_arg, capacity_arg);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002263 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Elliott Hughesb465ab02011-08-24 11:21:21 -07002266 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002268 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273
Elliott Hughes75770752011-08-24 17:52:38 -07002274 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275
2276 // Do we definitely know what kind of reference this is?
2277 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2278 IndirectRefKind kind = GetIndirectRefKind(ref);
2279 switch (kind) {
2280 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002281 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2282 return JNILocalRefType;
2283 }
2284 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002285 case kGlobal:
2286 return JNIGlobalRefType;
2287 case kWeakGlobal:
2288 return JNIWeakGlobalRefType;
2289 case kSirtOrInvalid:
2290 // Is it in a stack IRT?
Ian Rogers0399dde2012-06-06 17:09:28 -07002291 if (ts.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 return JNILocalRefType;
2293 }
2294
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002295 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002296 return JNIInvalidRefType;
2297 }
2298
Elliott Hughesb465ab02011-08-24 11:21:21 -07002299 // If we're handing out direct pointers, check whether it's a direct pointer
2300 // to a local reference.
Ian Rogers365c1022012-06-22 15:05:28 -07002301 if (ts.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002302 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002303 return JNILocalRefType;
2304 }
2305 }
2306
2307 return JNIInvalidRefType;
2308 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002309 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2310 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002313
Elliott Hughes88c5c352012-03-15 18:49:48 -07002314const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002315 NULL, // reserved0.
2316 NULL, // reserved1.
2317 NULL, // reserved2.
2318 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 JNI::GetVersion,
2320 JNI::DefineClass,
2321 JNI::FindClass,
2322 JNI::FromReflectedMethod,
2323 JNI::FromReflectedField,
2324 JNI::ToReflectedMethod,
2325 JNI::GetSuperclass,
2326 JNI::IsAssignableFrom,
2327 JNI::ToReflectedField,
2328 JNI::Throw,
2329 JNI::ThrowNew,
2330 JNI::ExceptionOccurred,
2331 JNI::ExceptionDescribe,
2332 JNI::ExceptionClear,
2333 JNI::FatalError,
2334 JNI::PushLocalFrame,
2335 JNI::PopLocalFrame,
2336 JNI::NewGlobalRef,
2337 JNI::DeleteGlobalRef,
2338 JNI::DeleteLocalRef,
2339 JNI::IsSameObject,
2340 JNI::NewLocalRef,
2341 JNI::EnsureLocalCapacity,
2342 JNI::AllocObject,
2343 JNI::NewObject,
2344 JNI::NewObjectV,
2345 JNI::NewObjectA,
2346 JNI::GetObjectClass,
2347 JNI::IsInstanceOf,
2348 JNI::GetMethodID,
2349 JNI::CallObjectMethod,
2350 JNI::CallObjectMethodV,
2351 JNI::CallObjectMethodA,
2352 JNI::CallBooleanMethod,
2353 JNI::CallBooleanMethodV,
2354 JNI::CallBooleanMethodA,
2355 JNI::CallByteMethod,
2356 JNI::CallByteMethodV,
2357 JNI::CallByteMethodA,
2358 JNI::CallCharMethod,
2359 JNI::CallCharMethodV,
2360 JNI::CallCharMethodA,
2361 JNI::CallShortMethod,
2362 JNI::CallShortMethodV,
2363 JNI::CallShortMethodA,
2364 JNI::CallIntMethod,
2365 JNI::CallIntMethodV,
2366 JNI::CallIntMethodA,
2367 JNI::CallLongMethod,
2368 JNI::CallLongMethodV,
2369 JNI::CallLongMethodA,
2370 JNI::CallFloatMethod,
2371 JNI::CallFloatMethodV,
2372 JNI::CallFloatMethodA,
2373 JNI::CallDoubleMethod,
2374 JNI::CallDoubleMethodV,
2375 JNI::CallDoubleMethodA,
2376 JNI::CallVoidMethod,
2377 JNI::CallVoidMethodV,
2378 JNI::CallVoidMethodA,
2379 JNI::CallNonvirtualObjectMethod,
2380 JNI::CallNonvirtualObjectMethodV,
2381 JNI::CallNonvirtualObjectMethodA,
2382 JNI::CallNonvirtualBooleanMethod,
2383 JNI::CallNonvirtualBooleanMethodV,
2384 JNI::CallNonvirtualBooleanMethodA,
2385 JNI::CallNonvirtualByteMethod,
2386 JNI::CallNonvirtualByteMethodV,
2387 JNI::CallNonvirtualByteMethodA,
2388 JNI::CallNonvirtualCharMethod,
2389 JNI::CallNonvirtualCharMethodV,
2390 JNI::CallNonvirtualCharMethodA,
2391 JNI::CallNonvirtualShortMethod,
2392 JNI::CallNonvirtualShortMethodV,
2393 JNI::CallNonvirtualShortMethodA,
2394 JNI::CallNonvirtualIntMethod,
2395 JNI::CallNonvirtualIntMethodV,
2396 JNI::CallNonvirtualIntMethodA,
2397 JNI::CallNonvirtualLongMethod,
2398 JNI::CallNonvirtualLongMethodV,
2399 JNI::CallNonvirtualLongMethodA,
2400 JNI::CallNonvirtualFloatMethod,
2401 JNI::CallNonvirtualFloatMethodV,
2402 JNI::CallNonvirtualFloatMethodA,
2403 JNI::CallNonvirtualDoubleMethod,
2404 JNI::CallNonvirtualDoubleMethodV,
2405 JNI::CallNonvirtualDoubleMethodA,
2406 JNI::CallNonvirtualVoidMethod,
2407 JNI::CallNonvirtualVoidMethodV,
2408 JNI::CallNonvirtualVoidMethodA,
2409 JNI::GetFieldID,
2410 JNI::GetObjectField,
2411 JNI::GetBooleanField,
2412 JNI::GetByteField,
2413 JNI::GetCharField,
2414 JNI::GetShortField,
2415 JNI::GetIntField,
2416 JNI::GetLongField,
2417 JNI::GetFloatField,
2418 JNI::GetDoubleField,
2419 JNI::SetObjectField,
2420 JNI::SetBooleanField,
2421 JNI::SetByteField,
2422 JNI::SetCharField,
2423 JNI::SetShortField,
2424 JNI::SetIntField,
2425 JNI::SetLongField,
2426 JNI::SetFloatField,
2427 JNI::SetDoubleField,
2428 JNI::GetStaticMethodID,
2429 JNI::CallStaticObjectMethod,
2430 JNI::CallStaticObjectMethodV,
2431 JNI::CallStaticObjectMethodA,
2432 JNI::CallStaticBooleanMethod,
2433 JNI::CallStaticBooleanMethodV,
2434 JNI::CallStaticBooleanMethodA,
2435 JNI::CallStaticByteMethod,
2436 JNI::CallStaticByteMethodV,
2437 JNI::CallStaticByteMethodA,
2438 JNI::CallStaticCharMethod,
2439 JNI::CallStaticCharMethodV,
2440 JNI::CallStaticCharMethodA,
2441 JNI::CallStaticShortMethod,
2442 JNI::CallStaticShortMethodV,
2443 JNI::CallStaticShortMethodA,
2444 JNI::CallStaticIntMethod,
2445 JNI::CallStaticIntMethodV,
2446 JNI::CallStaticIntMethodA,
2447 JNI::CallStaticLongMethod,
2448 JNI::CallStaticLongMethodV,
2449 JNI::CallStaticLongMethodA,
2450 JNI::CallStaticFloatMethod,
2451 JNI::CallStaticFloatMethodV,
2452 JNI::CallStaticFloatMethodA,
2453 JNI::CallStaticDoubleMethod,
2454 JNI::CallStaticDoubleMethodV,
2455 JNI::CallStaticDoubleMethodA,
2456 JNI::CallStaticVoidMethod,
2457 JNI::CallStaticVoidMethodV,
2458 JNI::CallStaticVoidMethodA,
2459 JNI::GetStaticFieldID,
2460 JNI::GetStaticObjectField,
2461 JNI::GetStaticBooleanField,
2462 JNI::GetStaticByteField,
2463 JNI::GetStaticCharField,
2464 JNI::GetStaticShortField,
2465 JNI::GetStaticIntField,
2466 JNI::GetStaticLongField,
2467 JNI::GetStaticFloatField,
2468 JNI::GetStaticDoubleField,
2469 JNI::SetStaticObjectField,
2470 JNI::SetStaticBooleanField,
2471 JNI::SetStaticByteField,
2472 JNI::SetStaticCharField,
2473 JNI::SetStaticShortField,
2474 JNI::SetStaticIntField,
2475 JNI::SetStaticLongField,
2476 JNI::SetStaticFloatField,
2477 JNI::SetStaticDoubleField,
2478 JNI::NewString,
2479 JNI::GetStringLength,
2480 JNI::GetStringChars,
2481 JNI::ReleaseStringChars,
2482 JNI::NewStringUTF,
2483 JNI::GetStringUTFLength,
2484 JNI::GetStringUTFChars,
2485 JNI::ReleaseStringUTFChars,
2486 JNI::GetArrayLength,
2487 JNI::NewObjectArray,
2488 JNI::GetObjectArrayElement,
2489 JNI::SetObjectArrayElement,
2490 JNI::NewBooleanArray,
2491 JNI::NewByteArray,
2492 JNI::NewCharArray,
2493 JNI::NewShortArray,
2494 JNI::NewIntArray,
2495 JNI::NewLongArray,
2496 JNI::NewFloatArray,
2497 JNI::NewDoubleArray,
2498 JNI::GetBooleanArrayElements,
2499 JNI::GetByteArrayElements,
2500 JNI::GetCharArrayElements,
2501 JNI::GetShortArrayElements,
2502 JNI::GetIntArrayElements,
2503 JNI::GetLongArrayElements,
2504 JNI::GetFloatArrayElements,
2505 JNI::GetDoubleArrayElements,
2506 JNI::ReleaseBooleanArrayElements,
2507 JNI::ReleaseByteArrayElements,
2508 JNI::ReleaseCharArrayElements,
2509 JNI::ReleaseShortArrayElements,
2510 JNI::ReleaseIntArrayElements,
2511 JNI::ReleaseLongArrayElements,
2512 JNI::ReleaseFloatArrayElements,
2513 JNI::ReleaseDoubleArrayElements,
2514 JNI::GetBooleanArrayRegion,
2515 JNI::GetByteArrayRegion,
2516 JNI::GetCharArrayRegion,
2517 JNI::GetShortArrayRegion,
2518 JNI::GetIntArrayRegion,
2519 JNI::GetLongArrayRegion,
2520 JNI::GetFloatArrayRegion,
2521 JNI::GetDoubleArrayRegion,
2522 JNI::SetBooleanArrayRegion,
2523 JNI::SetByteArrayRegion,
2524 JNI::SetCharArrayRegion,
2525 JNI::SetShortArrayRegion,
2526 JNI::SetIntArrayRegion,
2527 JNI::SetLongArrayRegion,
2528 JNI::SetFloatArrayRegion,
2529 JNI::SetDoubleArrayRegion,
2530 JNI::RegisterNatives,
2531 JNI::UnregisterNatives,
2532 JNI::MonitorEnter,
2533 JNI::MonitorExit,
2534 JNI::GetJavaVM,
2535 JNI::GetStringRegion,
2536 JNI::GetStringUTFRegion,
2537 JNI::GetPrimitiveArrayCritical,
2538 JNI::ReleasePrimitiveArrayCritical,
2539 JNI::GetStringCritical,
2540 JNI::ReleaseStringCritical,
2541 JNI::NewWeakGlobalRef,
2542 JNI::DeleteWeakGlobalRef,
2543 JNI::ExceptionCheck,
2544 JNI::NewDirectByteBuffer,
2545 JNI::GetDirectBufferAddress,
2546 JNI::GetDirectBufferCapacity,
2547 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002548};
2549
Elliott Hughes75770752011-08-24 17:52:38 -07002550JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002551 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002552 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002553 local_ref_cookie(IRT_FIRST_SEGMENT),
2554 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002555 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002556 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002557 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002558 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002559 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002560 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002561 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002562 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2563 // errors will ensue.
2564 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2565 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002566}
2567
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002568JNIEnvExt::~JNIEnvExt() {
2569}
2570
Elliott Hughes88c5c352012-03-15 18:49:48 -07002571void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2572 check_jni = enabled;
2573 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002574}
2575
Elliott Hughes73e66f72012-05-09 09:34:45 -07002576void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2577 locals.Dump(os);
2578 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002579}
2580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002581void JNIEnvExt::PushFrame(int /*capacity*/) {
2582 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002583 stacked_local_ref_cookies.push_back(local_ref_cookie);
2584 local_ref_cookie = locals.GetSegmentState();
2585}
2586
2587void JNIEnvExt::PopFrame() {
2588 locals.SetSegmentState(local_ref_cookie);
2589 local_ref_cookie = stacked_local_ref_cookies.back();
2590 stacked_local_ref_cookies.pop_back();
2591}
2592
Carl Shapiroea4dca82011-08-01 13:45:38 -07002593// JNI Invocation interface.
2594
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2596 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2597 if (args->version < JNI_VERSION_1_2) {
2598 return JNI_EVERSION;
2599 }
2600 Runtime::Options options;
2601 for (int i = 0; i < args->nOptions; ++i) {
2602 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002603 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604 }
2605 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002606 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002607 if (runtime == NULL) {
2608 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002610 runtime->Start();
2611 *p_env = Thread::Current()->GetJniEnv();
2612 *p_vm = runtime->GetJavaVM();
2613 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614}
2615
Elliott Hughesf2682d52011-08-15 16:37:04 -07002616extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 Runtime* runtime = Runtime::Current();
2618 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002619 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002620 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002621 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002622 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002623 }
2624 return JNI_OK;
2625}
2626
2627// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002628extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 return JNI_ERR;
2630}
2631
Elliott Hughescdf53122011-08-19 15:46:09 -07002632class JII {
2633 public:
2634 static jint DestroyJavaVM(JavaVM* vm) {
2635 if (vm == NULL) {
2636 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002637 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002638 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2639 delete raw_vm->runtime;
2640 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642
Elliott Hughescdf53122011-08-19 15:46:09 -07002643 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002644 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002645 }
2646
2647 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002648 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002649 }
2650
2651 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002652 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002653 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002654 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002655 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2656 Runtime* runtime = raw_vm->runtime;
2657 runtime->DetachCurrentThread();
2658 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002659 }
2660
2661 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2662 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2663 return JNI_EVERSION;
2664 }
2665 if (vm == NULL || env == NULL) {
2666 return JNI_ERR;
2667 }
2668 Thread* thread = Thread::Current();
2669 if (thread == NULL) {
2670 *env = NULL;
2671 return JNI_EDETACHED;
2672 }
2673 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002674 return JNI_OK;
2675 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002676};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002677
Elliott Hughes88c5c352012-03-15 18:49:48 -07002678const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002679 NULL, // reserved0
2680 NULL, // reserved1
2681 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 JII::DestroyJavaVM,
2683 JII::AttachCurrentThread,
2684 JII::DetachCurrentThread,
2685 JII::GetEnv,
2686 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002687};
2688
Elliott Hughesa0957642011-09-02 14:27:33 -07002689JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002690 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002691 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002692 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002693 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002694 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002695 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002696 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002697 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002698 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002699 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002700 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002701 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002702 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002703 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002704 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002705 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002706 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002707 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002708 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002709}
2710
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002711JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002712 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002713}
2714
Elliott Hughes88c5c352012-03-15 18:49:48 -07002715void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2716 check_jni = enabled;
2717 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002718}
2719
Elliott Hughesae80b492012-04-24 10:43:17 -07002720void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2721 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2722 if (force_copy) {
2723 os << " (with forcecopy)";
2724 }
2725 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2726 {
2727 MutexLock mu(pins_lock);
2728 os << "; pins=" << pin_table.Size();
2729 }
2730 {
2731 MutexLock mu(globals_lock);
2732 os << "; globals=" << globals.Capacity();
2733 }
2734 {
2735 MutexLock mu(weak_globals_lock);
2736 if (weak_globals.Capacity() > 0) {
2737 os << " (plus " << weak_globals.Capacity() << " weak)";
2738 }
2739 }
2740 os << '\n';
2741
2742 {
2743 MutexLock mu(libraries_lock);
2744 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2745 }
2746}
2747
Elliott Hughes73e66f72012-05-09 09:34:45 -07002748void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002749 {
2750 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002751 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002752 }
2753 {
2754 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002755 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002756 }
2757 {
2758 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002759 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002760 }
2761}
2762
Elliott Hughes75770752011-08-24 17:52:38 -07002763bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2764 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002765
2766 // See if we've already loaded this library. If we have, and the class loader
2767 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002768 // TODO: for better results we should canonicalize the pathname (or even compare
2769 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002770 SharedLibrary* library;
2771 {
2772 // TODO: move the locking (and more of this logic) into Libraries.
2773 MutexLock mu(libraries_lock);
2774 library = libraries->Get(path);
2775 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002776 if (library != NULL) {
2777 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002778 // The library will be associated with class_loader. The JNI
2779 // spec says we can't load the same library into more than one
2780 // class loader.
2781 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2782 "ClassLoader %p; can't open in ClassLoader %p",
2783 path.c_str(), library->GetClassLoader(), class_loader);
2784 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002785 return false;
2786 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002787 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2788 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002789 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002790 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2791 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 return false;
2793 }
2794 return true;
2795 }
2796
2797 // Open the shared library. Because we're using a full path, the system
2798 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2799 // resolve this library's dependencies though.)
2800
2801 // Failures here are expected when java.library.path has several entries
2802 // and we have to hunt for the lib.
2803
2804 // The current version of the dynamic linker prints detailed information
2805 // about dlopen() failures. Some things to check if the message is
2806 // cryptic:
2807 // - make sure the library exists on the device
2808 // - verify that the right path is being opened (the debug log message
2809 // above can help with that)
2810 // - check to see if the library is valid (e.g. not zero bytes long)
2811 // - check config/prelink-linux-arm.map to ensure that the library
2812 // is listed and is not being overrun by the previous entry (if
2813 // loading suddenly stops working on a prelinked library, this is
2814 // a good one to check)
2815 // - write a trivial app that calls sleep() then dlopen(), attach
2816 // to it with "strace -p <pid>" while it sleeps, and watch for
2817 // attempts to open nonexistent dependent shared libs
2818
2819 // TODO: automate some of these checks!
2820
2821 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002822 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002823 // the GC to ignore us.
2824 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002825 void* handle = NULL;
2826 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002827 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002828 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002829 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002830
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002831 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002832
2833 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002834 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002835 return false;
2836 }
2837
2838 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002839 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002840 // TODO: move the locking (and more of this logic) into Libraries.
2841 MutexLock mu(libraries_lock);
2842 library = libraries->Get(path);
2843 if (library != NULL) {
2844 LOG(INFO) << "WOW: we lost a race to add shared library: "
2845 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002846 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002847 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002848 library = new SharedLibrary(path, handle, class_loader);
2849 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002851
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002852 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002853
2854 bool result = true;
2855 void* sym = dlsym(handle, "JNI_OnLoad");
2856 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002857 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002858 } else {
2859 // Call JNI_OnLoad. We have to override the current class
2860 // loader, which will always be "null" since the stuff at the
2861 // top of the stack is around Runtime.loadLibrary(). (See
2862 // the comments in the JNI FindClass function.)
2863 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2864 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002865 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002866 self->SetClassLoaderOverride(class_loader);
2867
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002868 int version = 0;
2869 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002870 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002871 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002872 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002873 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002874
Brian Carlstromaded5f72011-10-07 17:15:04 -07002875 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002876
2877 if (version != JNI_VERSION_1_2 &&
2878 version != JNI_VERSION_1_4 &&
2879 version != JNI_VERSION_1_6) {
2880 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2881 << "bad version: " << version;
2882 // It's unwise to call dlclose() here, but we can mark it
2883 // as bad and ensure that future load attempts will fail.
2884 // We don't know how far JNI_OnLoad got, so there could
2885 // be some partially-initialized stuff accessible through
2886 // newly-registered native method calls. We could try to
2887 // unregister them, but that doesn't seem worthwhile.
2888 result = false;
2889 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002890 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2891 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002892 }
2893 }
2894
2895 library->SetResult(result);
2896 return result;
2897}
2898
2899void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2900 CHECK(m->IsNative());
2901
2902 Class* c = m->GetDeclaringClass();
2903
2904 // If this is a static method, it could be called before the class
2905 // has been initialized.
2906 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002907 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002908 return NULL;
2909 }
2910 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002911 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002912 }
2913
Brian Carlstrom16192862011-09-12 17:50:06 -07002914 std::string detail;
2915 void* native_method;
2916 {
2917 MutexLock mu(libraries_lock);
2918 native_method = libraries->FindNativeMethod(m, detail);
2919 }
2920 // throwing can cause libraries_lock to be reacquired
2921 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002922 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002923 }
2924 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002925}
2926
Elliott Hughes410c0c82011-09-01 17:58:25 -07002927void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2928 {
2929 MutexLock mu(globals_lock);
2930 globals.VisitRoots(visitor, arg);
2931 }
2932 {
2933 MutexLock mu(pins_lock);
2934 pin_table.VisitRoots(visitor, arg);
2935 }
2936 // The weak_globals table is visited by the GC itself (because it mutates the table).
2937}
2938
Ian Rogersdf20fe02011-07-20 20:34:16 -07002939} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002940
2941std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2942 switch (rhs) {
2943 case JNIInvalidRefType:
2944 os << "JNIInvalidRefType";
2945 return os;
2946 case JNILocalRefType:
2947 os << "JNILocalRefType";
2948 return os;
2949 case JNIGlobalRefType:
2950 os << "JNIGlobalRefType";
2951 return os;
2952 case JNIWeakGlobalRefType:
2953 os << "JNIWeakGlobalRefType";
2954 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002955 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002956 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002957 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002958 }
2959}