blob: 00976c1f6774a596dfce03eb2667dacd7ecd719b [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070014#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070015#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070016#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070018#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070019#include "runtime.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070020#include "scoped_ptr.h"
21#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070023
buzbeec143c552011-08-20 17:38:58 -070024extern bool oatCompileMethod(art::Method*, art::InstructionSet);
25
Ian Rogersdf20fe02011-07-20 20:34:16 -070026namespace art {
27
Elliott Hughescdf53122011-08-19 15:46:09 -070028// This is private API, but with two different implementations: ARM and x86.
29void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070030
Elliott Hughescdf53122011-08-19 15:46:09 -070031// TODO: this should be in our anonymous namespace, but is currently needed
32// for testing in "jni_internal_test.cc".
Elliott Hughes79082e32011-08-25 12:07:32 -070033void EnsureInvokeStub(Method* method) {
Elliott Hughescdf53122011-08-19 15:46:09 -070034 if (method->GetInvokeStub() != NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -070035 return;
Elliott Hughescdf53122011-08-19 15:46:09 -070036 }
37 // TODO: use signature to find a matching stub
38 // TODO: failed, acquire a lock on the stub table
39 Assembler assembler;
40 CreateInvokeStub(&assembler, method);
41 // TODO: store native_entry in the stub table
42 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
43 size_t length = assembler.CodeSize();
44 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
45 if (addr == MAP_FAILED) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070046 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070047 }
48 MemoryRegion region(addr, length);
49 assembler.FinalizeInstructions(region);
50 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
Elliott Hughescdf53122011-08-19 15:46:09 -070051}
Elliott Hughes0af55432011-08-17 18:37:28 -070052
Elliott Hughescdf53122011-08-19 15:46:09 -070053namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -070054
Elliott Hughes22f40932011-08-12 13:06:37 -070055// Entry/exit processing for all JNI calls.
56//
57// This performs the necessary thread state switching, lets us amortize the
58// cost of working out the current thread, and lets us check (and repair) apps
59// that are using a JNIEnv on the wrong thread.
60class ScopedJniThreadState {
61 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -070062 explicit ScopedJniThreadState(JNIEnv* env)
63 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -070064 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -070065 self_->SetState(Thread::kRunnable);
66 }
67
68 ~ScopedJniThreadState() {
69 self_->SetState(Thread::kNative);
70 }
71
Elliott Hughesc5f7c912011-08-18 14:00:42 -070072 JNIEnvExt* Env() {
73 return env_;
74 }
75
Elliott Hughesb20a5542011-08-12 18:03:12 -070076 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -070077 return self_;
78 }
79
Elliott Hughes75770752011-08-24 17:52:38 -070080 JavaVMExt* Vm() {
81 return env_->vm;
82 }
83
Elliott Hughesb20a5542011-08-12 18:03:12 -070084 private:
85 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -070086 // TODO: need replacement for gDvmJni.
87 bool workAroundAppJniBugs = true;
88 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
89 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
90 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -070091 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
92 << " used on " << *self;
93 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -070094 }
95 return self;
96 }
97
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -070099 Thread* self_;
100 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
101};
102
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103/*
104 * Add a local reference for an object to the current stack frame. When
105 * the native function returns, the reference will be discarded.
106 *
107 * We need to allow the same reference to be added multiple times.
108 *
109 * This will be called on otherwise unreferenced objects. We cannot do
110 * GC allocations here, and it's best if we don't grab a mutex.
111 *
112 * Returns the local reference (currently just the same pointer that was
113 * passed in), or NULL on failure.
114 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700115template<typename T>
116T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117 if (obj == NULL) {
118 return NULL;
119 }
120
121 IndirectReferenceTable& locals = ts.Env()->locals;
122
123 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
124 IndirectRef ref = locals.Add(cookie, obj);
125 if (ref == NULL) {
126 // TODO: just change Add's DCHECK to CHECK and lose this?
127 locals.Dump();
128 LOG(FATAL) << "Failed adding to JNI local reference table "
129 << "(has " << locals.Capacity() << " entries)";
130 // TODO: dvmDumpThread(dvmThreadSelf(), false);
131 }
132
133#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
134 if (ts.Env()->check_jni) {
135 size_t entry_count = locals.Capacity();
136 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700137 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700138 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700139 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700140 locals.Dump();
141 // TODO: dvmDumpThread(dvmThreadSelf(), false);
142 // dvmAbort();
143 }
144 }
145#endif
146
147 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
148 // Hand out direct pointers to support broken old apps.
149 return reinterpret_cast<T>(obj);
150 }
151
152 return reinterpret_cast<T>(ref);
153}
154
Elliott Hughescdf53122011-08-19 15:46:09 -0700155jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
156 if (obj == NULL) {
157 return NULL;
158 }
Elliott Hughes75770752011-08-24 17:52:38 -0700159 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700160 IndirectReferenceTable& weak_globals = vm->weak_globals;
161 MutexLock mu(vm->weak_globals_lock);
162 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
163 return reinterpret_cast<jweak>(ref);
164}
165
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700166template<typename T>
167T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700168 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700169}
170
Elliott Hughescdf53122011-08-19 15:46:09 -0700171Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
172 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
173}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700174
Elliott Hughescdf53122011-08-19 15:46:09 -0700175Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
176 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700177}
178
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700179byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700180 size_t num_bytes = method->NumArgArrayBytes();
181 scoped_array<byte> arg_array(new byte[num_bytes]);
182 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700183 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 switch (shorty[i]) {
185 case 'Z':
186 case 'B':
187 case 'C':
188 case 'S':
189 case 'I':
190 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
191 offset += 4;
192 break;
193 case 'F':
194 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
195 offset += 4;
196 break;
197 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700198 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
200 offset += sizeof(Object*);
201 break;
202 }
203 case 'D':
204 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
205 offset += 8;
206 break;
207 case 'J':
208 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
209 offset += 8;
210 break;
211 }
212 }
213 return arg_array.release();
214}
215
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700216byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700217 size_t num_bytes = method->NumArgArrayBytes();
218 scoped_array<byte> arg_array(new byte[num_bytes]);
219 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700220 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700221 switch (shorty[i]) {
222 case 'Z':
223 case 'B':
224 case 'C':
225 case 'S':
226 case 'I':
227 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
228 offset += 4;
229 break;
230 case 'F':
231 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
232 offset += 4;
233 break;
234 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700235 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700236 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
237 offset += sizeof(Object*);
238 break;
239 }
240 case 'D':
241 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
242 offset += 8;
243 break;
244 case 'J':
245 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
246 offset += 8;
247 break;
248 }
249 }
250 return arg_array.release();
251}
252
Elliott Hughes72025e52011-08-23 17:50:30 -0700253JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
254 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700255 Thread* self = ts.Self();
256
257 // Push a transition back into managed code onto the linked list in thread
258 CHECK_EQ(Thread::kRunnable, self->GetState());
259 NativeToManagedRecord record;
260 self->PushNativeToManagedRecord(&record);
261
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700262 // Call the invoke stub associated with the method
263 // Pass everything as arguments
264 const Method::InvokeStub* stub = method->GetInvokeStub();
265 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700266
267#ifdef __arm__
268 // Compile...
269 // TODO: not here!
270 oatCompileMethod(method, kThumb2);
271#endif
272
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700273 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700274 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700275 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700276 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700277 LOG(WARNING) << "Not invoking method with no associated code: "
278 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700279 result.j = 0;
280 }
buzbeec143c552011-08-20 17:38:58 -0700281
Ian Rogers6de08602011-08-19 14:52:39 -0700282 // Pop transition
283 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700284 return result;
285}
286
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700287JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700288 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700289 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700290 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700291 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700292 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700293}
294
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700295JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700296 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700297 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700298 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700299 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700300 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
301}
302
Elliott Hughes75770752011-08-24 17:52:38 -0700303Method* FindVirtualMethod(Object* receiver, Method* method) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700304 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
305}
306
307JValue InvokeVirtualWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
308 Object* receiver = Decode<Object*>(ts, obj);
309 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
310 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
311 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
312}
313
314JValue InvokeVirtualWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
315 Object* receiver = Decode<Object*>(ts, obj);
316 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
317 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
318 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700319}
320
Elliott Hughes6b436852011-08-12 10:16:44 -0700321// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
322// separated with slashes but aren't wrapped with "L;" like regular descriptors
323// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
324// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
325// supported names with dots too (such as "a.b.C").
326std::string NormalizeJniClassDescriptor(const char* name) {
327 std::string result;
328 // Add the missing "L;" if necessary.
329 if (name[0] == '[') {
330 result = name;
331 } else {
332 result += 'L';
333 result += name;
334 result += ';';
335 }
336 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700337 if (result.find('.') != std::string::npos) {
338 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
339 << "\"" << name << "\"";
340 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700341 }
342 return result;
343}
344
Elliott Hughescdf53122011-08-19 15:46:09 -0700345jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
346 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700347 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
348 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700349 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700350
351 Method* method = NULL;
352 if (is_static) {
353 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700354 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700355 method = c->FindVirtualMethod(name, sig);
356 if (method == NULL) {
357 // No virtual method matching the signature. Search declared
358 // private methods and constructors.
359 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700360 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700361 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700362
Elliott Hughescdf53122011-08-19 15:46:09 -0700363 if (method == NULL || method->IsStatic() != is_static) {
364 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700365 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700366 // TODO: try searching for the opposite kind of method from is_static
367 // for better diagnostics?
368 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700369 "no %s method %s", is_static ? "static" : "non-static",
370 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700371 return NULL;
372 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700373
Elliott Hughes79082e32011-08-25 12:07:32 -0700374 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700375
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700377}
378
Elliott Hughescdf53122011-08-19 15:46:09 -0700379jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
380 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700381 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
382 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700383 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700384
385 Field* field = NULL;
386 if (is_static) {
387 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700388 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700390 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700391
Elliott Hughescdf53122011-08-19 15:46:09 -0700392 if (field == NULL) {
393 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700394 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
396 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700397 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700398 return NULL;
399 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700400
Elliott Hughescdf53122011-08-19 15:46:09 -0700401 jweak fid = AddWeakGlobalReference(ts, field);
402 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403}
404
Elliott Hughes75770752011-08-24 17:52:38 -0700405void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
406 JavaVMExt* vm = ts.Vm();
407 MutexLock mu(vm->pins_lock);
408 vm->pin_table.Add(array);
409}
410
411void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
412 JavaVMExt* vm = ts.Vm();
413 MutexLock mu(vm->pins_lock);
414 vm->pin_table.Remove(array);
415}
416
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700417template<typename JniT, typename ArtT>
418JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
419 CHECK_GE(length, 0); // TODO: ReportJniError
420 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700421 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424template <typename ArrayT, typename CArrayT, typename ArtArrayT>
425CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
426 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
427 PinPrimitiveArray(ts, array);
428 if (is_copy != NULL) {
429 *is_copy = JNI_FALSE;
430 }
431 return array->GetData();
432}
433
434template <typename ArrayT>
435void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
436 if (mode != JNI_COMMIT) {
437 Array* array = Decode<Array*>(ts, java_array);
438 UnpinPrimitiveArray(ts, array);
439 }
440}
441
Elliott Hughes814e4032011-08-23 12:07:56 -0700442void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
443 std::string type(PrettyType(array));
444 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
445 "%s offset=%d length=%d %s.length=%d",
446 type.c_str(), start, length, identifier, array->GetLength());
447}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700448void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
449 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
450 "offset=%d length=%d string.length()=%d", start, length, array_length);
451}
Elliott Hughes814e4032011-08-23 12:07:56 -0700452
453template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700454void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700455 ArrayT* array = Decode<ArrayT*>(ts, java_array);
456 if (start < 0 || length < 0 || start + length > array->GetLength()) {
457 ThrowAIOOBE(ts, array, start, length, "src");
458 } else {
459 JavaT* data = array->GetData();
460 memcpy(buf, data + start, length * sizeof(JavaT));
461 }
462}
463
464template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700465void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700466 ArrayT* array = Decode<ArrayT*>(ts, java_array);
467 if (start < 0 || length < 0 || start + length > array->GetLength()) {
468 ThrowAIOOBE(ts, array, start, length, "dst");
469 } else {
470 JavaT* data = array->GetData();
471 memcpy(data + start, buf, length * sizeof(JavaT));
472 }
473}
474
Elliott Hughes75770752011-08-24 17:52:38 -0700475jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700476 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
477 CHECK(buffer_class.get() != NULL);
478 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
479}
480
Elliott Hughes75770752011-08-24 17:52:38 -0700481jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700482 static jclass buffer_class = InitDirectByteBufferClass(env);
483 return buffer_class;
484}
485
Elliott Hughes75770752011-08-24 17:52:38 -0700486jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
487 if (vm == NULL || p_env == NULL) {
488 return JNI_ERR;
489 }
490
491 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
492 JavaVMAttachArgs args;
493 if (thr_args == NULL) {
494 // Allow the v1.1 calling convention.
495 args.version = JNI_VERSION_1_2;
496 args.name = NULL;
497 args.group = NULL; // TODO: get "main" thread group
498 } else {
499 args.version = in_args->version;
500 args.name = in_args->name;
501 if (in_args->group != NULL) {
502 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
503 args.group = NULL; // TODO: decode in_args->group
504 } else {
505 args.group = NULL; // TODO: get "main" thread group
506 }
507 }
508 CHECK_GE(args.version, JNI_VERSION_1_2);
509
510 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
511 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
512}
513
Elliott Hughes79082e32011-08-25 12:07:32 -0700514class SharedLibrary {
515 public:
516 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
517 : path_(path),
518 handle_(handle),
519 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
520 jni_on_load_tid_(Thread::Current()->GetId()),
521 jni_on_load_result_(kPending) {
522 pthread_cond_init(&jni_on_load_cond_, NULL);
523 }
524
525 ~SharedLibrary() {
526 delete jni_on_load_lock_;
527 }
528
529 Object* GetClassLoader() {
530 return class_loader_;
531 }
532
533 std::string GetPath() {
534 return path_;
535 }
536
537 /*
538 * Check the result of an earlier call to JNI_OnLoad on this library. If
539 * the call has not yet finished in another thread, wait for it.
540 */
541 bool CheckOnLoadResult(JavaVMExt* vm) {
542 Thread* self = Thread::Current();
543 if (jni_on_load_tid_ == self->GetId()) {
544 // Check this so we don't end up waiting for ourselves. We need
545 // to return "true" so the caller can continue.
546 LOG(INFO) << *self << " recursive attempt to load library "
547 << "\"" << path_ << "\"";
548 return true;
549 }
550
551 MutexLock mu(jni_on_load_lock_);
552 while (jni_on_load_result_ == kPending) {
553 if (vm->verbose_jni) {
554 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
555 << "JNI_OnLoad...]";
556 }
557 Thread::State old_state = self->GetState();
558 self->SetState(Thread::kWaiting); // TODO: VMWAIT
559 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
560 self->SetState(old_state);
561 }
562
563 bool okay = (jni_on_load_result_ == kOkay);
564 if (vm->verbose_jni) {
565 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
566 << (okay ? "succeeded" : "failed") << "]";
567 }
568 return okay;
569 }
570
571 void SetResult(bool result) {
572 jni_on_load_result_ = result ? kOkay : kFailed;
573 jni_on_load_tid_ = 0;
574
575 // Broadcast a wakeup to anybody sleeping on the condition variable.
576 MutexLock mu(jni_on_load_lock_);
577 pthread_cond_broadcast(&jni_on_load_cond_);
578 }
579
580 void* FindSymbol(const std::string& symbol_name) {
581 return dlsym(handle_, symbol_name.c_str());
582 }
583
584 private:
585 enum JNI_OnLoadState {
586 kPending,
587 kFailed,
588 kOkay,
589 };
590
591 // Path to library "/system/lib/libjni.so".
592 std::string path_;
593
594 // The void* returned by dlopen(3).
595 void* handle_;
596
597 // The ClassLoader this library is associated with.
598 Object* class_loader_;
599
600 // Guards remaining items.
601 Mutex* jni_on_load_lock_;
602 // Wait for JNI_OnLoad in other thread.
603 pthread_cond_t jni_on_load_cond_;
604 // Recursive invocation guard.
605 uint32_t jni_on_load_tid_;
606 // Result of earlier JNI_OnLoad call.
607 JNI_OnLoadState jni_on_load_result_;
608};
609
Elliott Hughescdf53122011-08-19 15:46:09 -0700610} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700611
Elliott Hughes79082e32011-08-25 12:07:32 -0700612// This exists mainly to keep implementation details out of the header file.
613class Libraries {
614 public:
615 Libraries() {
616 }
617
618 ~Libraries() {
619 // Delete our map values. (The keys will be cleaned up by the map itself.)
620 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
621 delete it->second;
622 }
623 }
624
625 SharedLibrary* Get(const std::string& path) {
626 return libraries_[path];
627 }
628
629 void Put(const std::string& path, SharedLibrary* library) {
630 libraries_[path] = library;
631 }
632
633 // See section 11.3 "Linking Native Methods" of the JNI spec.
634 void* FindNativeMethod(const Method* m) {
635 std::string jni_short_name(JniShortName(m));
636 std::string jni_long_name(JniLongName(m));
637 ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
638 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) {
650 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
651 LOG(INFO) << "[Found native code for " << PrettyMethod(m, true)
652 << " in \"" << library->GetPath() << "\"]";
653 }
654 return fn;
655 }
656 }
657 std::string detail;
658 detail += "No implementation found for ";
659 detail += PrettyMethod(m, true);
660 LOG(ERROR) << detail;
661 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
662 "%s", detail.c_str());
663 return NULL;
664 }
665
666 private:
667 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
668
669 std::map<std::string, SharedLibrary*> libraries_;
670};
671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672class JNI {
673 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 static jint GetVersion(JNIEnv* env) {
676 ScopedJniThreadState ts(env);
677 return JNI_VERSION_1_6;
678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
681 ScopedJniThreadState ts(env);
682 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700683 return NULL;
684 }
685
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 static jclass FindClass(JNIEnv* env, const char* name) {
687 ScopedJniThreadState ts(env);
688 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
689 std::string descriptor(NormalizeJniClassDescriptor(name));
690 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700691 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700692 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
697 ScopedJniThreadState ts(env);
698 Method* method = Decode<Method*>(ts, java_method);
699 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700700 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700701
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
703 ScopedJniThreadState ts(env);
704 Field* field = Decode<Field*>(ts, java_field);
705 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
709 ScopedJniThreadState ts(env);
710 Method* method = DecodeMethod(ts, mid);
711 return AddLocalReference<jobject>(ts, method);
712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
715 ScopedJniThreadState ts(env);
716 Field* field = DecodeField(ts, fid);
717 return AddLocalReference<jobject>(ts, field);
718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
721 ScopedJniThreadState ts(env);
722 Object* o = Decode<Object*>(ts, java_object);
723 return AddLocalReference<jclass>(ts, o->GetClass());
724 }
725
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700726 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700728 Class* c = Decode<Class*>(ts, java_class);
729 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 Class* c1 = Decode<Class*>(ts, java_class1);
735 Class* c2 = Decode<Class*>(ts, java_class2);
736 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700738
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700741 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 if (jobj == NULL) {
743 // NB. JNI is different from regular Java instanceof in this respect
744 return JNI_TRUE;
745 } else {
746 Object* obj = Decode<Object*>(ts, jobj);
747 Class* klass = Decode<Class*>(ts, clazz);
748 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
749 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700751
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 static jint Throw(JNIEnv* env, jthrowable java_exception) {
753 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700754 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700755 if (exception == NULL) {
756 return JNI_ERR;
757 }
758 ts.Self()->SetException(exception);
759 return JNI_OK;
760 }
761
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700762 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700764 // TODO: check for a pending exception to decide what constructor to call.
765 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
766 if (mid == NULL) {
767 return JNI_ERR;
768 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700769 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
770 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700771 return JNI_ERR;
772 }
773
774 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 args[0].l = s.get();
776 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
777 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700778 return JNI_ERR;
779 }
780
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700782 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700784
Elliott Hughes37f7a402011-08-22 18:56:01 -0700785 return JNI_OK;
786 }
787
788 static jboolean ExceptionCheck(JNIEnv* env) {
789 ScopedJniThreadState ts(env);
790 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
791 }
792
793 static void ExceptionClear(JNIEnv* env) {
794 ScopedJniThreadState ts(env);
795 ts.Self()->ClearException();
796 }
797
798 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700800
801 Thread* self = ts.Self();
802 Throwable* original_exception = self->GetException();
803 self->ClearException();
804
805 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
806 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
807 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
808 if (mid == NULL) {
809 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
810 << PrettyType(original_exception);
811 } else {
812 env->CallVoidMethod(exception.get(), mid);
813 if (self->IsExceptionPending()) {
814 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
815 << " thrown while calling printStackTrace";
816 self->ClearException();
817 }
818 }
819
820 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700822
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 static jthrowable ExceptionOccurred(JNIEnv* env) {
824 ScopedJniThreadState ts(env);
825 Object* exception = ts.Self()->GetException();
826 if (exception == NULL) {
827 return NULL;
828 } else {
829 // TODO: if adding a local reference failing causes the VM to abort
830 // then the following check will never occur.
831 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
832 if (localException == NULL) {
833 // We were unable to add a new local reference, and threw a new
834 // exception. We can't return "exception", because it's not a
835 // local reference. So we have to return NULL, indicating that
836 // there was no exception, even though it's pretty much raining
837 // exceptions in here.
838 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
839 }
840 return localException;
841 }
842 }
843
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 static void FatalError(JNIEnv* env, const char* msg) {
845 ScopedJniThreadState ts(env);
846 LOG(FATAL) << "JNI FatalError called: " << msg;
847 }
848
849 static jint PushLocalFrame(JNIEnv* env, jint cap) {
850 ScopedJniThreadState ts(env);
851 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
852 return JNI_OK;
853 }
854
855 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
856 ScopedJniThreadState ts(env);
857 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
858 return res;
859 }
860
Elliott Hughes72025e52011-08-23 17:50:30 -0700861 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
862 ScopedJniThreadState ts(env);
863 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
864 return 0;
865 }
866
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
868 ScopedJniThreadState ts(env);
869 if (obj == NULL) {
870 return NULL;
871 }
872
Elliott Hughes75770752011-08-24 17:52:38 -0700873 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 IndirectReferenceTable& globals = vm->globals;
875 MutexLock mu(vm->globals_lock);
876 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
877 return reinterpret_cast<jobject>(ref);
878 }
879
880 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
881 ScopedJniThreadState ts(env);
882 if (obj == NULL) {
883 return;
884 }
885
Elliott Hughes75770752011-08-24 17:52:38 -0700886 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 IndirectReferenceTable& globals = vm->globals;
888 MutexLock mu(vm->globals_lock);
889
890 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
891 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
892 << "failed to find entry";
893 }
894 }
895
896 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
897 ScopedJniThreadState ts(env);
898 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
899 }
900
901 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
902 ScopedJniThreadState ts(env);
903 if (obj == NULL) {
904 return;
905 }
906
Elliott Hughes75770752011-08-24 17:52:38 -0700907 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 IndirectReferenceTable& weak_globals = vm->weak_globals;
909 MutexLock mu(vm->weak_globals_lock);
910
911 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
912 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
913 << "failed to find entry";
914 }
915 }
916
917 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
918 ScopedJniThreadState ts(env);
919 if (obj == NULL) {
920 return NULL;
921 }
922
923 IndirectReferenceTable& locals = ts.Env()->locals;
924
925 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
926 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
927 return reinterpret_cast<jobject>(ref);
928 }
929
930 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
931 ScopedJniThreadState ts(env);
932 if (obj == NULL) {
933 return;
934 }
935
936 IndirectReferenceTable& locals = ts.Env()->locals;
937
938 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
939 if (!locals.Remove(cookie, obj)) {
940 // Attempting to delete a local reference that is not in the
941 // topmost local reference frame is a no-op. DeleteLocalRef returns
942 // void and doesn't throw any exceptions, but we should probably
943 // complain about it so the user will notice that things aren't
944 // going quite the way they expect.
945 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
946 << "failed to find entry";
947 }
948 }
949
950 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
951 ScopedJniThreadState ts(env);
952 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
953 ? JNI_TRUE : JNI_FALSE;
954 }
955
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700956 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700958 Class* c = Decode<Class*>(ts, java_class);
959 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
960 return NULL;
961 }
962 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 ScopedJniThreadState ts(env);
967 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 va_start(args, mid);
969 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 va_end(args);
971 return result;
972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700976 Class* c = Decode<Class*>(ts, java_class);
977 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
978 return NULL;
979 }
980 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 return local_result;
984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700988 Class* c = Decode<Class*>(ts, java_class);
989 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
990 return NULL;
991 }
992 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 return local_result;
996 }
997
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
999 ScopedJniThreadState ts(env);
1000 return FindMethodID(ts, c, name, sig, false);
1001 }
1002
1003 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1004 ScopedJniThreadState ts(env);
1005 return FindMethodID(ts, c, name, sig, true);
1006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 va_list ap;
1011 va_start(ap, mid);
1012 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1013 va_end(ap);
1014 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
1020 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
1026 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
1033 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1034 va_end(ap);
1035 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_list ap;
1051 va_start(ap, mid);
1052 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1053 va_end(ap);
1054 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
1071 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1072 va_end(ap);
1073 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 return InvokeVirtualWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 return InvokeVirtualWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
1090 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1091 va_end(ap);
1092 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 return InvokeVirtualWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 return InvokeVirtualWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_list ap;
1108 va_start(ap, mid);
1109 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1110 va_end(ap);
1111 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 return InvokeVirtualWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 return InvokeVirtualWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_list ap;
1127 va_start(ap, mid);
1128 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1129 va_end(ap);
1130 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 return InvokeVirtualWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 return InvokeVirtualWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_list ap;
1146 va_start(ap, mid);
1147 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1148 va_end(ap);
1149 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 return InvokeVirtualWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 return InvokeVirtualWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_list ap;
1165 va_start(ap, mid);
1166 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1167 va_end(ap);
1168 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 return InvokeVirtualWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 return InvokeVirtualWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_list ap;
1184 va_start(ap, mid);
1185 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1186 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
1202 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_start(ap, mid);
1204 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1206 va_end(ap);
1207 return local_result;
1208 }
1209
1210 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 return AddLocalReference<jobject>(ts, result.l);
1215 }
1216
1217 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 return AddLocalReference<jobject>(ts, result.l);
1222 }
1223
1224 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
1227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
1229 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
1231 return result.z;
1232 }
1233
1234 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
1249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
1251 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_end(ap);
1253 return result.b;
1254 }
1255
1256 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
1273 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return result.c;
1276 }
1277
1278 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
1295 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.s;
1298 }
1299
1300 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
1315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
1317 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 return result.i;
1320 }
1321
1322 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
1334 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
1337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
1339 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
1341 return result.j;
1342 }
1343
1344 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
1356 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
1359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
1361 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_end(ap);
1363 return result.f;
1364 }
1365
1366 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
1372 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
1378 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001379 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
1381 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 va_start(ap, mid);
1383 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 va_end(ap);
1385 return result.d;
1386 }
1387
1388 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
1394 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
1400 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001401 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
1403 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 va_start(ap, mid);
1405 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_end(ap);
1407 }
1408
1409 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001412 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001418 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
1421 static jfieldID GetFieldID(JNIEnv* env,
1422 jclass c, const char* name, const char* sig) {
1423 ScopedJniThreadState ts(env);
1424 return FindFieldID(ts, c, name, sig, false);
1425 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001426
1427
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 static jfieldID GetStaticFieldID(JNIEnv* env,
1429 jclass c, const char* name, const char* sig) {
1430 ScopedJniThreadState ts(env);
1431 return FindFieldID(ts, c, name, sig, true);
1432 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 Object* o = Decode<Object*>(ts, obj);
1437 Field* f = DecodeField(ts, fid);
1438 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001440
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 Field* f = DecodeField(ts, fid);
1444 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 Object* o = Decode<Object*>(ts, java_object);
1450 Object* v = Decode<Object*>(ts, java_value);
1451 Field* f = DecodeField(ts, fid);
1452 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 Object* v = Decode<Object*>(ts, java_value);
1458 Field* f = DecodeField(ts, fid);
1459 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462#define GET_PRIMITIVE_FIELD(fn, instance) \
1463 ScopedJniThreadState ts(env); \
1464 Object* o = Decode<Object*>(ts, instance); \
1465 Field* f = DecodeField(ts, fid); \
1466 return f->fn(o)
1467
1468#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1469 ScopedJniThreadState ts(env); \
1470 Object* o = Decode<Object*>(ts, instance); \
1471 Field* f = DecodeField(ts, fid); \
1472 f->fn(o, value)
1473
1474 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1491 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1495 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1499 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1503 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1507 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1511 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1515 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1519 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1523 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1527 GET_PRIMITIVE_FIELD(GetLong, NULL);
1528 }
1529
1530 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1531 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1532 }
1533
1534 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1535 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1536 }
1537
1538 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1539 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1540 }
1541
1542 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1543 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1544 }
1545
1546 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1547 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1548 }
1549
1550 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1551 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1552 }
1553
1554 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1555 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1556 }
1557
1558 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1559 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1560 }
1561
1562 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1563 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1564 }
1565
1566 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1567 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1568 }
1569
1570 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1571 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1572 }
1573
1574 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1575 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1576 }
1577
1578 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1579 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1580 }
1581
1582 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1583 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1584 }
1585
1586 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1587 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1588 }
1589
1590 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1591 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1592 }
1593
1594 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1595 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1596 }
1597
1598 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1599 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
1602 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
1605 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 va_start(ap, mid);
1607 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1609 va_end(ap);
1610 return local_result;
1611 }
1612
1613 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 return AddLocalReference<jobject>(ts, result.l);
1618 }
1619
1620 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 return AddLocalReference<jobject>(ts, result.l);
1625 }
1626
1627 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
1630 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 va_start(ap, mid);
1632 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return result.z;
1635 }
1636
1637 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
1643 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 ScopedJniThreadState ts(env);
1651 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 va_start(ap, mid);
1653 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_end(ap);
1655 return result.b;
1656 }
1657
1658 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
1664 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
1672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
1674 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
1676 return result.c;
1677 }
1678
1679 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
1685 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
1693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
1695 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_end(ap);
1697 return result.s;
1698 }
1699
1700 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
1706 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
1714 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 va_start(ap, mid);
1716 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
1718 return result.i;
1719 }
1720
1721 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
1727 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
1735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
1737 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_end(ap);
1739 return result.j;
1740 }
1741
1742 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
1748 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
1756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
1758 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_end(ap);
1760 return result.f;
1761 }
1762
1763 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
1769 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes72025e52011-08-23 17:50:30 -07001775 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 ScopedJniThreadState ts(env);
1777 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001778 va_start(ap, mid);
1779 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 va_end(ap);
1781 return result.d;
1782 }
1783
1784 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001787 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
1790 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001793 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes72025e52011-08-23 17:50:30 -07001796 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
1798 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001799 va_start(ap, mid);
1800 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
1802 }
1803
1804 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001807 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001811 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001813 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001818 if (chars == NULL && char_count == 0) {
1819 return NULL;
1820 }
1821 String* result = String::AllocFromUtf16(char_count, chars);
1822 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
1825 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1826 ScopedJniThreadState ts(env);
1827 if (utf == NULL) {
1828 return NULL;
1829 }
1830 String* result = String::AllocFromModifiedUtf8(utf);
1831 return AddLocalReference<jstring>(ts, result);
1832 }
1833
Elliott Hughes814e4032011-08-23 12:07:56 -07001834 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1835 ScopedJniThreadState ts(env);
1836 return Decode<String*>(ts, java_string)->GetLength();
1837 }
1838
1839 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1840 ScopedJniThreadState ts(env);
1841 return Decode<String*>(ts, java_string)->GetUtfLength();
1842 }
1843
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001846 String* s = Decode<String*>(ts, java_string);
1847 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1848 ThrowSIOOBE(ts, start, length, s->GetLength());
1849 } else {
1850 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1851 memcpy(buf, chars + start, length * sizeof(jchar));
1852 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 }
1854
Elliott Hughesb465ab02011-08-24 11:21:21 -07001855 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001857 String* s = Decode<String*>(ts, java_string);
1858 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1859 ThrowSIOOBE(ts, start, length, s->GetLength());
1860 } else {
1861 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1862 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1863 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 }
1865
Elliott Hughes75770752011-08-24 17:52:38 -07001866 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001868 String* s = Decode<String*>(ts, java_string);
1869 const CharArray* chars = s->GetCharArray();
1870 PinPrimitiveArray(ts, chars);
1871 if (is_copy != NULL) {
1872 *is_copy = JNI_FALSE;
1873 }
1874 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 }
1876
Elliott Hughes75770752011-08-24 17:52:38 -07001877 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001878 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001879 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes75770752011-08-24 17:52:38 -07001882 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001884 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes75770752011-08-24 17:52:38 -07001887 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001889 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
Elliott Hughes75770752011-08-24 17:52:38 -07001892 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001893 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001894 if (java_string == NULL) {
1895 return NULL;
1896 }
1897 if (is_copy != NULL) {
1898 *is_copy = JNI_TRUE;
1899 }
1900 String* s = Decode<String*>(ts, java_string);
1901 size_t byte_count = s->GetUtfLength();
1902 char* bytes = new char[byte_count + 1];
1903 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001904 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001905 return NULL;
1906 }
1907 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1908 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1909 bytes[byte_count] = '\0';
1910 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001911 }
1912
Elliott Hughes75770752011-08-24 17:52:38 -07001913 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001915 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 }
1917
Elliott Hughesbd935992011-08-22 11:59:34 -07001918 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001920 Object* obj = Decode<Object*>(ts, java_array);
1921 CHECK(obj->IsArray()); // TODO: ReportJniError
1922 Array* array = obj->AsArray();
1923 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001928 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1929 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
1932 static void SetObjectArrayElement(JNIEnv* env,
1933 jobjectArray java_array, jsize index, jobject java_value) {
1934 ScopedJniThreadState ts(env);
1935 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1936 Object* value = Decode<Object*>(ts, java_value);
1937 array->Set(index, value);
1938 }
1939
1940 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1941 ScopedJniThreadState ts(env);
1942 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1943 }
1944
1945 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1948 }
1949
1950 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1951 ScopedJniThreadState ts(env);
1952 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1953 }
1954
1955 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1958 }
1959
1960 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1961 ScopedJniThreadState ts(env);
1962 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1963 }
1964
1965 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1966 ScopedJniThreadState ts(env);
1967 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1968 }
1969
1970 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1971 ScopedJniThreadState ts(env);
1972 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1973 }
1974
1975 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1976 ScopedJniThreadState ts(env);
1977 CHECK_GE(length, 0); // TODO: ReportJniError
1978
1979 // Compute the array class corresponding to the given element class.
1980 Class* element_class = Decode<Class*>(ts, element_jclass);
1981 std::string descriptor;
1982 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001983 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001984
1985 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001986 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1987 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 return NULL;
1989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 // Allocate and initialize if necessary.
1992 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001994 if (initial_element != NULL) {
1995 Object* initial_object = Decode<Object*>(ts, initial_element);
1996 for (jsize i = 0; i < length; ++i) {
1997 result->Set(i, initial_object);
1998 }
1999 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 return AddLocalReference<jobjectArray>(ts, result);
2001 }
2002
2003 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2004 ScopedJniThreadState ts(env);
2005 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes75770752011-08-24 17:52:38 -07002078 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002080 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes75770752011-08-24 17:52:38 -07002093 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 Class* c = Decode<Class*>(ts, java_class);
2181
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 const char* name = methods[i].name;
2184 const char* sig = methods[i].signature;
2185
2186 if (*sig == '!') {
2187 // TODO: fast jni. it's too noisy to log all these.
2188 ++sig;
2189 }
2190
Elliott Hughes5174fe62011-08-23 15:12:35 -07002191 Method* m = c->FindDirectMethod(name, sig);
2192 if (m == NULL) {
2193 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002195 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002197 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2199 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002200 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002204 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2206 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002207 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 return JNI_ERR;
2209 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002210
Elliott Hughes75770752011-08-24 17:52:38 -07002211 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002212 LOG(INFO) << "[Registering JNI native method "
2213 << PrettyMethod(m, true) << "]";
2214 }
2215
2216 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218 return JNI_OK;
2219 }
2220
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002223 Class* c = Decode<Class*>(ts, java_class);
2224
Elliott Hughes75770752011-08-24 17:52:38 -07002225 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226 LOG(INFO) << "[Unregistering JNI native methods for "
2227 << PrettyDescriptor(c->GetDescriptor()) << "]";
2228 }
2229
2230 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2231 Method* m = c->GetDirectMethod(i);
2232 if (m->IsNative()) {
2233 m->UnregisterNative();
2234 }
2235 }
2236 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2237 Method* m = c->GetVirtualMethod(i);
2238 if (m->IsNative()) {
2239 m->UnregisterNative();
2240 }
2241 }
2242
2243 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Elliott Hughes72025e52011-08-23 17:50:30 -07002246 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002248 Decode<Object*>(ts, java_object)->MonitorEnter();
2249 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughes72025e52011-08-23 17:50:30 -07002252 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002254 Decode<Object*>(ts, java_object)->MonitorEnter();
2255 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
2258 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2259 ScopedJniThreadState ts(env);
2260 Runtime* runtime = Runtime::Current();
2261 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002262 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 } else {
2264 *vm = NULL;
2265 }
2266 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2267 }
2268
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2270 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271
2272 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002273 CHECK(address != NULL); // TODO: ReportJniError
2274 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275
2276 jclass buffer_class = GetDirectByteBufferClass(env);
2277 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2278 if (mid == NULL) {
2279 return NULL;
2280 }
2281
2282 // At the moment, the Java side is limited to 32 bits.
2283 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2284 CHECK_LE(capacity, 0xffffffff);
2285 jint address_arg = reinterpret_cast<jint>(address);
2286 jint capacity_arg = static_cast<jint>(capacity);
2287
2288 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2289 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002294 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2295 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002300 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2301 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306
Elliott Hughes75770752011-08-24 17:52:38 -07002307 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308
2309 // Do we definitely know what kind of reference this is?
2310 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2311 IndirectRefKind kind = GetIndirectRefKind(ref);
2312 switch (kind) {
2313 case kLocal:
2314 return JNILocalRefType;
2315 case kGlobal:
2316 return JNIGlobalRefType;
2317 case kWeakGlobal:
2318 return JNIWeakGlobalRefType;
2319 case kSirtOrInvalid:
2320 // Is it in a stack IRT?
2321 if (ts.Self()->SirtContains(java_object)) {
2322 return JNILocalRefType;
2323 }
2324
2325 // If we're handing out direct pointers, check whether it's a direct pointer
2326 // to a local reference.
2327 // TODO: replace 'false' with the replacement for gDvmJni.workAroundAppJniBugs
2328 if (false && Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
2329 if (ts.Env()->locals.Contains(java_object)) {
2330 return JNILocalRefType;
2331 }
2332 }
2333
2334 return JNIInvalidRefType;
2335 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 }
2337};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002338
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002339static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002340 NULL, // reserved0.
2341 NULL, // reserved1.
2342 NULL, // reserved2.
2343 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 JNI::GetVersion,
2345 JNI::DefineClass,
2346 JNI::FindClass,
2347 JNI::FromReflectedMethod,
2348 JNI::FromReflectedField,
2349 JNI::ToReflectedMethod,
2350 JNI::GetSuperclass,
2351 JNI::IsAssignableFrom,
2352 JNI::ToReflectedField,
2353 JNI::Throw,
2354 JNI::ThrowNew,
2355 JNI::ExceptionOccurred,
2356 JNI::ExceptionDescribe,
2357 JNI::ExceptionClear,
2358 JNI::FatalError,
2359 JNI::PushLocalFrame,
2360 JNI::PopLocalFrame,
2361 JNI::NewGlobalRef,
2362 JNI::DeleteGlobalRef,
2363 JNI::DeleteLocalRef,
2364 JNI::IsSameObject,
2365 JNI::NewLocalRef,
2366 JNI::EnsureLocalCapacity,
2367 JNI::AllocObject,
2368 JNI::NewObject,
2369 JNI::NewObjectV,
2370 JNI::NewObjectA,
2371 JNI::GetObjectClass,
2372 JNI::IsInstanceOf,
2373 JNI::GetMethodID,
2374 JNI::CallObjectMethod,
2375 JNI::CallObjectMethodV,
2376 JNI::CallObjectMethodA,
2377 JNI::CallBooleanMethod,
2378 JNI::CallBooleanMethodV,
2379 JNI::CallBooleanMethodA,
2380 JNI::CallByteMethod,
2381 JNI::CallByteMethodV,
2382 JNI::CallByteMethodA,
2383 JNI::CallCharMethod,
2384 JNI::CallCharMethodV,
2385 JNI::CallCharMethodA,
2386 JNI::CallShortMethod,
2387 JNI::CallShortMethodV,
2388 JNI::CallShortMethodA,
2389 JNI::CallIntMethod,
2390 JNI::CallIntMethodV,
2391 JNI::CallIntMethodA,
2392 JNI::CallLongMethod,
2393 JNI::CallLongMethodV,
2394 JNI::CallLongMethodA,
2395 JNI::CallFloatMethod,
2396 JNI::CallFloatMethodV,
2397 JNI::CallFloatMethodA,
2398 JNI::CallDoubleMethod,
2399 JNI::CallDoubleMethodV,
2400 JNI::CallDoubleMethodA,
2401 JNI::CallVoidMethod,
2402 JNI::CallVoidMethodV,
2403 JNI::CallVoidMethodA,
2404 JNI::CallNonvirtualObjectMethod,
2405 JNI::CallNonvirtualObjectMethodV,
2406 JNI::CallNonvirtualObjectMethodA,
2407 JNI::CallNonvirtualBooleanMethod,
2408 JNI::CallNonvirtualBooleanMethodV,
2409 JNI::CallNonvirtualBooleanMethodA,
2410 JNI::CallNonvirtualByteMethod,
2411 JNI::CallNonvirtualByteMethodV,
2412 JNI::CallNonvirtualByteMethodA,
2413 JNI::CallNonvirtualCharMethod,
2414 JNI::CallNonvirtualCharMethodV,
2415 JNI::CallNonvirtualCharMethodA,
2416 JNI::CallNonvirtualShortMethod,
2417 JNI::CallNonvirtualShortMethodV,
2418 JNI::CallNonvirtualShortMethodA,
2419 JNI::CallNonvirtualIntMethod,
2420 JNI::CallNonvirtualIntMethodV,
2421 JNI::CallNonvirtualIntMethodA,
2422 JNI::CallNonvirtualLongMethod,
2423 JNI::CallNonvirtualLongMethodV,
2424 JNI::CallNonvirtualLongMethodA,
2425 JNI::CallNonvirtualFloatMethod,
2426 JNI::CallNonvirtualFloatMethodV,
2427 JNI::CallNonvirtualFloatMethodA,
2428 JNI::CallNonvirtualDoubleMethod,
2429 JNI::CallNonvirtualDoubleMethodV,
2430 JNI::CallNonvirtualDoubleMethodA,
2431 JNI::CallNonvirtualVoidMethod,
2432 JNI::CallNonvirtualVoidMethodV,
2433 JNI::CallNonvirtualVoidMethodA,
2434 JNI::GetFieldID,
2435 JNI::GetObjectField,
2436 JNI::GetBooleanField,
2437 JNI::GetByteField,
2438 JNI::GetCharField,
2439 JNI::GetShortField,
2440 JNI::GetIntField,
2441 JNI::GetLongField,
2442 JNI::GetFloatField,
2443 JNI::GetDoubleField,
2444 JNI::SetObjectField,
2445 JNI::SetBooleanField,
2446 JNI::SetByteField,
2447 JNI::SetCharField,
2448 JNI::SetShortField,
2449 JNI::SetIntField,
2450 JNI::SetLongField,
2451 JNI::SetFloatField,
2452 JNI::SetDoubleField,
2453 JNI::GetStaticMethodID,
2454 JNI::CallStaticObjectMethod,
2455 JNI::CallStaticObjectMethodV,
2456 JNI::CallStaticObjectMethodA,
2457 JNI::CallStaticBooleanMethod,
2458 JNI::CallStaticBooleanMethodV,
2459 JNI::CallStaticBooleanMethodA,
2460 JNI::CallStaticByteMethod,
2461 JNI::CallStaticByteMethodV,
2462 JNI::CallStaticByteMethodA,
2463 JNI::CallStaticCharMethod,
2464 JNI::CallStaticCharMethodV,
2465 JNI::CallStaticCharMethodA,
2466 JNI::CallStaticShortMethod,
2467 JNI::CallStaticShortMethodV,
2468 JNI::CallStaticShortMethodA,
2469 JNI::CallStaticIntMethod,
2470 JNI::CallStaticIntMethodV,
2471 JNI::CallStaticIntMethodA,
2472 JNI::CallStaticLongMethod,
2473 JNI::CallStaticLongMethodV,
2474 JNI::CallStaticLongMethodA,
2475 JNI::CallStaticFloatMethod,
2476 JNI::CallStaticFloatMethodV,
2477 JNI::CallStaticFloatMethodA,
2478 JNI::CallStaticDoubleMethod,
2479 JNI::CallStaticDoubleMethodV,
2480 JNI::CallStaticDoubleMethodA,
2481 JNI::CallStaticVoidMethod,
2482 JNI::CallStaticVoidMethodV,
2483 JNI::CallStaticVoidMethodA,
2484 JNI::GetStaticFieldID,
2485 JNI::GetStaticObjectField,
2486 JNI::GetStaticBooleanField,
2487 JNI::GetStaticByteField,
2488 JNI::GetStaticCharField,
2489 JNI::GetStaticShortField,
2490 JNI::GetStaticIntField,
2491 JNI::GetStaticLongField,
2492 JNI::GetStaticFloatField,
2493 JNI::GetStaticDoubleField,
2494 JNI::SetStaticObjectField,
2495 JNI::SetStaticBooleanField,
2496 JNI::SetStaticByteField,
2497 JNI::SetStaticCharField,
2498 JNI::SetStaticShortField,
2499 JNI::SetStaticIntField,
2500 JNI::SetStaticLongField,
2501 JNI::SetStaticFloatField,
2502 JNI::SetStaticDoubleField,
2503 JNI::NewString,
2504 JNI::GetStringLength,
2505 JNI::GetStringChars,
2506 JNI::ReleaseStringChars,
2507 JNI::NewStringUTF,
2508 JNI::GetStringUTFLength,
2509 JNI::GetStringUTFChars,
2510 JNI::ReleaseStringUTFChars,
2511 JNI::GetArrayLength,
2512 JNI::NewObjectArray,
2513 JNI::GetObjectArrayElement,
2514 JNI::SetObjectArrayElement,
2515 JNI::NewBooleanArray,
2516 JNI::NewByteArray,
2517 JNI::NewCharArray,
2518 JNI::NewShortArray,
2519 JNI::NewIntArray,
2520 JNI::NewLongArray,
2521 JNI::NewFloatArray,
2522 JNI::NewDoubleArray,
2523 JNI::GetBooleanArrayElements,
2524 JNI::GetByteArrayElements,
2525 JNI::GetCharArrayElements,
2526 JNI::GetShortArrayElements,
2527 JNI::GetIntArrayElements,
2528 JNI::GetLongArrayElements,
2529 JNI::GetFloatArrayElements,
2530 JNI::GetDoubleArrayElements,
2531 JNI::ReleaseBooleanArrayElements,
2532 JNI::ReleaseByteArrayElements,
2533 JNI::ReleaseCharArrayElements,
2534 JNI::ReleaseShortArrayElements,
2535 JNI::ReleaseIntArrayElements,
2536 JNI::ReleaseLongArrayElements,
2537 JNI::ReleaseFloatArrayElements,
2538 JNI::ReleaseDoubleArrayElements,
2539 JNI::GetBooleanArrayRegion,
2540 JNI::GetByteArrayRegion,
2541 JNI::GetCharArrayRegion,
2542 JNI::GetShortArrayRegion,
2543 JNI::GetIntArrayRegion,
2544 JNI::GetLongArrayRegion,
2545 JNI::GetFloatArrayRegion,
2546 JNI::GetDoubleArrayRegion,
2547 JNI::SetBooleanArrayRegion,
2548 JNI::SetByteArrayRegion,
2549 JNI::SetCharArrayRegion,
2550 JNI::SetShortArrayRegion,
2551 JNI::SetIntArrayRegion,
2552 JNI::SetLongArrayRegion,
2553 JNI::SetFloatArrayRegion,
2554 JNI::SetDoubleArrayRegion,
2555 JNI::RegisterNatives,
2556 JNI::UnregisterNatives,
2557 JNI::MonitorEnter,
2558 JNI::MonitorExit,
2559 JNI::GetJavaVM,
2560 JNI::GetStringRegion,
2561 JNI::GetStringUTFRegion,
2562 JNI::GetPrimitiveArrayCritical,
2563 JNI::ReleasePrimitiveArrayCritical,
2564 JNI::GetStringCritical,
2565 JNI::ReleaseStringCritical,
2566 JNI::NewWeakGlobalRef,
2567 JNI::DeleteWeakGlobalRef,
2568 JNI::ExceptionCheck,
2569 JNI::NewDirectByteBuffer,
2570 JNI::GetDirectBufferAddress,
2571 JNI::GetDirectBufferCapacity,
2572 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002573};
2574
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002575static const size_t kMonitorsInitial = 32; // Arbitrary.
2576static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2577
2578static const size_t kLocalsInitial = 64; // Arbitrary.
2579static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002580
Elliott Hughes75770752011-08-24 17:52:38 -07002581JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002582 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002583 vm(vm),
2584 check_jni(vm->check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002585 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002586 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2587 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002588 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002589}
2590
Carl Shapiroea4dca82011-08-01 13:45:38 -07002591// JNI Invocation interface.
2592
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002593extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2594 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2595 if (args->version < JNI_VERSION_1_2) {
2596 return JNI_EVERSION;
2597 }
2598 Runtime::Options options;
2599 for (int i = 0; i < args->nOptions; ++i) {
2600 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002601 options.push_back(std::make_pair(StringPiece(option->optionString),
2602 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002603 }
2604 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002605 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002606 if (runtime == NULL) {
2607 return JNI_ERR;
2608 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002609 *p_env = Thread::Current()->GetJniEnv();
2610 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 return JNI_OK;
2612 }
2613}
2614
Elliott Hughesf2682d52011-08-15 16:37:04 -07002615extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616 Runtime* runtime = Runtime::Current();
2617 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002618 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002619 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002620 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002621 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002622 }
2623 return JNI_OK;
2624}
2625
2626// Historically unsupported.
2627extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2628 return JNI_ERR;
2629}
2630
Elliott Hughescdf53122011-08-19 15:46:09 -07002631class JII {
2632 public:
2633 static jint DestroyJavaVM(JavaVM* vm) {
2634 if (vm == NULL) {
2635 return JNI_ERR;
2636 } else {
2637 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2638 delete raw_vm->runtime;
2639 return JNI_OK;
2640 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642
Elliott Hughescdf53122011-08-19 15:46:09 -07002643 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002644 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002645 }
2646
2647 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002648 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002649 }
2650
2651 static jint DetachCurrentThread(JavaVM* vm) {
2652 if (vm == NULL) {
2653 return JNI_ERR;
2654 } else {
2655 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2656 Runtime* runtime = raw_vm->runtime;
2657 runtime->DetachCurrentThread();
2658 return JNI_OK;
2659 }
2660 }
2661
2662 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2663 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2664 return JNI_EVERSION;
2665 }
2666 if (vm == NULL || env == NULL) {
2667 return JNI_ERR;
2668 }
2669 Thread* thread = Thread::Current();
2670 if (thread == NULL) {
2671 *env = NULL;
2672 return JNI_EDETACHED;
2673 }
2674 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002675 return JNI_OK;
2676 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002677};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002678
Elliott Hughesf2682d52011-08-15 16:37:04 -07002679struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002680 NULL, // reserved0
2681 NULL, // reserved1
2682 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002683 JII::DestroyJavaVM,
2684 JII::AttachCurrentThread,
2685 JII::DetachCurrentThread,
2686 JII::GetEnv,
2687 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002688};
2689
Elliott Hughesbbd76712011-08-17 10:25:24 -07002690static const size_t kPinTableInitialSize = 16;
2691static const size_t kPinTableMaxSize = 1024;
2692
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002693static const size_t kGlobalsInitial = 512; // Arbitrary.
2694static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2695
2696static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2697static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2698
Elliott Hughes0af55432011-08-17 18:37:28 -07002699JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002700 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002701 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002702 verbose_jni(verbose_jni),
Elliott Hughes75770752011-08-24 17:52:38 -07002703 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002704 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002705 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002706 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002707 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002708 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2709 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2710 libraries(new Libraries) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002711 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002712}
2713
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002714JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002715 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002716 delete globals_lock;
2717 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002718 delete libraries_lock;
2719 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002720}
2721
Elliott Hughes75770752011-08-24 17:52:38 -07002722bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2723 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002724
2725 // See if we've already loaded this library. If we have, and the class loader
2726 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002727 // TODO: for better results we should canonicalize the pathname (or even compare
2728 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002729 SharedLibrary* library;
2730 {
2731 // TODO: move the locking (and more of this logic) into Libraries.
2732 MutexLock mu(libraries_lock);
2733 library = libraries->Get(path);
2734 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002735 if (library != NULL) {
2736 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002737 // The library will be associated with class_loader. The JNI
2738 // spec says we can't load the same library into more than one
2739 // class loader.
2740 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2741 "ClassLoader %p; can't open in ClassLoader %p",
2742 path.c_str(), library->GetClassLoader(), class_loader);
2743 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002744 return false;
2745 }
2746 if (verbose_jni) {
2747 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2748 << "ClassLoader " << class_loader << "]";
2749 }
2750 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002751 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2752 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002753 return false;
2754 }
2755 return true;
2756 }
2757
2758 // Open the shared library. Because we're using a full path, the system
2759 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2760 // resolve this library's dependencies though.)
2761
2762 // Failures here are expected when java.library.path has several entries
2763 // and we have to hunt for the lib.
2764
2765 // The current version of the dynamic linker prints detailed information
2766 // about dlopen() failures. Some things to check if the message is
2767 // cryptic:
2768 // - make sure the library exists on the device
2769 // - verify that the right path is being opened (the debug log message
2770 // above can help with that)
2771 // - check to see if the library is valid (e.g. not zero bytes long)
2772 // - check config/prelink-linux-arm.map to ensure that the library
2773 // is listed and is not being overrun by the previous entry (if
2774 // loading suddenly stops working on a prelinked library, this is
2775 // a good one to check)
2776 // - write a trivial app that calls sleep() then dlopen(), attach
2777 // to it with "strace -p <pid>" while it sleeps, and watch for
2778 // attempts to open nonexistent dependent shared libs
2779
2780 // TODO: automate some of these checks!
2781
2782 // This can execute slowly for a large library on a busy system, so we
2783 // want to switch from RUNNING to VMWAIT while it executes. This allows
2784 // the GC to ignore us.
2785 Thread* self = Thread::Current();
2786 Thread::State old_state = self->GetState();
2787 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2788 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2789 self->SetState(old_state);
2790
2791 if (verbose_jni) {
2792 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2793 }
2794
2795 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002796 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002797 return false;
2798 }
2799
2800 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002801 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 // TODO: move the locking (and more of this logic) into Libraries.
2803 MutexLock mu(libraries_lock);
2804 library = libraries->Get(path);
2805 if (library != NULL) {
2806 LOG(INFO) << "WOW: we lost a race to add shared library: "
2807 << "\"" << path << "\" ClassLoader=" << class_loader;
2808 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002809 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002810 library = new SharedLibrary(path, handle, class_loader);
2811 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002812 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002813
2814 if (verbose_jni) {
2815 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2816 }
2817
2818 bool result = true;
2819 void* sym = dlsym(handle, "JNI_OnLoad");
2820 if (sym == NULL) {
2821 if (verbose_jni) {
2822 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2823 }
2824 } else {
2825 // Call JNI_OnLoad. We have to override the current class
2826 // loader, which will always be "null" since the stuff at the
2827 // top of the stack is around Runtime.loadLibrary(). (See
2828 // the comments in the JNI FindClass function.)
2829 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2830 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
2831 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2832 self->SetClassLoaderOverride(class_loader);
2833
2834 old_state = self->GetState();
2835 self->SetState(Thread::kNative);
2836 if (verbose_jni) {
2837 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2838 }
2839 int version = (*jni_on_load)(this, NULL);
2840 self->SetState(old_state);
2841
2842 self->SetClassLoaderOverride(old_class_loader);;
2843
2844 if (version != JNI_VERSION_1_2 &&
2845 version != JNI_VERSION_1_4 &&
2846 version != JNI_VERSION_1_6) {
2847 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2848 << "bad version: " << version;
2849 // It's unwise to call dlclose() here, but we can mark it
2850 // as bad and ensure that future load attempts will fail.
2851 // We don't know how far JNI_OnLoad got, so there could
2852 // be some partially-initialized stuff accessible through
2853 // newly-registered native method calls. We could try to
2854 // unregister them, but that doesn't seem worthwhile.
2855 result = false;
2856 } else {
2857 if (verbose_jni) {
2858 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2859 << " from JNI_OnLoad in \"" << path << "\"]";
2860 }
2861 }
2862 }
2863
2864 library->SetResult(result);
2865 return result;
2866}
2867
2868void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2869 CHECK(m->IsNative());
2870
2871 Class* c = m->GetDeclaringClass();
2872
2873 // If this is a static method, it could be called before the class
2874 // has been initialized.
2875 if (m->IsStatic()) {
2876 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2877 return NULL;
2878 }
2879 } else {
2880 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2881 }
2882
2883 MutexLock mu(libraries_lock);
2884 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002885}
2886
Ian Rogersdf20fe02011-07-20 20:34:16 -07002887} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002888
2889std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2890 switch (rhs) {
2891 case JNIInvalidRefType:
2892 os << "JNIInvalidRefType";
2893 return os;
2894 case JNILocalRefType:
2895 os << "JNILocalRefType";
2896 return os;
2897 case JNIGlobalRefType:
2898 os << "JNIGlobalRefType";
2899 return os;
2900 case JNIWeakGlobalRefType:
2901 os << "JNIWeakGlobalRefType";
2902 return os;
2903 }
2904}