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