1 /* native.c - table of native functions
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
31 The .hh files created with the header file generator are all
32 included here as are the C functions implementing these methods.
34 $Id: native.c 785 2003-12-15 16:34:26Z twisti $
52 #include "nativetypes.hh"
58 #include "toolbox/loging.h"
59 #include "toolbox/memory.h"
60 #include "threads/thread.h"
61 #include "threads/threadio.h"
62 #include "threads/locks.h"
64 /* Include files for IO functions */
68 #include <sys/types.h>
74 #include "threads/threadio.h"
76 /* searchpath for classfiles */
79 /* for java-string to char conversion */
80 #define MAXSTRINGSIZE 1000
82 /******************** systemclasses required for native methods ***************/
84 classinfo *class_java_lang_Class;
85 classinfo *class_java_lang_VMClass;
86 methodinfo *method_vmclass_init;
87 /* static classinfo *class_java_lang_Cloneable=0; */ /* now in global.h */
88 classinfo *class_java_lang_CloneNotSupportedException;
89 classinfo *class_java_lang_System;
90 classinfo *class_java_lang_ClassLoader;
91 classinfo *class_java_lang_NoClassDefFoundError;
92 classinfo *class_java_lang_ClassNotFoundException;
93 classinfo *class_java_lang_LinkageError;
94 classinfo *class_java_lang_InstantiationException;
95 classinfo *class_java_lang_NoSuchMethodError;
96 classinfo *class_java_lang_NoSuchFieldError;
97 classinfo *class_java_lang_ClassFormatError;
98 classinfo *class_java_lang_IllegalArgumentException;
99 classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
100 classinfo *class_java_lang_NoSuchFieldException;
101 classinfo *class_java_io_SyncFailedException;
102 classinfo *class_java_io_IOException;
103 classinfo *class_java_io_FileNotFoundException;
104 classinfo *class_java_io_UnixFileSystem;
105 classinfo *class_java_security_PrivilegedActionException;
106 classinfo *class_java_net_UnknownHostException;
107 classinfo *class_java_net_SocketException;
108 classinfo *class_java_lang_NoSuchMethodException;
109 classinfo *class_java_lang_Double;
110 classinfo *class_java_lang_Float;
111 classinfo *class_java_lang_Long;
112 classinfo *class_java_lang_Byte;
113 classinfo *class_java_lang_Short;
114 classinfo *class_java_lang_Boolean;
115 classinfo *class_java_lang_Void;
116 classinfo *class_java_lang_Character;
117 classinfo *class_java_lang_Integer;
119 /* the system classloader object */
120 struct java_lang_ClassLoader *SystemClassLoader = NULL;
122 /* for raising exceptions from native methods */
123 java_objectheader* exceptionptr = NULL;
125 /************* use classinfo structure as java.lang.Class object **************/
127 void use_class_as_object(classinfo *c)
132 if (!class_java_lang_Class)
133 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
135 vt = class_java_lang_Class->vftbl;
138 if (!c->classvftbl) {
139 c->classvftbl = true;
141 /* copy_vftbl(&newtbl, vt);
142 newtbl->class = c->header.vftbl->class;
143 newtbl->baseval = c->header.vftbl->baseval;
144 newtbl->diffval = c->header.vftbl->diffval;
145 c->header.vftbl = newtbl;*/
147 c->header.vftbl = class_java_lang_Class->vftbl;
149 if (!class_java_lang_VMClass) {
150 loader_load_sysclass(&class_java_lang_VMClass,
151 utf_new_char("java/lang/VMClass"));
153 method_vmclass_init =
154 class_findmethod(class_java_lang_VMClass,
155 utf_new_char("<init>"),
156 utf_new_char("(Lgnu/classpath/RawData;)V"));
158 if (method_vmclass_init == 0) {
159 class_showmethods(class_java_lang_VMClass);
160 panic("Needed class initializer for VMClass could not be found");
164 java_objectheader *vmo = builtin_new(class_java_lang_VMClass);
166 if (!vmo) panic("Error while creating instance of java/lang/VMClass");
167 asm_calljavafunction(method_vmclass_init, vmo, c, NULL, NULL);
168 c->vmClass = (java_lang_VMClass *) vmo;
169 /*log_text("VMCLASS has been attached");*/
175 /*************************** include native methods ***************************/
178 #include "nat/GdkGraphics.c"
179 #include "nat/GtkComponentPeer.c"
180 #include "nat/GdkPixbufDecoder.c"
181 #include "nat/GtkScrollPanePeer.c"
182 #include "nat/GtkFileDialogPeer.c"
183 #include "nat/GtkLabelPeer.c"
187 /************************** tables for methods ********************************/
192 /* table for locating native methods */
193 static struct nativeref {
201 #include "nativetable.hh"
206 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
208 /* table for fast string comparison */
209 static struct nativecompref {
215 } nativecomptable [NATIVETABLESIZE];
217 /* string comparsion table initialized */
218 static bool nativecompdone = false;
221 /******************************************************************************/
222 /******************************************************************************/
223 #include "natcalls.h"
225 /* string call comparison table initialized */
227 /******************************************************************************/
228 /******************************************************************************/
230 /*--------------- native method calls & classes used -------------------------*/
234 /* throw some loader exceptions */
236 void throw_noclassdeffounderror_message(utf* classname)
238 if (!class_java_lang_NoClassDefFoundError) {
239 panic("java.lang.NoClassDefFoundError not found. Maybe wrong classpath?");
242 /* throws a NoClassDefFoundError with message */
243 exceptionptr = native_new_and_init_string(class_java_lang_NoClassDefFoundError,
244 javastring_new(classname));
248 void throw_linkageerror_message(utf* classname)
250 if (!class_java_lang_LinkageError) {
251 panic("java.lang.LinkageError not found. Maybe wrong classpath?");
254 /* throws a LinkageError with message */
255 exceptionptr = native_new_and_init_string(class_java_lang_LinkageError,
256 javastring_new(classname));
260 /*********************** function: native_loadclasses **************************
262 load classes required for native methods
264 *******************************************************************************/
266 void native_loadclasses()
268 static int classesLoaded=0; /*temporary hack JoWenn*/
269 if (classesLoaded) return;
271 /* log_text("loadclasses entered");*/
274 /*class_java_lang_System =*/
275 (void) class_new(utf_new_char("java/lang/VMClass"));/*JoWenn*/
276 (void) class_new(utf_new_char("java/lang/Class"));/*JoWenn*/
278 /* class_new adds the class to the list of classes to be loaded */
279 if (!class_java_lang_Cloneable)
280 class_java_lang_Cloneable =
281 class_new(utf_new_char("java/lang/Cloneable"));
282 /* log_text("loadclasses: class_java_lang_Cloneable has been initialized");*/
283 class_java_lang_CloneNotSupportedException =
284 class_new(utf_new_char("java/lang/CloneNotSupportedException"));
285 if (!class_java_lang_Class)
286 class_java_lang_Class =
287 class_new(utf_new_char("java/lang/Class"));
288 class_java_io_IOException =
289 class_new(utf_new_char("java/io/IOException"));
290 class_java_io_FileNotFoundException =
291 class_new(utf_new_char("java/io/FileNotFoundException"));
292 class_java_lang_NoClassDefFoundError =
293 class_new(utf_new_char("java/lang/NoClassDefFoundError"));
294 class_java_lang_ClassNotFoundException =
295 class_new(utf_new_char("java/lang/ClassNotFoundException"));
296 class_java_lang_LinkageError =
297 class_new(utf_new_char("java/lang/LinkageError"));
298 class_java_lang_InstantiationException =
299 class_new(utf_new_char("java/lang/InstantiationException"));
300 class_java_lang_NoSuchMethodError =
301 class_new(utf_new_char("java/lang/NoSuchMethodError"));
302 class_java_lang_NoSuchFieldError =
303 class_new(utf_new_char("java/lang/NoSuchFieldError"));
304 class_java_lang_ClassFormatError =
305 class_new(utf_new_char("java/lang/ClassFormatError"));
306 class_java_io_SyncFailedException =
307 class_new(utf_new_char("java/io/SyncFailedException"));
309 /* log_text("native_loadclasses: class_new(\"java/lang/ClassLoader\")"); */
310 class_java_lang_ClassLoader =
311 class_new(utf_new_char("java/lang/ClassLoader"));
312 /* log_text("native_loadclasses: class_new(\"java/security/PrivilegedActionException\")"); */
313 class_java_security_PrivilegedActionException =
314 class_new(utf_new_char("java/security/PrivilegedActionException"));
316 loader_load_sysclass(&class_java_net_UnknownHostException,
317 utf_new_char("java/net/UnknownHostException"));
318 loader_load_sysclass(&class_java_net_SocketException,
319 utf_new_char("java/net/SocketException"));
321 class_java_lang_IllegalArgumentException =
322 class_new(utf_new_char("java/lang/IllegalArgumentException"));
323 class_java_lang_ArrayIndexOutOfBoundsException =
324 class_new(utf_new_char("java/lang/ArrayIndexOutOfBoundsException"));
325 class_java_lang_NoSuchFieldException =
326 class_new(utf_new_char("java/lang/NoSuchFieldException"));
327 class_java_lang_NoSuchMethodException =
328 class_new(utf_new_char("java/lang/NoSuchMethodException"));
330 /* load classes for wrapping primitive types */
331 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
332 class_init(class_java_lang_Double);
334 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
335 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
336 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
337 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
338 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
339 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
340 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
341 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
344 log_text("native_loadclasses finished");
348 /*************** adds a class to the vector of loaded classes ****************/
350 void systemclassloader_addclass(classinfo *c)
354 /* find method addClass of java.lang.ClassLoader */
355 m = class_resolvemethod(
356 class_java_lang_ClassLoader,
357 utf_new_char("addClass"),
358 utf_new_char("(Ljava/lang/Class;)")
361 if (!m) panic("warning: cannot initialize classloader");
363 /* prepare class to be passed as argument */
364 use_class_as_object (c);
366 /* call 'addClass' */
367 asm_calljavafunction(m,
368 (java_objectheader*) SystemClassLoader,
369 (java_objectheader*) c,
376 /*************** adds a library to the vector of loaded libraries *************/
378 void systemclassloader_addlibrary(java_objectheader *o)
380 log_text("systemclassloader_addlibrary");
384 /*****************************************************************************
386 create systemclassloader object and initialize instance fields
388 ******************************************************************************/
390 void init_systemclassloader()
392 if (!SystemClassLoader) {
393 native_loadclasses();
394 log_text("Initializing new system class loader");
395 /* create object and call initializer */
396 SystemClassLoader = (java_lang_ClassLoader*) native_new_and_init(class_java_lang_ClassLoader);
398 /* systemclassloader has no parent */
399 SystemClassLoader->parent = NULL;
400 SystemClassLoader->initialized = true;
402 log_text("leaving system class loader");
406 /********************* add loaded library name *******************************/
408 void systemclassloader_addlibname(java_objectheader *o)
413 m = class_resolvemethod(loader_load_sysclass(NULL, utf_new_char ("java/util/Vector")),
414 utf_new_char("addElement"),
415 utf_new_char("(Ljava/lang/Object;)V"));
417 if (!m) panic("cannot initialize classloader");
419 id = envTable.GetStaticFieldID(&env,
420 class_java_lang_ClassLoader,
421 "loadedLibraryNames",
422 "Ljava/util/Vector;");
424 if (!id) panic("can not access ClassLoader");
426 asm_calljavafunction(m,
427 envTable.GetStaticObjectField(&env, class_java_lang_ClassLoader, id),
434 /********************* function: native_setclasspath **************************/
436 void native_setclasspath(char *path)
438 /* set searchpath for classfiles */
443 /*********************** Function: native_findfunction *************************
445 Looks up a method (must have the same class name, method name, descriptor
446 and 'static'ness) and returns a function pointer to it.
447 Returns: function pointer or NULL (if there is no such method)
449 Remark: For faster operation, the names/descriptors are converted from C
450 strings to Unicode the first time this function is called.
452 *******************************************************************************/
454 functionptr native_findfunction(utf *cname, utf *mname,
455 utf *desc, bool isstatic)
458 /* entry of table for fast string comparison */
459 struct nativecompref *n;
460 /* for warning message if no function is found */
464 isstatic = isstatic ? true : false;
466 if (!nativecompdone) {
467 for (i = 0; i < NATIVETABLESIZE; i++) {
468 nativecomptable[i].classname =
469 utf_new_char(nativetable[i].classname);
470 nativecomptable[i].methodname =
471 utf_new_char(nativetable[i].methodname);
472 nativecomptable[i].descriptor =
473 utf_new_char(nativetable[i].descriptor);
474 nativecomptable[i].isstatic =
475 nativetable[i].isstatic;
476 nativecomptable[i].func =
479 nativecompdone = true;
484 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
486 buffer = MNEW(char, buffer_len);
488 strcpy(buffer, "searching matching function in native table:");
489 utf_sprint(buffer+strlen(buffer), mname);
490 strcpy(buffer+strlen(buffer), ": ");
491 utf_sprint(buffer+strlen(buffer), desc);
492 strcpy(buffer+strlen(buffer), " for class ");
493 utf_sprint(buffer+strlen(buffer), cname);
497 MFREE(buffer, char, buffer_len);
500 for (i = 0; i < NATIVETABLESIZE; i++) {
501 n = &(nativecomptable[i]);
503 if (cname == n->classname && mname == n->methodname &&
504 desc == n->descriptor && isstatic == n->isstatic)
508 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
512 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
514 buffer = MNEW(char, buffer_len);
516 strcpy(buffer, "comparing with:");
517 utf_sprint(buffer+strlen(buffer), n->methodname);
518 strcpy (buffer+strlen(buffer), ": ");
519 utf_sprint(buffer+strlen(buffer), n->descriptor);
520 strcpy(buffer+strlen(buffer), " for class ");
521 utf_sprint(buffer+strlen(buffer), n->classname);
525 MFREE(buffer, char, buffer_len);
532 /* no function was found, display warning */
535 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
537 buffer = MNEW(char, buffer_len);
539 strcpy(buffer, "warning: native function ");
540 utf_sprint(buffer + strlen(buffer), mname);
541 strcpy(buffer + strlen(buffer), ": ");
542 utf_sprint(buffer + strlen(buffer), desc);
543 strcpy(buffer + strlen(buffer), " not found in class ");
544 utf_sprint(buffer + strlen(buffer), cname);
548 MFREE(buffer, char, buffer_len);
552 /* keep compiler happy */
557 /********************** function: javastring_new *******************************
559 creates a new object of type java/lang/String with the text of
560 the specified utf8-string
562 return: pointer to the string or NULL if memory is exhausted.
564 *******************************************************************************/
566 /* java_objectheader *javastring_new(utf *u) */
567 java_lang_String *javastring_new(utf *u)
569 char *utf_ptr = u->text; /* current utf character in utf string */
570 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
571 java_lang_String *s; /* result-string */
575 /* log_text("javastring_new");*/
577 s = (java_lang_String*) builtin_new(class_java_lang_String);
578 a = builtin_newarray_char(utflength);
580 /* javastring or character-array could not be created */
584 /* decompress utf-string */
585 for (i = 0; i < utflength; i++)
586 a->data[i] = utf_nextu2(&utf_ptr);
588 /* set fields of the javastring-object */
591 s->count = utflength;
593 /* return (java_objectheader*) s; */
598 /********************** function: javastring_new_char **************************
600 creates a new java/lang/String object which contains the convertet
601 C-string passed via text.
603 return: the object pointer or NULL if memory is exhausted.
605 *******************************************************************************/
607 /* java_objectheader *javastring_new_char (char *text) */
608 java_lang_String *javastring_new_char (char *text)
611 s4 len = strlen(text); /* length of the string */
612 java_lang_String *s; /* result-string */
615 /*log_text("javastring_new_char");*/
616 s = (java_lang_String*) builtin_new(class_java_lang_String);
617 a = builtin_newarray_char(len);
619 /* javastring or character-array could not be created */
624 for (i = 0; i < len; i++)
625 a->data[i] = text[i];
627 /* set fields of the javastring-object */
632 /* return (java_objectheader*) s; */
637 /************************* function javastring_tochar **************************
639 converts a Java string into a C string.
641 return: pointer to C string
643 Caution: every call of this function overwrites the previous string !!!
645 *******************************************************************************/
647 static char stringbuffer[MAXSTRINGSIZE];
649 char *javastring_tochar (java_objectheader *so)
651 java_lang_String *s = (java_lang_String*) so;
655 log_text("javastring_tochar");
662 if (s->count > MAXSTRINGSIZE)
664 for (i = 0; i < s->count; i++)
665 stringbuffer[i] = a->data[s->offset+i];
666 stringbuffer[i] = '\0';
671 /****************** function class_findfield_approx ****************************
673 searches in 'classinfo'-structure for a field with the
676 *******************************************************************************/
678 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
681 for (i = 0; i < c->fieldscount; i++) {
682 /* compare field names */
683 if ((c->fields[i].name == name))
684 return &(c->fields[i]);
687 /* field was not found, raise exception */
688 exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
693 s4 class_findfield_index_approx (classinfo *c, utf *name)
696 for (i = 0; i < c->fieldscount; i++) {
697 /* compare field names */
698 if ((c->fields[i].name == name))
702 /* field was not found, raise exception */
703 exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
708 /********************** function: native_new_and_init *************************
710 Creates a new object on the heap and calls the initializer.
711 Returns the object pointer or NULL if memory is exhausted.
713 *******************************************************************************/
715 java_objectheader *native_new_and_init(classinfo *c)
718 java_objectheader *o;
720 /* if c==NULL it is probebly because loader_load failed */
721 if (!c) return exceptionptr;
723 o = builtin_new(c); /* create object */
726 printf("native_new_and_init ");
727 utf_display(c->name);
731 /* printf("o!=NULL\n"); */
732 /* find initializer */
734 m = class_findmethod(c, utf_new_char("<init>"), utf_new_char("()V"));
736 if (!m) { /* initializer not found */
738 char logtext[MAXLOGTEXT];
739 sprintf(logtext, "Warning: class has no instance-initializer: ");
740 utf_sprint(logtext + strlen(logtext), c->name);
746 /* call initializer */
748 asm_calljavafunction(m, o, NULL, NULL, NULL);
754 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
757 java_objectheader *o;
759 /* if c==NULL it is probebly because loader_load failed */
760 if (!c) return exceptionptr;
762 o = builtin_new(c); /* create object */
766 /* find initializer */
768 m = class_findmethod(c,
769 utf_new_char("<init>"),
770 utf_new_char("(Ljava/lang/String;)V"));
772 if (!m) { /* initializer not found */
774 char logtext[MAXLOGTEXT];
775 sprintf(logtext, "Warning: class has no instance-initializer: ");
776 utf_sprint(logtext + strlen(logtext), c->name);
782 /* call initializer */
784 asm_calljavafunction(m, o, s, NULL, NULL);
790 /******************** function: stringtable_update ****************************
792 traverses the javastring hashtable and sets the vftbl-entries of
793 javastrings which were temporarily set to NULL, because
794 java.lang.Object was not yet loaded
796 *******************************************************************************/
798 void stringtable_update ()
800 java_lang_String *js;
802 literalstring *s; /* hashtable entry */
805 for (i = 0; i < string_hash.size; i++) {
806 s = string_hash.ptr[i];
810 js = (java_lang_String *) s->string;
812 if (!js || !js->value)
813 /* error in hashtable found */
814 panic("invalid literalstring in hashtable");
818 if (!js->header.vftbl)
819 /* vftbl of javastring is NULL */
820 js->header.vftbl = class_java_lang_String -> vftbl;
822 if (!a->header.objheader.vftbl)
823 /* vftbl of character-array is NULL */
824 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
826 /* follow link in external hash chain */
834 /************************* function: u2_utflength ***************************
836 returns the utf length in bytes of a u2 array
838 *****************************************************************************/
840 u4 u2_utflength(u2 *text, u4 u2_length)
842 u4 result_len = 0; /* utf length in bytes */
843 u2 ch; /* current unicode character */
846 for (len = 0; len < u2_length; len++) {
847 /* next unicode character */
850 /* determine bytes required to store unicode character as utf */
851 if (ch && (ch < 0x80))
863 /********************* function: utf_new_u2 ***********************************
865 make utf symbol from u2 array,
866 if isclassname is true '.' is replaced by '/'
868 *******************************************************************************/
870 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
872 char *buffer; /* memory buffer for unicode characters */
873 char *pos; /* pointer to current position in buffer */
874 u4 left; /* unicode characters left */
875 u4 buflength; /* utf length in bytes of the u2 array */
876 utf *result; /* resulting utf-string */
879 /* determine utf length in bytes and allocate memory */
880 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
881 buflength = u2_utflength(unicode_pos, unicode_length);
882 buffer = MNEW(char, buflength);
884 /* memory allocation failed */
886 printf("length: %d\n",buflength);
887 log_text("utf_new_u2:buffer==NULL");
893 for (i = 0; i++ < unicode_length; unicode_pos++) {
894 /* next unicode character */
897 if ((c != 0) && (c < 0x80)) {
900 if ((int) left < 0) break;
901 /* convert classname */
902 if (isclassname && c == '.')
907 } else if (c < 0x800) {
909 unsigned char high = c >> 6;
910 unsigned char low = c & 0x3F;
912 if ((int) left < 0) break;
913 *pos++ = high | 0xC0;
919 char mid = (c >> 6) & 0x3F;
922 if ((int) left < 0) break;
923 *pos++ = high | 0xE0;
929 /* insert utf-string into symbol-table */
930 result = utf_new(buffer,buflength);
932 MFREE(buffer, char, buflength);
938 /********************* function: javastring_toutf *****************************
940 make utf symbol from javastring
942 *******************************************************************************/
944 utf *javastring_toutf(java_lang_String *string, bool isclassname)
946 java_lang_String *str = (java_lang_String *) string;
947 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count);
949 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
953 /********************* function: literalstring_u2 *****************************
955 searches for the javastring with the specified u2-array in
956 the string hashtable, if there is no such string a new one is
959 if copymode is true a copy of the u2-array is made
961 *******************************************************************************/
963 java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode )
965 literalstring *s; /* hashtable element */
966 java_lang_String *js; /* u2-array wrapped in javastring */
967 java_chararray *stringdata; /* copy of u2-array */
973 printf("literalstring_u2: length: %d\n",length);
974 log_text("literalstring_u2");
977 /* find location in hashtable */
978 key = unicode_hashkey (a->data, length);
979 slot = key & (string_hash.size-1);
980 s = string_hash.ptr[slot];
984 js = (java_lang_String *) s->string;
986 if (js->count == length) {
988 for (i=0; i<length; i++)
989 if (js->value->data[i] != a->data[i]) goto nomatch;
991 /* string already in hashtable, free memory */
993 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
996 log_text("literalstring_u2: foundentry");
997 utf_display(javastring_toutf(js,0));
999 return (java_objectheader *) js;
1003 /* follow link in external hash chain */
1008 /* create copy of u2-array for new javastring */
1009 u4 arraysize = sizeof(java_chararray) + sizeof(u2)*(length-1)+10;
1010 stringdata = lit_mem_alloc ( arraysize );
1011 memcpy(stringdata, a, arraysize );
1016 /* location in hashtable found, complete arrayheader */
1017 stringdata -> header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1018 stringdata -> header.size = length;
1020 /* create new javastring */
1021 js = LNEW (java_lang_String);
1022 js -> header.vftbl = class_java_lang_String -> vftbl;
1023 js -> value = stringdata;
1025 js -> count = length;
1027 /* create new literalstring */
1028 s = NEW (literalstring);
1029 s->hashlink = string_hash.ptr[slot];
1030 s->string = (java_objectheader *) js;
1031 string_hash.ptr[slot] = s;
1033 /* update numbe of hashtable entries */
1034 string_hash.entries++;
1036 /* reorganization of hashtable */
1037 if ( string_hash.entries > (string_hash.size*2)) {
1039 /* reorganization of hashtable, average length of
1040 the external chains is approx. 2 */
1044 hashtable newhash; /* the new hashtable */
1046 /* create new hashtable, double the size */
1047 init_hashtable(&newhash, string_hash.size*2);
1048 newhash.entries=string_hash.entries;
1050 /* transfer elements to new hashtable */
1051 for (i=0; i<string_hash.size; i++) {
1052 s = string_hash.ptr[i];
1054 literalstring *nexts = s -> hashlink;
1055 js = (java_lang_String*) s->string;
1056 slot = (unicode_hashkey(js->value->data,js->count)) & (newhash.size-1);
1058 s->hashlink = newhash.ptr[slot];
1059 newhash.ptr[slot] = s;
1061 /* follow link in external hash chain */
1066 /* dispose old table */
1067 MFREE (string_hash.ptr, void*, string_hash.size);
1068 string_hash = newhash;
1070 #ifdef JOWENN_DEBUG1
1071 log_text("literalstring_u2: newly created");
1072 /* utf_display(javastring_toutf(js,0));*/
1075 return (java_objectheader *) js;
1078 /******************** Function: literalstring_new *****************************
1080 creates a new javastring with the text of the utf-symbol
1081 and inserts it into the string hashtable
1083 *******************************************************************************/
1085 java_objectheader *literalstring_new (utf *u)
1087 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1088 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1089 java_chararray *a; /* u2-array constructed from utf string */
1091 /* log_text("literalstring_new"); */
1092 /* utf_display(u);*/
1093 /*if (utflength==0) while (1) sleep(60);*/
1094 /* log_text("------------------"); */
1095 /* allocate memory */
1096 a = lit_mem_alloc (sizeof(java_chararray) + sizeof(u2)*(utflength-1)+10 );
1097 /* convert utf-string to u2-array */
1098 for (i=0; i<utflength; i++) a->data[i] = utf_nextu2(&utf_ptr);
1100 return literalstring_u2(a, utflength, false);
1104 /********************** function: literalstring_free **************************
1106 removes a javastring from memory
1108 ******************************************************************************/
1110 void literalstring_free (java_objectheader* sobj)
1112 java_lang_String *s = (java_lang_String*) sobj;
1113 java_chararray *a = s->value;
1115 log_text("literalstring_free called");
1117 /* dispose memory of java.lang.String object */
1118 LFREE (s, java_lang_String);
1119 /* dispose memory of java-characterarray */
1120 LFREE (a, sizeof(java_chararray) + sizeof(u2)*(a->header.size-1)); /* +10 ?? */
1126 void copy_vftbl(vftbl **dest, vftbl *src)
1130 /* XXX this kind of copying does not work (in the general
1131 * case). The interface tables would have to be copied, too. I
1132 * don't see why we should make a copy anyway. -Edwin
1134 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1135 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1136 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1141 /******************************************************************************************
1143 creates method signature (excluding return type) from array of
1144 class-objects representing the parameters of the method
1146 *******************************************************************************************/
1149 utf *create_methodsig(java_objectarray* types, char *retType)
1151 char *buffer; /* buffer for building the desciptor */
1152 char *pos; /* current position in buffer */
1153 utf *result; /* the method signature */
1154 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1157 if (!types) return NULL;
1159 /* determine required buffer-size */
1160 for (i = 0; i < types->header.size; i++) {
1161 classinfo *c = (classinfo *) types->data[i];
1162 buffer_size = buffer_size + c->name->blength + 2;
1165 if (retType) buffer_size += strlen(retType);
1167 /* allocate buffer */
1168 buffer = MNEW(u1, buffer_size);
1171 /* method-desciptor starts with parenthesis */
1174 for (i = 0; i < types->header.size; i++) {
1177 /* current argument */
1178 classinfo *c = (classinfo *) types->data[i];
1180 /* current position in utf-text */
1181 char *utf_ptr = c->name->text;
1183 /* determine type of argument */
1184 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1186 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1187 *pos++ = *utf_ptr; /* copy text */
1191 /* check for primitive types */
1192 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1193 char *utf_pos = utf_ptr - 1;
1194 char *primitive = primitivetype_table[j].wrapname;
1197 while (utf_pos < utf_end(c->name)) {
1198 if (*utf_pos++ != *primitive++) goto nomatch;
1201 /* primitive type found */
1202 *pos++ = primitivetype_table[j].typesig;
1208 /* no primitive type and no arrayclass, so must be object */
1212 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1225 for (i = 0; i < strlen(retType); i++) {
1226 *pos++ = retType[i];
1230 /* create utf-string */
1231 result = utf_new(buffer, (pos - buffer));
1232 MFREE(buffer, u1, buffer_size);
1238 /******************************************************************************************
1240 retrieve the next argument or returntype from a descriptor
1241 and return the corresponding class
1243 *******************************************************************************************/
1245 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1247 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1248 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1251 panic("illegal descriptor");
1253 if (skip) return NULL;
1255 use_class_as_object(c);
1260 /******************************************************************************************
1262 use the descriptor of a method to generate a java/lang/Class array
1263 which contains the classes of the parametertypes of the method
1265 *******************************************************************************************/
1267 java_objectarray* get_parametertypes(methodinfo *m)
1269 utf *descr = m->descriptor; /* method-descriptor */
1270 char *utf_ptr = descr->text; /* current position in utf-text */
1271 char *desc_end = utf_end(descr); /* points behind utf string */
1272 java_objectarray* result;
1273 int parametercount = 0;
1277 utf_nextu2(&utf_ptr);
1279 /* determine number of parameters */
1280 while ( *utf_ptr != ')' ) {
1281 get_type(&utf_ptr,desc_end,true);
1285 /* create class-array */
1286 result = builtin_anewarray(parametercount, class_java_lang_Class);
1288 utf_ptr = descr->text;
1289 utf_nextu2(&utf_ptr);
1291 /* get returntype classes */
1292 for (i = 0; i < parametercount; i++)
1293 result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1299 /******************************************************************************************
1301 get the returntype class of a method
1303 *******************************************************************************************/
1305 classinfo *get_returntype(methodinfo *m)
1307 char *utf_ptr; /* current position in utf-text */
1308 char *desc_end; /* points behind utf string */
1309 utf *desc = m->descriptor; /* method-descriptor */
1311 utf_ptr = desc->text;
1312 desc_end = utf_end(desc);
1314 /* ignore parametertypes */
1315 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1318 return get_type(&utf_ptr,desc_end, false);
1322 /*****************************************************************************/
1323 /*****************************************************************************/
1326 /*--------------------------------------------------------*/
1327 void printNativeCall(nativeCall nc) {
1330 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1331 for (i=0; i<nc.methCnt; i++) {
1332 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1334 for (j=0; j<nc.callCnt[i]; j++) {
1335 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1336 nc.methods[i].methodCalls[j].classname,
1337 nc.methods[i].methodCalls[j].methodname,
1338 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1341 printf("-+++++--------------------\n");fflush(stdout);
1344 /*--------------------------------------------------------*/
1345 void printCompNativeCall(nativeCompCall nc) {
1347 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1348 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1349 utf_display(nc.classname); fflush(stdout);
1351 for (i=0; i<nc.methCnt; i++) {
1352 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1353 utf_display(nc.methods[i].methodname); fflush(stdout);
1354 utf_display(nc.methods[i].descriptor);fflush(stdout);
1355 printf("\n");fflush(stdout);
1357 for (j=0; j<nc.callCnt[i]; j++) {
1358 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1359 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1360 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1361 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1362 printf("\n");fflush(stdout);
1365 printf("---------------------\n");fflush(stdout);
1369 /*--------------------------------------------------------*/
1370 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1383 /*--------------------------------------------------------*/
1384 nativeCall* findNativeClassCalls(char *aclassname ) {
1387 for (i=0;i<NATIVECALLSSIZE; i++) {
1388 /* convert table to utf later to speed up search */
1389 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1390 return &nativeCalls[i];
1395 /*--------------------------------------------------------*/
1396 /*--------------------------------------------------------*/
1397 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1401 ncc->classname = utf_new_char(nc.classname);
1402 ncc->methCnt = nc.methCnt;
1404 for (i=0; i<nc.methCnt; i++) {
1405 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1406 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1407 ncc->callCnt[i] = nc.callCnt[i];
1409 for (j=0; j<nc.callCnt[i]; j++) {
1411 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1413 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1414 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1415 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1418 ncc->methods[i].methodCalls[j].methodname = NULL;
1419 ncc->methods[i].methodCalls[j].descriptor = NULL;
1427 /*--------------------------------------------------------*/
1429 bool natcall2utf(bool natcallcompdone) {
1432 if (natcallcompdone)
1435 for (i=0;i<NATIVECALLSSIZE; i++) {
1436 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1441 /*--------------------------------------------------------*/
1445 * These are local overrides for various environment variables in Emacs.
1446 * Please do not remove this and leave it at the end of the file, where
1447 * Emacs will automagically detect them.
1448 * ---------------------------------------------------------------------
1451 * indent-tabs-mode: t