blob: 0d7b2560b3927bfaaab8efdec2af7938cd224a5e [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
24namespace art {
25
Elliott Hughescdf53122011-08-19 15:46:09 -070026// This is private API, but with two different implementations: ARM and x86.
27void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070028
Elliott Hughescdf53122011-08-19 15:46:09 -070029// TODO: this should be in our anonymous namespace, but is currently needed
30// for testing in "jni_internal_test.cc".
Elliott Hughes79082e32011-08-25 12:07:32 -070031void EnsureInvokeStub(Method* method) {
Elliott Hughescdf53122011-08-19 15:46:09 -070032 if (method->GetInvokeStub() != NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -070033 return;
Elliott Hughescdf53122011-08-19 15:46:09 -070034 }
35 // TODO: use signature to find a matching stub
36 // TODO: failed, acquire a lock on the stub table
37 Assembler assembler;
38 CreateInvokeStub(&assembler, method);
39 // TODO: store native_entry in the stub table
40 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
41 size_t length = assembler.CodeSize();
42 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
43 if (addr == MAP_FAILED) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070044 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070045 }
46 MemoryRegion region(addr, length);
47 assembler.FinalizeInstructions(region);
48 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
Elliott Hughescdf53122011-08-19 15:46:09 -070049}
Elliott Hughes0af55432011-08-17 18:37:28 -070050
Elliott Hughescdf53122011-08-19 15:46:09 -070051namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -070052
Elliott Hughes22f40932011-08-12 13:06:37 -070053// Entry/exit processing for all JNI calls.
54//
55// This performs the necessary thread state switching, lets us amortize the
56// cost of working out the current thread, and lets us check (and repair) apps
57// that are using a JNIEnv on the wrong thread.
58class ScopedJniThreadState {
59 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -070060 explicit ScopedJniThreadState(JNIEnv* env)
61 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -070062 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -070063 self_->SetState(Thread::kRunnable);
64 }
65
66 ~ScopedJniThreadState() {
67 self_->SetState(Thread::kNative);
68 }
69
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070 JNIEnvExt* Env() {
71 return env_;
72 }
73
Elliott Hughesb20a5542011-08-12 18:03:12 -070074 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -070075 return self_;
76 }
77
Elliott Hughes75770752011-08-24 17:52:38 -070078 JavaVMExt* Vm() {
79 return env_->vm;
80 }
81
Elliott Hughesb20a5542011-08-12 18:03:12 -070082 private:
83 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -070084 // TODO: need replacement for gDvmJni.
85 bool workAroundAppJniBugs = true;
86 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
87 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
88 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -070089 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
90 << " used on " << *self;
91 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -070092 }
93 return self;
94 }
95
Elliott Hughesc5f7c912011-08-18 14:00:42 -070096 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -070097 Thread* self_;
98 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
99};
100
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101/*
102 * Add a local reference for an object to the current stack frame. When
103 * the native function returns, the reference will be discarded.
104 *
105 * We need to allow the same reference to be added multiple times.
106 *
107 * This will be called on otherwise unreferenced objects. We cannot do
108 * GC allocations here, and it's best if we don't grab a mutex.
109 *
110 * Returns the local reference (currently just the same pointer that was
111 * passed in), or NULL on failure.
112 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700113template<typename T>
114T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700115 if (obj == NULL) {
116 return NULL;
117 }
118
119 IndirectReferenceTable& locals = ts.Env()->locals;
120
121 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
122 IndirectRef ref = locals.Add(cookie, obj);
123 if (ref == NULL) {
124 // TODO: just change Add's DCHECK to CHECK and lose this?
125 locals.Dump();
126 LOG(FATAL) << "Failed adding to JNI local reference table "
127 << "(has " << locals.Capacity() << " entries)";
128 // TODO: dvmDumpThread(dvmThreadSelf(), false);
129 }
130
131#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
132 if (ts.Env()->check_jni) {
133 size_t entry_count = locals.Capacity();
134 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700135 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700136 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700137 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700138 locals.Dump();
139 // TODO: dvmDumpThread(dvmThreadSelf(), false);
140 // dvmAbort();
141 }
142 }
143#endif
144
145 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
146 // Hand out direct pointers to support broken old apps.
147 return reinterpret_cast<T>(obj);
148 }
149
150 return reinterpret_cast<T>(ref);
151}
152
Elliott Hughescdf53122011-08-19 15:46:09 -0700153jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
154 if (obj == NULL) {
155 return NULL;
156 }
Elliott Hughes75770752011-08-24 17:52:38 -0700157 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700158 IndirectReferenceTable& weak_globals = vm->weak_globals;
159 MutexLock mu(vm->weak_globals_lock);
160 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
161 return reinterpret_cast<jweak>(ref);
162}
163
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700164template<typename T>
165T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700166 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700167}
168
Elliott Hughescdf53122011-08-19 15:46:09 -0700169Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
170 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
171}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700172
Elliott Hughescdf53122011-08-19 15:46:09 -0700173Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
174 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700175}
176
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700177byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 size_t num_bytes = method->NumArgArrayBytes();
179 scoped_array<byte> arg_array(new byte[num_bytes]);
180 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700181 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700182 switch (shorty[i]) {
183 case 'Z':
184 case 'B':
185 case 'C':
186 case 'S':
187 case 'I':
188 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
189 offset += 4;
190 break;
191 case 'F':
192 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
193 offset += 4;
194 break;
195 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700196 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
198 offset += sizeof(Object*);
199 break;
200 }
201 case 'D':
202 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
203 offset += 8;
204 break;
205 case 'J':
206 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
207 offset += 8;
208 break;
209 }
210 }
211 return arg_array.release();
212}
213
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700214byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700215 size_t num_bytes = method->NumArgArrayBytes();
216 scoped_array<byte> arg_array(new byte[num_bytes]);
217 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700218 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700219 switch (shorty[i]) {
220 case 'Z':
221 case 'B':
222 case 'C':
223 case 'S':
224 case 'I':
225 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
226 offset += 4;
227 break;
228 case 'F':
229 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
230 offset += 4;
231 break;
232 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700233 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700234 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
235 offset += sizeof(Object*);
236 break;
237 }
238 case 'D':
239 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
240 offset += 8;
241 break;
242 case 'J':
243 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
244 offset += 8;
245 break;
246 }
247 }
248 return arg_array.release();
249}
250
Elliott Hughes72025e52011-08-23 17:50:30 -0700251JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
252 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700253 Thread* self = ts.Self();
254
255 // Push a transition back into managed code onto the linked list in thread
256 CHECK_EQ(Thread::kRunnable, self->GetState());
257 NativeToManagedRecord record;
258 self->PushNativeToManagedRecord(&record);
259
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700260 // Call the invoke stub associated with the method
261 // Pass everything as arguments
262 const Method::InvokeStub* stub = method->GetInvokeStub();
263 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700264
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700265 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700266 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700267 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700268 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700269 LOG(WARNING) << "Not invoking method with no associated code: "
270 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700271 result.j = 0;
272 }
buzbeec143c552011-08-20 17:38:58 -0700273
Ian Rogers6de08602011-08-19 14:52:39 -0700274 // Pop transition
275 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700276 return result;
277}
278
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700279JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700280 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700281 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700282 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700283 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700284 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700285}
286
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700287JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700288 jmethodID mid, va_list 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());
293}
294
Elliott Hughes75770752011-08-24 17:52:38 -0700295Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700296 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700297}
298
Brian Carlstrom30b94452011-08-25 21:35:26 -0700299JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700300 Object* receiver = Decode<Object*>(ts, obj);
301 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
302 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
303 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
304}
305
Brian Carlstrom30b94452011-08-25 21:35:26 -0700306JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700307 Object* receiver = Decode<Object*>(ts, obj);
308 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
309 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
310 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Elliott Hughes6b436852011-08-12 10:16:44 -0700313// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
314// separated with slashes but aren't wrapped with "L;" like regular descriptors
315// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
316// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
317// supported names with dots too (such as "a.b.C").
318std::string NormalizeJniClassDescriptor(const char* name) {
319 std::string result;
320 // Add the missing "L;" if necessary.
321 if (name[0] == '[') {
322 result = name;
323 } else {
324 result += 'L';
325 result += name;
326 result += ';';
327 }
328 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700329 if (result.find('.') != std::string::npos) {
330 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
331 << "\"" << name << "\"";
332 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700333 }
334 return result;
335}
336
Elliott Hughescdf53122011-08-19 15:46:09 -0700337jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
338 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700339 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
340 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700341 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700342
343 Method* method = NULL;
344 if (is_static) {
345 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700346 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 method = c->FindVirtualMethod(name, sig);
348 if (method == NULL) {
349 // No virtual method matching the signature. Search declared
350 // private methods and constructors.
351 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700352 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700353 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700354
Elliott Hughescdf53122011-08-19 15:46:09 -0700355 if (method == NULL || method->IsStatic() != is_static) {
356 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700357 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700358 // TODO: try searching for the opposite kind of method from is_static
359 // for better diagnostics?
360 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700361 "no %s method %s", is_static ? "static" : "non-static",
362 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700363 return NULL;
364 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365
Elliott Hughes79082e32011-08-25 12:07:32 -0700366 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700367
Elliott Hughescdf53122011-08-19 15:46:09 -0700368 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700369}
370
Elliott Hughescdf53122011-08-19 15:46:09 -0700371jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
372 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700373 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
374 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700375 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700376
377 Field* field = NULL;
378 if (is_static) {
379 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700380 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700381 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700382 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700383
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 if (field == NULL) {
385 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700386 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700387 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
388 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700389 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700390 return NULL;
391 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700392
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 jweak fid = AddWeakGlobalReference(ts, field);
394 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700395}
396
Elliott Hughes75770752011-08-24 17:52:38 -0700397void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
398 JavaVMExt* vm = ts.Vm();
399 MutexLock mu(vm->pins_lock);
400 vm->pin_table.Add(array);
401}
402
403void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
404 JavaVMExt* vm = ts.Vm();
405 MutexLock mu(vm->pins_lock);
406 vm->pin_table.Remove(array);
407}
408
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700409template<typename JniT, typename ArtT>
410JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
411 CHECK_GE(length, 0); // TODO: ReportJniError
412 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700413 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700414}
415
Elliott Hughes75770752011-08-24 17:52:38 -0700416template <typename ArrayT, typename CArrayT, typename ArtArrayT>
417CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
418 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
419 PinPrimitiveArray(ts, array);
420 if (is_copy != NULL) {
421 *is_copy = JNI_FALSE;
422 }
423 return array->GetData();
424}
425
426template <typename ArrayT>
427void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
428 if (mode != JNI_COMMIT) {
429 Array* array = Decode<Array*>(ts, java_array);
430 UnpinPrimitiveArray(ts, array);
431 }
432}
433
Elliott Hughes814e4032011-08-23 12:07:56 -0700434void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
435 std::string type(PrettyType(array));
436 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
437 "%s offset=%d length=%d %s.length=%d",
438 type.c_str(), start, length, identifier, array->GetLength());
439}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700440void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
441 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
442 "offset=%d length=%d string.length()=%d", start, length, array_length);
443}
Elliott Hughes814e4032011-08-23 12:07:56 -0700444
445template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700446void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700447 ArrayT* array = Decode<ArrayT*>(ts, java_array);
448 if (start < 0 || length < 0 || start + length > array->GetLength()) {
449 ThrowAIOOBE(ts, array, start, length, "src");
450 } else {
451 JavaT* data = array->GetData();
452 memcpy(buf, data + start, length * sizeof(JavaT));
453 }
454}
455
456template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700457void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700458 ArrayT* array = Decode<ArrayT*>(ts, java_array);
459 if (start < 0 || length < 0 || start + length > array->GetLength()) {
460 ThrowAIOOBE(ts, array, start, length, "dst");
461 } else {
462 JavaT* data = array->GetData();
463 memcpy(data + start, buf, length * sizeof(JavaT));
464 }
465}
466
Elliott Hughes75770752011-08-24 17:52:38 -0700467jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700468 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
469 CHECK(buffer_class.get() != NULL);
470 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
471}
472
Elliott Hughes75770752011-08-24 17:52:38 -0700473jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700474 static jclass buffer_class = InitDirectByteBufferClass(env);
475 return buffer_class;
476}
477
Elliott Hughes75770752011-08-24 17:52:38 -0700478jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
479 if (vm == NULL || p_env == NULL) {
480 return JNI_ERR;
481 }
482
483 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
484 JavaVMAttachArgs args;
485 if (thr_args == NULL) {
486 // Allow the v1.1 calling convention.
487 args.version = JNI_VERSION_1_2;
488 args.name = NULL;
489 args.group = NULL; // TODO: get "main" thread group
490 } else {
491 args.version = in_args->version;
492 args.name = in_args->name;
493 if (in_args->group != NULL) {
494 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
495 args.group = NULL; // TODO: decode in_args->group
496 } else {
497 args.group = NULL; // TODO: get "main" thread group
498 }
499 }
500 CHECK_GE(args.version, JNI_VERSION_1_2);
501
502 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
503 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
504}
505
Elliott Hughes79082e32011-08-25 12:07:32 -0700506class SharedLibrary {
507 public:
508 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
509 : path_(path),
510 handle_(handle),
511 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
512 jni_on_load_tid_(Thread::Current()->GetId()),
513 jni_on_load_result_(kPending) {
514 pthread_cond_init(&jni_on_load_cond_, NULL);
515 }
516
517 ~SharedLibrary() {
518 delete jni_on_load_lock_;
519 }
520
521 Object* GetClassLoader() {
522 return class_loader_;
523 }
524
525 std::string GetPath() {
526 return path_;
527 }
528
529 /*
530 * Check the result of an earlier call to JNI_OnLoad on this library. If
531 * the call has not yet finished in another thread, wait for it.
532 */
533 bool CheckOnLoadResult(JavaVMExt* vm) {
534 Thread* self = Thread::Current();
535 if (jni_on_load_tid_ == self->GetId()) {
536 // Check this so we don't end up waiting for ourselves. We need
537 // to return "true" so the caller can continue.
538 LOG(INFO) << *self << " recursive attempt to load library "
539 << "\"" << path_ << "\"";
540 return true;
541 }
542
543 MutexLock mu(jni_on_load_lock_);
544 while (jni_on_load_result_ == kPending) {
545 if (vm->verbose_jni) {
546 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
547 << "JNI_OnLoad...]";
548 }
549 Thread::State old_state = self->GetState();
550 self->SetState(Thread::kWaiting); // TODO: VMWAIT
551 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
552 self->SetState(old_state);
553 }
554
555 bool okay = (jni_on_load_result_ == kOkay);
556 if (vm->verbose_jni) {
557 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
558 << (okay ? "succeeded" : "failed") << "]";
559 }
560 return okay;
561 }
562
563 void SetResult(bool result) {
564 jni_on_load_result_ = result ? kOkay : kFailed;
565 jni_on_load_tid_ = 0;
566
567 // Broadcast a wakeup to anybody sleeping on the condition variable.
568 MutexLock mu(jni_on_load_lock_);
569 pthread_cond_broadcast(&jni_on_load_cond_);
570 }
571
572 void* FindSymbol(const std::string& symbol_name) {
573 return dlsym(handle_, symbol_name.c_str());
574 }
575
576 private:
577 enum JNI_OnLoadState {
578 kPending,
579 kFailed,
580 kOkay,
581 };
582
583 // Path to library "/system/lib/libjni.so".
584 std::string path_;
585
586 // The void* returned by dlopen(3).
587 void* handle_;
588
589 // The ClassLoader this library is associated with.
590 Object* class_loader_;
591
592 // Guards remaining items.
593 Mutex* jni_on_load_lock_;
594 // Wait for JNI_OnLoad in other thread.
595 pthread_cond_t jni_on_load_cond_;
596 // Recursive invocation guard.
597 uint32_t jni_on_load_tid_;
598 // Result of earlier JNI_OnLoad call.
599 JNI_OnLoadState jni_on_load_result_;
600};
601
Elliott Hughescdf53122011-08-19 15:46:09 -0700602} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700603
Elliott Hughes79082e32011-08-25 12:07:32 -0700604// This exists mainly to keep implementation details out of the header file.
605class Libraries {
606 public:
607 Libraries() {
608 }
609
610 ~Libraries() {
611 // Delete our map values. (The keys will be cleaned up by the map itself.)
612 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
613 delete it->second;
614 }
615 }
616
617 SharedLibrary* Get(const std::string& path) {
618 return libraries_[path];
619 }
620
621 void Put(const std::string& path, SharedLibrary* library) {
622 libraries_[path] = library;
623 }
624
625 // See section 11.3 "Linking Native Methods" of the JNI spec.
626 void* FindNativeMethod(const Method* m) {
627 std::string jni_short_name(JniShortName(m));
628 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700629 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700630 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
631 SharedLibrary* library = it->second;
632 if (library->GetClassLoader() != declaring_class_loader) {
633 // We only search libraries loaded by the appropriate ClassLoader.
634 continue;
635 }
636 // Try the short name then the long name...
637 void* fn = library->FindSymbol(jni_short_name);
638 if (fn == NULL) {
639 fn = library->FindSymbol(jni_long_name);
640 }
641 if (fn != NULL) {
642 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
643 LOG(INFO) << "[Found native code for " << PrettyMethod(m, true)
644 << " in \"" << library->GetPath() << "\"]";
645 }
646 return fn;
647 }
648 }
649 std::string detail;
650 detail += "No implementation found for ";
651 detail += PrettyMethod(m, true);
652 LOG(ERROR) << detail;
653 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
654 "%s", detail.c_str());
655 return NULL;
656 }
657
658 private:
659 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
660
661 std::map<std::string, SharedLibrary*> libraries_;
662};
663
Elliott Hughescdf53122011-08-19 15:46:09 -0700664class JNI {
665 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 static jint GetVersion(JNIEnv* env) {
668 ScopedJniThreadState ts(env);
669 return JNI_VERSION_1_6;
670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
673 ScopedJniThreadState ts(env);
674 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700675 return NULL;
676 }
677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jclass FindClass(JNIEnv* env, const char* name) {
679 ScopedJniThreadState ts(env);
680 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
681 std::string descriptor(NormalizeJniClassDescriptor(name));
682 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700683 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700684 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
689 ScopedJniThreadState ts(env);
690 Method* method = Decode<Method*>(ts, java_method);
691 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
695 ScopedJniThreadState ts(env);
696 Field* field = Decode<Field*>(ts, java_field);
697 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
698 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700699
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
701 ScopedJniThreadState ts(env);
702 Method* method = DecodeMethod(ts, mid);
703 return AddLocalReference<jobject>(ts, method);
704 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700705
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
707 ScopedJniThreadState ts(env);
708 Field* field = DecodeField(ts, fid);
709 return AddLocalReference<jobject>(ts, field);
710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
713 ScopedJniThreadState ts(env);
714 Object* o = Decode<Object*>(ts, java_object);
715 return AddLocalReference<jclass>(ts, o->GetClass());
716 }
717
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700718 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700720 Class* c = Decode<Class*>(ts, java_class);
721 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700726 Class* c1 = Decode<Class*>(ts, java_class1);
727 Class* c2 = Decode<Class*>(ts, java_class2);
728 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700730
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700733 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 if (jobj == NULL) {
735 // NB. JNI is different from regular Java instanceof in this respect
736 return JNI_TRUE;
737 } else {
738 Object* obj = Decode<Object*>(ts, jobj);
739 Class* klass = Decode<Class*>(ts, clazz);
740 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
741 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700743
Elliott Hughes37f7a402011-08-22 18:56:01 -0700744 static jint Throw(JNIEnv* env, jthrowable java_exception) {
745 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700746 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700747 if (exception == NULL) {
748 return JNI_ERR;
749 }
750 ts.Self()->SetException(exception);
751 return JNI_OK;
752 }
753
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700754 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700755 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700756 // TODO: check for a pending exception to decide what constructor to call.
757 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
758 if (mid == NULL) {
759 return JNI_ERR;
760 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
762 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700763 return JNI_ERR;
764 }
765
766 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 args[0].l = s.get();
768 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
769 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700770 return JNI_ERR;
771 }
772
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700774 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 return JNI_OK;
778 }
779
780 static jboolean ExceptionCheck(JNIEnv* env) {
781 ScopedJniThreadState ts(env);
782 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
783 }
784
785 static void ExceptionClear(JNIEnv* env) {
786 ScopedJniThreadState ts(env);
787 ts.Self()->ClearException();
788 }
789
790 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700792
793 Thread* self = ts.Self();
794 Throwable* original_exception = self->GetException();
795 self->ClearException();
796
797 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
798 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
799 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
800 if (mid == NULL) {
801 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
802 << PrettyType(original_exception);
803 } else {
804 env->CallVoidMethod(exception.get(), mid);
805 if (self->IsExceptionPending()) {
806 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
807 << " thrown while calling printStackTrace";
808 self->ClearException();
809 }
810 }
811
812 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700814
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 static jthrowable ExceptionOccurred(JNIEnv* env) {
816 ScopedJniThreadState ts(env);
817 Object* exception = ts.Self()->GetException();
818 if (exception == NULL) {
819 return NULL;
820 } else {
821 // TODO: if adding a local reference failing causes the VM to abort
822 // then the following check will never occur.
823 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
824 if (localException == NULL) {
825 // We were unable to add a new local reference, and threw a new
826 // exception. We can't return "exception", because it's not a
827 // local reference. So we have to return NULL, indicating that
828 // there was no exception, even though it's pretty much raining
829 // exceptions in here.
830 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
831 }
832 return localException;
833 }
834 }
835
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 static void FatalError(JNIEnv* env, const char* msg) {
837 ScopedJniThreadState ts(env);
838 LOG(FATAL) << "JNI FatalError called: " << msg;
839 }
840
841 static jint PushLocalFrame(JNIEnv* env, jint cap) {
842 ScopedJniThreadState ts(env);
843 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
844 return JNI_OK;
845 }
846
847 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
848 ScopedJniThreadState ts(env);
849 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
850 return res;
851 }
852
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
854 ScopedJniThreadState ts(env);
855 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
856 return 0;
857 }
858
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
860 ScopedJniThreadState ts(env);
861 if (obj == NULL) {
862 return NULL;
863 }
864
Elliott Hughes75770752011-08-24 17:52:38 -0700865 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 IndirectReferenceTable& globals = vm->globals;
867 MutexLock mu(vm->globals_lock);
868 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
869 return reinterpret_cast<jobject>(ref);
870 }
871
872 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
873 ScopedJniThreadState ts(env);
874 if (obj == NULL) {
875 return;
876 }
877
Elliott Hughes75770752011-08-24 17:52:38 -0700878 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 IndirectReferenceTable& globals = vm->globals;
880 MutexLock mu(vm->globals_lock);
881
882 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
883 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
884 << "failed to find entry";
885 }
886 }
887
888 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
889 ScopedJniThreadState ts(env);
890 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
891 }
892
893 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
894 ScopedJniThreadState ts(env);
895 if (obj == NULL) {
896 return;
897 }
898
Elliott Hughes75770752011-08-24 17:52:38 -0700899 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 IndirectReferenceTable& weak_globals = vm->weak_globals;
901 MutexLock mu(vm->weak_globals_lock);
902
903 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
904 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
905 << "failed to find entry";
906 }
907 }
908
909 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
910 ScopedJniThreadState ts(env);
911 if (obj == NULL) {
912 return NULL;
913 }
914
915 IndirectReferenceTable& locals = ts.Env()->locals;
916
917 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
918 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
919 return reinterpret_cast<jobject>(ref);
920 }
921
922 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
923 ScopedJniThreadState ts(env);
924 if (obj == NULL) {
925 return;
926 }
927
928 IndirectReferenceTable& locals = ts.Env()->locals;
929
930 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
931 if (!locals.Remove(cookie, obj)) {
932 // Attempting to delete a local reference that is not in the
933 // topmost local reference frame is a no-op. DeleteLocalRef returns
934 // void and doesn't throw any exceptions, but we should probably
935 // complain about it so the user will notice that things aren't
936 // going quite the way they expect.
937 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
938 << "failed to find entry";
939 }
940 }
941
942 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
943 ScopedJniThreadState ts(env);
944 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
945 ? JNI_TRUE : JNI_FALSE;
946 }
947
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700948 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700950 Class* c = Decode<Class*>(ts, java_class);
951 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
952 return NULL;
953 }
954 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 ScopedJniThreadState ts(env);
959 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_start(args, mid);
961 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 va_end(args);
963 return result;
964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700968 Class* c = Decode<Class*>(ts, java_class);
969 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
970 return NULL;
971 }
972 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 return local_result;
976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700980 Class* c = Decode<Class*>(ts, java_class);
981 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
982 return NULL;
983 }
984 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 return local_result;
988 }
989
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
991 ScopedJniThreadState ts(env);
992 return FindMethodID(ts, c, name, sig, false);
993 }
994
995 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
996 ScopedJniThreadState ts(env);
997 return FindMethodID(ts, c, name, sig, true);
998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_list ap;
1003 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001004 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
1006 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001011 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_list ap;
1024 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001025 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 va_end(ap);
1027 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001032 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001037 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_list ap;
1043 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001044 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_end(ap);
1046 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001051 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001056 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_list ap;
1062 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001063 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_end(ap);
1065 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001070 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001075 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_list ap;
1081 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001082 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_end(ap);
1084 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001089 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001094 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_list ap;
1100 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001101 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_end(ap);
1103 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001108 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001113 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_list ap;
1119 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001120 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_end(ap);
1122 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001127 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001132 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_list ap;
1138 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001139 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_end(ap);
1141 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001146 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001151 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_list ap;
1157 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001158 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 va_end(ap);
1160 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001165 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001170 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_list ap;
1176 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001177 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001183 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001188 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
1194 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_start(ap, mid);
1196 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1198 va_end(ap);
1199 return local_result;
1200 }
1201
1202 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 return AddLocalReference<jobject>(ts, result.l);
1207 }
1208
1209 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 return AddLocalReference<jobject>(ts, result.l);
1214 }
1215
1216 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
1219 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 va_start(ap, mid);
1221 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 va_end(ap);
1223 return result.z;
1224 }
1225
1226 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
1232 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
1238 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
1241 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 va_start(ap, mid);
1243 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_end(ap);
1245 return result.b;
1246 }
1247
1248 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
1254 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 ScopedJniThreadState ts(env);
1263 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 va_start(ap, mid);
1265 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 va_end(ap);
1267 return result.c;
1268 }
1269
1270 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
1276 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 ScopedJniThreadState ts(env);
1285 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 va_start(ap, mid);
1287 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 va_end(ap);
1289 return result.s;
1290 }
1291
1292 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
1298 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
1304 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
1307 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 va_start(ap, mid);
1309 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_end(ap);
1311 return result.i;
1312 }
1313
1314 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
1320 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
1326 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 ScopedJniThreadState ts(env);
1329 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 va_start(ap, mid);
1331 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_end(ap);
1333 return result.j;
1334 }
1335
1336 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
1342 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
1348 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
1351 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 va_start(ap, mid);
1353 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 va_end(ap);
1355 return result.f;
1356 }
1357
1358 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
1364 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
1370 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001371 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
1373 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 va_start(ap, mid);
1375 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 va_end(ap);
1377 return result.d;
1378 }
1379
1380 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001383 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
1386 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
1395 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 va_start(ap, mid);
1397 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 va_end(ap);
1399 }
1400
1401 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001402 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
1407 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
1413 static jfieldID GetFieldID(JNIEnv* env,
1414 jclass c, const char* name, const char* sig) {
1415 ScopedJniThreadState ts(env);
1416 return FindFieldID(ts, c, name, sig, false);
1417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001418
1419
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 static jfieldID GetStaticFieldID(JNIEnv* env,
1421 jclass c, const char* name, const char* sig) {
1422 ScopedJniThreadState ts(env);
1423 return FindFieldID(ts, c, name, sig, true);
1424 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 Object* o = Decode<Object*>(ts, obj);
1429 Field* f = DecodeField(ts, fid);
1430 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001432
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 Field* f = DecodeField(ts, fid);
1436 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 Object* o = Decode<Object*>(ts, java_object);
1442 Object* v = Decode<Object*>(ts, java_value);
1443 Field* f = DecodeField(ts, fid);
1444 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static void SetStaticObjectField(JNIEnv* env, jclass, 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* v = Decode<Object*>(ts, java_value);
1450 Field* f = DecodeField(ts, fid);
1451 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454#define GET_PRIMITIVE_FIELD(fn, instance) \
1455 ScopedJniThreadState ts(env); \
1456 Object* o = Decode<Object*>(ts, instance); \
1457 Field* f = DecodeField(ts, fid); \
1458 return f->fn(o)
1459
1460#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1461 ScopedJniThreadState ts(env); \
1462 Object* o = Decode<Object*>(ts, instance); \
1463 Field* f = DecodeField(ts, fid); \
1464 f->fn(o, value)
1465
1466 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1491 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1495 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1499 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1503 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1507 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1511 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1515 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1519 GET_PRIMITIVE_FIELD(GetLong, NULL);
1520 }
1521
1522 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1523 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1524 }
1525
1526 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1527 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1528 }
1529
1530 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1531 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1532 }
1533
1534 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1535 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1536 }
1537
1538 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1539 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1540 }
1541
1542 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1543 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1544 }
1545
1546 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1547 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1548 }
1549
1550 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1551 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1552 }
1553
1554 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1555 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1556 }
1557
1558 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1559 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1560 }
1561
1562 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1563 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1564 }
1565
1566 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1567 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1568 }
1569
1570 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1571 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1572 }
1573
1574 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1575 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1576 }
1577
1578 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1579 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1580 }
1581
1582 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1583 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1584 }
1585
1586 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1587 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1588 }
1589
1590 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1591 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
1594 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
1597 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 va_start(ap, mid);
1599 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1601 va_end(ap);
1602 return local_result;
1603 }
1604
1605 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 return AddLocalReference<jobject>(ts, result.l);
1610 }
1611
1612 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 return AddLocalReference<jobject>(ts, result.l);
1617 }
1618
1619 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 ScopedJniThreadState ts(env);
1622 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 va_start(ap, mid);
1624 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 va_end(ap);
1626 return result.z;
1627 }
1628
1629 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
1635 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
1643 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 va_start(ap, mid);
1645 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 va_end(ap);
1647 return result.b;
1648 }
1649
1650 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
1656 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
1664 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 va_start(ap, mid);
1666 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 va_end(ap);
1668 return result.c;
1669 }
1670
1671 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
1677 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
1685 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 va_start(ap, mid);
1687 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 va_end(ap);
1689 return result.s;
1690 }
1691
1692 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
1698 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
1706 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 va_start(ap, mid);
1708 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_end(ap);
1710 return result.i;
1711 }
1712
1713 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
1719 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
1727 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 va_start(ap, mid);
1729 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_end(ap);
1731 return result.j;
1732 }
1733
1734 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
1740 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 ScopedJniThreadState ts(env);
1748 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 va_start(ap, mid);
1750 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_end(ap);
1752 return result.f;
1753 }
1754
1755 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
1761 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
1769 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 va_start(ap, mid);
1771 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 va_end(ap);
1773 return result.d;
1774 }
1775
1776 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001777 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
1782 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes72025e52011-08-23 17:50:30 -07001788 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
1790 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 va_start(ap, mid);
1792 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 va_end(ap);
1794 }
1795
1796 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001797 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001799 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001810 if (chars == NULL && char_count == 0) {
1811 return NULL;
1812 }
1813 String* result = String::AllocFromUtf16(char_count, chars);
1814 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1818 ScopedJniThreadState ts(env);
1819 if (utf == NULL) {
1820 return NULL;
1821 }
1822 String* result = String::AllocFromModifiedUtf8(utf);
1823 return AddLocalReference<jstring>(ts, result);
1824 }
1825
Elliott Hughes814e4032011-08-23 12:07:56 -07001826 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1827 ScopedJniThreadState ts(env);
1828 return Decode<String*>(ts, java_string)->GetLength();
1829 }
1830
1831 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1832 ScopedJniThreadState ts(env);
1833 return Decode<String*>(ts, java_string)->GetUtfLength();
1834 }
1835
Elliott Hughesb465ab02011-08-24 11:21:21 -07001836 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001838 String* s = Decode<String*>(ts, java_string);
1839 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1840 ThrowSIOOBE(ts, start, length, s->GetLength());
1841 } else {
1842 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1843 memcpy(buf, chars + start, length * sizeof(jchar));
1844 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001845 }
1846
Elliott Hughesb465ab02011-08-24 11:21:21 -07001847 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001848 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001849 String* s = Decode<String*>(ts, java_string);
1850 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1851 ThrowSIOOBE(ts, start, length, s->GetLength());
1852 } else {
1853 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1854 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1855 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 String* s = Decode<String*>(ts, java_string);
1861 const CharArray* chars = s->GetCharArray();
1862 PinPrimitiveArray(ts, chars);
1863 if (is_copy != NULL) {
1864 *is_copy = JNI_FALSE;
1865 }
1866 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001871 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes75770752011-08-24 17:52:38 -07001879 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001881 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes75770752011-08-24 17:52:38 -07001884 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001885 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001886 if (java_string == NULL) {
1887 return NULL;
1888 }
1889 if (is_copy != NULL) {
1890 *is_copy = JNI_TRUE;
1891 }
1892 String* s = Decode<String*>(ts, java_string);
1893 size_t byte_count = s->GetUtfLength();
1894 char* bytes = new char[byte_count + 1];
1895 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001896 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001897 return NULL;
1898 }
1899 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1900 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1901 bytes[byte_count] = '\0';
1902 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001903 }
1904
Elliott Hughes75770752011-08-24 17:52:38 -07001905 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001907 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 }
1909
Elliott Hughesbd935992011-08-22 11:59:34 -07001910 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001912 Object* obj = Decode<Object*>(ts, java_array);
1913 CHECK(obj->IsArray()); // TODO: ReportJniError
1914 Array* array = obj->AsArray();
1915 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 }
1917
Elliott Hughes814e4032011-08-23 12:07:56 -07001918 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1921 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
1924 static void SetObjectArrayElement(JNIEnv* env,
1925 jobjectArray java_array, jsize index, jobject java_value) {
1926 ScopedJniThreadState ts(env);
1927 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1928 Object* value = Decode<Object*>(ts, java_value);
1929 array->Set(index, value);
1930 }
1931
1932 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1933 ScopedJniThreadState ts(env);
1934 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1935 }
1936
1937 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1938 ScopedJniThreadState ts(env);
1939 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1940 }
1941
1942 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1943 ScopedJniThreadState ts(env);
1944 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1945 }
1946
1947 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1948 ScopedJniThreadState ts(env);
1949 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1950 }
1951
1952 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1953 ScopedJniThreadState ts(env);
1954 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1955 }
1956
1957 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1958 ScopedJniThreadState ts(env);
1959 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1960 }
1961
1962 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1963 ScopedJniThreadState ts(env);
1964 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1965 }
1966
1967 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1968 ScopedJniThreadState ts(env);
1969 CHECK_GE(length, 0); // TODO: ReportJniError
1970
1971 // Compute the array class corresponding to the given element class.
1972 Class* element_class = Decode<Class*>(ts, element_jclass);
1973 std::string descriptor;
1974 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001975 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001976
1977 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001978 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1979 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 return NULL;
1981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 // Allocate and initialize if necessary.
1984 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 if (initial_element != NULL) {
1987 Object* initial_object = Decode<Object*>(ts, initial_element);
1988 for (jsize i = 0; i < length; ++i) {
1989 result->Set(i, initial_object);
1990 }
1991 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 return AddLocalReference<jobjectArray>(ts, result);
1993 }
1994
1995 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1996 ScopedJniThreadState ts(env);
1997 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002052 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002057 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes75770752011-08-24 17:52:38 -07002060 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002062 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes75770752011-08-24 17:52:38 -07002065 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002067 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes75770752011-08-24 17:52:38 -07002070 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002072 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes75770752011-08-24 17:52:38 -07002075 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002077 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes75770752011-08-24 17:52:38 -07002080 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002082 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes75770752011-08-24 17:52:38 -07002085 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002087 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 Class* c = Decode<Class*>(ts, java_class);
2173
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 const char* name = methods[i].name;
2176 const char* sig = methods[i].signature;
2177
2178 if (*sig == '!') {
2179 // TODO: fast jni. it's too noisy to log all these.
2180 ++sig;
2181 }
2182
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 Method* m = c->FindDirectMethod(name, sig);
2184 if (m == NULL) {
2185 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002189 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2191 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002192 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2198 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002199 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 return JNI_ERR;
2201 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202
Elliott Hughes75770752011-08-24 17:52:38 -07002203 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002204 LOG(INFO) << "[Registering JNI native method "
2205 << PrettyMethod(m, true) << "]";
2206 }
2207
2208 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210 return JNI_OK;
2211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 Class* c = Decode<Class*>(ts, java_class);
2216
Elliott Hughes75770752011-08-24 17:52:38 -07002217 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002218 LOG(INFO) << "[Unregistering JNI native methods for "
2219 << PrettyDescriptor(c->GetDescriptor()) << "]";
2220 }
2221
2222 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2223 Method* m = c->GetDirectMethod(i);
2224 if (m->IsNative()) {
2225 m->UnregisterNative();
2226 }
2227 }
2228 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2229 Method* m = c->GetVirtualMethod(i);
2230 if (m->IsNative()) {
2231 m->UnregisterNative();
2232 }
2233 }
2234
2235 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughes72025e52011-08-23 17:50:30 -07002238 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002240 Decode<Object*>(ts, java_object)->MonitorEnter();
2241 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes72025e52011-08-23 17:50:30 -07002244 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002246 Decode<Object*>(ts, java_object)->MonitorEnter();
2247 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
2250 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2251 ScopedJniThreadState ts(env);
2252 Runtime* runtime = Runtime::Current();
2253 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002254 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 } else {
2256 *vm = NULL;
2257 }
2258 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2259 }
2260
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2262 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263
2264 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002265 CHECK(address != NULL); // TODO: ReportJniError
2266 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002267
2268 jclass buffer_class = GetDirectByteBufferClass(env);
2269 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2270 if (mid == NULL) {
2271 return NULL;
2272 }
2273
2274 // At the moment, the Java side is limited to 32 bits.
2275 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2276 CHECK_LE(capacity, 0xffffffff);
2277 jint address_arg = reinterpret_cast<jint>(address);
2278 jint capacity_arg = static_cast<jint>(capacity);
2279
2280 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2281 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2287 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2293 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298
Elliott Hughes75770752011-08-24 17:52:38 -07002299 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002300
2301 // Do we definitely know what kind of reference this is?
2302 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2303 IndirectRefKind kind = GetIndirectRefKind(ref);
2304 switch (kind) {
2305 case kLocal:
2306 return JNILocalRefType;
2307 case kGlobal:
2308 return JNIGlobalRefType;
2309 case kWeakGlobal:
2310 return JNIWeakGlobalRefType;
2311 case kSirtOrInvalid:
2312 // Is it in a stack IRT?
2313 if (ts.Self()->SirtContains(java_object)) {
2314 return JNILocalRefType;
2315 }
2316
2317 // If we're handing out direct pointers, check whether it's a direct pointer
2318 // to a local reference.
2319 // TODO: replace 'false' with the replacement for gDvmJni.workAroundAppJniBugs
2320 if (false && Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
2321 if (ts.Env()->locals.Contains(java_object)) {
2322 return JNILocalRefType;
2323 }
2324 }
2325
2326 return JNIInvalidRefType;
2327 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002330
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002331static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002332 NULL, // reserved0.
2333 NULL, // reserved1.
2334 NULL, // reserved2.
2335 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 JNI::GetVersion,
2337 JNI::DefineClass,
2338 JNI::FindClass,
2339 JNI::FromReflectedMethod,
2340 JNI::FromReflectedField,
2341 JNI::ToReflectedMethod,
2342 JNI::GetSuperclass,
2343 JNI::IsAssignableFrom,
2344 JNI::ToReflectedField,
2345 JNI::Throw,
2346 JNI::ThrowNew,
2347 JNI::ExceptionOccurred,
2348 JNI::ExceptionDescribe,
2349 JNI::ExceptionClear,
2350 JNI::FatalError,
2351 JNI::PushLocalFrame,
2352 JNI::PopLocalFrame,
2353 JNI::NewGlobalRef,
2354 JNI::DeleteGlobalRef,
2355 JNI::DeleteLocalRef,
2356 JNI::IsSameObject,
2357 JNI::NewLocalRef,
2358 JNI::EnsureLocalCapacity,
2359 JNI::AllocObject,
2360 JNI::NewObject,
2361 JNI::NewObjectV,
2362 JNI::NewObjectA,
2363 JNI::GetObjectClass,
2364 JNI::IsInstanceOf,
2365 JNI::GetMethodID,
2366 JNI::CallObjectMethod,
2367 JNI::CallObjectMethodV,
2368 JNI::CallObjectMethodA,
2369 JNI::CallBooleanMethod,
2370 JNI::CallBooleanMethodV,
2371 JNI::CallBooleanMethodA,
2372 JNI::CallByteMethod,
2373 JNI::CallByteMethodV,
2374 JNI::CallByteMethodA,
2375 JNI::CallCharMethod,
2376 JNI::CallCharMethodV,
2377 JNI::CallCharMethodA,
2378 JNI::CallShortMethod,
2379 JNI::CallShortMethodV,
2380 JNI::CallShortMethodA,
2381 JNI::CallIntMethod,
2382 JNI::CallIntMethodV,
2383 JNI::CallIntMethodA,
2384 JNI::CallLongMethod,
2385 JNI::CallLongMethodV,
2386 JNI::CallLongMethodA,
2387 JNI::CallFloatMethod,
2388 JNI::CallFloatMethodV,
2389 JNI::CallFloatMethodA,
2390 JNI::CallDoubleMethod,
2391 JNI::CallDoubleMethodV,
2392 JNI::CallDoubleMethodA,
2393 JNI::CallVoidMethod,
2394 JNI::CallVoidMethodV,
2395 JNI::CallVoidMethodA,
2396 JNI::CallNonvirtualObjectMethod,
2397 JNI::CallNonvirtualObjectMethodV,
2398 JNI::CallNonvirtualObjectMethodA,
2399 JNI::CallNonvirtualBooleanMethod,
2400 JNI::CallNonvirtualBooleanMethodV,
2401 JNI::CallNonvirtualBooleanMethodA,
2402 JNI::CallNonvirtualByteMethod,
2403 JNI::CallNonvirtualByteMethodV,
2404 JNI::CallNonvirtualByteMethodA,
2405 JNI::CallNonvirtualCharMethod,
2406 JNI::CallNonvirtualCharMethodV,
2407 JNI::CallNonvirtualCharMethodA,
2408 JNI::CallNonvirtualShortMethod,
2409 JNI::CallNonvirtualShortMethodV,
2410 JNI::CallNonvirtualShortMethodA,
2411 JNI::CallNonvirtualIntMethod,
2412 JNI::CallNonvirtualIntMethodV,
2413 JNI::CallNonvirtualIntMethodA,
2414 JNI::CallNonvirtualLongMethod,
2415 JNI::CallNonvirtualLongMethodV,
2416 JNI::CallNonvirtualLongMethodA,
2417 JNI::CallNonvirtualFloatMethod,
2418 JNI::CallNonvirtualFloatMethodV,
2419 JNI::CallNonvirtualFloatMethodA,
2420 JNI::CallNonvirtualDoubleMethod,
2421 JNI::CallNonvirtualDoubleMethodV,
2422 JNI::CallNonvirtualDoubleMethodA,
2423 JNI::CallNonvirtualVoidMethod,
2424 JNI::CallNonvirtualVoidMethodV,
2425 JNI::CallNonvirtualVoidMethodA,
2426 JNI::GetFieldID,
2427 JNI::GetObjectField,
2428 JNI::GetBooleanField,
2429 JNI::GetByteField,
2430 JNI::GetCharField,
2431 JNI::GetShortField,
2432 JNI::GetIntField,
2433 JNI::GetLongField,
2434 JNI::GetFloatField,
2435 JNI::GetDoubleField,
2436 JNI::SetObjectField,
2437 JNI::SetBooleanField,
2438 JNI::SetByteField,
2439 JNI::SetCharField,
2440 JNI::SetShortField,
2441 JNI::SetIntField,
2442 JNI::SetLongField,
2443 JNI::SetFloatField,
2444 JNI::SetDoubleField,
2445 JNI::GetStaticMethodID,
2446 JNI::CallStaticObjectMethod,
2447 JNI::CallStaticObjectMethodV,
2448 JNI::CallStaticObjectMethodA,
2449 JNI::CallStaticBooleanMethod,
2450 JNI::CallStaticBooleanMethodV,
2451 JNI::CallStaticBooleanMethodA,
2452 JNI::CallStaticByteMethod,
2453 JNI::CallStaticByteMethodV,
2454 JNI::CallStaticByteMethodA,
2455 JNI::CallStaticCharMethod,
2456 JNI::CallStaticCharMethodV,
2457 JNI::CallStaticCharMethodA,
2458 JNI::CallStaticShortMethod,
2459 JNI::CallStaticShortMethodV,
2460 JNI::CallStaticShortMethodA,
2461 JNI::CallStaticIntMethod,
2462 JNI::CallStaticIntMethodV,
2463 JNI::CallStaticIntMethodA,
2464 JNI::CallStaticLongMethod,
2465 JNI::CallStaticLongMethodV,
2466 JNI::CallStaticLongMethodA,
2467 JNI::CallStaticFloatMethod,
2468 JNI::CallStaticFloatMethodV,
2469 JNI::CallStaticFloatMethodA,
2470 JNI::CallStaticDoubleMethod,
2471 JNI::CallStaticDoubleMethodV,
2472 JNI::CallStaticDoubleMethodA,
2473 JNI::CallStaticVoidMethod,
2474 JNI::CallStaticVoidMethodV,
2475 JNI::CallStaticVoidMethodA,
2476 JNI::GetStaticFieldID,
2477 JNI::GetStaticObjectField,
2478 JNI::GetStaticBooleanField,
2479 JNI::GetStaticByteField,
2480 JNI::GetStaticCharField,
2481 JNI::GetStaticShortField,
2482 JNI::GetStaticIntField,
2483 JNI::GetStaticLongField,
2484 JNI::GetStaticFloatField,
2485 JNI::GetStaticDoubleField,
2486 JNI::SetStaticObjectField,
2487 JNI::SetStaticBooleanField,
2488 JNI::SetStaticByteField,
2489 JNI::SetStaticCharField,
2490 JNI::SetStaticShortField,
2491 JNI::SetStaticIntField,
2492 JNI::SetStaticLongField,
2493 JNI::SetStaticFloatField,
2494 JNI::SetStaticDoubleField,
2495 JNI::NewString,
2496 JNI::GetStringLength,
2497 JNI::GetStringChars,
2498 JNI::ReleaseStringChars,
2499 JNI::NewStringUTF,
2500 JNI::GetStringUTFLength,
2501 JNI::GetStringUTFChars,
2502 JNI::ReleaseStringUTFChars,
2503 JNI::GetArrayLength,
2504 JNI::NewObjectArray,
2505 JNI::GetObjectArrayElement,
2506 JNI::SetObjectArrayElement,
2507 JNI::NewBooleanArray,
2508 JNI::NewByteArray,
2509 JNI::NewCharArray,
2510 JNI::NewShortArray,
2511 JNI::NewIntArray,
2512 JNI::NewLongArray,
2513 JNI::NewFloatArray,
2514 JNI::NewDoubleArray,
2515 JNI::GetBooleanArrayElements,
2516 JNI::GetByteArrayElements,
2517 JNI::GetCharArrayElements,
2518 JNI::GetShortArrayElements,
2519 JNI::GetIntArrayElements,
2520 JNI::GetLongArrayElements,
2521 JNI::GetFloatArrayElements,
2522 JNI::GetDoubleArrayElements,
2523 JNI::ReleaseBooleanArrayElements,
2524 JNI::ReleaseByteArrayElements,
2525 JNI::ReleaseCharArrayElements,
2526 JNI::ReleaseShortArrayElements,
2527 JNI::ReleaseIntArrayElements,
2528 JNI::ReleaseLongArrayElements,
2529 JNI::ReleaseFloatArrayElements,
2530 JNI::ReleaseDoubleArrayElements,
2531 JNI::GetBooleanArrayRegion,
2532 JNI::GetByteArrayRegion,
2533 JNI::GetCharArrayRegion,
2534 JNI::GetShortArrayRegion,
2535 JNI::GetIntArrayRegion,
2536 JNI::GetLongArrayRegion,
2537 JNI::GetFloatArrayRegion,
2538 JNI::GetDoubleArrayRegion,
2539 JNI::SetBooleanArrayRegion,
2540 JNI::SetByteArrayRegion,
2541 JNI::SetCharArrayRegion,
2542 JNI::SetShortArrayRegion,
2543 JNI::SetIntArrayRegion,
2544 JNI::SetLongArrayRegion,
2545 JNI::SetFloatArrayRegion,
2546 JNI::SetDoubleArrayRegion,
2547 JNI::RegisterNatives,
2548 JNI::UnregisterNatives,
2549 JNI::MonitorEnter,
2550 JNI::MonitorExit,
2551 JNI::GetJavaVM,
2552 JNI::GetStringRegion,
2553 JNI::GetStringUTFRegion,
2554 JNI::GetPrimitiveArrayCritical,
2555 JNI::ReleasePrimitiveArrayCritical,
2556 JNI::GetStringCritical,
2557 JNI::ReleaseStringCritical,
2558 JNI::NewWeakGlobalRef,
2559 JNI::DeleteWeakGlobalRef,
2560 JNI::ExceptionCheck,
2561 JNI::NewDirectByteBuffer,
2562 JNI::GetDirectBufferAddress,
2563 JNI::GetDirectBufferCapacity,
2564 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002565};
2566
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002567static const size_t kMonitorsInitial = 32; // Arbitrary.
2568static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2569
2570static const size_t kLocalsInitial = 64; // Arbitrary.
2571static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002572
Elliott Hughes75770752011-08-24 17:52:38 -07002573JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002574 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002575 vm(vm),
2576 check_jni(vm->check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002577 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002578 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2579 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002580 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002581}
2582
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002583JNIEnvExt::~JNIEnvExt() {
2584}
2585
Carl Shapiroea4dca82011-08-01 13:45:38 -07002586// JNI Invocation interface.
2587
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002588extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2589 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2590 if (args->version < JNI_VERSION_1_2) {
2591 return JNI_EVERSION;
2592 }
2593 Runtime::Options options;
2594 for (int i = 0; i < args->nOptions; ++i) {
2595 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002596 options.push_back(std::make_pair(StringPiece(option->optionString),
2597 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598 }
2599 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002600 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002601 if (runtime == NULL) {
2602 return JNI_ERR;
2603 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002604 *p_env = Thread::Current()->GetJniEnv();
2605 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002606 return JNI_OK;
2607 }
2608}
2609
Elliott Hughesf2682d52011-08-15 16:37:04 -07002610extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 Runtime* runtime = Runtime::Current();
2612 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002613 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002615 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002616 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 }
2618 return JNI_OK;
2619}
2620
2621// Historically unsupported.
2622extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2623 return JNI_ERR;
2624}
2625
Elliott Hughescdf53122011-08-19 15:46:09 -07002626class JII {
2627 public:
2628 static jint DestroyJavaVM(JavaVM* vm) {
2629 if (vm == NULL) {
2630 return JNI_ERR;
2631 } else {
2632 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2633 delete raw_vm->runtime;
2634 return JNI_OK;
2635 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637
Elliott Hughescdf53122011-08-19 15:46:09 -07002638 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002639 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002640 }
2641
2642 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002643 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002644 }
2645
2646 static jint DetachCurrentThread(JavaVM* vm) {
2647 if (vm == NULL) {
2648 return JNI_ERR;
2649 } else {
2650 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2651 Runtime* runtime = raw_vm->runtime;
2652 runtime->DetachCurrentThread();
2653 return JNI_OK;
2654 }
2655 }
2656
2657 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2658 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2659 return JNI_EVERSION;
2660 }
2661 if (vm == NULL || env == NULL) {
2662 return JNI_ERR;
2663 }
2664 Thread* thread = Thread::Current();
2665 if (thread == NULL) {
2666 *env = NULL;
2667 return JNI_EDETACHED;
2668 }
2669 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002670 return JNI_OK;
2671 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002672};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673
Elliott Hughesf2682d52011-08-15 16:37:04 -07002674struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002675 NULL, // reserved0
2676 NULL, // reserved1
2677 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002678 JII::DestroyJavaVM,
2679 JII::AttachCurrentThread,
2680 JII::DetachCurrentThread,
2681 JII::GetEnv,
2682 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002683};
2684
Elliott Hughesbbd76712011-08-17 10:25:24 -07002685static const size_t kPinTableInitialSize = 16;
2686static const size_t kPinTableMaxSize = 1024;
2687
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002688static const size_t kGlobalsInitial = 512; // Arbitrary.
2689static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2690
2691static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2692static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2693
Elliott Hughes0af55432011-08-17 18:37:28 -07002694JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002695 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002696 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002697 verbose_jni(verbose_jni),
Elliott Hughes75770752011-08-24 17:52:38 -07002698 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002699 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002700 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002701 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002702 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002703 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2704 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2705 libraries(new Libraries) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002706 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002707}
2708
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002709JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002710 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002711 delete globals_lock;
2712 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002713 delete libraries_lock;
2714 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002715}
2716
Elliott Hughes75770752011-08-24 17:52:38 -07002717bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2718 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002719
2720 // See if we've already loaded this library. If we have, and the class loader
2721 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002722 // TODO: for better results we should canonicalize the pathname (or even compare
2723 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002724 SharedLibrary* library;
2725 {
2726 // TODO: move the locking (and more of this logic) into Libraries.
2727 MutexLock mu(libraries_lock);
2728 library = libraries->Get(path);
2729 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002730 if (library != NULL) {
2731 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002732 // The library will be associated with class_loader. The JNI
2733 // spec says we can't load the same library into more than one
2734 // class loader.
2735 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2736 "ClassLoader %p; can't open in ClassLoader %p",
2737 path.c_str(), library->GetClassLoader(), class_loader);
2738 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002739 return false;
2740 }
2741 if (verbose_jni) {
2742 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2743 << "ClassLoader " << class_loader << "]";
2744 }
2745 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002746 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2747 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002748 return false;
2749 }
2750 return true;
2751 }
2752
2753 // Open the shared library. Because we're using a full path, the system
2754 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2755 // resolve this library's dependencies though.)
2756
2757 // Failures here are expected when java.library.path has several entries
2758 // and we have to hunt for the lib.
2759
2760 // The current version of the dynamic linker prints detailed information
2761 // about dlopen() failures. Some things to check if the message is
2762 // cryptic:
2763 // - make sure the library exists on the device
2764 // - verify that the right path is being opened (the debug log message
2765 // above can help with that)
2766 // - check to see if the library is valid (e.g. not zero bytes long)
2767 // - check config/prelink-linux-arm.map to ensure that the library
2768 // is listed and is not being overrun by the previous entry (if
2769 // loading suddenly stops working on a prelinked library, this is
2770 // a good one to check)
2771 // - write a trivial app that calls sleep() then dlopen(), attach
2772 // to it with "strace -p <pid>" while it sleeps, and watch for
2773 // attempts to open nonexistent dependent shared libs
2774
2775 // TODO: automate some of these checks!
2776
2777 // This can execute slowly for a large library on a busy system, so we
2778 // want to switch from RUNNING to VMWAIT while it executes. This allows
2779 // the GC to ignore us.
2780 Thread* self = Thread::Current();
2781 Thread::State old_state = self->GetState();
2782 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2783 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2784 self->SetState(old_state);
2785
2786 if (verbose_jni) {
2787 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2788 }
2789
2790 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002791 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 return false;
2793 }
2794
2795 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002797 // TODO: move the locking (and more of this logic) into Libraries.
2798 MutexLock mu(libraries_lock);
2799 library = libraries->Get(path);
2800 if (library != NULL) {
2801 LOG(INFO) << "WOW: we lost a race to add shared library: "
2802 << "\"" << path << "\" ClassLoader=" << class_loader;
2803 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002805 library = new SharedLibrary(path, handle, class_loader);
2806 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002808
2809 if (verbose_jni) {
2810 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2811 }
2812
2813 bool result = true;
2814 void* sym = dlsym(handle, "JNI_OnLoad");
2815 if (sym == NULL) {
2816 if (verbose_jni) {
2817 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2818 }
2819 } else {
2820 // Call JNI_OnLoad. We have to override the current class
2821 // loader, which will always be "null" since the stuff at the
2822 // top of the stack is around Runtime.loadLibrary(). (See
2823 // the comments in the JNI FindClass function.)
2824 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2825 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002826 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002827 self->SetClassLoaderOverride(class_loader);
2828
2829 old_state = self->GetState();
2830 self->SetState(Thread::kNative);
2831 if (verbose_jni) {
2832 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2833 }
2834 int version = (*jni_on_load)(this, NULL);
2835 self->SetState(old_state);
2836
2837 self->SetClassLoaderOverride(old_class_loader);;
2838
2839 if (version != JNI_VERSION_1_2 &&
2840 version != JNI_VERSION_1_4 &&
2841 version != JNI_VERSION_1_6) {
2842 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2843 << "bad version: " << version;
2844 // It's unwise to call dlclose() here, but we can mark it
2845 // as bad and ensure that future load attempts will fail.
2846 // We don't know how far JNI_OnLoad got, so there could
2847 // be some partially-initialized stuff accessible through
2848 // newly-registered native method calls. We could try to
2849 // unregister them, but that doesn't seem worthwhile.
2850 result = false;
2851 } else {
2852 if (verbose_jni) {
2853 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2854 << " from JNI_OnLoad in \"" << path << "\"]";
2855 }
2856 }
2857 }
2858
2859 library->SetResult(result);
2860 return result;
2861}
2862
2863void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2864 CHECK(m->IsNative());
2865
2866 Class* c = m->GetDeclaringClass();
2867
2868 // If this is a static method, it could be called before the class
2869 // has been initialized.
2870 if (m->IsStatic()) {
2871 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2872 return NULL;
2873 }
2874 } else {
2875 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2876 }
2877
2878 MutexLock mu(libraries_lock);
2879 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002880}
2881
Ian Rogersdf20fe02011-07-20 20:34:16 -07002882} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002883
2884std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2885 switch (rhs) {
2886 case JNIInvalidRefType:
2887 os << "JNIInvalidRefType";
2888 return os;
2889 case JNILocalRefType:
2890 os << "JNILocalRefType";
2891 return os;
2892 case JNIGlobalRefType:
2893 os << "JNIGlobalRefType";
2894 return os;
2895 case JNIWeakGlobalRefType:
2896 os << "JNIWeakGlobalRefType";
2897 return os;
2898 }
2899}