- exception stuff
[cacao.git] / src / native / vm / VMRuntime.c
1 /* nat/Runtime.c - java/lang/Runtime
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: VMRuntime.c 1042 2004-04-26 17:12:47Z twisti $
33
34 */
35
36
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/utsname.h>
41 #include "jni.h"
42 #include "builtin.h"
43 #include "loader.h"
44 #include "native.h"
45 #include "tables.h"
46 #include "asmpart.h"
47 #include "mm/boehm.h"
48 #include "toolbox/loging.h"
49 #include "toolbox/memory.h"
50 #include "java_io_File.h"
51 #include "java_lang_String.h"
52 #include "java_lang_Process.h"
53 #include "java_util_Properties.h"    /* needed for java_lang_Runtime.h */
54 #include "java_lang_Runtime.h"
55
56
57 #define JOWENN_DEBUG
58
59
60 #define MAXPROPS 100
61 static int activeprops = 19;  
62    
63 static char *proplist[MAXPROPS][2] = {
64         { "java.class.path", NULL },
65         { "java.home", NULL },
66         { "user.home", NULL },  
67         { "user.name", NULL },
68         { "user.dir",  NULL },
69                                 
70         { "os.arch", NULL },
71         { "os.name", NULL },
72         { "os.version", NULL },
73                                          
74         { "java.class.version", "45.3" },
75         { "java.version", PACKAGE":"VERSION },
76         { "java.vendor", "CACAO Team" },
77         { "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/" },
78         { "java.vm.name", "CACAO"}, 
79         { "java.tmpdir", "/tmp/"},
80         { "java.io.tmpdir", "/tmp/"},
81
82         { "path.separator", ":" },
83         { "file.separator", "/" },
84         { "line.separator", "\n" },
85         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
86 };
87
88 void attach_property(char *name, char *value)
89 {
90         if (activeprops >= MAXPROPS) panic("Too many properties defined");
91         proplist[activeprops][0] = name;
92         proplist[activeprops][1] = value;
93         activeprops++;
94 }
95 /*
96  * Class:     java_lang_Runtime
97  * Method:    execInternal
98  * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
99  */
100 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_Runtime_execInternal(JNIEnv *env, java_lang_Runtime *this, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
101 {
102         log_text("Java_java_lang_Runtime_execInternal called");
103
104         return NULL;
105 }
106
107
108 /*
109  * Class:     java/lang/Runtime
110  * Method:    exitInternal
111  * Signature: (I)V
112  */
113 JNIEXPORT void JNICALL Java_java_lang_Runtime_exitInternal(JNIEnv *env, java_lang_Runtime *this, s4 par1)
114 {
115         cacao_shutdown(par1);
116 }
117
118
119 /*
120  * Class:     java/lang/Runtime
121  * Method:    freeMemory
122  * Signature: ()J
123  */
124 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_freeMemory(JNIEnv *env, java_lang_Runtime *this)
125 {
126         return gc_get_free_bytes();
127 }
128
129
130 /*
131  * Class:     java/lang/Runtime
132  * Method:    gc
133  * Signature: ()V
134  */
135 JNIEXPORT void JNICALL Java_java_lang_Runtime_gc(JNIEnv *env, java_lang_Runtime *this)
136 {
137         gc_call();
138 }
139
140
141 /*
142  * Class:     java/lang/Runtime
143  * Method:    runFinalization
144  * Signature: ()V
145  */
146 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalization(JNIEnv *env, java_lang_Runtime *this)
147 {
148         gc_finalize_all();
149 }
150
151
152 /*
153  * Class:     java/lang/Runtime
154  * Method:    runFinalizersOnExit
155  * Signature: (Z)V
156  */
157 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalizersOnExitInternal(JNIEnv *env, jclass clazz, s4 par1)
158 {
159         log_text("Java_java_lang_Runtime_runFinalizersOnExit0 called");
160 }
161
162
163 /*
164  * Class:     java/lang/Runtime
165  * Method:    totalMemory
166  * Signature: ()J
167  */
168 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_totalMemory(JNIEnv *env, java_lang_Runtime *this)
169 {
170         return gc_get_heap_size();
171 }
172
173
174 /*
175  * Class:     java/lang/Runtime
176  * Method:    traceInstructions
177  * Signature: (Z)V
178  */
179 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceInstructions(JNIEnv *env, java_lang_Runtime *this, s4 par1)
180 {
181         /* not supported */
182 }
183
184
185 /*
186  * Class:     java/lang/Runtime
187  * Method:    traceMethodCalls
188  * Signature: (Z)V
189  */
190 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceMethodCalls(JNIEnv *env, java_lang_Runtime *this, s4 par1)
191 {
192         /* not supported */
193 }
194
195
196 /*
197  * Class:     java_lang_Runtime
198  * Method:    availableProcessors
199  * Signature: ()I
200  */
201 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_availableProcessors(JNIEnv *env, java_lang_Runtime *this)
202 {
203 #if defined(_SC_NPROC_ONLN)
204         return (s4) sysconf(_SC_NPROC_ONLN);
205
206 #elif defined(_SC_NPROCESSORS_ONLN)
207         return (s4) sysconf(_SC_NPROCESSORS_ONLN);
208
209 #else
210         return 1;
211 #endif
212 }
213
214
215 /*
216  * Class:     java_lang_Runtime
217  * Method:    nativeLoad
218  * Signature: (Ljava/lang/String;)I
219  */
220 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_nativeLoad(JNIEnv *env, java_lang_Runtime *this, java_lang_String *par1)
221 {
222 #ifdef JOWENN_DEBUG     
223         char *buffer;
224         int buffer_len;
225         utf *data;
226         
227         data = javastring_toutf(par1, 0);
228         
229         if (!data) {
230                 log_text("nativeLoad: Error: empty string");
231                 return 1;
232         }
233         
234         buffer_len = utf_strlen(data) + 40;
235
236                 
237         buffer = MNEW(char, buffer_len);
238
239         strcpy(buffer, "Java_java_lang_Runtime_nativeLoad:");
240         utf_sprint(buffer + strlen((char *) data), data);
241         log_text(buffer);       
242
243         MFREE(buffer, char, buffer_len);
244 #endif
245         log_text("Java_java_lang_Runtime_nativeLoad");
246
247         return 1;
248 }
249
250
251 /*
252  * Class:     java_lang_Runtime
253  * Method:    nativeGetLibname
254  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
255  */
256 JNIEXPORT java_lang_String* JNICALL Java_java_lang_Runtime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
257 {
258 #ifdef JOWENN_DEBUG     
259         char *buffer;
260         int buffer_len;
261         utf *data;
262         
263         data = javastring_toutf(par2, 0);
264         
265         if (!data) {
266                 log_text("nativeGetLibName: Error: empty string");
267                 return 0;;
268         }
269         
270         buffer_len = utf_strlen(data) + 40;
271         
272         buffer = MNEW(char, buffer_len);
273
274         strcpy(buffer, "Java_java_lang_Runtime_nativeGetLibname:");
275         utf_sprint(buffer + strlen((char *) data), data);
276         log_text(buffer);       
277
278         MFREE(buffer, char, buffer_len);
279 #endif
280         log_text("Java_java_lang_Runtime_nativeGetLibname");
281
282         return 0;
283 }
284
285
286 /*
287  * Class:     java_lang_Runtime
288  * Method:    insertSystemProperties
289  * Signature: (Ljava/util/Properties;)V
290  */
291 JNIEXPORT void JNICALL Java_java_lang_Runtime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
292 {
293
294 #define BUFFERSIZE 200
295         u4 i;
296         methodinfo *m;
297         char buffer[BUFFERSIZE];
298         struct utsname utsnamebuf;
299
300         proplist[0][1] = classpath;
301         proplist[1][1] = getenv("JAVA_HOME");
302         proplist[2][1] = getenv("HOME");
303         proplist[3][1] = getenv("USER");
304         proplist[4][1] = getcwd(buffer, BUFFERSIZE);
305
306         /* get properties from system */
307         uname(&utsnamebuf);
308         proplist[5][1] = utsnamebuf.machine;
309         proplist[6][1] = utsnamebuf.sysname;
310         proplist[7][1] = utsnamebuf.release;
311
312         if (!p) {
313                 *exceptionptr = new_exception(string_java_lang_NullPointerException);
314                 return;
315         }
316
317         /* search for method to add properties */
318         m = class_resolvemethod(p->header.vftbl->class,
319                                                         utf_new_char("put"),
320                                                         utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
321                                                         );
322
323         if (!m) {
324                 *exceptionptr = new_exception_message(string_java_lang_NoSuchMethodError,
325                                                                                           "java.lang.Properties.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;)");
326                 return;
327         }
328
329         /* add the properties */
330         for (i = 0; i < activeprops; i++) {
331
332                 if (proplist[i][1] == NULL) proplist[i][1] = "";
333
334                 asm_calljavafunction(m,
335                                                          p,
336                                                          javastring_new_char(proplist[i][0]),
337                                                          javastring_new_char(proplist[i][1]),
338                                                          NULL
339                                                          );
340         }
341
342         return;
343 }
344
345
346 /*
347  * Class:     java_lang_Runtime
348  * Method:    maxMemory
349  * Signature: ()J
350  */
351 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_maxMemory(JNIEnv *env, java_lang_Runtime *this)
352 {
353         return gc_get_max_heap_size();
354 }
355
356
357 /*
358  * These are local overrides for various environment variables in Emacs.
359  * Please do not remove this and leave it at the end of the file, where
360  * Emacs will automagically detect them.
361  * ---------------------------------------------------------------------
362  * Local variables:
363  * mode: c
364  * indent-tabs-mode: t
365  * c-basic-offset: 4
366  * tab-width: 4
367  * End:
368  */