1 /****************************** native.c ***************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Enth"alt die Tabellen f"ur die native-methods.
8 Die vom Headerfile-Generator erzeugten -.hh - Dateien werden hier
9 eingebunden, und ebenso alle C-Funktionen, mit denen diese
10 Methoden implementiert werden.
12 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
14 Last Change: 1996/11/14
16 *******************************************************************************/
25 #include "nativetypes.hh"
31 java_objectheader* exceptionptr = NULL;
35 static char *classpath;
38 /******************** die f"r die native-Methoden n"otigen Systemklassen *****/
40 static classinfo *class_java_lang_Class;
41 static classinfo *class_java_lang_Cloneable;
42 static classinfo *class_java_lang_CloneNotSupportedException;
43 static classinfo *class_java_lang_Double;
44 static classinfo *class_java_lang_Float;
45 static classinfo *class_java_io_IOException;
46 static classinfo *class_java_lang_ClassNotFoundException;
47 static classinfo *class_java_lang_InstantiationException;
51 /************************** alle Funktionen einbinden ************************/
58 /********************** Tabellen f"ur die Methoden ***************************/
60 static struct nativeref {
68 #include "nativetable.hh"
73 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
75 static struct nativecompref {
81 } nativecomptable [NATIVETABLESIZE];
83 static bool nativecompdone = false;
86 /*********************** Funktion: native_loadclasses **************************
88 L"adt alle Klassen, die die native Methoden zus"atzlich ben"otigen
90 *******************************************************************************/
92 void native_loadclasses()
94 class_java_lang_Cloneable =
95 loader_load ( unicode_new_char ("java/lang/Cloneable") );
96 class_java_lang_CloneNotSupportedException =
97 loader_load ( unicode_new_char ("java/lang/CloneNotSupportedException") );
98 class_java_lang_Class =
99 loader_load ( unicode_new_char ("java/lang/Class") );
100 class_java_lang_Double =
101 loader_load ( unicode_new_char ("java/lang/Double") );
102 class_java_lang_Float =
103 loader_load ( unicode_new_char ("java/lang/Float") );
104 class_java_io_IOException =
105 loader_load ( unicode_new_char ("java/io/IOException") );
106 class_java_lang_ClassNotFoundException =
107 loader_load ( unicode_new_char ("java/lang/ClassNotFoundException") );
108 class_java_lang_InstantiationException=
109 loader_load ( unicode_new_char ("java/lang/InstantiationException") );
113 /********************* Funktion: native_setclasspath ***************************/
115 void native_setclasspath (char *path)
121 /*********************** Funktion: native_findfunction ************************
123 Sucht in der Tabelle die passende Methode (muss mit Klassennamen,
124 Methodennamen, Descriptor und 'static'-Status "ubereinstimmen),
125 und gibt den Funktionszeiger darauf zur"uck.
126 Return: Funktionszeiger oder NULL (wenn es keine solche Methode gibt)
128 Anmerkung: Zu Beschleunigung des Suchens werden die als C-Strings
129 vorliegenden Namen/Descriptors in entsprechende unicode-Symbole
130 umgewandelt (beim ersten Aufruf dieser Funktion).
132 *******************************************************************************/
134 functionptr native_findfunction (unicode *cname, unicode *mname,
135 unicode *desc, bool isstatic)
138 struct nativecompref *n;
140 isstatic = isstatic ? true : false;
142 if (!nativecompdone) {
143 for (i=0; i<NATIVETABLESIZE; i++) {
144 nativecomptable[i].classname =
145 unicode_new_char(nativetable[i].classname);
146 nativecomptable[i].methodname =
147 unicode_new_char(nativetable[i].methodname);
148 nativecomptable[i].descriptor =
149 unicode_new_char(nativetable[i].descriptor);
150 nativecomptable[i].isstatic =
151 nativetable[i].isstatic;
152 nativecomptable[i].func =
155 nativecompdone = true;
158 for (i=0; i<NATIVETABLESIZE; i++) {
159 n = &(nativecomptable[i]);
161 if (cname==n->classname && mname==n->methodname &&
162 desc==n->descriptor && isstatic==n->isstatic) return n->func;
169 /********************** Funktion: javastring_new *****************************
171 Legt ein neues Objekt vom Typ java/lang/String an, und tr"agt als Text
172 das "ubergebene unicode-Symbol ein.
173 Return: Zeiger auf den String, oder NULL (wenn Speicher aus)
175 *****************************************************************************/
177 java_objectheader *javastring_new (unicode *text)
183 s = (java_lang_String*) builtin_new (class_java_lang_String);
184 a = builtin_newarray_char (text->length);
186 if ( (!a) || (!s) ) return NULL;
188 for (i=0; i<text->length; i++) a->data[i] = text->text[i];
191 s -> count = text->length;
193 return (java_objectheader*) s;
197 /********************** Funktion: javastring_new_char ************************
199 Legt ein neues Objekt vom Typ java/lang/String an, und tr"agt als Text
200 den "ubergebenen C-String ein.
201 Return: Zeiger auf den String, oder NULL (wenn Speicher aus)
203 *****************************************************************************/
205 java_objectheader *javastring_new_char (char *text)
208 u4 len = strlen(text);
212 s = (java_lang_String*) builtin_new (class_java_lang_String);
213 a = builtin_newarray_char (len);
215 if ( (!a) || (!s) ) return NULL;
217 for (i=0; i<len; i++) a->data[i] = text[i];
222 return (java_objectheader*) s;
226 /************************* Funktion: javastring_tochar *****************************
228 Macht aus einem java-string einen C-String, und liefert den Zeiger
230 Achtung: Beim n"achsten Aufruf der Funktion wird der vorige String
233 ***********************************************************************************/
235 #define MAXSTRINGSIZE 1000
236 char stringbuffer[MAXSTRINGSIZE];
238 char *javastring_tochar (java_objectheader *so)
240 java_lang_String *s = (java_lang_String*) so;
247 if (s->count > MAXSTRINGSIZE) return "";
248 for (i=0; i<s->count; i++) stringbuffer[i] = a->data[s->offset+i];
249 stringbuffer[i] = '\0';
254 /******************** Funktion: native_new_and_init *************************
256 Legt ein neues Objekt einer Klasse am Heap an, und ruft automatisch
257 die Initialisierungsmethode auf.
258 Return: Der Zeiger auf das Objekt, oder NULL, wenn kein Speicher
261 *****************************************************************************/
263 java_objectheader *native_new_and_init (classinfo *c)
266 java_objectheader *o = builtin_new (c);
270 m = class_findmethod (c,
271 unicode_new_char ("<init>"),
272 unicode_new_char ("()V"));
274 log_text ("warning: class has no instance-initializer:");
275 unicode_sprint (logtext, c->name);
280 asm_calljavamethod (m, o,NULL,NULL,NULL);
285 /********************* Funktion: literalstring_new ****************************
287 erzeugt einen Java-String mit dem angegebenen Text, allerdings nicht
288 auf dem HEAP, sondern in einem anderen Speicherbereich (der String
289 muss dann sp"ater explizit wieder freigegeben werden).
290 Alle Strings, die auf diese Art erzeugt werden, werden in einer
291 gemeinsamen Struktur gespeichert (n"amlich auch "uber die
292 Unicode-Hashtabelle), sodass identische Strings auch wirklich den
293 gleichen Zeiger liefern.
295 ******************************************************************************/
297 java_objectheader *literalstring_new (unicode *text)
303 if (text->string) return text->string;
305 a = lit_mem_alloc (sizeof(java_chararray) + sizeof(u2)*(text->length-1) );
306 a -> header.objheader.vftbl = class_array -> vftbl;
307 a -> header.size = text->length;
308 a -> header.arraytype = ARRAYTYPE_CHAR;
309 for (i=0; i<text->length; i++) a->data[i] = text->text[i];
311 s = LNEW (java_lang_String);
312 s -> header.vftbl = class_java_lang_String -> vftbl;
315 s -> count = text->length;
317 unicode_setstringlink (text, (java_objectheader*) s);
318 return (java_objectheader*) s;
322 /********************** Funktion: literalstring_free **************************
324 L"oscht einen Java-String wieder aus dem Speicher (wird zu Systemende
325 vom Hashtabellen-Verwalter aufgerufen)
327 ******************************************************************************/
329 void literalstring_free (java_objectheader* sobj)
331 java_lang_String *s = (java_lang_String*) sobj;
332 java_chararray *a = s->value;
334 LFREE (s, java_lang_String);
335 LFREE (a, sizeof(java_chararray) + sizeof(u2)*(a->header.size-1));