blob: 8485cede231a268a8498b813820a4321c3b46e6a [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070014#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070015#include "class_linker.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070016#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070018#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070019#include "runtime.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070020#include "scoped_ptr.h"
21#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070023
buzbeec143c552011-08-20 17:38:58 -070024extern bool oatCompileMethod(art::Method*, art::InstructionSet);
25
Ian Rogersdf20fe02011-07-20 20:34:16 -070026namespace art {
27
Elliott Hughescdf53122011-08-19 15:46:09 -070028// This is private API, but with two different implementations: ARM and x86.
29void CreateInvokeStub(Assembler* assembler, Method* method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070030
Elliott Hughescdf53122011-08-19 15:46:09 -070031// TODO: this should be in our anonymous namespace, but is currently needed
32// for testing in "jni_internal_test.cc".
Elliott Hughes79082e32011-08-25 12:07:32 -070033void EnsureInvokeStub(Method* method) {
Elliott Hughescdf53122011-08-19 15:46:09 -070034 if (method->GetInvokeStub() != NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -070035 return;
Elliott Hughescdf53122011-08-19 15:46:09 -070036 }
37 // TODO: use signature to find a matching stub
38 // TODO: failed, acquire a lock on the stub table
39 Assembler assembler;
40 CreateInvokeStub(&assembler, method);
41 // TODO: store native_entry in the stub table
42 int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
43 size_t length = assembler.CodeSize();
44 void* addr = mmap(NULL, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
45 if (addr == MAP_FAILED) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070046 PLOG(FATAL) << "mmap failed for " << PrettyMethod(method, true);
Elliott Hughescdf53122011-08-19 15:46:09 -070047 }
48 MemoryRegion region(addr, length);
49 assembler.FinalizeInstructions(region);
50 method->SetInvokeStub(reinterpret_cast<Method::InvokeStub*>(region.pointer()));
Elliott Hughescdf53122011-08-19 15:46:09 -070051}
Elliott Hughes0af55432011-08-17 18:37:28 -070052
Elliott Hughescdf53122011-08-19 15:46:09 -070053namespace {
Elliott Hughes0af55432011-08-17 18:37:28 -070054
Elliott Hughes22f40932011-08-12 13:06:37 -070055// Entry/exit processing for all JNI calls.
56//
57// This performs the necessary thread state switching, lets us amortize the
58// cost of working out the current thread, and lets us check (and repair) apps
59// that are using a JNIEnv on the wrong thread.
60class ScopedJniThreadState {
61 public:
Elliott Hughesc5f7c912011-08-18 14:00:42 -070062 explicit ScopedJniThreadState(JNIEnv* env)
63 : env_(reinterpret_cast<JNIEnvExt*>(env)) {
Elliott Hughesb20a5542011-08-12 18:03:12 -070064 self_ = ThreadForEnv(env);
Elliott Hughes22f40932011-08-12 13:06:37 -070065 self_->SetState(Thread::kRunnable);
66 }
67
68 ~ScopedJniThreadState() {
69 self_->SetState(Thread::kNative);
70 }
71
Elliott Hughesc5f7c912011-08-18 14:00:42 -070072 JNIEnvExt* Env() {
73 return env_;
74 }
75
Elliott Hughesb20a5542011-08-12 18:03:12 -070076 Thread* Self() {
Elliott Hughes22f40932011-08-12 13:06:37 -070077 return self_;
78 }
79
Elliott Hughes75770752011-08-24 17:52:38 -070080 JavaVMExt* Vm() {
81 return env_->vm;
82 }
83
Elliott Hughesb20a5542011-08-12 18:03:12 -070084 private:
85 static Thread* ThreadForEnv(JNIEnv* env) {
Elliott Hughes22f40932011-08-12 13:06:37 -070086 // TODO: need replacement for gDvmJni.
87 bool workAroundAppJniBugs = true;
88 Thread* env_self = reinterpret_cast<JNIEnvExt*>(env)->self;
89 Thread* self = workAroundAppJniBugs ? Thread::Current() : env_self;
90 if (self != env_self) {
Elliott Hughes330304d2011-08-12 14:28:05 -070091 LOG(ERROR) << "JNI ERROR: JNIEnv for " << *env_self
92 << " used on " << *self;
93 // TODO: dump stack
Elliott Hughes22f40932011-08-12 13:06:37 -070094 }
95 return self;
96 }
97
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 JNIEnvExt* env_;
Elliott Hughes22f40932011-08-12 13:06:37 -070099 Thread* self_;
100 DISALLOW_COPY_AND_ASSIGN(ScopedJniThreadState);
101};
102
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103/*
104 * Add a local reference for an object to the current stack frame. When
105 * the native function returns, the reference will be discarded.
106 *
107 * We need to allow the same reference to be added multiple times.
108 *
109 * This will be called on otherwise unreferenced objects. We cannot do
110 * GC allocations here, and it's best if we don't grab a mutex.
111 *
112 * Returns the local reference (currently just the same pointer that was
113 * passed in), or NULL on failure.
114 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700115template<typename T>
116T AddLocalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117 if (obj == NULL) {
118 return NULL;
119 }
120
121 IndirectReferenceTable& locals = ts.Env()->locals;
122
123 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
124 IndirectRef ref = locals.Add(cookie, obj);
125 if (ref == NULL) {
126 // TODO: just change Add's DCHECK to CHECK and lose this?
127 locals.Dump();
128 LOG(FATAL) << "Failed adding to JNI local reference table "
129 << "(has " << locals.Capacity() << " entries)";
130 // TODO: dvmDumpThread(dvmThreadSelf(), false);
131 }
132
133#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
134 if (ts.Env()->check_jni) {
135 size_t entry_count = locals.Capacity();
136 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700137 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700138 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700139 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700140 locals.Dump();
141 // TODO: dvmDumpThread(dvmThreadSelf(), false);
142 // dvmAbort();
143 }
144 }
145#endif
146
147 if (false /*gDvmJni.workAroundAppJniBugs*/) { // TODO
148 // Hand out direct pointers to support broken old apps.
149 return reinterpret_cast<T>(obj);
150 }
151
152 return reinterpret_cast<T>(ref);
153}
154
Elliott Hughescdf53122011-08-19 15:46:09 -0700155jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
156 if (obj == NULL) {
157 return NULL;
158 }
Elliott Hughes75770752011-08-24 17:52:38 -0700159 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700160 IndirectReferenceTable& weak_globals = vm->weak_globals;
161 MutexLock mu(vm->weak_globals_lock);
162 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
163 return reinterpret_cast<jweak>(ref);
164}
165
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700166template<typename T>
167T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700168 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700169}
170
Elliott Hughescdf53122011-08-19 15:46:09 -0700171Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
172 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
173}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700174
Elliott Hughescdf53122011-08-19 15:46:09 -0700175Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
176 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700177}
178
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700179byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700180 size_t num_bytes = method->NumArgArrayBytes();
181 scoped_array<byte> arg_array(new byte[num_bytes]);
182 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700183 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 switch (shorty[i]) {
185 case 'Z':
186 case 'B':
187 case 'C':
188 case 'S':
189 case 'I':
190 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
191 offset += 4;
192 break;
193 case 'F':
194 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
195 offset += 4;
196 break;
197 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700198 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
200 offset += sizeof(Object*);
201 break;
202 }
203 case 'D':
204 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
205 offset += 8;
206 break;
207 case 'J':
208 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
209 offset += 8;
210 break;
211 }
212 }
213 return arg_array.release();
214}
215
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700216byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700217 size_t num_bytes = method->NumArgArrayBytes();
218 scoped_array<byte> arg_array(new byte[num_bytes]);
219 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700220 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700221 switch (shorty[i]) {
222 case 'Z':
223 case 'B':
224 case 'C':
225 case 'S':
226 case 'I':
227 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
228 offset += 4;
229 break;
230 case 'F':
231 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
232 offset += 4;
233 break;
234 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700235 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700236 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
237 offset += sizeof(Object*);
238 break;
239 }
240 case 'D':
241 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
242 offset += 8;
243 break;
244 case 'J':
245 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
246 offset += 8;
247 break;
248 }
249 }
250 return arg_array.release();
251}
252
Elliott Hughes72025e52011-08-23 17:50:30 -0700253JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
254 Method* method, byte* args) {
Ian Rogers6de08602011-08-19 14:52:39 -0700255 Thread* self = ts.Self();
256
257 // Push a transition back into managed code onto the linked list in thread
258 CHECK_EQ(Thread::kRunnable, self->GetState());
259 NativeToManagedRecord record;
260 self->PushNativeToManagedRecord(&record);
261
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700262 // Call the invoke stub associated with the method
263 // Pass everything as arguments
264 const Method::InvokeStub* stub = method->GetInvokeStub();
265 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700266
buzbeec143c552011-08-20 17:38:58 -0700267 // Compile...
268 // TODO: not here!
269 oatCompileMethod(method, kThumb2);
buzbeec143c552011-08-20 17:38:58 -0700270
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700271 JValue result;
buzbeec143c552011-08-20 17:38:58 -0700272 if (method->HasCode()) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700273 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700274 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700275 LOG(WARNING) << "Not invoking method with no associated code: "
276 << PrettyMethod(method, true);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700277 result.j = 0;
278 }
buzbeec143c552011-08-20 17:38:58 -0700279
Ian Rogers6de08602011-08-19 14:52:39 -0700280 // Pop transition
281 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700282 return result;
283}
284
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700285JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700286 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700287 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700288 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700289 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700290 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700291}
292
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700293JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700294 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700295 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700296 Method* method = DecodeMethod(ts, mid);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700297 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700298 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
299}
300
Elliott Hughes75770752011-08-24 17:52:38 -0700301Method* FindVirtualMethod(Object* receiver, Method* method) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700302 return receiver->GetClass()->GetMethodByVtableIndex(method->GetVtableIndex());
303}
304
305JValue InvokeVirtualWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
306 Object* receiver = Decode<Object*>(ts, obj);
307 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
308 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
309 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
310}
311
312JValue InvokeVirtualWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
313 Object* receiver = Decode<Object*>(ts, obj);
314 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
315 scoped_array<byte> arg_array(CreateArgArray(ts, method, args));
316 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700317}
318
Elliott Hughes6b436852011-08-12 10:16:44 -0700319// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
320// separated with slashes but aren't wrapped with "L;" like regular descriptors
321// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
322// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
323// supported names with dots too (such as "a.b.C").
324std::string NormalizeJniClassDescriptor(const char* name) {
325 std::string result;
326 // Add the missing "L;" if necessary.
327 if (name[0] == '[') {
328 result = name;
329 } else {
330 result += 'L';
331 result += name;
332 result += ';';
333 }
334 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700335 if (result.find('.') != std::string::npos) {
336 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
337 << "\"" << name << "\"";
338 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700339 }
340 return result;
341}
342
Elliott Hughescdf53122011-08-19 15:46:09 -0700343jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
344 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700345 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
346 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700347 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700348
349 Method* method = NULL;
350 if (is_static) {
351 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700352 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 method = c->FindVirtualMethod(name, sig);
354 if (method == NULL) {
355 // No virtual method matching the signature. Search declared
356 // private methods and constructors.
357 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700358 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700359 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700360
Elliott Hughescdf53122011-08-19 15:46:09 -0700361 if (method == NULL || method->IsStatic() != is_static) {
362 Thread* self = Thread::Current();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700363 std::string method_name(PrettyMethod(method, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700364 // TODO: try searching for the opposite kind of method from is_static
365 // for better diagnostics?
366 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700367 "no %s method %s", is_static ? "static" : "non-static",
368 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 return NULL;
370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700371
Elliott Hughes79082e32011-08-25 12:07:32 -0700372 EnsureInvokeStub(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700373
Elliott Hughescdf53122011-08-19 15:46:09 -0700374 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700375}
376
Elliott Hughescdf53122011-08-19 15:46:09 -0700377jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
378 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700379 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
380 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700381 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700382
383 Field* field = NULL;
384 if (is_static) {
385 field = c->FindStaticField(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700386 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700387 field = c->FindInstanceField(name, sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700388 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700389
Elliott Hughescdf53122011-08-19 15:46:09 -0700390 if (field == NULL) {
391 Thread* self = Thread::Current();
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700392 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
394 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700395 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700396 return NULL;
397 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700398
Elliott Hughescdf53122011-08-19 15:46:09 -0700399 jweak fid = AddWeakGlobalReference(ts, field);
400 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700401}
402
Elliott Hughes75770752011-08-24 17:52:38 -0700403void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
404 JavaVMExt* vm = ts.Vm();
405 MutexLock mu(vm->pins_lock);
406 vm->pin_table.Add(array);
407}
408
409void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
410 JavaVMExt* vm = ts.Vm();
411 MutexLock mu(vm->pins_lock);
412 vm->pin_table.Remove(array);
413}
414
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700415template<typename JniT, typename ArtT>
416JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
417 CHECK_GE(length, 0); // TODO: ReportJniError
418 ArtT* result = ArtT::Alloc(length);
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700419 return AddLocalReference<JniT>(ts, result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700420}
421
Elliott Hughes75770752011-08-24 17:52:38 -0700422template <typename ArrayT, typename CArrayT, typename ArtArrayT>
423CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
424 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
425 PinPrimitiveArray(ts, array);
426 if (is_copy != NULL) {
427 *is_copy = JNI_FALSE;
428 }
429 return array->GetData();
430}
431
432template <typename ArrayT>
433void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
434 if (mode != JNI_COMMIT) {
435 Array* array = Decode<Array*>(ts, java_array);
436 UnpinPrimitiveArray(ts, array);
437 }
438}
439
Elliott Hughes814e4032011-08-23 12:07:56 -0700440void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
441 std::string type(PrettyType(array));
442 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
443 "%s offset=%d length=%d %s.length=%d",
444 type.c_str(), start, length, identifier, array->GetLength());
445}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700446void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
447 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
448 "offset=%d length=%d string.length()=%d", start, length, array_length);
449}
Elliott Hughes814e4032011-08-23 12:07:56 -0700450
451template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700452void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700453 ArrayT* array = Decode<ArrayT*>(ts, java_array);
454 if (start < 0 || length < 0 || start + length > array->GetLength()) {
455 ThrowAIOOBE(ts, array, start, length, "src");
456 } else {
457 JavaT* data = array->GetData();
458 memcpy(buf, data + start, length * sizeof(JavaT));
459 }
460}
461
462template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700463void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700464 ArrayT* array = Decode<ArrayT*>(ts, java_array);
465 if (start < 0 || length < 0 || start + length > array->GetLength()) {
466 ThrowAIOOBE(ts, array, start, length, "dst");
467 } else {
468 JavaT* data = array->GetData();
469 memcpy(data + start, buf, length * sizeof(JavaT));
470 }
471}
472
Elliott Hughes75770752011-08-24 17:52:38 -0700473jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700474 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
475 CHECK(buffer_class.get() != NULL);
476 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
477}
478
Elliott Hughes75770752011-08-24 17:52:38 -0700479jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700480 static jclass buffer_class = InitDirectByteBufferClass(env);
481 return buffer_class;
482}
483
Elliott Hughes75770752011-08-24 17:52:38 -0700484jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
485 if (vm == NULL || p_env == NULL) {
486 return JNI_ERR;
487 }
488
489 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
490 JavaVMAttachArgs args;
491 if (thr_args == NULL) {
492 // Allow the v1.1 calling convention.
493 args.version = JNI_VERSION_1_2;
494 args.name = NULL;
495 args.group = NULL; // TODO: get "main" thread group
496 } else {
497 args.version = in_args->version;
498 args.name = in_args->name;
499 if (in_args->group != NULL) {
500 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
501 args.group = NULL; // TODO: decode in_args->group
502 } else {
503 args.group = NULL; // TODO: get "main" thread group
504 }
505 }
506 CHECK_GE(args.version, JNI_VERSION_1_2);
507
508 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
509 return runtime->AttachCurrentThread(args.name, p_env, as_daemon) ? JNI_OK : JNI_ERR;
510}
511
Elliott Hughes79082e32011-08-25 12:07:32 -0700512class SharedLibrary {
513 public:
514 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
515 : path_(path),
516 handle_(handle),
517 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
518 jni_on_load_tid_(Thread::Current()->GetId()),
519 jni_on_load_result_(kPending) {
520 pthread_cond_init(&jni_on_load_cond_, NULL);
521 }
522
523 ~SharedLibrary() {
524 delete jni_on_load_lock_;
525 }
526
527 Object* GetClassLoader() {
528 return class_loader_;
529 }
530
531 std::string GetPath() {
532 return path_;
533 }
534
535 /*
536 * Check the result of an earlier call to JNI_OnLoad on this library. If
537 * the call has not yet finished in another thread, wait for it.
538 */
539 bool CheckOnLoadResult(JavaVMExt* vm) {
540 Thread* self = Thread::Current();
541 if (jni_on_load_tid_ == self->GetId()) {
542 // Check this so we don't end up waiting for ourselves. We need
543 // to return "true" so the caller can continue.
544 LOG(INFO) << *self << " recursive attempt to load library "
545 << "\"" << path_ << "\"";
546 return true;
547 }
548
549 MutexLock mu(jni_on_load_lock_);
550 while (jni_on_load_result_ == kPending) {
551 if (vm->verbose_jni) {
552 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
553 << "JNI_OnLoad...]";
554 }
555 Thread::State old_state = self->GetState();
556 self->SetState(Thread::kWaiting); // TODO: VMWAIT
557 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
558 self->SetState(old_state);
559 }
560
561 bool okay = (jni_on_load_result_ == kOkay);
562 if (vm->verbose_jni) {
563 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
564 << (okay ? "succeeded" : "failed") << "]";
565 }
566 return okay;
567 }
568
569 void SetResult(bool result) {
570 jni_on_load_result_ = result ? kOkay : kFailed;
571 jni_on_load_tid_ = 0;
572
573 // Broadcast a wakeup to anybody sleeping on the condition variable.
574 MutexLock mu(jni_on_load_lock_);
575 pthread_cond_broadcast(&jni_on_load_cond_);
576 }
577
578 void* FindSymbol(const std::string& symbol_name) {
579 return dlsym(handle_, symbol_name.c_str());
580 }
581
582 private:
583 enum JNI_OnLoadState {
584 kPending,
585 kFailed,
586 kOkay,
587 };
588
589 // Path to library "/system/lib/libjni.so".
590 std::string path_;
591
592 // The void* returned by dlopen(3).
593 void* handle_;
594
595 // The ClassLoader this library is associated with.
596 Object* class_loader_;
597
598 // Guards remaining items.
599 Mutex* jni_on_load_lock_;
600 // Wait for JNI_OnLoad in other thread.
601 pthread_cond_t jni_on_load_cond_;
602 // Recursive invocation guard.
603 uint32_t jni_on_load_tid_;
604 // Result of earlier JNI_OnLoad call.
605 JNI_OnLoadState jni_on_load_result_;
606};
607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700609
Elliott Hughes79082e32011-08-25 12:07:32 -0700610// This exists mainly to keep implementation details out of the header file.
611class Libraries {
612 public:
613 Libraries() {
614 }
615
616 ~Libraries() {
617 // Delete our map values. (The keys will be cleaned up by the map itself.)
618 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
619 delete it->second;
620 }
621 }
622
623 SharedLibrary* Get(const std::string& path) {
624 return libraries_[path];
625 }
626
627 void Put(const std::string& path, SharedLibrary* library) {
628 libraries_[path] = library;
629 }
630
631 // See section 11.3 "Linking Native Methods" of the JNI spec.
632 void* FindNativeMethod(const Method* m) {
633 std::string jni_short_name(JniShortName(m));
634 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700635 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700636 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
637 SharedLibrary* library = it->second;
638 if (library->GetClassLoader() != declaring_class_loader) {
639 // We only search libraries loaded by the appropriate ClassLoader.
640 continue;
641 }
642 // Try the short name then the long name...
643 void* fn = library->FindSymbol(jni_short_name);
644 if (fn == NULL) {
645 fn = library->FindSymbol(jni_long_name);
646 }
647 if (fn != NULL) {
648 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
649 LOG(INFO) << "[Found native code for " << PrettyMethod(m, true)
650 << " in \"" << library->GetPath() << "\"]";
651 }
652 return fn;
653 }
654 }
655 std::string detail;
656 detail += "No implementation found for ";
657 detail += PrettyMethod(m, true);
658 LOG(ERROR) << detail;
659 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
660 "%s", detail.c_str());
661 return NULL;
662 }
663
664 private:
665 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
666
667 std::map<std::string, SharedLibrary*> libraries_;
668};
669
Elliott Hughescdf53122011-08-19 15:46:09 -0700670class JNI {
671 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 static jint GetVersion(JNIEnv* env) {
674 ScopedJniThreadState ts(env);
675 return JNI_VERSION_1_6;
676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
679 ScopedJniThreadState ts(env);
680 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700681 return NULL;
682 }
683
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 static jclass FindClass(JNIEnv* env, const char* name) {
685 ScopedJniThreadState ts(env);
686 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
687 std::string descriptor(NormalizeJniClassDescriptor(name));
688 // TODO: need to get the appropriate ClassLoader.
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700689 ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700690 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 return AddLocalReference<jclass>(ts, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
695 ScopedJniThreadState ts(env);
696 Method* method = Decode<Method*>(ts, java_method);
697 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700698 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700699
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
701 ScopedJniThreadState ts(env);
702 Field* field = Decode<Field*>(ts, java_field);
703 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
704 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700705
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
707 ScopedJniThreadState ts(env);
708 Method* method = DecodeMethod(ts, mid);
709 return AddLocalReference<jobject>(ts, method);
710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
713 ScopedJniThreadState ts(env);
714 Field* field = DecodeField(ts, fid);
715 return AddLocalReference<jobject>(ts, field);
716 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700717
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
719 ScopedJniThreadState ts(env);
720 Object* o = Decode<Object*>(ts, java_object);
721 return AddLocalReference<jclass>(ts, o->GetClass());
722 }
723
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700724 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700726 Class* c = Decode<Class*>(ts, java_class);
727 return AddLocalReference<jclass>(ts, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700729
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 Class* c1 = Decode<Class*>(ts, java_class1);
733 Class* c2 = Decode<Class*>(ts, java_class2);
734 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700736
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700739 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 if (jobj == NULL) {
741 // NB. JNI is different from regular Java instanceof in this respect
742 return JNI_TRUE;
743 } else {
744 Object* obj = Decode<Object*>(ts, jobj);
745 Class* klass = Decode<Class*>(ts, clazz);
746 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
747 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700749
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 static jint Throw(JNIEnv* env, jthrowable java_exception) {
751 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700752 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 if (exception == NULL) {
754 return JNI_ERR;
755 }
756 ts.Self()->SetException(exception);
757 return JNI_OK;
758 }
759
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700760 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700762 // TODO: check for a pending exception to decide what constructor to call.
763 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
764 if (mid == NULL) {
765 return JNI_ERR;
766 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
768 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700769 return JNI_ERR;
770 }
771
772 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 args[0].l = s.get();
774 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
775 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776 return JNI_ERR;
777 }
778
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700782
Elliott Hughes37f7a402011-08-22 18:56:01 -0700783 return JNI_OK;
784 }
785
786 static jboolean ExceptionCheck(JNIEnv* env) {
787 ScopedJniThreadState ts(env);
788 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
789 }
790
791 static void ExceptionClear(JNIEnv* env) {
792 ScopedJniThreadState ts(env);
793 ts.Self()->ClearException();
794 }
795
796 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700798
799 Thread* self = ts.Self();
800 Throwable* original_exception = self->GetException();
801 self->ClearException();
802
803 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(ts, original_exception));
804 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
805 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
806 if (mid == NULL) {
807 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
808 << PrettyType(original_exception);
809 } else {
810 env->CallVoidMethod(exception.get(), mid);
811 if (self->IsExceptionPending()) {
812 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
813 << " thrown while calling printStackTrace";
814 self->ClearException();
815 }
816 }
817
818 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700820
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 static jthrowable ExceptionOccurred(JNIEnv* env) {
822 ScopedJniThreadState ts(env);
823 Object* exception = ts.Self()->GetException();
824 if (exception == NULL) {
825 return NULL;
826 } else {
827 // TODO: if adding a local reference failing causes the VM to abort
828 // then the following check will never occur.
829 jthrowable localException = AddLocalReference<jthrowable>(ts, exception);
830 if (localException == NULL) {
831 // We were unable to add a new local reference, and threw a new
832 // exception. We can't return "exception", because it's not a
833 // local reference. So we have to return NULL, indicating that
834 // there was no exception, even though it's pretty much raining
835 // exceptions in here.
836 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
837 }
838 return localException;
839 }
840 }
841
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 static void FatalError(JNIEnv* env, const char* msg) {
843 ScopedJniThreadState ts(env);
844 LOG(FATAL) << "JNI FatalError called: " << msg;
845 }
846
847 static jint PushLocalFrame(JNIEnv* env, jint cap) {
848 ScopedJniThreadState ts(env);
849 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
850 return JNI_OK;
851 }
852
853 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
854 ScopedJniThreadState ts(env);
855 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
856 return res;
857 }
858
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
860 ScopedJniThreadState ts(env);
861 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
862 return 0;
863 }
864
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
866 ScopedJniThreadState ts(env);
867 if (obj == NULL) {
868 return NULL;
869 }
870
Elliott Hughes75770752011-08-24 17:52:38 -0700871 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 IndirectReferenceTable& globals = vm->globals;
873 MutexLock mu(vm->globals_lock);
874 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
875 return reinterpret_cast<jobject>(ref);
876 }
877
878 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return;
882 }
883
Elliott Hughes75770752011-08-24 17:52:38 -0700884 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 IndirectReferenceTable& globals = vm->globals;
886 MutexLock mu(vm->globals_lock);
887
888 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
889 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
890 << "failed to find entry";
891 }
892 }
893
894 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
895 ScopedJniThreadState ts(env);
896 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
897 }
898
899 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
900 ScopedJniThreadState ts(env);
901 if (obj == NULL) {
902 return;
903 }
904
Elliott Hughes75770752011-08-24 17:52:38 -0700905 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 IndirectReferenceTable& weak_globals = vm->weak_globals;
907 MutexLock mu(vm->weak_globals_lock);
908
909 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
910 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
911 << "failed to find entry";
912 }
913 }
914
915 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
916 ScopedJniThreadState ts(env);
917 if (obj == NULL) {
918 return NULL;
919 }
920
921 IndirectReferenceTable& locals = ts.Env()->locals;
922
923 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
924 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
925 return reinterpret_cast<jobject>(ref);
926 }
927
928 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
929 ScopedJniThreadState ts(env);
930 if (obj == NULL) {
931 return;
932 }
933
934 IndirectReferenceTable& locals = ts.Env()->locals;
935
936 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
937 if (!locals.Remove(cookie, obj)) {
938 // Attempting to delete a local reference that is not in the
939 // topmost local reference frame is a no-op. DeleteLocalRef returns
940 // void and doesn't throw any exceptions, but we should probably
941 // complain about it so the user will notice that things aren't
942 // going quite the way they expect.
943 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
944 << "failed to find entry";
945 }
946 }
947
948 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
949 ScopedJniThreadState ts(env);
950 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
951 ? JNI_TRUE : JNI_FALSE;
952 }
953
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700954 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700956 Class* c = Decode<Class*>(ts, java_class);
957 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
958 return NULL;
959 }
960 return AddLocalReference<jobject>(ts, c->NewInstance());
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
965 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_start(args, mid);
967 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 va_end(args);
969 return result;
970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700974 Class* c = Decode<Class*>(ts, java_class);
975 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
976 return NULL;
977 }
978 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 jobject local_result = AddLocalReference<jobject>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 return local_result;
982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700986 Class* c = Decode<Class*>(ts, java_class);
987 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
988 return NULL;
989 }
990 Object* result = c->NewInstance();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 jobject local_result = AddLocalReference<jobjectArray>(ts, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 return local_result;
994 }
995
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
997 ScopedJniThreadState ts(env);
998 return FindMethodID(ts, c, name, sig, false);
999 }
1000
1001 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1002 ScopedJniThreadState ts(env);
1003 return FindMethodID(ts, c, name, sig, true);
1004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
1010 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1011 va_end(ap);
1012 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 CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, args);
1018 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 JValue result = InvokeVirtualWithJValues(ts, obj, mid, args);
1024 return AddLocalReference<jobject>(ts, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 va_list ap;
1030 va_start(ap, mid);
1031 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1032 va_end(ap);
1033 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 return InvokeVirtualWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 return InvokeVirtualWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_list ap;
1049 va_start(ap, mid);
1050 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1051 va_end(ap);
1052 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 return InvokeVirtualWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 return InvokeVirtualWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_list ap;
1068 va_start(ap, mid);
1069 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1070 va_end(ap);
1071 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 return InvokeVirtualWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 return InvokeVirtualWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_list ap;
1087 va_start(ap, mid);
1088 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1089 va_end(ap);
1090 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 return InvokeVirtualWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 return InvokeVirtualWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 va_list ap;
1106 va_start(ap, mid);
1107 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1108 va_end(ap);
1109 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 return InvokeVirtualWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 return InvokeVirtualWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_list ap;
1125 va_start(ap, mid);
1126 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1127 va_end(ap);
1128 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 return InvokeVirtualWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 return InvokeVirtualWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_list ap;
1144 va_start(ap, mid);
1145 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1146 va_end(ap);
1147 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 return InvokeVirtualWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 return InvokeVirtualWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 va_list ap;
1163 va_start(ap, mid);
1164 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1165 va_end(ap);
1166 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 return InvokeVirtualWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 return InvokeVirtualWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_list ap;
1182 va_start(ap, mid);
1183 JValue result = InvokeVirtualWithVarArgs(ts, obj, mid, ap);
1184 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 InvokeVirtualWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 InvokeVirtualWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
1200 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 va_start(ap, mid);
1202 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1204 va_end(ap);
1205 return local_result;
1206 }
1207
1208 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 return AddLocalReference<jobject>(ts, result.l);
1213 }
1214
1215 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 return AddLocalReference<jobject>(ts, result.l);
1220 }
1221
1222 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
1225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
1227 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 va_end(ap);
1229 return result.z;
1230 }
1231
1232 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
1238 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
1244 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
1247 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 va_start(ap, mid);
1249 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 va_end(ap);
1251 return result.b;
1252 }
1253
1254 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
1266 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
1269 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 va_start(ap, mid);
1271 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_end(ap);
1273 return result.c;
1274 }
1275
1276 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
1288 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
1291 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 va_start(ap, mid);
1293 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_end(ap);
1295 return result.s;
1296 }
1297
1298 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
1304 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
1310 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
1313 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 va_start(ap, mid);
1315 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_end(ap);
1317 return result.i;
1318 }
1319
1320 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
1326 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
1332 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
1335 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 va_start(ap, mid);
1337 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_end(ap);
1339 return result.j;
1340 }
1341
1342 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
1348 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
1354 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 ScopedJniThreadState ts(env);
1357 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 va_start(ap, mid);
1359 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 va_end(ap);
1361 return result.f;
1362 }
1363
1364 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
1370 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001371 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
1376 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
1379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
1381 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 va_end(ap);
1383 return result.d;
1384 }
1385
1386 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
1398 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
1401 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001402 va_start(ap, mid);
1403 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 va_end(ap);
1405 }
1406
1407 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
1413 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001414 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
1419 static jfieldID GetFieldID(JNIEnv* env,
1420 jclass c, const char* name, const char* sig) {
1421 ScopedJniThreadState ts(env);
1422 return FindFieldID(ts, c, name, sig, false);
1423 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001424
1425
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 static jfieldID GetStaticFieldID(JNIEnv* env,
1427 jclass c, const char* name, const char* sig) {
1428 ScopedJniThreadState ts(env);
1429 return FindFieldID(ts, c, name, sig, true);
1430 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 Object* o = Decode<Object*>(ts, obj);
1435 Field* f = DecodeField(ts, fid);
1436 return AddLocalReference<jobject>(ts, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 Field* f = DecodeField(ts, fid);
1442 return AddLocalReference<jobject>(ts, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 Object* o = Decode<Object*>(ts, java_object);
1448 Object* v = Decode<Object*>(ts, java_value);
1449 Field* f = DecodeField(ts, fid);
1450 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 Object* v = Decode<Object*>(ts, java_value);
1456 Field* f = DecodeField(ts, fid);
1457 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460#define GET_PRIMITIVE_FIELD(fn, instance) \
1461 ScopedJniThreadState ts(env); \
1462 Object* o = Decode<Object*>(ts, instance); \
1463 Field* f = DecodeField(ts, fid); \
1464 return f->fn(o)
1465
1466#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1467 ScopedJniThreadState ts(env); \
1468 Object* o = Decode<Object*>(ts, instance); \
1469 Field* f = DecodeField(ts, fid); \
1470 f->fn(o, value)
1471
1472 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1505 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1509 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1513 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1517 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1521 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1525 GET_PRIMITIVE_FIELD(GetLong, NULL);
1526 }
1527
1528 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1529 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1530 }
1531
1532 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1533 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1534 }
1535
1536 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1537 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1538 }
1539
1540 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1541 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1542 }
1543
1544 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1545 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1546 }
1547
1548 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1549 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1550 }
1551
1552 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1553 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1554 }
1555
1556 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1557 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1558 }
1559
1560 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1561 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1562 }
1563
1564 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1565 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1566 }
1567
1568 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1569 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1570 }
1571
1572 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1573 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1574 }
1575
1576 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1577 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1578 }
1579
1580 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1581 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1582 }
1583
1584 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1585 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1586 }
1587
1588 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1589 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1590 }
1591
1592 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1593 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1594 }
1595
1596 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1597 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
1600 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
1603 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 va_start(ap, mid);
1605 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 jobject local_result = AddLocalReference<jobject>(ts, result.l);
1607 va_end(ap);
1608 return local_result;
1609 }
1610
1611 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 return AddLocalReference<jobject>(ts, result.l);
1616 }
1617
1618 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 return AddLocalReference<jobject>(ts, result.l);
1623 }
1624
1625 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
1630 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return result.z;
1633 }
1634
1635 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
1641 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
1651 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return result.b;
1654 }
1655
1656 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
1662 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
1670 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 va_start(ap, mid);
1672 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_end(ap);
1674 return result.c;
1675 }
1676
1677 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
1683 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
1691 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 va_start(ap, mid);
1693 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
1695 return result.s;
1696 }
1697
1698 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
1704 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
1712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
1714 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
1716 return result.i;
1717 }
1718
1719 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
1725 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
1733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
1735 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
1737 return result.j;
1738 }
1739
1740 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
1746 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
1754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
1756 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
1758 return result.f;
1759 }
1760
1761 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
1767 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001768 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
1775 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 va_start(ap, mid);
1777 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_end(ap);
1779 return result.d;
1780 }
1781
1782 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
1788 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001789 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes72025e52011-08-23 17:50:30 -07001794 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 ScopedJniThreadState ts(env);
1796 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001797 va_start(ap, mid);
1798 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 va_end(ap);
1800 }
1801
1802 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001805 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
1808 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001809 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001811 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 if (chars == NULL && char_count == 0) {
1817 return NULL;
1818 }
1819 String* result = String::AllocFromUtf16(char_count, chars);
1820 return AddLocalReference<jstring>(ts, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
1823 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1824 ScopedJniThreadState ts(env);
1825 if (utf == NULL) {
1826 return NULL;
1827 }
1828 String* result = String::AllocFromModifiedUtf8(utf);
1829 return AddLocalReference<jstring>(ts, result);
1830 }
1831
Elliott Hughes814e4032011-08-23 12:07:56 -07001832 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1833 ScopedJniThreadState ts(env);
1834 return Decode<String*>(ts, java_string)->GetLength();
1835 }
1836
1837 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1838 ScopedJniThreadState ts(env);
1839 return Decode<String*>(ts, java_string)->GetUtfLength();
1840 }
1841
Elliott Hughesb465ab02011-08-24 11:21:21 -07001842 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 String* s = Decode<String*>(ts, java_string);
1845 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1846 ThrowSIOOBE(ts, start, length, s->GetLength());
1847 } else {
1848 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1849 memcpy(buf, chars + start, length * sizeof(jchar));
1850 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 }
1852
Elliott Hughesb465ab02011-08-24 11:21:21 -07001853 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001855 String* s = Decode<String*>(ts, java_string);
1856 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1857 ThrowSIOOBE(ts, start, length, s->GetLength());
1858 } else {
1859 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1860 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1861 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001862 }
1863
Elliott Hughes75770752011-08-24 17:52:38 -07001864 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001865 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001866 String* s = Decode<String*>(ts, java_string);
1867 const CharArray* chars = s->GetCharArray();
1868 PinPrimitiveArray(ts, chars);
1869 if (is_copy != NULL) {
1870 *is_copy = JNI_FALSE;
1871 }
1872 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001876 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001877 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes75770752011-08-24 17:52:38 -07001880 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001882 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes75770752011-08-24 17:52:38 -07001885 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001887 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes75770752011-08-24 17:52:38 -07001890 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001891 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001892 if (java_string == NULL) {
1893 return NULL;
1894 }
1895 if (is_copy != NULL) {
1896 *is_copy = JNI_TRUE;
1897 }
1898 String* s = Decode<String*>(ts, java_string);
1899 size_t byte_count = s->GetUtfLength();
1900 char* bytes = new char[byte_count + 1];
1901 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001902 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001903 return NULL;
1904 }
1905 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1906 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1907 bytes[byte_count] = '\0';
1908 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001909 }
1910
Elliott Hughes75770752011-08-24 17:52:38 -07001911 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001913 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 }
1915
Elliott Hughesbd935992011-08-22 11:59:34 -07001916 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001918 Object* obj = Decode<Object*>(ts, java_array);
1919 CHECK(obj->IsArray()); // TODO: ReportJniError
1920 Array* array = obj->AsArray();
1921 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes814e4032011-08-23 12:07:56 -07001924 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1927 return AddLocalReference<jobject>(ts, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
1930 static void SetObjectArrayElement(JNIEnv* env,
1931 jobjectArray java_array, jsize index, jobject java_value) {
1932 ScopedJniThreadState ts(env);
1933 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1934 Object* value = Decode<Object*>(ts, java_value);
1935 array->Set(index, value);
1936 }
1937
1938 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1939 ScopedJniThreadState ts(env);
1940 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1941 }
1942
1943 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1944 ScopedJniThreadState ts(env);
1945 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1946 }
1947
1948 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1949 ScopedJniThreadState ts(env);
1950 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1951 }
1952
1953 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1954 ScopedJniThreadState ts(env);
1955 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1956 }
1957
1958 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1959 ScopedJniThreadState ts(env);
1960 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1961 }
1962
1963 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1964 ScopedJniThreadState ts(env);
1965 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1966 }
1967
1968 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1969 ScopedJniThreadState ts(env);
1970 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1971 }
1972
1973 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1974 ScopedJniThreadState ts(env);
1975 CHECK_GE(length, 0); // TODO: ReportJniError
1976
1977 // Compute the array class corresponding to the given element class.
1978 Class* element_class = Decode<Class*>(ts, element_jclass);
1979 std::string descriptor;
1980 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001981 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001982
1983 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001984 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1985 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 return NULL;
1987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 // Allocate and initialize if necessary.
1990 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 if (initial_element != NULL) {
1993 Object* initial_object = Decode<Object*>(ts, initial_element);
1994 for (jsize i = 0; i < length; ++i) {
1995 result->Set(i, initial_object);
1996 }
1997 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 return AddLocalReference<jobjectArray>(ts, result);
1999 }
2000
2001 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2002 ScopedJniThreadState ts(env);
2003 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes75770752011-08-24 17:52:38 -07002051 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes75770752011-08-24 17:52:38 -07002056 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002058 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002063 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes75770752011-08-24 17:52:38 -07002066 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002068 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes75770752011-08-24 17:52:38 -07002071 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002073 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes75770752011-08-24 17:52:38 -07002076 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002078 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes75770752011-08-24 17:52:38 -07002081 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002083 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes75770752011-08-24 17:52:38 -07002086 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002088 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes75770752011-08-24 17:52:38 -07002091 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002093 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 Class* c = Decode<Class*>(ts, java_class);
2179
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 const char* name = methods[i].name;
2182 const char* sig = methods[i].signature;
2183
2184 if (*sig == '!') {
2185 // TODO: fast jni. it's too noisy to log all these.
2186 ++sig;
2187 }
2188
Elliott Hughes5174fe62011-08-23 15:12:35 -07002189 Method* m = c->FindDirectMethod(name, sig);
2190 if (m == NULL) {
2191 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002193 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002195 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2197 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002198 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2204 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002205 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 return JNI_ERR;
2207 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002208
Elliott Hughes75770752011-08-24 17:52:38 -07002209 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002210 LOG(INFO) << "[Registering JNI native method "
2211 << PrettyMethod(m, true) << "]";
2212 }
2213
2214 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216 return JNI_OK;
2217 }
2218
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221 Class* c = Decode<Class*>(ts, java_class);
2222
Elliott Hughes75770752011-08-24 17:52:38 -07002223 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 LOG(INFO) << "[Unregistering JNI native methods for "
2225 << PrettyDescriptor(c->GetDescriptor()) << "]";
2226 }
2227
2228 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2229 Method* m = c->GetDirectMethod(i);
2230 if (m->IsNative()) {
2231 m->UnregisterNative();
2232 }
2233 }
2234 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2235 Method* m = c->GetVirtualMethod(i);
2236 if (m->IsNative()) {
2237 m->UnregisterNative();
2238 }
2239 }
2240
2241 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes72025e52011-08-23 17:50:30 -07002244 static jint MonitorEnter(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
Elliott Hughes72025e52011-08-23 17:50:30 -07002250 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002252 Decode<Object*>(ts, java_object)->MonitorEnter();
2253 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
2256 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2257 ScopedJniThreadState ts(env);
2258 Runtime* runtime = Runtime::Current();
2259 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002260 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 } else {
2262 *vm = NULL;
2263 }
2264 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2265 }
2266
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2268 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002269
2270 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002271 CHECK(address != NULL); // TODO: ReportJniError
2272 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273
2274 jclass buffer_class = GetDirectByteBufferClass(env);
2275 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2276 if (mid == NULL) {
2277 return NULL;
2278 }
2279
2280 // At the moment, the Java side is limited to 32 bits.
2281 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2282 CHECK_LE(capacity, 0xffffffff);
2283 jint address_arg = reinterpret_cast<jint>(address);
2284 jint capacity_arg = static_cast<jint>(capacity);
2285
2286 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2287 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 static void* GetDirectBufferAddress(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), "effectiveDirectAddress", "I");
2293 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2299 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304
Elliott Hughes75770752011-08-24 17:52:38 -07002305 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306
2307 // Do we definitely know what kind of reference this is?
2308 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2309 IndirectRefKind kind = GetIndirectRefKind(ref);
2310 switch (kind) {
2311 case kLocal:
2312 return JNILocalRefType;
2313 case kGlobal:
2314 return JNIGlobalRefType;
2315 case kWeakGlobal:
2316 return JNIWeakGlobalRefType;
2317 case kSirtOrInvalid:
2318 // Is it in a stack IRT?
2319 if (ts.Self()->SirtContains(java_object)) {
2320 return JNILocalRefType;
2321 }
2322
2323 // If we're handing out direct pointers, check whether it's a direct pointer
2324 // to a local reference.
2325 // TODO: replace 'false' with the replacement for gDvmJni.workAroundAppJniBugs
2326 if (false && Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
2327 if (ts.Env()->locals.Contains(java_object)) {
2328 return JNILocalRefType;
2329 }
2330 }
2331
2332 return JNIInvalidRefType;
2333 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
2335};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002336
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002337static const struct JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002338 NULL, // reserved0.
2339 NULL, // reserved1.
2340 NULL, // reserved2.
2341 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002342 JNI::GetVersion,
2343 JNI::DefineClass,
2344 JNI::FindClass,
2345 JNI::FromReflectedMethod,
2346 JNI::FromReflectedField,
2347 JNI::ToReflectedMethod,
2348 JNI::GetSuperclass,
2349 JNI::IsAssignableFrom,
2350 JNI::ToReflectedField,
2351 JNI::Throw,
2352 JNI::ThrowNew,
2353 JNI::ExceptionOccurred,
2354 JNI::ExceptionDescribe,
2355 JNI::ExceptionClear,
2356 JNI::FatalError,
2357 JNI::PushLocalFrame,
2358 JNI::PopLocalFrame,
2359 JNI::NewGlobalRef,
2360 JNI::DeleteGlobalRef,
2361 JNI::DeleteLocalRef,
2362 JNI::IsSameObject,
2363 JNI::NewLocalRef,
2364 JNI::EnsureLocalCapacity,
2365 JNI::AllocObject,
2366 JNI::NewObject,
2367 JNI::NewObjectV,
2368 JNI::NewObjectA,
2369 JNI::GetObjectClass,
2370 JNI::IsInstanceOf,
2371 JNI::GetMethodID,
2372 JNI::CallObjectMethod,
2373 JNI::CallObjectMethodV,
2374 JNI::CallObjectMethodA,
2375 JNI::CallBooleanMethod,
2376 JNI::CallBooleanMethodV,
2377 JNI::CallBooleanMethodA,
2378 JNI::CallByteMethod,
2379 JNI::CallByteMethodV,
2380 JNI::CallByteMethodA,
2381 JNI::CallCharMethod,
2382 JNI::CallCharMethodV,
2383 JNI::CallCharMethodA,
2384 JNI::CallShortMethod,
2385 JNI::CallShortMethodV,
2386 JNI::CallShortMethodA,
2387 JNI::CallIntMethod,
2388 JNI::CallIntMethodV,
2389 JNI::CallIntMethodA,
2390 JNI::CallLongMethod,
2391 JNI::CallLongMethodV,
2392 JNI::CallLongMethodA,
2393 JNI::CallFloatMethod,
2394 JNI::CallFloatMethodV,
2395 JNI::CallFloatMethodA,
2396 JNI::CallDoubleMethod,
2397 JNI::CallDoubleMethodV,
2398 JNI::CallDoubleMethodA,
2399 JNI::CallVoidMethod,
2400 JNI::CallVoidMethodV,
2401 JNI::CallVoidMethodA,
2402 JNI::CallNonvirtualObjectMethod,
2403 JNI::CallNonvirtualObjectMethodV,
2404 JNI::CallNonvirtualObjectMethodA,
2405 JNI::CallNonvirtualBooleanMethod,
2406 JNI::CallNonvirtualBooleanMethodV,
2407 JNI::CallNonvirtualBooleanMethodA,
2408 JNI::CallNonvirtualByteMethod,
2409 JNI::CallNonvirtualByteMethodV,
2410 JNI::CallNonvirtualByteMethodA,
2411 JNI::CallNonvirtualCharMethod,
2412 JNI::CallNonvirtualCharMethodV,
2413 JNI::CallNonvirtualCharMethodA,
2414 JNI::CallNonvirtualShortMethod,
2415 JNI::CallNonvirtualShortMethodV,
2416 JNI::CallNonvirtualShortMethodA,
2417 JNI::CallNonvirtualIntMethod,
2418 JNI::CallNonvirtualIntMethodV,
2419 JNI::CallNonvirtualIntMethodA,
2420 JNI::CallNonvirtualLongMethod,
2421 JNI::CallNonvirtualLongMethodV,
2422 JNI::CallNonvirtualLongMethodA,
2423 JNI::CallNonvirtualFloatMethod,
2424 JNI::CallNonvirtualFloatMethodV,
2425 JNI::CallNonvirtualFloatMethodA,
2426 JNI::CallNonvirtualDoubleMethod,
2427 JNI::CallNonvirtualDoubleMethodV,
2428 JNI::CallNonvirtualDoubleMethodA,
2429 JNI::CallNonvirtualVoidMethod,
2430 JNI::CallNonvirtualVoidMethodV,
2431 JNI::CallNonvirtualVoidMethodA,
2432 JNI::GetFieldID,
2433 JNI::GetObjectField,
2434 JNI::GetBooleanField,
2435 JNI::GetByteField,
2436 JNI::GetCharField,
2437 JNI::GetShortField,
2438 JNI::GetIntField,
2439 JNI::GetLongField,
2440 JNI::GetFloatField,
2441 JNI::GetDoubleField,
2442 JNI::SetObjectField,
2443 JNI::SetBooleanField,
2444 JNI::SetByteField,
2445 JNI::SetCharField,
2446 JNI::SetShortField,
2447 JNI::SetIntField,
2448 JNI::SetLongField,
2449 JNI::SetFloatField,
2450 JNI::SetDoubleField,
2451 JNI::GetStaticMethodID,
2452 JNI::CallStaticObjectMethod,
2453 JNI::CallStaticObjectMethodV,
2454 JNI::CallStaticObjectMethodA,
2455 JNI::CallStaticBooleanMethod,
2456 JNI::CallStaticBooleanMethodV,
2457 JNI::CallStaticBooleanMethodA,
2458 JNI::CallStaticByteMethod,
2459 JNI::CallStaticByteMethodV,
2460 JNI::CallStaticByteMethodA,
2461 JNI::CallStaticCharMethod,
2462 JNI::CallStaticCharMethodV,
2463 JNI::CallStaticCharMethodA,
2464 JNI::CallStaticShortMethod,
2465 JNI::CallStaticShortMethodV,
2466 JNI::CallStaticShortMethodA,
2467 JNI::CallStaticIntMethod,
2468 JNI::CallStaticIntMethodV,
2469 JNI::CallStaticIntMethodA,
2470 JNI::CallStaticLongMethod,
2471 JNI::CallStaticLongMethodV,
2472 JNI::CallStaticLongMethodA,
2473 JNI::CallStaticFloatMethod,
2474 JNI::CallStaticFloatMethodV,
2475 JNI::CallStaticFloatMethodA,
2476 JNI::CallStaticDoubleMethod,
2477 JNI::CallStaticDoubleMethodV,
2478 JNI::CallStaticDoubleMethodA,
2479 JNI::CallStaticVoidMethod,
2480 JNI::CallStaticVoidMethodV,
2481 JNI::CallStaticVoidMethodA,
2482 JNI::GetStaticFieldID,
2483 JNI::GetStaticObjectField,
2484 JNI::GetStaticBooleanField,
2485 JNI::GetStaticByteField,
2486 JNI::GetStaticCharField,
2487 JNI::GetStaticShortField,
2488 JNI::GetStaticIntField,
2489 JNI::GetStaticLongField,
2490 JNI::GetStaticFloatField,
2491 JNI::GetStaticDoubleField,
2492 JNI::SetStaticObjectField,
2493 JNI::SetStaticBooleanField,
2494 JNI::SetStaticByteField,
2495 JNI::SetStaticCharField,
2496 JNI::SetStaticShortField,
2497 JNI::SetStaticIntField,
2498 JNI::SetStaticLongField,
2499 JNI::SetStaticFloatField,
2500 JNI::SetStaticDoubleField,
2501 JNI::NewString,
2502 JNI::GetStringLength,
2503 JNI::GetStringChars,
2504 JNI::ReleaseStringChars,
2505 JNI::NewStringUTF,
2506 JNI::GetStringUTFLength,
2507 JNI::GetStringUTFChars,
2508 JNI::ReleaseStringUTFChars,
2509 JNI::GetArrayLength,
2510 JNI::NewObjectArray,
2511 JNI::GetObjectArrayElement,
2512 JNI::SetObjectArrayElement,
2513 JNI::NewBooleanArray,
2514 JNI::NewByteArray,
2515 JNI::NewCharArray,
2516 JNI::NewShortArray,
2517 JNI::NewIntArray,
2518 JNI::NewLongArray,
2519 JNI::NewFloatArray,
2520 JNI::NewDoubleArray,
2521 JNI::GetBooleanArrayElements,
2522 JNI::GetByteArrayElements,
2523 JNI::GetCharArrayElements,
2524 JNI::GetShortArrayElements,
2525 JNI::GetIntArrayElements,
2526 JNI::GetLongArrayElements,
2527 JNI::GetFloatArrayElements,
2528 JNI::GetDoubleArrayElements,
2529 JNI::ReleaseBooleanArrayElements,
2530 JNI::ReleaseByteArrayElements,
2531 JNI::ReleaseCharArrayElements,
2532 JNI::ReleaseShortArrayElements,
2533 JNI::ReleaseIntArrayElements,
2534 JNI::ReleaseLongArrayElements,
2535 JNI::ReleaseFloatArrayElements,
2536 JNI::ReleaseDoubleArrayElements,
2537 JNI::GetBooleanArrayRegion,
2538 JNI::GetByteArrayRegion,
2539 JNI::GetCharArrayRegion,
2540 JNI::GetShortArrayRegion,
2541 JNI::GetIntArrayRegion,
2542 JNI::GetLongArrayRegion,
2543 JNI::GetFloatArrayRegion,
2544 JNI::GetDoubleArrayRegion,
2545 JNI::SetBooleanArrayRegion,
2546 JNI::SetByteArrayRegion,
2547 JNI::SetCharArrayRegion,
2548 JNI::SetShortArrayRegion,
2549 JNI::SetIntArrayRegion,
2550 JNI::SetLongArrayRegion,
2551 JNI::SetFloatArrayRegion,
2552 JNI::SetDoubleArrayRegion,
2553 JNI::RegisterNatives,
2554 JNI::UnregisterNatives,
2555 JNI::MonitorEnter,
2556 JNI::MonitorExit,
2557 JNI::GetJavaVM,
2558 JNI::GetStringRegion,
2559 JNI::GetStringUTFRegion,
2560 JNI::GetPrimitiveArrayCritical,
2561 JNI::ReleasePrimitiveArrayCritical,
2562 JNI::GetStringCritical,
2563 JNI::ReleaseStringCritical,
2564 JNI::NewWeakGlobalRef,
2565 JNI::DeleteWeakGlobalRef,
2566 JNI::ExceptionCheck,
2567 JNI::NewDirectByteBuffer,
2568 JNI::GetDirectBufferAddress,
2569 JNI::GetDirectBufferCapacity,
2570 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002571};
2572
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002573static const size_t kMonitorsInitial = 32; // Arbitrary.
2574static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2575
2576static const size_t kLocalsInitial = 64; // Arbitrary.
2577static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002578
Elliott Hughes75770752011-08-24 17:52:38 -07002579JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002580 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002581 vm(vm),
2582 check_jni(vm->check_jni),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002583 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002584 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2585 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002586 functions = &gNativeInterface;
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002587}
2588
Carl Shapiroea4dca82011-08-01 13:45:38 -07002589// JNI Invocation interface.
2590
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002591extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2592 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2593 if (args->version < JNI_VERSION_1_2) {
2594 return JNI_EVERSION;
2595 }
2596 Runtime::Options options;
2597 for (int i = 0; i < args->nOptions; ++i) {
2598 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002599 options.push_back(std::make_pair(StringPiece(option->optionString),
2600 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002601 }
2602 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002603 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604 if (runtime == NULL) {
2605 return JNI_ERR;
2606 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002607 *p_env = Thread::Current()->GetJniEnv();
2608 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609 return JNI_OK;
2610 }
2611}
2612
Elliott Hughesf2682d52011-08-15 16:37:04 -07002613extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 Runtime* runtime = Runtime::Current();
2615 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002616 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002618 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002619 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002620 }
2621 return JNI_OK;
2622}
2623
2624// Historically unsupported.
2625extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2626 return JNI_ERR;
2627}
2628
Elliott Hughescdf53122011-08-19 15:46:09 -07002629class JII {
2630 public:
2631 static jint DestroyJavaVM(JavaVM* vm) {
2632 if (vm == NULL) {
2633 return JNI_ERR;
2634 } else {
2635 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2636 delete raw_vm->runtime;
2637 return JNI_OK;
2638 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002639 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640
Elliott Hughescdf53122011-08-19 15:46:09 -07002641 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002642 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002643 }
2644
2645 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002646 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002647 }
2648
2649 static jint DetachCurrentThread(JavaVM* vm) {
2650 if (vm == NULL) {
2651 return JNI_ERR;
2652 } else {
2653 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2654 Runtime* runtime = raw_vm->runtime;
2655 runtime->DetachCurrentThread();
2656 return JNI_OK;
2657 }
2658 }
2659
2660 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2661 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2662 return JNI_EVERSION;
2663 }
2664 if (vm == NULL || env == NULL) {
2665 return JNI_ERR;
2666 }
2667 Thread* thread = Thread::Current();
2668 if (thread == NULL) {
2669 *env = NULL;
2670 return JNI_EDETACHED;
2671 }
2672 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673 return JNI_OK;
2674 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002675};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676
Elliott Hughesf2682d52011-08-15 16:37:04 -07002677struct JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002678 NULL, // reserved0
2679 NULL, // reserved1
2680 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002681 JII::DestroyJavaVM,
2682 JII::AttachCurrentThread,
2683 JII::DetachCurrentThread,
2684 JII::GetEnv,
2685 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002686};
2687
Elliott Hughesbbd76712011-08-17 10:25:24 -07002688static const size_t kPinTableInitialSize = 16;
2689static const size_t kPinTableMaxSize = 1024;
2690
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002691static const size_t kGlobalsInitial = 512; // Arbitrary.
2692static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2693
2694static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2695static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2696
Elliott Hughes0af55432011-08-17 18:37:28 -07002697JavaVMExt::JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002698 : runtime(runtime),
Elliott Hughes515a5bc2011-08-17 11:08:34 -07002699 check_jni(check_jni),
Elliott Hughes0af55432011-08-17 18:37:28 -07002700 verbose_jni(verbose_jni),
Elliott Hughes75770752011-08-24 17:52:38 -07002701 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002702 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002703 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002704 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002705 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002706 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2707 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2708 libraries(new Libraries) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002709 functions = &gInvokeInterface;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002710}
2711
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002712JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002713 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002714 delete globals_lock;
2715 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002716 delete libraries_lock;
2717 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002718}
2719
Elliott Hughes75770752011-08-24 17:52:38 -07002720bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2721 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002722
2723 // See if we've already loaded this library. If we have, and the class loader
2724 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002725 // TODO: for better results we should canonicalize the pathname (or even compare
2726 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002727 SharedLibrary* library;
2728 {
2729 // TODO: move the locking (and more of this logic) into Libraries.
2730 MutexLock mu(libraries_lock);
2731 library = libraries->Get(path);
2732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 if (library != NULL) {
2734 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 // The library will be associated with class_loader. The JNI
2736 // spec says we can't load the same library into more than one
2737 // class loader.
2738 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2739 "ClassLoader %p; can't open in ClassLoader %p",
2740 path.c_str(), library->GetClassLoader(), class_loader);
2741 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 return false;
2743 }
2744 if (verbose_jni) {
2745 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2746 << "ClassLoader " << class_loader << "]";
2747 }
2748 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002749 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2750 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 return false;
2752 }
2753 return true;
2754 }
2755
2756 // Open the shared library. Because we're using a full path, the system
2757 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2758 // resolve this library's dependencies though.)
2759
2760 // Failures here are expected when java.library.path has several entries
2761 // and we have to hunt for the lib.
2762
2763 // The current version of the dynamic linker prints detailed information
2764 // about dlopen() failures. Some things to check if the message is
2765 // cryptic:
2766 // - make sure the library exists on the device
2767 // - verify that the right path is being opened (the debug log message
2768 // above can help with that)
2769 // - check to see if the library is valid (e.g. not zero bytes long)
2770 // - check config/prelink-linux-arm.map to ensure that the library
2771 // is listed and is not being overrun by the previous entry (if
2772 // loading suddenly stops working on a prelinked library, this is
2773 // a good one to check)
2774 // - write a trivial app that calls sleep() then dlopen(), attach
2775 // to it with "strace -p <pid>" while it sleeps, and watch for
2776 // attempts to open nonexistent dependent shared libs
2777
2778 // TODO: automate some of these checks!
2779
2780 // This can execute slowly for a large library on a busy system, so we
2781 // want to switch from RUNNING to VMWAIT while it executes. This allows
2782 // the GC to ignore us.
2783 Thread* self = Thread::Current();
2784 Thread::State old_state = self->GetState();
2785 self->SetState(Thread::kWaiting); // TODO: VMWAIT
2786 void* handle = dlopen(path.c_str(), RTLD_LAZY);
2787 self->SetState(old_state);
2788
2789 if (verbose_jni) {
2790 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2791 }
2792
2793 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002794 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002795 return false;
2796 }
2797
2798 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002799 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 // TODO: move the locking (and more of this logic) into Libraries.
2801 MutexLock mu(libraries_lock);
2802 library = libraries->Get(path);
2803 if (library != NULL) {
2804 LOG(INFO) << "WOW: we lost a race to add shared library: "
2805 << "\"" << path << "\" ClassLoader=" << class_loader;
2806 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002808 library = new SharedLibrary(path, handle, class_loader);
2809 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002811
2812 if (verbose_jni) {
2813 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2814 }
2815
2816 bool result = true;
2817 void* sym = dlsym(handle, "JNI_OnLoad");
2818 if (sym == NULL) {
2819 if (verbose_jni) {
2820 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2821 }
2822 } else {
2823 // Call JNI_OnLoad. We have to override the current class
2824 // loader, which will always be "null" since the stuff at the
2825 // top of the stack is around Runtime.loadLibrary(). (See
2826 // the comments in the JNI FindClass function.)
2827 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2828 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
2829 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
2830 self->SetClassLoaderOverride(class_loader);
2831
2832 old_state = self->GetState();
2833 self->SetState(Thread::kNative);
2834 if (verbose_jni) {
2835 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2836 }
2837 int version = (*jni_on_load)(this, NULL);
2838 self->SetState(old_state);
2839
2840 self->SetClassLoaderOverride(old_class_loader);;
2841
2842 if (version != JNI_VERSION_1_2 &&
2843 version != JNI_VERSION_1_4 &&
2844 version != JNI_VERSION_1_6) {
2845 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2846 << "bad version: " << version;
2847 // It's unwise to call dlclose() here, but we can mark it
2848 // as bad and ensure that future load attempts will fail.
2849 // We don't know how far JNI_OnLoad got, so there could
2850 // be some partially-initialized stuff accessible through
2851 // newly-registered native method calls. We could try to
2852 // unregister them, but that doesn't seem worthwhile.
2853 result = false;
2854 } else {
2855 if (verbose_jni) {
2856 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2857 << " from JNI_OnLoad in \"" << path << "\"]";
2858 }
2859 }
2860 }
2861
2862 library->SetResult(result);
2863 return result;
2864}
2865
2866void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2867 CHECK(m->IsNative());
2868
2869 Class* c = m->GetDeclaringClass();
2870
2871 // If this is a static method, it could be called before the class
2872 // has been initialized.
2873 if (m->IsStatic()) {
2874 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2875 return NULL;
2876 }
2877 } else {
2878 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2879 }
2880
2881 MutexLock mu(libraries_lock);
2882 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002883}
2884
Ian Rogersdf20fe02011-07-20 20:34:16 -07002885} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002886
2887std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2888 switch (rhs) {
2889 case JNIInvalidRefType:
2890 os << "JNIInvalidRefType";
2891 return os;
2892 case JNILocalRefType:
2893 os << "JNILocalRefType";
2894 return os;
2895 case JNIGlobalRefType:
2896 os << "JNIGlobalRefType";
2897 return os;
2898 case JNIWeakGlobalRefType:
2899 os << "JNIWeakGlobalRefType";
2900 return os;
2901 }
2902}