blob: 22438a0147177c1dd3f03883272210a9b3c5aee3 [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "mutex.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070036#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070037#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
60 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070061 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
62 if (c.get() == NULL) {
63 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
64 }
65 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
66 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
67 }
68}
69
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070void SetJniGlobalsMax(size_t max) {
71 if (max != 0) {
72 gGlobalsMax = max;
73 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
74 }
75}
Ian Rogersdf20fe02011-07-20 20:34:16 -070076
Ian Rogers45619fc2012-02-29 11:15:25 -080077size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
78 size_t num_bytes = 0;
79 for (size_t i = 1; i < shorty_len; ++i) {
80 char ch = shorty[i];
81 if (ch == 'D' || ch == 'J') {
82 num_bytes += 8;
83 } else if (ch == 'L') {
84 // Argument is a reference or an array. The shorty descriptor
85 // does not distinguish between these types.
86 num_bytes += sizeof(Object*);
87 } else {
88 num_bytes += 4;
89 }
90 }
91 return num_bytes;
92}
93
94class ArgArray {
95 public:
Mathieu Chartier66f19252012-09-18 08:57:04 -070096 explicit ArgArray(AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers45619fc2012-02-29 11:15:25 -080097 MethodHelper mh(method);
98 shorty_ = mh.GetShorty();
99 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700100 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800101 arg_array_ = small_arg_array_;
102 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700103 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800104 arg_array_ = large_arg_array_.get();
105 }
106 }
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800109 return arg_array_;
110 }
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void BuildArgArray(const ScopedObjectAccess& soa, va_list ap)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700114 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800115 switch (shorty_[i]) {
116 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700117 arg_array_[offset].SetZ(va_arg(ap, jint));
118 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800119 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700120 arg_array_[offset].SetB(va_arg(ap, jint));
121 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800122 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700123 arg_array_[offset].SetC(va_arg(ap, jint));
124 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800125 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 arg_array_[offset].SetS(va_arg(ap, jint));
127 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800128 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800130 break;
131 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800133 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700134 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 arg_array_[offset].SetL(soa.Decode<Object*>(va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800136 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800137 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800139 break;
140 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700141 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800142 break;
143 }
144 }
145 }
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void BuildArgArray(const ScopedObjectAccess& soa, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700149 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800150 switch (shorty_[i]) {
151 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 arg_array_[offset].SetZ(args[offset].z);
153 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800154 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700155 arg_array_[offset].SetB(args[offset].b);
156 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800157 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700158 arg_array_[offset].SetC(args[offset].c);
159 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800160 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 arg_array_[offset].SetS(args[offset].s);
162 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800163 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800165 break;
166 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800168 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700169 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 arg_array_[offset].SetL(soa.Decode<Object*>(args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800171 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800172 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 break;
175 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 break;
178 }
179 }
180 }
181
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700183 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800184 const char* shorty_;
185 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700186 JValue* arg_array_;
187 JValue small_arg_array_[kSmallArgArraySize];
188 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800189};
190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700193 if (obj == NULL) {
194 return NULL;
195 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700197 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700198 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700199 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
200 return reinterpret_cast<jweak>(ref);
201}
202
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700205 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700206 const DexFile::TypeList* params = mh.GetParameterTypeList();
207 if (params == NULL) {
208 return; // No arguments so nothing to check.
209 }
210 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700211 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700212 for (uint32_t i = 0; i < num_params; i++) {
213 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
214 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
215 if (param_type == NULL) {
216 Thread* self = Thread::Current();
217 CHECK(self->IsExceptionPending());
218 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
219 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
220 << self->GetException()->Dump();
221 self->ClearException();
222 ++error_count;
223 } else if (!param_type->IsPrimitive()) {
224 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700225 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700226 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700227 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
228 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
229 ++error_count;
230 }
231 }
232 }
233 if (error_count > 0) {
234 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
235 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700236 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700237 }
238}
Elliott Hughesb264f082012-04-06 17:10:10 -0700239
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700241 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700244 CheckMethodArguments(method, args);
245 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700246 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700248 return result;
249}
250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
252 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700255 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800256 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 arg_array.BuildArgArray(soa, args);
258 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700259}
260
Mathieu Chartier66f19252012-09-18 08:57:04 -0700261static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700263 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700264}
265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
267 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700270 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800271 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 arg_array.BuildArgArray(soa, args);
273 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700274}
275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
277 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700280 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800281 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 arg_array.BuildArgArray(soa, args);
283 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700284}
285
Elliott Hughes6b436852011-08-12 10:16:44 -0700286// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
287// separated with slashes but aren't wrapped with "L;" like regular descriptors
288// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
289// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
290// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700291static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700292 std::string result;
293 // Add the missing "L;" if necessary.
294 if (name[0] == '[') {
295 result = name;
296 } else {
297 result += 'L';
298 result += name;
299 result += ';';
300 }
301 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700302 if (result.find('.') != std::string::npos) {
303 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
304 << "\"" << name << "\"";
305 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700306 }
307 return result;
308}
309
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
311 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800314 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
318 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700321 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700322 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700323 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700324
Mathieu Chartier66f19252012-09-18 08:57:04 -0700325 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700326 if (is_static) {
327 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700328 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700329 method = c->FindVirtualMethod(name, sig);
330 if (method == NULL) {
331 // No virtual method matching the signature. Search declared
332 // private methods and constructors.
333 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700336
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 return NULL;
340 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343}
344
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345static ClassLoader* GetClassLoader(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700347 AbstractMethod* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700348 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
349 return self->GetClassLoaderOverride();
350 }
351 return method->GetDeclaringClass()->GetClassLoader();
352}
353
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
355 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700358 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700359 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700360 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700361
362 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 Class* field_type;
364 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
365 if (sig[1] != '\0') {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 ClassLoader* cl = GetClassLoader(soa.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700368 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700370 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 if (field_type == NULL) {
372 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373 DCHECK(soa.Self()->IsExceptionPending());
374 soa.Self()->ClearException();
375 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700376 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800377 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 return NULL;
379 }
380 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700385 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700387 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800388 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700389 return NULL;
390 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700392}
393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700397 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700398 vm->pin_table.Add(array);
399}
400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700404 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700405 vm->pin_table.Remove(array);
406}
407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
409 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700411 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700413 "%s offset=%d length=%d %s.length=%d",
414 type.c_str(), start, length, identifier, array->GetLength());
415}
Ian Rogers0571d352011-11-03 19:51:38 -0700416
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
418 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700421 "offset=%d length=%d string.length()=%d", start, length, array_length);
422}
Elliott Hughes814e4032011-08-23 12:07:56 -0700423
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 ScopedObjectAccess soa(env);
427
428 // Turn the const char* into a java.lang.String.
429 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
430 if (msg != NULL && s.get() == NULL) {
431 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700432 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700433
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 // Choose an appropriate constructor and set up the arguments.
435 jvalue args[2];
436 const char* signature;
437 if (msg == NULL && cause == NULL) {
438 signature = "()V";
439 } else if (msg != NULL && cause == NULL) {
440 signature = "(Ljava/lang/String;)V";
441 args[0].l = s.get();
442 } else if (msg == NULL && cause != NULL) {
443 signature = "(Ljava/lang/Throwable;)V";
444 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700445 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
447 args[0].l = s.get();
448 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700449 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
451 if (mid == NULL) {
452 LOG(ERROR) << "No <init>" << signature << " in "
453 << PrettyClass(soa.Decode<Class*>(exception_class));
454 return JNI_ERR;
455 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700456
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
458 if (exception.get() == NULL) {
459 return JNI_ERR;
460 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700461
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700463
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700465}
466
Elliott Hughes462c9442012-03-23 18:47:50 -0700467static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700468 if (vm == NULL || p_env == NULL) {
469 return JNI_ERR;
470 }
471
Elliott Hughes462c9442012-03-23 18:47:50 -0700472 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700473 Thread* self = Thread::Current();
474 if (self != NULL) {
475 *p_env = self->GetJniEnv();
476 return JNI_OK;
477 }
478
479 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
480
481 // No threads allowed in zygote mode.
482 if (runtime->IsZygote()) {
483 LOG(ERROR) << "Attempt to attach a thread in the zygote";
484 return JNI_ERR;
485 }
486
Elliott Hughes462c9442012-03-23 18:47:50 -0700487 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
488 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700489 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700490 if (args != NULL) {
491 CHECK_GE(args->version, JNI_VERSION_1_2);
492 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700493 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700494 }
Elliott Hughes75770752011-08-24 17:52:38 -0700495
Ian Rogers120f1c72012-09-28 17:17:10 -0700496 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group)) {
497 *p_env = NULL;
498 return JNI_ERR;
499 } else {
500 *p_env = Thread::Current()->GetJniEnv();
501 return JNI_OK;
502 }
Elliott Hughes75770752011-08-24 17:52:38 -0700503}
504
Elliott Hughes79082e32011-08-25 12:07:32 -0700505class SharedLibrary {
506 public:
507 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
508 : path_(path),
509 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700510 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700511 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700512 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 }
516
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 Object* GetClassLoader() {
518 return class_loader_;
519 }
520
521 std::string GetPath() {
522 return path_;
523 }
524
525 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700526 * Check the result of an earlier call to JNI_OnLoad on this library.
527 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 bool CheckOnLoadResult()
530 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
534 bool okay;
535 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700536 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
539 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
540 // caller can continue.
541 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
542 okay = true;
543 } else {
544 while (jni_on_load_result_ == kPending) {
545 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogers81d425b2012-09-27 16:03:43 -0700546 jni_on_load_cond_.Wait(self, jni_on_load_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700548
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 okay = (jni_on_load_result_ == kOkay);
550 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
551 << (okay ? "succeeded" : "failed") << "]";
552 }
553 }
554 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 return okay;
556 }
557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700559 MutexLock mu(Thread::Current(), jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700560
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700562 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700563
564 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -0700565 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 }
567
568 void* FindSymbol(const std::string& symbol_name) {
569 return dlsym(handle_, symbol_name.c_str());
570 }
571
572 private:
573 enum JNI_OnLoadState {
574 kPending,
575 kFailed,
576 kOkay,
577 };
578
579 // Path to library "/system/lib/libjni.so".
580 std::string path_;
581
582 // The void* returned by dlopen(3).
583 void* handle_;
584
585 // The ClassLoader this library is associated with.
586 Object* class_loader_;
587
588 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700591 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700593 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700595 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700596};
597
Elliott Hughes79082e32011-08-25 12:07:32 -0700598// This exists mainly to keep implementation details out of the header file.
599class Libraries {
600 public:
601 Libraries() {
602 }
603
604 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700605 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700606 }
607
Elliott Hughesae80b492012-04-24 10:43:17 -0700608 void Dump(std::ostream& os) const {
609 bool first = true;
610 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
611 if (!first) {
612 os << ' ';
613 }
614 first = false;
615 os << it->first;
616 }
617 }
618
619 size_t size() const {
620 return libraries_.size();
621 }
622
Elliott Hughes79082e32011-08-25 12:07:32 -0700623 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700624 It it = libraries_.find(path);
625 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 }
627
628 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700629 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700630 }
631
632 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700633 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700634 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700635 std::string jni_short_name(JniShortName(m));
636 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700637 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700638 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
639 SharedLibrary* library = it->second;
640 if (library->GetClassLoader() != declaring_class_loader) {
641 // We only search libraries loaded by the appropriate ClassLoader.
642 continue;
643 }
644 // Try the short name then the long name...
645 void* fn = library->FindSymbol(jni_short_name);
646 if (fn == NULL) {
647 fn = library->FindSymbol(jni_long_name);
648 }
649 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800650 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
651 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700652 return fn;
653 }
654 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700655 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700656 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700657 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700658 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700659 return NULL;
660 }
661
662 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700663 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700664
Elliott Hughesa0e18062012-04-13 15:59:59 -0700665 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700666};
667
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
669 jvalue* args) {
670 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700671 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800672 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 arg_array.BuildArgArray(soa, args);
674 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700675}
676
Mathieu Chartier66f19252012-09-18 08:57:04 -0700677JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800681}
682
Elliott Hughescdf53122011-08-19 15:46:09 -0700683class JNI {
684 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700685 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 return JNI_VERSION_1_6;
687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Ian Rogers25e8b912012-09-07 11:31:36 -0700689 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700691 return NULL;
692 }
693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700696 Runtime* runtime = Runtime::Current();
697 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700699 Class* c = NULL;
700 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800702 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700703 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800704 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700705 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700711 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccess soa(env);
717 Field* field = soa.Decode<Field*>(java_field);
718 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700723 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700726
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
729 Field* field = soa.DecodeField(fid);
730 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 ScopedObjectAccess soa(env);
735 Object* o = soa.Decode<Object*>(java_object);
736 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 }
738
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700739 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 ScopedObjectAccess soa(env);
741 Class* c = soa.Decode<Class*>(java_class);
742 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 ScopedObjectAccess soa(env);
747 Class* c1 = soa.Decode<Class*>(java_class1);
748 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700751
Elliott Hughese84278b2012-03-22 10:06:53 -0700752 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700754 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700755 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700756 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 return JNI_TRUE;
758 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 Object* obj = soa.Decode<Object*>(jobj);
760 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700761 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700764
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
767 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 if (exception == NULL) {
769 return JNI_ERR;
770 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700772 return JNI_OK;
773 }
774
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700775 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700776 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 }
778
779 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700780 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 }
782
783 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700784 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700785 }
786
787 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700789
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 Throwable* original_exception = self->GetException();
792 self->ClearException();
793
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
796 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
797 if (mid == NULL) {
798 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700799 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700800 } else {
801 env->CallVoidMethod(exception.get(), mid);
802 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700803 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 << " thrown while calling printStackTrace";
805 self->ClearException();
806 }
807 }
808
809 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700811
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(env);
814 Object* exception = soa.Self()->GetException();
815 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 }
817
Ian Rogers25e8b912012-09-07 11:31:36 -0700818 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 LOG(FATAL) << "JNI FatalError called: " << msg;
820 }
821
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700822 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 ScopedObjectAccess soa(env);
824 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700825 return JNI_ERR;
826 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 return JNI_OK;
829 }
830
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 ScopedObjectAccess soa(env);
833 Object* survivor = soa.Decode<Object*>(java_survivor);
834 soa.Env()->PopFrame();
835 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 }
837
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700838 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 ScopedObjectAccess soa(env);
840 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 }
842
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 if (obj == NULL) {
845 return NULL;
846 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700847 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700851 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 return reinterpret_cast<jobject>(ref);
854 }
855
856 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 if (obj == NULL) {
858 return;
859 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700860 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 IndirectReferenceTable& globals = vm->globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700863 MutexLock mu(soa.Self(), vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700864
865 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
866 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
867 << "failed to find entry";
868 }
869 }
870
871 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 ScopedObjectAccess soa(env);
873 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
876 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 if (obj == NULL) {
878 return;
879 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700880 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700881 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700883 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884
885 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
886 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
887 << "failed to find entry";
888 }
889 }
890
891 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 if (obj == NULL) {
893 return NULL;
894 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700895 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700897
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 uint32_t cookie = soa.Env()->local_ref_cookie;
899 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 return reinterpret_cast<jobject>(ref);
901 }
902
903 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 if (obj == NULL) {
905 return;
906 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700907 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700909
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 if (!locals.Remove(cookie, obj)) {
912 // Attempting to delete a local reference that is not in the
913 // topmost local reference frame is a no-op. DeleteLocalRef returns
914 // void and doesn't throw any exceptions, but we should probably
915 // complain about it so the user will notice that things aren't
916 // going quite the way they expect.
917 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
918 << "failed to find entry";
919 }
920 }
921
922 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700924 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
929 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700930 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700931 return NULL;
932 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700933 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughese84278b2012-03-22 10:06:53 -0700936 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700939 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 va_end(args);
941 return result;
942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 ScopedObjectAccess soa(env);
946 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700947 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700948 return NULL;
949 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700950 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700951 if (result == NULL) {
952 return NULL;
953 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700957 return local_result;
958 } else {
959 return NULL;
960 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 ScopedObjectAccess soa(env);
965 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700966 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700967 return NULL;
968 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700969 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700970 if (result == NULL) {
971 return NULL;
972 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700976 return local_result;
977 } else {
978 return NULL;
979 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(env);
984 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
987 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 ScopedObjectAccess soa(env);
989 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_list ap;
994 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
1003 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1004 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1010 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_list ap;
1015 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001016 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001019 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 ScopedObjectAccess soa(env);
1024 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
1029 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001038 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
1043 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 ScopedObjectAccess soa(env);
1048 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_list ap;
1053 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001054 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001057 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
1062 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066 ScopedObjectAccess soa(env);
1067 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_list ap;
1072 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001073 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001076 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
1081 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001095 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(env);
1100 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 ScopedObjectAccess soa(env);
1105 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_list ap;
1110 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001111 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001114 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 ScopedObjectAccess soa(env);
1119 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 ScopedObjectAccess soa(env);
1124 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_list ap;
1129 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001130 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001133 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_list ap;
1148 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001149 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001152 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_list ap;
1167 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001168 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001169 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
1175 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 ScopedObjectAccess soa(env);
1180 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001183 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001186 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1188 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_end(ap);
1190 return local_result;
1191 }
1192
1193 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
1196 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1197 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
1200 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001201 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
1203 JValue result(InvokeWithJValues(soa, obj, mid, args));
1204 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001211 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001214 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
1217 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001218 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
1220 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
1223 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
1226 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001232 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001235 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
1238 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001239 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
1241 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
1244 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
1247 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001256 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001260 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env);
1262 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
1268 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001274 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001277 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001281 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001287 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001288 ScopedObjectAccess soa(env);
1289 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001292 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001295 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001298 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
1301 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001302 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedObjectAccess soa(env);
1304 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
1307 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
1310 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001316 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001319 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 ScopedObjectAccess soa(env);
1325 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 ScopedObjectAccess soa(env);
1331 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001337 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001340 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001344 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 ScopedObjectAccess soa(env);
1346 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
1349 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001350 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 ScopedObjectAccess soa(env);
1352 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001355 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001358 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001361 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
1364 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001365 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
1370 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001371 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 ScopedObjectAccess soa(env);
1373 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001376 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001378 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001379 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 va_end(ap);
1382 }
1383
1384 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 ScopedObjectAccess soa(env);
1387 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
1390 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 ScopedObjectAccess soa(env);
1393 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001396 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 ScopedObjectAccess soa(env);
1398 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001400
1401
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001402 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env);
1409 Object* o = soa.Decode<Object*>(obj);
1410 Field* f = soa.DecodeField(fid);
1411 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001415 ScopedObjectAccess soa(env);
1416 Field* f = soa.DecodeField(fid);
1417 return soa.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
1422 Object* o = soa.Decode<Object*>(java_object);
1423 Object* v = soa.Decode<Object*>(java_value);
1424 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
1430 Object* v = soa.Decode<Object*>(java_value);
1431 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001436 ScopedObjectAccess soa(env); \
1437 Object* o = soa.Decode<Object*>(instance); \
1438 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 return f->fn(o)
1440
1441#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001442 ScopedObjectAccess soa(env); \
1443 Object* o = soa.Decode<Object*>(instance); \
1444 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 f->fn(o, value)
1446
1447 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 GET_PRIMITIVE_FIELD(GetLong, NULL);
1501 }
1502
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1505 }
1506
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001507 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1509 }
1510
1511 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1512 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1513 }
1514
1515 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1516 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1517 }
1518
1519 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1520 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1521 }
1522
1523 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1524 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1525 }
1526
1527 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1528 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1529 }
1530
1531 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1532 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1533 }
1534
1535 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1536 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1537 }
1538
1539 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1540 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1541 }
1542
1543 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1544 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1545 }
1546
1547 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1548 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1549 }
1550
1551 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1552 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1553 }
1554
1555 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1556 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1557 }
1558
1559 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1560 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1561 }
1562
1563 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1564 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1565 }
1566
1567 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1568 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1569 }
1570
1571 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1572 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001578 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1580 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 va_end(ap);
1582 return local_result;
1583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001586 ScopedObjectAccess soa(env);
1587 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1588 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001592 ScopedObjectAccess soa(env);
1593 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1594 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001600 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001601 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001603 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 ScopedObjectAccess soa(env);
1608 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001612 ScopedObjectAccess soa(env);
1613 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001619 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001620 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001622 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 ScopedObjectAccess soa(env);
1627 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 ScopedObjectAccess soa(env);
1632 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001638 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001641 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
1646 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 ScopedObjectAccess soa(env);
1651 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001657 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001660 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 ScopedObjectAccess soa(env);
1665 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001668 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001669 ScopedObjectAccess soa(env);
1670 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001673 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001676 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001679 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
1684 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001687 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
1689 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001692 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001695 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001698 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001702 ScopedObjectAccess soa(env);
1703 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001706 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ScopedObjectAccess soa(env);
1708 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001714 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001717 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
1722 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
1727 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001733 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001734 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 ScopedObjectAccess soa(env);
1741 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
1746 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001752 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 va_end(ap);
1755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
1759 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
1764 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes814e4032011-08-23 12:07:56 -07001767 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001768 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001769 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
1773 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 if (utf == NULL) {
1775 return NULL;
1776 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001778 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes814e4032011-08-23 12:07:56 -07001782 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
1784 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001785 }
1786
1787 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
1789 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 }
1791
Elliott Hughesb465ab02011-08-24 11:21:21 -07001792 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
1794 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001795 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001797 } else {
1798 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1799 memcpy(buf, chars + start, length * sizeof(jchar));
1800 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 }
1802
Elliott Hughesb465ab02011-08-24 11:21:21 -07001803 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ScopedObjectAccess soa(env);
1805 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001806 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001808 } else {
1809 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1810 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1811 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 }
1813
Elliott Hughes75770752011-08-24 17:52:38 -07001814 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 ScopedObjectAccess soa(env);
1816 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001817 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 if (is_copy != NULL) {
1820 *is_copy = JNI_FALSE;
1821 }
1822 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001826 ScopedObjectAccess soa(env);
1827 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes75770752011-08-24 17:52:38 -07001830 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001831 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001832 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes75770752011-08-24 17:52:38 -07001835 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes75770752011-08-24 17:52:38 -07001840 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 if (java_string == NULL) {
1843 return NULL;
1844 }
1845 if (is_copy != NULL) {
1846 *is_copy = JNI_TRUE;
1847 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001848 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001849 size_t byte_count = s->GetUtfLength();
1850 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001851 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001852 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1853 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1854 bytes[byte_count] = '\0';
1855 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001861 }
1862
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 ScopedObjectAccess soa(env);
1865 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001866 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001867 Array* array = obj->AsArray();
1868 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
1873 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1874 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
1877 static void SetObjectArrayElement(JNIEnv* env,
1878 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
1880 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1881 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 array->Set(index, value);
1883 }
1884
1885 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
1887 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
1890 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
1892 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
1895 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
1897 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
1900 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
1902 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
1905 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
1907 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
1910 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
1912 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
1915 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
1917 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
1920 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 CHECK_GE(length, 0); // TODO: ReportJniError
1923
1924 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 std::string descriptor;
1927 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001928 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001929
1930 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001931 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1932 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 return NULL;
1934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001938 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001941 for (jsize i = 0; i < length; ++i) {
1942 result->Set(i, initial_object);
1943 }
1944 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
1948 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 ScopedObjectAccess soa(env);
1950 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Ian Rogersa15e67d2012-02-28 13:51:55 -08001953 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 ScopedObjectAccess soa(env);
1955 Array* array = soa.Decode<Array*>(java_array);
1956 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001957 if (is_copy != NULL) {
1958 *is_copy = JNI_FALSE;
1959 }
1960 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001963 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
1965 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ScopedObjectAccess soa(env);
1970 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
1975 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 ScopedObjectAccess soa(env);
1980 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ScopedObjectAccess soa(env);
1985 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
1990 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
1995 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
2000 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 ScopedObjectAccess soa(env);
2005 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002008 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ScopedObjectAccess soa(env);
2010 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002013 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002014 ScopedObjectAccess soa(env);
2015 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002018 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 ScopedObjectAccess soa(env);
2020 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002023 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002024 ScopedObjectAccess soa(env);
2025 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002028 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002029 ScopedObjectAccess soa(env);
2030 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002033 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002034 ScopedObjectAccess soa(env);
2035 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002038 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002039 ScopedObjectAccess soa(env);
2040 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002043 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002044 ScopedObjectAccess soa(env);
2045 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ScopedObjectAccess soa(env);
2050 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002054 ScopedObjectAccess soa(env);
2055 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 ScopedObjectAccess soa(env);
2060 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002064 ScopedObjectAccess soa(env);
2065 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002069 ScopedObjectAccess soa(env);
2070 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 ScopedObjectAccess soa(env);
2075 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 ScopedObjectAccess soa(env);
2080 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
2085 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002089 ScopedObjectAccess soa(env);
2090 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ScopedObjectAccess soa(env);
2095 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 ScopedObjectAccess soa(env);
2100 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 ScopedObjectAccess soa(env);
2105 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002109 ScopedObjectAccess soa(env);
2110 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002114 ScopedObjectAccess soa(env);
2115 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002119 ScopedObjectAccess soa(env);
2120 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 ScopedObjectAccess soa(env);
2125 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002129 ScopedObjectAccess soa(env);
2130 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 const char* name = methods[i].name;
2134 const char* sig = methods[i].signature;
2135
2136 if (*sig == '!') {
2137 // TODO: fast jni. it's too noisy to log all these.
2138 ++sig;
2139 }
2140
Mathieu Chartier66f19252012-09-18 08:57:04 -07002141 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142 if (m == NULL) {
2143 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002146 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002147 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002150 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 return JNI_ERR;
2153 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002155 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002157 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159 return JNI_OK;
2160 }
2161
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002163 ScopedObjectAccess soa(env);
2164 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002166 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167
2168 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002169 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002171 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 }
2173 }
2174 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002175 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 }
2179 }
2180
2181 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2185 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2186 ScopedObjectAccess soa(env);
2187 Object* o = soa.Decode<Object*>(java_object);
2188 o->MonitorEnter(soa.Self());
2189 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002190 return JNI_ERR;
2191 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002193 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 static jint MonitorExit(JNIEnv* env, jobject java_object)
2197 UNLOCK_FUNCTION(monitor_lock_) {
2198 ScopedObjectAccess soa(env);
2199 Object* o = soa.Decode<Object*>(java_object);
2200 o->MonitorExit(soa.Self());
2201 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002202 return JNI_ERR;
2203 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002205 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
2208 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002209 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 Runtime* runtime = Runtime::Current();
2211 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002212 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 } else {
2214 *vm = NULL;
2215 }
2216 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2217 }
2218
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002220 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221
2222 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002223 CHECK(address != NULL); // TODO: ReportJniError
2224 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2228 CHECK_LE(capacity, 0xffffffff);
2229 jint address_arg = reinterpret_cast<jint>(address);
2230 jint capacity_arg = static_cast<jint>(capacity);
2231
Elliott Hugheseac76672012-05-24 21:56:51 -07002232 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2233 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2234 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002235 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002239 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002240 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002245 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250
Elliott Hughes75770752011-08-24 17:52:38 -07002251 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252
2253 // Do we definitely know what kind of reference this is?
2254 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2255 IndirectRefKind kind = GetIndirectRefKind(ref);
2256 switch (kind) {
2257 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002259 return JNILocalRefType;
2260 }
2261 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002262 case kGlobal:
2263 return JNIGlobalRefType;
2264 case kWeakGlobal:
2265 return JNIWeakGlobalRefType;
2266 case kSirtOrInvalid:
2267 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002268 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002269 return JNILocalRefType;
2270 }
2271
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002272 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002273 return JNIInvalidRefType;
2274 }
2275
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 // If we're handing out direct pointers, check whether it's a direct pointer
2277 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002278 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2279 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 return JNILocalRefType;
2281 }
2282 }
2283
2284 return JNIInvalidRefType;
2285 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002286 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2287 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289
2290 private:
2291 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2292 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002294 // TODO: we should try to expand the table if necessary.
2295 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2296 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2297 return JNI_ERR;
2298 }
2299 // TODO: this isn't quite right, since "capacity" includes holes.
2300 size_t capacity = soa.Env()->locals.Capacity();
2301 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2302 if (!okay) {
2303 soa.Self()->ThrowOutOfMemoryError(caller);
2304 }
2305 return okay ? JNI_OK : JNI_ERR;
2306 }
2307
2308 template<typename JniT, typename ArtT>
2309 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 CHECK_GE(length, 0); // TODO: ReportJniError
Ian Rogers50b35e22012-10-04 10:09:15 -07002312 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002313 return soa.AddLocalReference<JniT>(result);
2314 }
2315
2316 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2317 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2318 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002320 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2321 PinPrimitiveArray(soa, array);
2322 if (is_copy != NULL) {
2323 *is_copy = JNI_FALSE;
2324 }
2325 return array->GetData();
2326 }
2327
2328 template <typename ArrayT>
2329 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2330 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002332 if (mode != JNI_COMMIT) {
2333 Array* array = soa.Decode<Array*>(java_array);
2334 UnpinPrimitiveArray(soa, array);
2335 }
2336 }
2337
2338 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2339 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2340 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2343 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2344 ThrowAIOOBE(soa, array, start, length, "src");
2345 } else {
2346 JavaT* data = array->GetData();
2347 memcpy(buf, data + start, length * sizeof(JavaT));
2348 }
2349 }
2350
2351 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2352 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2353 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2356 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2357 ThrowAIOOBE(soa, array, start, length, "dst");
2358 } else {
2359 JavaT* data = array->GetData();
2360 memcpy(data + start, buf, length * sizeof(JavaT));
2361 }
2362 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002363};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002364
Elliott Hughes88c5c352012-03-15 18:49:48 -07002365const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002366 NULL, // reserved0.
2367 NULL, // reserved1.
2368 NULL, // reserved2.
2369 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002370 JNI::GetVersion,
2371 JNI::DefineClass,
2372 JNI::FindClass,
2373 JNI::FromReflectedMethod,
2374 JNI::FromReflectedField,
2375 JNI::ToReflectedMethod,
2376 JNI::GetSuperclass,
2377 JNI::IsAssignableFrom,
2378 JNI::ToReflectedField,
2379 JNI::Throw,
2380 JNI::ThrowNew,
2381 JNI::ExceptionOccurred,
2382 JNI::ExceptionDescribe,
2383 JNI::ExceptionClear,
2384 JNI::FatalError,
2385 JNI::PushLocalFrame,
2386 JNI::PopLocalFrame,
2387 JNI::NewGlobalRef,
2388 JNI::DeleteGlobalRef,
2389 JNI::DeleteLocalRef,
2390 JNI::IsSameObject,
2391 JNI::NewLocalRef,
2392 JNI::EnsureLocalCapacity,
2393 JNI::AllocObject,
2394 JNI::NewObject,
2395 JNI::NewObjectV,
2396 JNI::NewObjectA,
2397 JNI::GetObjectClass,
2398 JNI::IsInstanceOf,
2399 JNI::GetMethodID,
2400 JNI::CallObjectMethod,
2401 JNI::CallObjectMethodV,
2402 JNI::CallObjectMethodA,
2403 JNI::CallBooleanMethod,
2404 JNI::CallBooleanMethodV,
2405 JNI::CallBooleanMethodA,
2406 JNI::CallByteMethod,
2407 JNI::CallByteMethodV,
2408 JNI::CallByteMethodA,
2409 JNI::CallCharMethod,
2410 JNI::CallCharMethodV,
2411 JNI::CallCharMethodA,
2412 JNI::CallShortMethod,
2413 JNI::CallShortMethodV,
2414 JNI::CallShortMethodA,
2415 JNI::CallIntMethod,
2416 JNI::CallIntMethodV,
2417 JNI::CallIntMethodA,
2418 JNI::CallLongMethod,
2419 JNI::CallLongMethodV,
2420 JNI::CallLongMethodA,
2421 JNI::CallFloatMethod,
2422 JNI::CallFloatMethodV,
2423 JNI::CallFloatMethodA,
2424 JNI::CallDoubleMethod,
2425 JNI::CallDoubleMethodV,
2426 JNI::CallDoubleMethodA,
2427 JNI::CallVoidMethod,
2428 JNI::CallVoidMethodV,
2429 JNI::CallVoidMethodA,
2430 JNI::CallNonvirtualObjectMethod,
2431 JNI::CallNonvirtualObjectMethodV,
2432 JNI::CallNonvirtualObjectMethodA,
2433 JNI::CallNonvirtualBooleanMethod,
2434 JNI::CallNonvirtualBooleanMethodV,
2435 JNI::CallNonvirtualBooleanMethodA,
2436 JNI::CallNonvirtualByteMethod,
2437 JNI::CallNonvirtualByteMethodV,
2438 JNI::CallNonvirtualByteMethodA,
2439 JNI::CallNonvirtualCharMethod,
2440 JNI::CallNonvirtualCharMethodV,
2441 JNI::CallNonvirtualCharMethodA,
2442 JNI::CallNonvirtualShortMethod,
2443 JNI::CallNonvirtualShortMethodV,
2444 JNI::CallNonvirtualShortMethodA,
2445 JNI::CallNonvirtualIntMethod,
2446 JNI::CallNonvirtualIntMethodV,
2447 JNI::CallNonvirtualIntMethodA,
2448 JNI::CallNonvirtualLongMethod,
2449 JNI::CallNonvirtualLongMethodV,
2450 JNI::CallNonvirtualLongMethodA,
2451 JNI::CallNonvirtualFloatMethod,
2452 JNI::CallNonvirtualFloatMethodV,
2453 JNI::CallNonvirtualFloatMethodA,
2454 JNI::CallNonvirtualDoubleMethod,
2455 JNI::CallNonvirtualDoubleMethodV,
2456 JNI::CallNonvirtualDoubleMethodA,
2457 JNI::CallNonvirtualVoidMethod,
2458 JNI::CallNonvirtualVoidMethodV,
2459 JNI::CallNonvirtualVoidMethodA,
2460 JNI::GetFieldID,
2461 JNI::GetObjectField,
2462 JNI::GetBooleanField,
2463 JNI::GetByteField,
2464 JNI::GetCharField,
2465 JNI::GetShortField,
2466 JNI::GetIntField,
2467 JNI::GetLongField,
2468 JNI::GetFloatField,
2469 JNI::GetDoubleField,
2470 JNI::SetObjectField,
2471 JNI::SetBooleanField,
2472 JNI::SetByteField,
2473 JNI::SetCharField,
2474 JNI::SetShortField,
2475 JNI::SetIntField,
2476 JNI::SetLongField,
2477 JNI::SetFloatField,
2478 JNI::SetDoubleField,
2479 JNI::GetStaticMethodID,
2480 JNI::CallStaticObjectMethod,
2481 JNI::CallStaticObjectMethodV,
2482 JNI::CallStaticObjectMethodA,
2483 JNI::CallStaticBooleanMethod,
2484 JNI::CallStaticBooleanMethodV,
2485 JNI::CallStaticBooleanMethodA,
2486 JNI::CallStaticByteMethod,
2487 JNI::CallStaticByteMethodV,
2488 JNI::CallStaticByteMethodA,
2489 JNI::CallStaticCharMethod,
2490 JNI::CallStaticCharMethodV,
2491 JNI::CallStaticCharMethodA,
2492 JNI::CallStaticShortMethod,
2493 JNI::CallStaticShortMethodV,
2494 JNI::CallStaticShortMethodA,
2495 JNI::CallStaticIntMethod,
2496 JNI::CallStaticIntMethodV,
2497 JNI::CallStaticIntMethodA,
2498 JNI::CallStaticLongMethod,
2499 JNI::CallStaticLongMethodV,
2500 JNI::CallStaticLongMethodA,
2501 JNI::CallStaticFloatMethod,
2502 JNI::CallStaticFloatMethodV,
2503 JNI::CallStaticFloatMethodA,
2504 JNI::CallStaticDoubleMethod,
2505 JNI::CallStaticDoubleMethodV,
2506 JNI::CallStaticDoubleMethodA,
2507 JNI::CallStaticVoidMethod,
2508 JNI::CallStaticVoidMethodV,
2509 JNI::CallStaticVoidMethodA,
2510 JNI::GetStaticFieldID,
2511 JNI::GetStaticObjectField,
2512 JNI::GetStaticBooleanField,
2513 JNI::GetStaticByteField,
2514 JNI::GetStaticCharField,
2515 JNI::GetStaticShortField,
2516 JNI::GetStaticIntField,
2517 JNI::GetStaticLongField,
2518 JNI::GetStaticFloatField,
2519 JNI::GetStaticDoubleField,
2520 JNI::SetStaticObjectField,
2521 JNI::SetStaticBooleanField,
2522 JNI::SetStaticByteField,
2523 JNI::SetStaticCharField,
2524 JNI::SetStaticShortField,
2525 JNI::SetStaticIntField,
2526 JNI::SetStaticLongField,
2527 JNI::SetStaticFloatField,
2528 JNI::SetStaticDoubleField,
2529 JNI::NewString,
2530 JNI::GetStringLength,
2531 JNI::GetStringChars,
2532 JNI::ReleaseStringChars,
2533 JNI::NewStringUTF,
2534 JNI::GetStringUTFLength,
2535 JNI::GetStringUTFChars,
2536 JNI::ReleaseStringUTFChars,
2537 JNI::GetArrayLength,
2538 JNI::NewObjectArray,
2539 JNI::GetObjectArrayElement,
2540 JNI::SetObjectArrayElement,
2541 JNI::NewBooleanArray,
2542 JNI::NewByteArray,
2543 JNI::NewCharArray,
2544 JNI::NewShortArray,
2545 JNI::NewIntArray,
2546 JNI::NewLongArray,
2547 JNI::NewFloatArray,
2548 JNI::NewDoubleArray,
2549 JNI::GetBooleanArrayElements,
2550 JNI::GetByteArrayElements,
2551 JNI::GetCharArrayElements,
2552 JNI::GetShortArrayElements,
2553 JNI::GetIntArrayElements,
2554 JNI::GetLongArrayElements,
2555 JNI::GetFloatArrayElements,
2556 JNI::GetDoubleArrayElements,
2557 JNI::ReleaseBooleanArrayElements,
2558 JNI::ReleaseByteArrayElements,
2559 JNI::ReleaseCharArrayElements,
2560 JNI::ReleaseShortArrayElements,
2561 JNI::ReleaseIntArrayElements,
2562 JNI::ReleaseLongArrayElements,
2563 JNI::ReleaseFloatArrayElements,
2564 JNI::ReleaseDoubleArrayElements,
2565 JNI::GetBooleanArrayRegion,
2566 JNI::GetByteArrayRegion,
2567 JNI::GetCharArrayRegion,
2568 JNI::GetShortArrayRegion,
2569 JNI::GetIntArrayRegion,
2570 JNI::GetLongArrayRegion,
2571 JNI::GetFloatArrayRegion,
2572 JNI::GetDoubleArrayRegion,
2573 JNI::SetBooleanArrayRegion,
2574 JNI::SetByteArrayRegion,
2575 JNI::SetCharArrayRegion,
2576 JNI::SetShortArrayRegion,
2577 JNI::SetIntArrayRegion,
2578 JNI::SetLongArrayRegion,
2579 JNI::SetFloatArrayRegion,
2580 JNI::SetDoubleArrayRegion,
2581 JNI::RegisterNatives,
2582 JNI::UnregisterNatives,
2583 JNI::MonitorEnter,
2584 JNI::MonitorExit,
2585 JNI::GetJavaVM,
2586 JNI::GetStringRegion,
2587 JNI::GetStringUTFRegion,
2588 JNI::GetPrimitiveArrayCritical,
2589 JNI::ReleasePrimitiveArrayCritical,
2590 JNI::GetStringCritical,
2591 JNI::ReleaseStringCritical,
2592 JNI::NewWeakGlobalRef,
2593 JNI::DeleteWeakGlobalRef,
2594 JNI::ExceptionCheck,
2595 JNI::NewDirectByteBuffer,
2596 JNI::GetDirectBufferAddress,
2597 JNI::GetDirectBufferCapacity,
2598 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002599};
2600
Elliott Hughes75770752011-08-24 17:52:38 -07002601JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002602 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002603 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002604 local_ref_cookie(IRT_FIRST_SEGMENT),
2605 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002606 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002607 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002608 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002609 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002610 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002611 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002612 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002613 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2614 // errors will ensue.
2615 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2616 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002617}
2618
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002619JNIEnvExt::~JNIEnvExt() {
2620}
2621
Elliott Hughes88c5c352012-03-15 18:49:48 -07002622void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2623 check_jni = enabled;
2624 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002625}
2626
Elliott Hughes73e66f72012-05-09 09:34:45 -07002627void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2628 locals.Dump(os);
2629 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002630}
2631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002632void JNIEnvExt::PushFrame(int /*capacity*/) {
2633 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002634 stacked_local_ref_cookies.push_back(local_ref_cookie);
2635 local_ref_cookie = locals.GetSegmentState();
2636}
2637
2638void JNIEnvExt::PopFrame() {
2639 locals.SetSegmentState(local_ref_cookie);
2640 local_ref_cookie = stacked_local_ref_cookies.back();
2641 stacked_local_ref_cookies.pop_back();
2642}
2643
Carl Shapiroea4dca82011-08-01 13:45:38 -07002644// JNI Invocation interface.
2645
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2647 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2648 if (args->version < JNI_VERSION_1_2) {
2649 return JNI_EVERSION;
2650 }
2651 Runtime::Options options;
2652 for (int i = 0; i < args->nOptions; ++i) {
2653 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002654 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 }
2656 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002657 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002660 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002661 runtime->Start();
2662 *p_env = Thread::Current()->GetJniEnv();
2663 *p_vm = runtime->GetJavaVM();
2664 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002665}
2666
Elliott Hughesf2682d52011-08-15 16:37:04 -07002667extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668 Runtime* runtime = Runtime::Current();
2669 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002670 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002671 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002672 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002673 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002674 }
2675 return JNI_OK;
2676}
2677
2678// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002679extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 return JNI_ERR;
2681}
2682
Elliott Hughescdf53122011-08-19 15:46:09 -07002683class JII {
2684 public:
2685 static jint DestroyJavaVM(JavaVM* vm) {
2686 if (vm == NULL) {
2687 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002689 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2690 delete raw_vm->runtime;
2691 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002692 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002693
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 }
2697
2698 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002699 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002700 }
2701
2702 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002703 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002706 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2707 Runtime* runtime = raw_vm->runtime;
2708 runtime->DetachCurrentThread();
2709 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002710 }
2711
2712 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2713 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2714 return JNI_EVERSION;
2715 }
2716 if (vm == NULL || env == NULL) {
2717 return JNI_ERR;
2718 }
2719 Thread* thread = Thread::Current();
2720 if (thread == NULL) {
2721 *env = NULL;
2722 return JNI_EDETACHED;
2723 }
2724 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002725 return JNI_OK;
2726 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002727};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002728
Elliott Hughes88c5c352012-03-15 18:49:48 -07002729const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002730 NULL, // reserved0
2731 NULL, // reserved1
2732 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 JII::DestroyJavaVM,
2734 JII::AttachCurrentThread,
2735 JII::DetachCurrentThread,
2736 JII::GetEnv,
2737 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002738};
2739
Elliott Hughesa0957642011-09-02 14:27:33 -07002740JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002741 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002742 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002743 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002744 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002745 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002746 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002747 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002748 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002749 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002750 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002751 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002752 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002753 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002754 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002755 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002756 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002757 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002758 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002759 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002760}
2761
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002762JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002764}
2765
Elliott Hughes88c5c352012-03-15 18:49:48 -07002766void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2767 check_jni = enabled;
2768 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002769}
2770
Elliott Hughesae80b492012-04-24 10:43:17 -07002771void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2772 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2773 if (force_copy) {
2774 os << " (with forcecopy)";
2775 }
2776 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002777 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002778 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002779 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002780 os << "; pins=" << pin_table.Size();
2781 }
2782 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002783 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002784 os << "; globals=" << globals.Capacity();
2785 }
2786 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002787 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002788 if (weak_globals.Capacity() > 0) {
2789 os << " (plus " << weak_globals.Capacity() << " weak)";
2790 }
2791 }
2792 os << '\n';
2793
2794 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002795 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002796 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2797 }
2798}
2799
Elliott Hughes73e66f72012-05-09 09:34:45 -07002800void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002801 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002802 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002803 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002804 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002805 }
2806 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002807 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002808 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002809 }
2810 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002811 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002812 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002813 }
2814}
2815
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002816bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2817 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002818 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002819
2820 // See if we've already loaded this library. If we have, and the class loader
2821 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002822 // TODO: for better results we should canonicalize the pathname (or even compare
2823 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002824 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002825 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002826 {
2827 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002828 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002829 library = libraries->Get(path);
2830 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002831 if (library != NULL) {
2832 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002833 // The library will be associated with class_loader. The JNI
2834 // spec says we can't load the same library into more than one
2835 // class loader.
2836 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2837 "ClassLoader %p; can't open in ClassLoader %p",
2838 path.c_str(), library->GetClassLoader(), class_loader);
2839 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002840 return false;
2841 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002842 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2843 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002844 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002845 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2846 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002847 return false;
2848 }
2849 return true;
2850 }
2851
2852 // Open the shared library. Because we're using a full path, the system
2853 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2854 // resolve this library's dependencies though.)
2855
2856 // Failures here are expected when java.library.path has several entries
2857 // and we have to hunt for the lib.
2858
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002859 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2860 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2861 // dlopen) becomes zero from dlclose.
2862
Elliott Hughescdf53122011-08-19 15:46:09 -07002863 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002864 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002865 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2866 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2867 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002868
Elliott Hughes84b2f142012-09-27 09:16:28 -07002869 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002870
2871 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002872 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002873 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002874 return false;
2875 }
2876
2877 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002878 // TODO: move the locking (and more of this logic) into Libraries.
2879 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002880 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002881 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002882 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002883 if (library == NULL) { // We won race to get libraries_lock
2884 library = new SharedLibrary(path, handle, class_loader);
2885 libraries->Put(path, library);
2886 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002887 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002888 }
2889 if (!created_library) {
2890 LOG(INFO) << "WOW: we lost a race to add shared library: "
2891 << "\"" << path << "\" ClassLoader=" << class_loader;
2892 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002893 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002894
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002895 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002896
2897 bool result = true;
2898 void* sym = dlsym(handle, "JNI_OnLoad");
2899 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002900 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002901 } else {
2902 // Call JNI_OnLoad. We have to override the current class
2903 // loader, which will always be "null" since the stuff at the
2904 // top of the stack is around Runtime.loadLibrary(). (See
2905 // the comments in the JNI FindClass function.)
2906 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2907 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002908 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002909 self->SetClassLoaderOverride(class_loader);
2910
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002911 int version = 0;
2912 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002913 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002914 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002915 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002916 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002917
Brian Carlstromaded5f72011-10-07 17:15:04 -07002918 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002919
2920 if (version != JNI_VERSION_1_2 &&
2921 version != JNI_VERSION_1_4 &&
2922 version != JNI_VERSION_1_6) {
2923 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2924 << "bad version: " << version;
2925 // It's unwise to call dlclose() here, but we can mark it
2926 // as bad and ensure that future load attempts will fail.
2927 // We don't know how far JNI_OnLoad got, so there could
2928 // be some partially-initialized stuff accessible through
2929 // newly-registered native method calls. We could try to
2930 // unregister them, but that doesn't seem worthwhile.
2931 result = false;
2932 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002933 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2934 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002935 }
2936 }
2937
2938 library->SetResult(result);
2939 return result;
2940}
2941
Mathieu Chartier66f19252012-09-18 08:57:04 -07002942void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002943 CHECK(m->IsNative());
2944
2945 Class* c = m->GetDeclaringClass();
2946
2947 // If this is a static method, it could be called before the class
2948 // has been initialized.
2949 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002950 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002951 return NULL;
2952 }
2953 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002954 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002955 }
2956
Brian Carlstrom16192862011-09-12 17:50:06 -07002957 std::string detail;
2958 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002959 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002960 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002961 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002962 native_method = libraries->FindNativeMethod(m, detail);
2963 }
2964 // throwing can cause libraries_lock to be reacquired
2965 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002966 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002967 }
2968 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002969}
2970
Elliott Hughes410c0c82011-09-01 17:58:25 -07002971void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002972 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002973 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002974 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002975 globals.VisitRoots(visitor, arg);
2976 }
2977 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002978 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002979 pin_table.VisitRoots(visitor, arg);
2980 }
2981 // The weak_globals table is visited by the GC itself (because it mutates the table).
2982}
2983
Ian Rogersdf20fe02011-07-20 20:34:16 -07002984} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002985
2986std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2987 switch (rhs) {
2988 case JNIInvalidRefType:
2989 os << "JNIInvalidRefType";
2990 return os;
2991 case JNILocalRefType:
2992 os << "JNILocalRefType";
2993 return os;
2994 case JNIGlobalRefType:
2995 os << "JNIGlobalRefType";
2996 return os;
2997 case JNIWeakGlobalRefType:
2998 os << "JNIWeakGlobalRefType";
2999 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003000 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003001 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003002 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003003 }
3004}