blob: 83340598c8a0d84049d1aea581d55edab7df0423 [file] [log] [blame]
Elliott Hughes7ede61e2011-09-14 18:18:06 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class_linker.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070018#include "debugger.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070019#include "jni_internal.h"
20#include "object.h"
21#include "thread.h"
22
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24#include "toStringArray.h"
25
26#include <limits.h>
27
28namespace art {
29
30namespace {
31
32jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
33 return Heap::GetTargetHeapUtilization();
34}
35
36void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
37 Heap::SetTargetHeapUtilization(target);
38}
39
40void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
41}
42
43void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
44}
45
46jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
47#ifdef MOVING_GARBAGE_COLLECTOR
48 // TODO: right now, we don't have a copying collector, so there's no need
49 // to do anything special here, but we ought to pass the non-movability
50 // through to the allocator.
51 UNIMPLEMENTED(FATAL);
52#endif
53
54 Class* element_class = Decode<Class*>(env, javaElementClass);
55 if (element_class == NULL) {
56 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
57 return NULL;
58 }
59 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070060 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070061 return NULL;
62 }
63
64 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
65 std::string descriptor;
66 descriptor += "[";
67 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
68 Class* array_class = class_linker->FindClass(descriptor, NULL);
69 Array* result = Array::Alloc(array_class, length);
70 if (result == NULL) {
71 return NULL;
72 }
73 return AddLocalReference<jobject>(env, result);
74}
75
76jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
77 Array* array = Decode<Array*>(env, javaArray);
78 if (!array->IsArrayInstance()) {
79 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
80 return 0;
81 }
82 // TODO: we should also check that this is a non-movable array.
83 return reinterpret_cast<uintptr_t>(array->GetRawData());
84}
85
86void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
87 Heap::ClearGrowthLimit();
88}
89
90jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070091 return Dbg::IsDebuggerConnected();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070092}
93
94jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
95 return toStringArray(env, Runtime::Current()->GetProperties());
96}
97
98jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
99 return env->NewStringUTF(Runtime::Current()->GetBootClassPath().c_str());
100}
101
102jstring VMRuntime_classPath(JNIEnv* env, jobject) {
103 return env->NewStringUTF(Runtime::Current()->GetClassPath().c_str());
104}
105
106jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
107 return env->NewStringUTF(Runtime::Current()->GetVersion());
108}
109
110void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
111 // This is the target SDK version of the app we're about to run.
112 // Note that this value may be CUR_DEVELOPMENT (10000).
113 // Note that this value may be 0, meaning "current".
114 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
115 // TODO: running with CheckJNI should override this and force you to obey the strictest rules.
116 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " << targetSdkVersion << "...";
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700117 UNIMPLEMENTED(WARNING) << "can we get this as a command-line argument?";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118 //gDvmJni.work_around_app_jni_bugs = true;
119 }
120}
121
122JNINativeMethod gMethods[] = {
123 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
124 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
125 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
126 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
127 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
128 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
129 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
130 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
131 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
132 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
133 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
134 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
135 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
136};
137
138} // namespace
139
140void register_dalvik_system_VMRuntime(JNIEnv* env) {
141 jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods));
142}
143
144} // namespace art