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