Added standard includes, which are needed.
[cacao.git] / src / native / vm / VMRuntime.c
1 /* class: java/lang/Runtime */
2
3
4 #include <string.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/utsname.h>
8 #include "jni.h"
9 #include "builtin.h"
10 #include "loader.h"
11 #include "native.h"
12 #include "tables.h"
13 #include "asmpart.h"
14 #include "mm/boehm.h"
15 #include "toolbox/loging.h"
16 #include "toolbox/memory.h"
17 #include "java_io_File.h"
18 #include "java_lang_String.h"
19 #include "java_util_Properties.h"    /* needed for java_lang_Runtime.h */
20 #include "java_lang_Runtime.h"
21
22
23 #define JOWENN_DEBUG
24
25
26 #define MAXPROPS 100
27 static int activeprops = 19;  
28    
29 static char *proplist[MAXPROPS][2] = {
30         { "java.class.path", NULL },
31         { "java.home", NULL },
32         { "user.home", NULL },  
33         { "user.name", NULL },
34         { "user.dir",  NULL },
35                                 
36         { "os.arch", NULL },
37         { "os.name", NULL },
38         { "os.version", NULL },
39                                          
40         { "java.class.version", "45.3" },
41         { "java.version", PACKAGE":"VERSION },
42         { "java.vendor", "CACAO Team" },
43         { "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/" },
44         { "java.vm.name", "CACAO"}, 
45         { "java.tmpdir", "/tmp/"},
46         { "java.io.tmpdir", "/tmp/"},
47
48         { "path.separator", ":" },
49         { "file.separator", "/" },
50         { "line.separator", "\n" },
51         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
52 };
53
54
55 /*
56  * Class:     java_lang_Runtime
57  * Method:    execInternal
58  * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
59  */
60 JNIEXPORT struct java_lang_Process* JNICALL Java_java_lang_Runtime_execInternal (JNIEnv *env ,  struct java_lang_Runtime* this ,
61         java_objectarray* cmd, java_objectarray* shellenv, struct java_io_File* workingdir)
62 {
63   log_text("Java_java_lang_Runtime_execInternal  called");
64   return NULL;
65 }
66
67
68 /*
69  * Class:     java/lang/Runtime
70  * Method:    exitInternal
71  * Signature: (I)V
72  */
73 JNIEXPORT void JNICALL Java_java_lang_Runtime_exitInternal ( JNIEnv *env ,  struct java_lang_Runtime* this, s4 par1)
74 {
75         cacao_shutdown (par1);
76 }
77
78
79 /*
80  * Class:     java/lang/Runtime
81  * Method:    freeMemory
82  * Signature: ()J
83  */
84 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_freeMemory ( JNIEnv *env ,  struct java_lang_Runtime* this)
85 {
86         log_text ("java_lang_Runtime_freeMemory called");
87         return builtin_i2l (0);
88 }
89
90
91 /*
92  * Class:     java/lang/Runtime
93  * Method:    gc
94  * Signature: ()V
95  */
96 JNIEXPORT void JNICALL Java_java_lang_Runtime_gc ( JNIEnv *env ,  struct java_lang_Runtime* this)
97 {
98         gc_call();
99 }
100
101
102 /*
103  * Class:     java/lang/Runtime
104  * Method:    runFinalization
105  * Signature: ()V
106  */
107 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalization ( JNIEnv *env, struct java_lang_Runtime* this)
108 {
109   log_text("Java_java_lang_Runtime_runFinalization0  called");
110 }
111
112
113 /*
114  * Class:     java/lang/Runtime
115  * Method:    runFinalizersOnExit
116  * Signature: (Z)V
117  */
118 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalizersOnExitInternal ( JNIEnv *env ,  jclass clazz, s4 par1)
119 {
120   log_text("Java_java_lang_Runtime_runFinalizersOnExit0  called");
121 }
122
123
124 /*
125  * Class:     java/lang/Runtime
126  * Method:    totalMemory
127  * Signature: ()J
128  */
129 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_totalMemory ( JNIEnv *env ,  struct java_lang_Runtime* this)
130 {
131         log_text ("java_lang_Runtime_totalMemory called");
132         return builtin_i2l (0);
133 }
134
135
136 /*
137  * Class:     java/lang/Runtime
138  * Method:    traceInstructions
139  * Signature: (Z)V
140  */
141 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceInstructions ( JNIEnv *env ,  struct java_lang_Runtime* this, s4 par1)
142 {
143   log_text("Java_java_lang_Runtime_traceInstructions  called");
144 }
145
146
147 /*
148  * Class:     java/lang/Runtime
149  * Method:    traceMethodCalls
150  * Signature: (Z)V
151  */
152 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceMethodCalls ( JNIEnv *env ,  struct java_lang_Runtime* this, s4 par1)
153 {
154   log_text("Java_java_lang_Runtime_traceMethodCalls  called");
155 }
156
157
158 /*
159  * Class:     java_lang_Runtime
160  * Method:    availableProcessors
161  * Signature: ()I
162  */
163 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_availableProcessors (JNIEnv *env ,  struct java_lang_Runtime* this ) {
164         log_text("Java_java_lang_Runtime_availableProcessors called, returning hardcoded 1");
165         return 1;
166 }
167
168
169 /*
170  * Class:     java_lang_Runtime
171  * Method:    nativeLoad
172  * Signature: (Ljava/lang/String;)I
173  */
174 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_nativeLoad (JNIEnv *env ,  struct java_lang_Runtime* this , struct java_lang_String* 
175 par1)
176 {
177 #ifdef JOWENN_DEBUG     
178         char *buffer;                   
179         int buffer_len;
180         utf * data;
181         
182         data=javastring_toutf(par1,0);
183         
184         if (!data) {
185                 log_text("nativeLoad: Error: empty string");
186                 return 1;
187         }
188         
189         buffer_len = 
190           utf_strlen(data)+ 40;
191
192                 
193         buffer = MNEW(char, buffer_len);
194
195         strcpy(buffer, "Java_java_lang_Runtime_nativeLoad:");
196         utf_sprint(buffer+strlen(data), data);
197         log_text(buffer);       
198
199         MFREE(buffer, char, buffer_len);
200 #endif
201         log_text("Java_java_lang_Runtime_nativeLoad");
202         return 1;
203 }
204
205
206 /*
207  * Class:     java_lang_Runtime
208  * Method:    nativeGetLibname
209  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
210  */
211 JNIEXPORT struct java_lang_String* JNICALL Java_java_lang_Runtime_nativeGetLibname (JNIEnv *env , jclass clazz, struct java_lang_String* par1,
212 struct java_lang_String* par2) {
213 #ifdef JOWENN_DEBUG     
214         char *buffer;                   
215         int buffer_len;
216         utf * data;
217         
218         data=javastring_toutf(par2,0);
219         
220                 if (!data) {
221                 log_text("nativeGetLibName: Error: empty string");
222                 return 0;;
223         }
224         
225         buffer_len = 
226           utf_strlen(data)+ 40;
227         
228         buffer = MNEW(char, buffer_len);
229
230         strcpy(buffer, "Java_java_lang_Runtime_nativeGetLibname:");
231         utf_sprint(buffer+strlen(data), data);
232         log_text(buffer);       
233
234         MFREE(buffer, char, buffer_len);
235 #endif
236         log_text("Java_java_lang_Runtime_nativeGetLibname");
237         return 0;
238 }
239
240
241 /*
242  * Class:     java_lang_Runtime
243  * Method:    insertSystemProperties
244  * Signature: (Ljava/util/Properties;)V
245  */
246 JNIEXPORT void JNICALL Java_java_lang_Runtime_insertSystemProperties (JNIEnv *env , jclass clazz, struct java_util_Properties* p) {
247
248         #define BUFFERSIZE 200
249         u4 i;
250         methodinfo *m;
251         char buffer[BUFFERSIZE];
252         struct utsname utsnamebuf;
253
254         log_text("Java_java_lang_Runtime_insertSystemProperties");
255
256         proplist[0][1] = classpath;
257         proplist[1][1] = getenv("JAVA_HOME");
258         proplist[2][1] = getenv("HOME");
259         proplist[3][1] = getenv("USER");
260         proplist[4][1] = getcwd(buffer, BUFFERSIZE);
261
262         /* get properties from system */
263         uname(&utsnamebuf);
264         proplist[5][1] = utsnamebuf.machine;
265         proplist[6][1] = utsnamebuf.sysname;
266         proplist[7][1] = utsnamebuf.release;
267
268         if (!p) panic("Java_java_lang_Runtime_insertSystemProperties called with NULL-Argument");
269
270         /* search for method to add properties */
271         m = class_resolvemethod(
272                                 p->header.vftbl->class,
273                                 utf_new_char("put"),
274                                 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
275                                 );
276
277         if (!m) panic("Can not find method 'put' for class Properties");
278
279         /* add the properties */
280         for (i = 0; i < activeprops; i++) {
281
282             if (proplist[i][1] == NULL) proplist[i][1] = "";
283
284             asm_calljavamethod(m,
285                                p,
286                                javastring_new_char(proplist[i][0]),
287                                javastring_new_char(proplist[i][1]),
288                                NULL
289                                );
290         }
291
292         return;
293 }
294
295
296 /*
297  * Class:     java_lang_Runtime
298  * Method:    maxMemory
299  * Signature: ()J
300  */
301 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_maxMemory (JNIEnv *env ,  struct java_lang_Runtime* this ) {
302         log_text("Java_java_lang_Runtime_maxMemory");
303         return 0;
304 }
305
306
307 /*
308  * These are local overrides for various environment variables in Emacs.
309  * Please do not remove this and leave it at the end of the file, where
310  * Emacs will automagically detect them.
311  * ---------------------------------------------------------------------
312  * Local variables:
313  * mode: c
314  * indent-tabs-mode: t
315  * c-basic-offset: 4
316  * tab-width: 4
317  * End:
318  */