blob: 6c6f49323d0731bb036ad67ccd9ff7d8b62afd08 [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
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "logging.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07009#include "runtime.h"
10#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070011
12namespace art {
13
14static void JniMonitorEnter(JniEnvironment*, jobject) {
15 LOG(WARNING) << "Unimplemented: JNI Monitor Enter";
16}
17
18static void JniMonitorExit(JniEnvironment*, jobject) {
19 LOG(WARNING) << "Unimplemented: JNI Monitor Exit";
20}
21
22JniEnvironment::JniEnvironment() {
23 monitor_enter_ = &JniMonitorEnter;
24 monitor_exit_ = &JniMonitorExit;
25}
26
Carl Shapiro2ed144c2011-07-26 16:52:08 -070027extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
28 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
29 if (args->version < JNI_VERSION_1_2) {
30 return JNI_EVERSION;
31 }
32 Runtime::Options options;
33 for (int i = 0; i < args->nOptions; ++i) {
34 JavaVMOption* option = &args->options[i];
35 options.push_back(std::make_pair(option->optionString, option->extraInfo));
36 }
37 bool ignore_unrecognized = args->ignoreUnrecognized;
38 scoped_ptr<Runtime> runtime(Runtime::Create(options, ignore_unrecognized));
39 if (runtime == NULL) {
40 return JNI_ERR;
41 } else {
42 *p_env = reinterpret_cast<JNIEnv*>(Thread::Current()->GetJniEnv());
43 *p_vm = reinterpret_cast<JavaVM*>(runtime.release());
44 return JNI_OK;
45 }
46}
47
48extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen,
49 jsize* nVMs) {
50 Runtime* runtime = Runtime::Current();
51 if (runtime == NULL) {
52 *nVMs = 0;
53 } else {
54 *nVMs = 1;
55 vmBuf[0] = reinterpret_cast<JavaVM*>(runtime);
56 }
57 return JNI_OK;
58}
59
60// Historically unsupported.
61extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
62 return JNI_ERR;
63}
64
65jint JniInvoke::DestroyJavaVM(JavaVM* vm) {
66 if (vm == NULL) {
67 return JNI_ERR;
68 } else {
69 Runtime* runtime = reinterpret_cast<Runtime*>(vm);
70 delete runtime;
71 return JNI_OK;
72 }
73}
74
75jint JniInvoke::AttachCurrentThread(JavaVM* vm, JNIEnv** p_env,
76 void* thr_args) {
77 if (vm == NULL || p_env == NULL) {
78 return JNI_ERR;
79 }
80 Runtime* runtime = reinterpret_cast<Runtime*>(vm);
81 JniEnvironment** jni_env = reinterpret_cast<JniEnvironment**>(p_env);
82 const char* name = NULL;
83 if (thr_args != NULL) {
84 // TODO: check version
85 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
86 // TODO: thread group
87 }
88 bool success = runtime->AttachCurrentThread(name, jni_env);
89 if (!success) {
90 return JNI_ERR;
91 } else {
92 return JNI_OK;
93 }
94}
95
96jint JniInvoke::DetachCurrentThread(JavaVM* vm) {
97 if (vm == NULL) {
98 return JNI_ERR;
99 } else {
100 Runtime* runtime = reinterpret_cast<Runtime*>(vm);
101 runtime->DetachCurrentThread();
102 return JNI_OK;
103 }
104}
105
106jint JniInvoke::GetEnv(JavaVM *vm, void **env, jint version) {
107 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
108 return JNI_EVERSION;
109 }
110 if (vm == NULL || env == NULL) {
111 return JNI_ERR;
112 }
113 Thread* thread = Thread::Current();
114 if (thread == NULL) {
115 *env = NULL;
116 return JNI_EDETACHED;
117 }
118 *env = thread->GetJniEnv();
119 return JNI_OK;
120}
121
122jint JniInvoke::AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env,
123 void* thr_args) {
124 if (vm == NULL || p_env == NULL) {
125 return JNI_ERR;
126 }
127 Runtime* runtime = reinterpret_cast<Runtime*>(vm);
128 JniEnvironment** jni_env = reinterpret_cast<JniEnvironment**>(p_env);
129 const char* name = NULL;
130 if (thr_args != NULL) {
131 // TODO: check version
132 name = static_cast<JavaVMAttachArgs*>(thr_args)->name;
133 // TODO: thread group
134 }
135 bool success = runtime->AttachCurrentThreadAsDaemon(name, jni_env);
136 if (!success) {
137 return JNI_ERR;
138 } else {
139 return JNI_OK;
140 }
141}
142
Ian Rogersdf20fe02011-07-20 20:34:16 -0700143} // namespace art