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 *******************************************************************************/
26 #include "nativetypes.hh"
32 java_objectheader* exceptionptr = NULL;
36 static char *classpath;
39 /******************** die f"r die native-Methoden n"otigen Systemklassen *****/
41 static classinfo *class_java_lang_Class;
42 static classinfo *class_java_lang_Cloneable;
43 static classinfo *class_java_lang_CloneNotSupportedException;
44 static classinfo *class_java_lang_Double;
45 static classinfo *class_java_lang_Float;
46 static classinfo *class_java_io_IOException;
47 static classinfo *class_java_lang_ClassNotFoundException;
48 static classinfo *class_java_lang_InstantiationException;
52 /************************** alle Funktionen einbinden ************************/
59 /********************** Tabellen f"ur die Methoden ***************************/
61 static struct nativeref {
69 #include "nativetable.hh"
74 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
76 static struct nativecompref {
82 } nativecomptable [NATIVETABLESIZE];
84 static bool nativecompdone = false;
87 /*********************** Funktion: native_loadclasses **************************
89 L"adt alle Klassen, die die native Methoden zus"atzlich ben"otigen
91 *******************************************************************************/
93 void native_loadclasses()
95 class_java_lang_Cloneable =
96 loader_load ( unicode_new_char ("java/lang/Cloneable") );
97 class_java_lang_CloneNotSupportedException =
98 loader_load ( unicode_new_char ("java/lang/CloneNotSupportedException") );
99 class_java_lang_Class =
100 loader_load ( unicode_new_char ("java/lang/Class") );
101 class_java_lang_Double =
102 loader_load ( unicode_new_char ("java/lang/Double") );
103 class_java_lang_Float =
104 loader_load ( unicode_new_char ("java/lang/Float") );
105 class_java_io_IOException =
106 loader_load ( unicode_new_char ("java/io/IOException") );
107 class_java_lang_ClassNotFoundException =
108 loader_load ( unicode_new_char ("java/lang/ClassNotFoundException") );
109 class_java_lang_InstantiationException=
110 loader_load ( unicode_new_char ("java/lang/InstantiationException") );
114 /********************* Funktion: native_setclasspath ***************************/
116 void native_setclasspath (char *path)
122 /*********************** Funktion: native_findfunction ************************
124 Sucht in der Tabelle die passende Methode (muss mit Klassennamen,
125 Methodennamen, Descriptor und 'static'-Status "ubereinstimmen),
126 und gibt den Funktionszeiger darauf zur"uck.
127 Return: Funktionszeiger oder NULL (wenn es keine solche Methode gibt)
129 Anmerkung: Zu Beschleunigung des Suchens werden die als C-Strings
130 vorliegenden Namen/Descriptors in entsprechende unicode-Symbole
131 umgewandelt (beim ersten Aufruf dieser Funktion).
133 *******************************************************************************/
135 functionptr native_findfunction (unicode *cname, unicode *mname,
136 unicode *desc, bool isstatic)
139 struct nativecompref *n;
141 isstatic = isstatic ? true : false;
143 if (!nativecompdone) {
144 for (i=0; i<NATIVETABLESIZE; i++) {
145 nativecomptable[i].classname =
146 unicode_new_char(nativetable[i].classname);
147 nativecomptable[i].methodname =
148 unicode_new_char(nativetable[i].methodname);
149 nativecomptable[i].descriptor =
150 unicode_new_char(nativetable[i].descriptor);
151 nativecomptable[i].isstatic =
152 nativetable[i].isstatic;
153 nativecomptable[i].func =
156 nativecompdone = true;
159 for (i=0; i<NATIVETABLESIZE; i++) {
160 n = &(nativecomptable[i]);
162 if (cname==n->classname && mname==n->methodname &&
163 desc==n->descriptor && isstatic==n->isstatic) return n->func;
170 /********************** Funktion: javastring_new *****************************
172 Legt ein neues Objekt vom Typ java/lang/String an, und tr"agt als Text
173 das "ubergebene unicode-Symbol ein.
174 Return: Zeiger auf den String, oder NULL (wenn Speicher aus)
176 *****************************************************************************/
178 java_objectheader *javastring_new (unicode *text)
184 s = (java_lang_String*) builtin_new (class_java_lang_String);
185 a = builtin_newarray_char (text->length);
187 if ( (!a) || (!s) ) return NULL;
189 for (i=0; i<text->length; i++) a->data[i] = text->text[i];
192 s -> count = text->length;
194 return (java_objectheader*) s;
198 /********************** Funktion: javastring_new_char ************************
200 Legt ein neues Objekt vom Typ java/lang/String an, und tr"agt als Text
201 den "ubergebenen C-String ein.
202 Return: Zeiger auf den String, oder NULL (wenn Speicher aus)
204 *****************************************************************************/
206 java_objectheader *javastring_new_char (char *text)
209 u4 len = strlen(text);
213 s = (java_lang_String*) builtin_new (class_java_lang_String);
214 a = builtin_newarray_char (len);
216 if ( (!a) || (!s) ) return NULL;
218 for (i=0; i<len; i++) a->data[i] = text[i];
223 return (java_objectheader*) s;
227 /************************* Funktion: javastring_tochar *****************************
229 Macht aus einem java-string einen C-String, und liefert den Zeiger
231 Achtung: Beim n"achsten Aufruf der Funktion wird der vorige String
234 ***********************************************************************************/
236 #define MAXSTRINGSIZE 1000
237 char stringbuffer[MAXSTRINGSIZE];
239 char *javastring_tochar (java_objectheader *so)
241 java_lang_String *s = (java_lang_String*) so;
248 if (s->count > MAXSTRINGSIZE) return "";
249 for (i=0; i<s->count; i++) stringbuffer[i] = a->data[s->offset+i];
250 stringbuffer[i] = '\0';
255 /******************** Funktion: native_new_and_init *************************
257 Legt ein neues Objekt einer Klasse am Heap an, und ruft automatisch
258 die Initialisierungsmethode auf.
259 Return: Der Zeiger auf das Objekt, oder NULL, wenn kein Speicher
262 *****************************************************************************/
264 java_objectheader *native_new_and_init (classinfo *c)
267 java_objectheader *o = builtin_new (c);
271 m = class_findmethod (c,
272 unicode_new_char ("<init>"),
273 unicode_new_char ("()V"));
275 log_text ("warning: class has no instance-initializer:");
276 unicode_sprint (logtext, c->name);
281 asm_calljavamethod (m, o,NULL,NULL,NULL);
286 /********************* Funktion: literalstring_new ****************************
288 erzeugt einen Java-String mit dem angegebenen Text, allerdings nicht
289 auf dem HEAP, sondern in einem anderen Speicherbereich (der String
290 muss dann sp"ater explizit wieder freigegeben werden).
291 Alle Strings, die auf diese Art erzeugt werden, werden in einer
292 gemeinsamen Struktur gespeichert (n"amlich auch "uber die
293 Unicode-Hashtabelle), sodass identische Strings auch wirklich den
294 gleichen Zeiger liefern.
296 ******************************************************************************/
298 java_objectheader *literalstring_new (unicode *text)
304 if (text->string) return text->string;
306 a = lit_mem_alloc (sizeof(java_chararray) + sizeof(u2)*(text->length-1) );
307 a -> header.objheader.vftbl = class_array -> vftbl;
308 a -> header.size = text->length;
309 a -> header.arraytype = ARRAYTYPE_CHAR;
310 for (i=0; i<text->length; i++) a->data[i] = text->text[i];
312 s = LNEW (java_lang_String);
313 s -> header.vftbl = class_java_lang_String -> vftbl;
316 s -> count = text->length;
318 unicode_setstringlink (text, (java_objectheader*) s);
319 return (java_objectheader*) s;
323 /********************** Funktion: literalstring_free **************************
325 L"oscht einen Java-String wieder aus dem Speicher (wird zu Systemende
326 vom Hashtabellen-Verwalter aufgerufen)
328 ******************************************************************************/
330 void literalstring_free (java_objectheader* sobj)
332 java_lang_String *s = (java_lang_String*) sobj;
333 java_chararray *a = s->value;
335 LFREE (s, java_lang_String);
336 LFREE (a, sizeof(java_chararray) + sizeof(u2)*(a->header.size-1));