* src/native/vm/nativevm.cpp (nativevm_init): Return boolean to indicate error.
[cacao.git] / src / native / vm / nativevm.cpp
1 /* src/native/vm/nativevm.cpp - Register native VM interface functions.
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/vm/nativevm.hpp"
31
32 #include "vm/class.hpp"
33 #include "vm/exceptions.hpp"
34 #include "vm/initialize.hpp"
35 #include "vm/method.hpp"
36 #include "vm/options.h"
37 #include "vm/os.hpp"
38
39 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
40 # include "mm/memory.hpp"
41
42 # include "native/native.hpp"
43
44 # include "native/vm/openjdk/hpi.hpp"
45
46 # include "vm/globals.hpp"
47 # include "vm/properties.hpp"
48 # include "vm/utf8.h"
49 # include "vm/vm.hpp"
50 #endif
51
52
53 /* nativevm_preinit ************************************************************
54
55    Pre-initialize the implementation specific native stuff.
56
57 *******************************************************************************/
58
59 void nativevm_preinit(void)
60 {
61         TRACESUBSYSTEMINITIALIZATION("nativevm_preinit");
62
63         /* Register native methods of all classes implemented. */
64
65 #if defined(ENABLE_JAVASE)
66 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
67
68         _Jv_gnu_classpath_VMStackWalker_init();
69         _Jv_gnu_classpath_VMSystemProperties_init();
70         _Jv_gnu_java_lang_VMCPStringBuilder_init();
71         _Jv_gnu_java_lang_management_VMClassLoadingMXBeanImpl_init();
72         _Jv_gnu_java_lang_management_VMMemoryMXBeanImpl_init();
73         _Jv_gnu_java_lang_management_VMRuntimeMXBeanImpl_init();
74         _Jv_gnu_java_lang_management_VMThreadMXBeanImpl_init();
75         _Jv_java_lang_VMClass_init();
76         _Jv_java_lang_VMClassLoader_init();
77         _Jv_java_lang_VMObject_init();
78         _Jv_java_lang_VMRuntime_init();
79         _Jv_java_lang_VMSystem_init();
80         _Jv_java_lang_VMString_init();
81         _Jv_java_lang_VMThread_init();
82         _Jv_java_lang_VMThrowable_init();
83         _Jv_java_lang_management_VMManagementFactory_init();
84         _Jv_java_lang_reflect_VMConstructor_init();
85         _Jv_java_lang_reflect_VMField_init();
86         _Jv_java_lang_reflect_VMMethod_init();
87         //_Jv_java_lang_reflect_VMProxy_init();
88         _Jv_java_security_VMAccessController_init();
89         _Jv_java_util_concurrent_atomic_AtomicLong_init();
90         _Jv_sun_misc_Unsafe_init();
91
92 #if defined(ENABLE_ANNOTATIONS)
93         _Jv_sun_reflect_ConstantPool_init();
94 #endif
95
96 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
97
98         // Load libjava.so
99         VM* vm = VM::get_current();
100         Properties& properties = vm->get_properties();
101         const char* boot_library_path = properties.get("sun.boot.library.path");
102
103         size_t len =
104                 os::strlen(boot_library_path) +
105                 os::strlen("/libjava.so") +
106                 os::strlen("0");
107
108         char* p = MNEW(char, len);
109
110         os::strcpy(p, boot_library_path);
111         os::strcat(p, "/libjava.so");
112
113         utf* u = utf_new_char(p);
114
115         NativeLibrary nl(u);
116         void* handle = nl.open();
117
118         if (handle == NULL)
119                 os::abort("nativevm_init: failed to open libjava.so at: %s", p);
120
121         MFREE(p, char, len);
122
123         NativeLibraries& nls = vm->get_nativelibraries();
124         nls.add(nl);
125
126         // Initialize the HPI.
127         HPI& hpi = vm->get_hpi();
128         hpi.initialize();
129
130         _Jv_sun_misc_Unsafe_init();
131
132 # else
133 #  error unknown classpath configuration
134 # endif
135
136 #elif defined(ENABLE_JAVAME_CLDC1_1)
137
138         _Jv_com_sun_cldc_io_ResourceInputStream_init();
139         _Jv_com_sun_cldc_io_j2me_socket_Protocol_init();
140         _Jv_com_sun_cldchi_io_ConsoleOutputStream_init();
141         _Jv_com_sun_cldchi_jvm_JVM_init();
142         _Jv_java_lang_Class_init();
143         _Jv_java_lang_Double_init();
144         _Jv_java_lang_Float_init();
145         _Jv_java_lang_Math_init();
146         _Jv_java_lang_Object_init();
147         _Jv_java_lang_Runtime_init();
148         _Jv_java_lang_String_init();
149         _Jv_java_lang_System_init();
150         _Jv_java_lang_Thread_init();
151         _Jv_java_lang_Throwable_init();
152
153 #else
154 # error unknown Java configuration
155 #endif
156 }
157
158
159 /* nativevm_init ***************************************************************
160
161    Initialize the implementation specific native stuff.
162
163 *******************************************************************************/
164
165 bool nativevm_init(void)
166 {
167         TRACESUBSYSTEMINITIALIZATION("nativevm_init");
168
169 #if defined(ENABLE_JAVASE)
170
171 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
172
173         // Nothing to do.
174
175 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
176
177         methodinfo* m = class_resolveclassmethod(class_java_lang_System,
178                                                                                          utf_new_char("initializeSystemClass"),
179                                                                                          utf_void__void,
180                                                                                          class_java_lang_Object,
181                                                                                          false);
182
183         if (m == NULL)
184                 return false;
185
186         (void) vm_call_method(m, NULL);
187
188         if (exceptions_get_exception() != NULL)
189                 return false;
190
191 # else
192 #  error unknown classpath configuration
193 # endif
194
195 #elif defined(ENABLE_JAVAME_CLDC1_1)
196
197         // Nothing to do.
198
199 #else
200 # error unknown Java configuration
201 #endif
202
203         return true;
204 }
205
206
207 /*
208  * These are local overrides for various environment variables in Emacs.
209  * Please do not remove this and leave it at the end of the file, where
210  * Emacs will automagically detect them.
211  * ---------------------------------------------------------------------
212  * Local variables:
213  * mode: c++
214  * indent-tabs-mode: t
215  * c-basic-offset: 4
216  * tab-width: 4
217  * End:
218  * vim:noexpandtab:sw=4:ts=4:
219  */