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 1464 2004-11-06 22:55:46Z motse $
49 #include "exceptions.h"
60 #include "toolbox/logging.h"
61 #include "toolbox/memory.h"
62 #include "threads/thread.h"
63 #include "threads/threadio.h"
64 #include "threads/locks.h"
65 #include "nat/java_lang_VMClass.h"
66 #include "nat/java_lang_Throwable.h"
68 /* Include files for IO functions */
72 #include <sys/types.h>
78 #include "threads/threadio.h"
80 /* include table of native functions */
82 #include "nativetable.inc"
85 /* for java-string to char conversion */
86 #define MAXSTRINGSIZE 1000
89 /******************** systemclasses required for native methods ***************/
91 classinfo *class_java_lang_Class;
92 classinfo *class_java_lang_VMClass;
93 classinfo *class_java_lang_System;
94 classinfo *class_java_lang_ClassLoader;
95 classinfo *class_gnu_java_lang_SystemClassLoader;
96 classinfo *class_java_lang_SecurityManager;
97 classinfo *class_java_lang_Double;
98 classinfo *class_java_lang_Float;
99 classinfo *class_java_lang_Long;
100 classinfo *class_java_lang_Byte;
101 classinfo *class_java_lang_Short;
102 classinfo *class_java_lang_Boolean;
103 classinfo *class_java_lang_Void;
104 classinfo *class_java_lang_Character;
105 classinfo *class_java_lang_Integer;
107 methodinfo *method_vmclass_init;
110 /* the system classloader object */
111 struct java_lang_ClassLoader *SystemClassLoader = NULL;
113 /* for raising exceptions from native methods */
114 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
115 java_objectheader* _exceptionptr = NULL;
118 /************* use classinfo structure as java.lang.Class object **************/
120 void use_class_as_object(classinfo *c)
122 if (!c->classvftbl) {
123 c->header.vftbl = class_java_lang_Class->vftbl;
124 c->classvftbl = true;
130 /*************************** include native methods ***************************/
133 #include "nat/GtkComponentPeer.c"
134 #include "nat/GtkScrollPanePeer.c"
135 #include "nat/GtkFileDialogPeer.c"
139 /************************** tables for methods ********************************/
144 #ifdef STATIC_CLASSPATH
145 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
147 /* table for fast string comparison */
148 static nativecompref nativecomptable[NATIVETABLESIZE];
150 /* string comparsion table initialized */
151 static bool nativecompdone = false;
154 /******************************************************************************/
156 #include "natcalls.h"
159 /*********************** function: native_loadclasses **************************
161 load classes required for native methods
163 *******************************************************************************/
165 void native_loadclasses()
167 static int classesLoaded = 0; /*temporary hack JoWenn*/
174 class_java_lang_Cloneable =
175 class_new(utf_new_char("java/lang/Cloneable"));
176 class_load(class_java_lang_Cloneable);
177 class_link(class_java_lang_Cloneable);
179 class_java_lang_Class =
180 class_new(utf_new_char("java/lang/Class"));
181 class_load(class_java_lang_Class);
182 class_link(class_java_lang_Class);
184 class_java_lang_VMClass =
185 class_new(utf_new_char("java/lang/VMClass"));
186 class_load(class_java_lang_VMClass);
187 class_link(class_java_lang_VMClass);
189 class_java_lang_ClassLoader =
190 class_new(utf_new_char("java/lang/ClassLoader"));
191 class_load(class_java_lang_ClassLoader);
192 class_link(class_java_lang_ClassLoader);
194 /* load classes for wrapping primitive types */
195 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
196 class_load(class_java_lang_Double);
197 class_link(class_java_lang_Double);
199 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
200 class_load(class_java_lang_Float);
201 class_link(class_java_lang_Float);
203 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
204 class_load(class_java_lang_Character);
205 class_link(class_java_lang_Character);
207 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
208 class_load(class_java_lang_Integer);
209 class_link(class_java_lang_Integer);
211 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
212 class_load(class_java_lang_Long);
213 class_link(class_java_lang_Long);
215 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
216 class_load(class_java_lang_Byte);
217 class_link(class_java_lang_Byte);
219 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
220 class_load(class_java_lang_Short);
221 class_link(class_java_lang_Short);
223 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
224 class_load(class_java_lang_Boolean);
225 class_link(class_java_lang_Boolean);
227 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
228 class_load(class_java_lang_Void);
229 class_link(class_java_lang_Void);
234 /*****************************************************************************
236 create systemclassloader object and initialize instance fields
238 ******************************************************************************/
240 void init_systemclassloader()
242 log_text("init_systemclassloader");
243 if (!SystemClassLoader) {
244 native_loadclasses();
245 log_text("Initializing new system class loader");
246 /* create object and call initializer */
247 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
249 /* systemclassloader has no parent */
250 SystemClassLoader->parent = NULL;
251 SystemClassLoader->initialized = true;
253 log_text("leaving system class loader");
257 /*********************** Function: native_findfunction *************************
259 Looks up a method (must have the same class name, method name, descriptor
260 and 'static'ness) and returns a function pointer to it.
261 Returns: function pointer or NULL (if there is no such method)
263 Remark: For faster operation, the names/descriptors are converted from C
264 strings to Unicode the first time this function is called.
266 *******************************************************************************/
268 functionptr native_findfunction(utf *cname, utf *mname,
269 utf *desc, bool isstatic)
271 #ifdef STATIC_CLASSPATH
273 /* entry of table for fast string comparison */
274 struct nativecompref *n;
275 /* for warning message if no function is found */
279 isstatic = isstatic ? true : false;
281 if (!nativecompdone) {
282 for (i = 0; i < NATIVETABLESIZE; i++) {
283 nativecomptable[i].classname =
284 utf_new_char(nativetable[i].classname);
285 nativecomptable[i].methodname =
286 utf_new_char(nativetable[i].methodname);
287 nativecomptable[i].descriptor =
288 utf_new_char(nativetable[i].descriptor);
289 nativecomptable[i].isstatic =
290 nativetable[i].isstatic;
291 nativecomptable[i].func =
294 nativecompdone = true;
299 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
301 buffer = MNEW(char, buffer_len);
303 strcpy(buffer, "searching matching function in native table:");
304 utf_sprint(buffer+strlen(buffer), mname);
305 strcpy(buffer+strlen(buffer), ": ");
306 utf_sprint(buffer+strlen(buffer), desc);
307 strcpy(buffer+strlen(buffer), " for class ");
308 utf_sprint(buffer+strlen(buffer), cname);
312 MFREE(buffer, char, buffer_len);
315 for (i = 0; i < NATIVETABLESIZE; i++) {
316 n = &(nativecomptable[i]);
318 if (cname == n->classname && mname == n->methodname &&
319 desc == n->descriptor && isstatic == n->isstatic)
323 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
327 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
329 buffer = MNEW(char, buffer_len);
331 strcpy(buffer, "comparing with:");
332 utf_sprint(buffer+strlen(buffer), n->methodname);
333 strcpy (buffer+strlen(buffer), ": ");
334 utf_sprint(buffer+strlen(buffer), n->descriptor);
335 strcpy(buffer+strlen(buffer), " for class ");
336 utf_sprint(buffer+strlen(buffer), n->classname);
340 MFREE(buffer, char, buffer_len);
347 /* no function was found, display warning */
350 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
352 buffer = MNEW(char, buffer_len);
354 strcpy(buffer, "warning: native function ");
355 utf_sprint(buffer + strlen(buffer), mname);
356 strcpy(buffer + strlen(buffer), ": ");
357 utf_sprint(buffer + strlen(buffer), desc);
358 strcpy(buffer + strlen(buffer), " not found in class ");
359 utf_sprint(buffer + strlen(buffer), cname);
363 MFREE(buffer, char, buffer_len);
367 /* keep compiler happy */
370 /* dynamic classpath */
376 /********************** function: javastring_new *******************************
378 creates a new object of type java/lang/String with the text of
379 the specified utf8-string
381 return: pointer to the string or NULL if memory is exhausted.
383 *******************************************************************************/
385 java_lang_String *javastring_new(utf *u)
387 char *utf_ptr; /* current utf character in utf string */
388 u4 utflength; /* length of utf-string if uncompressed */
389 java_lang_String *s; /* result-string */
394 *exceptionptr = new_nullpointerexception();
399 utflength = utf_strlen(u);
401 s = (java_lang_String *) builtin_new(class_java_lang_String);
402 a = builtin_newarray_char(utflength);
404 /* javastring or character-array could not be created */
408 /* decompress utf-string */
409 for (i = 0; i < utflength; i++)
410 a->data[i] = utf_nextu2(&utf_ptr);
412 /* set fields of the javastring-object */
415 s->count = utflength;
421 /********************** function: javastring_new_char **************************
423 creates a new java/lang/String object which contains the convertet
424 C-string passed via text.
426 return: the object pointer or NULL if memory is exhausted.
428 *******************************************************************************/
430 java_lang_String *javastring_new_char(char *text)
433 s4 len; /* length of the string */
434 java_lang_String *s; /* result-string */
438 *exceptionptr = new_nullpointerexception();
444 s = (java_lang_String *) builtin_new(class_java_lang_String);
445 a = builtin_newarray_char(len);
447 /* javastring or character-array could not be created */
452 for (i = 0; i < len; i++)
453 a->data[i] = text[i];
455 /* set fields of the javastring-object */
464 /************************* function javastring_tochar **************************
466 converts a Java string into a C string.
468 return: pointer to C string
470 Caution: every call of this function overwrites the previous string !!!
472 *******************************************************************************/
474 static char stringbuffer[MAXSTRINGSIZE];
476 char *javastring_tochar(java_objectheader *so)
478 java_lang_String *s = (java_lang_String *) so;
490 if (s->count > MAXSTRINGSIZE)
493 for (i = 0; i < s->count; i++)
494 stringbuffer[i] = a->data[s->offset + i];
496 stringbuffer[i] = '\0';
502 /****************** function class_findfield_approx ****************************
504 searches in 'classinfo'-structure for a field with the
507 *******************************************************************************/
509 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
513 for (i = 0; i < c->fieldscount; i++) {
514 /* compare field names */
515 if ((c->fields[i].name == name))
516 return &(c->fields[i]);
519 /* field was not found, raise exception */
520 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
526 s4 class_findfield_index_approx(classinfo *c, utf *name)
530 for (i = 0; i < c->fieldscount; i++) {
531 /* compare field names */
532 if ((c->fields[i].name == name))
536 /* field was not found, raise exception */
537 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
543 /********************** function: native_new_and_init *************************
545 Creates a new object on the heap and calls the initializer.
546 Returns the object pointer or NULL if memory is exhausted.
548 *******************************************************************************/
550 java_objectheader *native_new_and_init(classinfo *c)
553 java_objectheader *o;
556 return *exceptionptr;
558 o = builtin_new(c); /* create object */
563 /* find initializer */
565 m = class_findmethod(c,
566 utf_new_char("<init>"),
567 utf_new_char("()V"));
569 if (!m) { /* initializer not found */
571 char logtext[MAXLOGTEXT];
572 sprintf(logtext, "Warning: class has no instance-initializer: ");
573 utf_sprint_classname(logtext + strlen(logtext), c->name);
579 /* call initializer */
581 asm_calljavafunction(m, o, NULL, NULL, NULL);
587 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
590 java_objectheader *o;
593 return *exceptionptr;
595 o = builtin_new(c); /* create object */
600 /* find initializer */
602 m = class_findmethod(c,
603 utf_new_char("<init>"),
604 utf_new_char("(Ljava/lang/String;)V"));
606 if (!m) { /* initializer not found */
608 char logtext[MAXLOGTEXT];
609 sprintf(logtext, "Warning: class has no instance-initializer: ");
610 utf_sprint_classname(logtext + strlen(logtext), c->name);
616 /* call initializer */
618 asm_calljavafunction(m, o, s, NULL, NULL);
624 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
627 java_objectheader *o;
630 return *exceptionptr;
632 o = builtin_new(c); /* create object */
636 /* find initializer */
638 m = class_findmethod(c,
639 utf_new_char("<init>"),
640 utf_new_char("(I)V"));
642 if (!m) { /* initializer not found */
644 char logtext[MAXLOGTEXT];
645 sprintf(logtext, "Warning: class has no instance-initializer: ");
646 utf_sprint_classname(logtext + strlen(logtext), c->name);
652 /* call initializer */
654 #if defined(__I386__) || defined(__POWERPC__)
655 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
657 asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
664 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
667 java_objectheader *o;
670 return *exceptionptr;
672 o = builtin_new(c); /* create object */
676 /* find initializer */
678 m = class_findmethod(c,
679 utf_new_char("<init>"),
680 utf_new_char("(Ljava/lang/Throwable;)V"));
682 if (!m) { /* initializer not found */
684 char logtext[MAXLOGTEXT];
685 sprintf(logtext, "Warning: class has no instance-initializer: ");
686 utf_sprint_classname(logtext + strlen(logtext), c->name);
692 /* call initializer */
694 asm_calljavafunction(m, o, t, NULL, NULL);
700 /******************** function: stringtable_update ****************************
702 traverses the javastring hashtable and sets the vftbl-entries of
703 javastrings which were temporarily set to NULL, because
704 java.lang.Object was not yet loaded
706 *******************************************************************************/
708 void stringtable_update ()
710 java_lang_String *js;
712 literalstring *s; /* hashtable entry */
715 for (i = 0; i < string_hash.size; i++) {
716 s = string_hash.ptr[i];
720 js = (java_lang_String *) s->string;
722 if (!js || !js->value)
723 /* error in hashtable found */
724 panic("invalid literalstring in hashtable");
728 if (!js->header.vftbl)
729 /* vftbl of javastring is NULL */
730 js->header.vftbl = class_java_lang_String->vftbl;
732 if (!a->header.objheader.vftbl)
733 /* vftbl of character-array is NULL */
734 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
736 /* follow link in external hash chain */
744 /************************* function: u2_utflength ***************************
746 returns the utf length in bytes of a u2 array
748 *****************************************************************************/
750 u4 u2_utflength(u2 *text, u4 u2_length)
752 u4 result_len = 0; /* utf length in bytes */
753 u2 ch; /* current unicode character */
756 for (len = 0; len < u2_length; len++) {
757 /* next unicode character */
760 /* determine bytes required to store unicode character as utf */
761 if (ch && (ch < 0x80))
773 /********************* function: utf_new_u2 ***********************************
775 make utf symbol from u2 array,
776 if isclassname is true '.' is replaced by '/'
778 *******************************************************************************/
780 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
782 char *buffer; /* memory buffer for unicode characters */
783 char *pos; /* pointer to current position in buffer */
784 u4 left; /* unicode characters left */
785 u4 buflength; /* utf length in bytes of the u2 array */
786 utf *result; /* resulting utf-string */
789 /* determine utf length in bytes and allocate memory */
790 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
791 buflength = u2_utflength(unicode_pos, unicode_length);
792 buffer = MNEW(char, buflength);
797 for (i = 0; i++ < unicode_length; unicode_pos++) {
798 /* next unicode character */
801 if ((c != 0) && (c < 0x80)) {
804 if ((int) left < 0) break;
805 /* convert classname */
806 if (isclassname && c == '.')
811 } else if (c < 0x800) {
813 unsigned char high = c >> 6;
814 unsigned char low = c & 0x3F;
816 if ((int) left < 0) break;
817 *pos++ = high | 0xC0;
823 char mid = (c >> 6) & 0x3F;
826 if ((int) left < 0) break;
827 *pos++ = high | 0xE0;
833 /* insert utf-string into symbol-table */
834 result = utf_new(buffer,buflength);
836 MFREE(buffer, char, buflength);
842 /********************* function: javastring_toutf *****************************
844 make utf symbol from javastring
846 *******************************************************************************/
848 utf *javastring_toutf(java_lang_String *string, bool isclassname)
850 java_lang_String *str = (java_lang_String *) string;
852 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
853 /* fflush(stdout); */
855 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
859 /********************* function: literalstring_u2 *****************************
861 searches for the javastring with the specified u2-array in
862 the string hashtable, if there is no such string a new one is
865 if copymode is true a copy of the u2-array is made
867 *******************************************************************************/
869 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
872 literalstring *s; /* hashtable element */
873 java_lang_String *js; /* u2-array wrapped in javastring */
874 java_chararray *stringdata; /* copy of u2-array */
880 //#define DEBUG_LITERALSTRING_U2
881 #ifdef DEBUG_LITERALSTRING_U2
882 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
886 /* find location in hashtable */
887 key = unicode_hashkey(a->data + offset, length);
888 slot = key & (string_hash.size - 1);
889 s = string_hash.ptr[slot];
892 js = (java_lang_String *) s->string;
894 if (length == js->count) {
896 for (i = 0; i < length; i++) {
897 if (a->data[offset + i] != js->value->data[i])
901 /* string already in hashtable, free memory */
903 mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
905 #ifdef DEBUG_LITERALSTRING_U2
906 printf("literalstring_u2: foundentry at %p\n", js);
907 utf_display(javastring_toutf(js, 0));
911 return (java_objectheader *) js;
915 /* follow link in external hash chain */
920 /* create copy of u2-array for new javastring */
921 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
922 stringdata = mem_alloc(arraysize);
923 /* memcpy(stringdata, a, arraysize); */
924 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
925 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
931 /* location in hashtable found, complete arrayheader */
932 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
933 stringdata->header.size = length;
935 /* if we use eager loading, we have to check loaded String class */
937 class_java_lang_String =
938 class_new_intern(utf_new_char("java/lang/String"));
940 if (!class_load(class_java_lang_String))
943 list_addfirst(&unlinkedclasses, class_java_lang_String);
946 /* create new javastring */
947 js = NEW(java_lang_String);
948 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
949 initObjectLock(&js->header);
951 js->header.vftbl = class_java_lang_String->vftbl;
952 js->value = stringdata;
956 #ifdef DEBUG_LITERALSTRING_U2
957 printf("literalstring_u2: newly created at %p\n", js);
958 utf_display(javastring_toutf(js, 0));
963 /* create new literalstring */
964 s = NEW(literalstring);
965 s->hashlink = string_hash.ptr[slot];
966 s->string = (java_objectheader *) js;
967 string_hash.ptr[slot] = s;
969 /* update number of hashtable entries */
970 string_hash.entries++;
972 /* reorganization of hashtable */
973 if (string_hash.entries > (string_hash.size * 2)) {
974 /* reorganization of hashtable, average length of
975 the external chains is approx. 2 */
979 hashtable newhash; /* the new hashtable */
981 /* create new hashtable, double the size */
982 init_hashtable(&newhash, string_hash.size * 2);
983 newhash.entries = string_hash.entries;
985 /* transfer elements to new hashtable */
986 for (i = 0; i < string_hash.size; i++) {
987 s = string_hash.ptr[i];
989 literalstring *nexts = s->hashlink;
990 js = (java_lang_String *) s->string;
991 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
993 s->hashlink = newhash.ptr[slot];
994 newhash.ptr[slot] = s;
996 /* follow link in external hash chain */
1001 /* dispose old table */
1002 MFREE(string_hash.ptr, void*, string_hash.size);
1003 string_hash = newhash;
1006 return (java_objectheader *) js;
1010 /******************** Function: literalstring_new *****************************
1012 creates a new javastring with the text of the utf-symbol
1013 and inserts it into the string hashtable
1015 *******************************************************************************/
1017 java_objectheader *literalstring_new(utf *u)
1019 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1020 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1021 java_chararray *a; /* u2-array constructed from utf string */
1024 /* allocate memory */
1025 a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1027 /* convert utf-string to u2-array */
1028 for (i = 0; i < utflength; i++)
1029 a->data[i] = utf_nextu2(&utf_ptr);
1031 return literalstring_u2(a, utflength, 0, false);
1035 /********************** function: literalstring_free **************************
1037 removes a javastring from memory
1039 ******************************************************************************/
1041 void literalstring_free(java_objectheader* sobj)
1043 java_lang_String *s = (java_lang_String *) sobj;
1044 java_chararray *a = s->value;
1046 /* dispose memory of java.lang.String object */
1047 FREE(s, java_lang_String);
1049 /* dispose memory of java-characterarray */
1050 FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1054 void copy_vftbl(vftbl_t **dest, vftbl_t *src)
1058 /* XXX this kind of copying does not work (in the general
1059 * case). The interface tables would have to be copied, too. I
1060 * don't see why we should make a copy anyway. -Edwin
1062 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1063 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1064 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1069 /******************************************************************************************
1071 creates method signature (excluding return type) from array of
1072 class-objects representing the parameters of the method
1074 *******************************************************************************************/
1077 utf *create_methodsig(java_objectarray* types, char *retType)
1079 char *buffer; /* buffer for building the desciptor */
1080 char *pos; /* current position in buffer */
1081 utf *result; /* the method signature */
1082 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1085 if (!types) return NULL;
1087 /* determine required buffer-size */
1088 for (i = 0; i < types->header.size; i++) {
1089 classinfo *c = (classinfo *) types->data[i];
1090 buffer_size = buffer_size + c->name->blength + 2;
1093 if (retType) buffer_size += strlen(retType);
1095 /* allocate buffer */
1096 buffer = MNEW(u1, buffer_size);
1099 /* method-desciptor starts with parenthesis */
1102 for (i = 0; i < types->header.size; i++) {
1105 /* current argument */
1106 classinfo *c = (classinfo *) types->data[i];
1108 /* current position in utf-text */
1109 char *utf_ptr = c->name->text;
1111 /* determine type of argument */
1112 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1114 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1115 *pos++ = *utf_ptr; /* copy text */
1119 /* check for primitive types */
1120 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1121 char *utf_pos = utf_ptr - 1;
1122 char *primitive = primitivetype_table[j].wrapname;
1125 while (utf_pos < utf_end(c->name)) {
1126 if (*utf_pos++ != *primitive++) goto nomatch;
1129 /* primitive type found */
1130 *pos++ = primitivetype_table[j].typesig;
1137 /* no primitive type and no arrayclass, so must be object */
1141 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1155 for (i = 0; i < strlen(retType); i++) {
1156 *pos++ = retType[i];
1160 /* create utf-string */
1161 result = utf_new(buffer, (pos - buffer));
1162 MFREE(buffer, u1, buffer_size);
1168 /******************************************************************************************
1170 retrieve the next argument or returntype from a descriptor
1171 and return the corresponding class
1173 *******************************************************************************************/
1175 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1177 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1178 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1181 panic("illegal descriptor");
1183 if (skip) return NULL;
1185 use_class_as_object(c);
1190 /* get_parametertypes **********************************************************
1192 use the descriptor of a method to generate a java/lang/Class array
1193 which contains the classes of the parametertypes of the method
1195 *******************************************************************************/
1197 java_objectarray* get_parametertypes(methodinfo *m)
1199 utf *descr = m->descriptor; /* method-descriptor */
1200 char *utf_ptr = descr->text; /* current position in utf-text */
1201 char *desc_end = utf_end(descr); /* points behind utf string */
1202 java_objectarray* result;
1203 int parametercount = 0;
1207 utf_nextu2(&utf_ptr);
1209 /* determine number of parameters */
1210 while (*utf_ptr != ')') {
1211 get_type(&utf_ptr, desc_end, true);
1215 /* create class-array */
1216 result = builtin_anewarray(parametercount, class_java_lang_Class);
1218 utf_ptr = descr->text;
1219 utf_nextu2(&utf_ptr);
1221 /* get returntype classes */
1222 for (i = 0; i < parametercount; i++)
1224 (java_objectheader *) get_type(&utf_ptr, desc_end, false);
1230 /* get_exceptiontypes **********************************************************
1232 get the exceptions which can be thrown by a method
1234 *******************************************************************************/
1236 java_objectarray* get_exceptiontypes(methodinfo *m) {
1237 u2 exccount=m->thrownexceptionscount;
1239 java_objectarray *result;
1240 /* create class-array */
1241 result = builtin_anewarray(exccount, class_java_lang_Class);
1242 for (i=0;i<exccount;i++) {
1243 java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1244 use_class_as_object(oh);
1254 /******************************************************************************************
1256 get the returntype class of a method
1258 *******************************************************************************************/
1260 classinfo *get_returntype(methodinfo *m)
1262 char *utf_ptr; /* current position in utf-text */
1263 char *desc_end; /* points behind utf string */
1264 utf *desc = m->descriptor; /* method-descriptor */
1266 utf_ptr = desc->text;
1267 desc_end = utf_end(desc);
1269 /* ignore parametertypes */
1270 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1273 return get_type(&utf_ptr,desc_end, false);
1277 /*****************************************************************************/
1278 /*****************************************************************************/
1281 /*--------------------------------------------------------*/
1282 void printNativeCall(nativeCall nc) {
1285 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1286 for (i=0; i<nc.methCnt; i++) {
1287 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1289 for (j=0; j<nc.callCnt[i]; j++) {
1290 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1291 nc.methods[i].methodCalls[j].classname,
1292 nc.methods[i].methodCalls[j].methodname,
1293 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1296 printf("-+++++--------------------\n");fflush(stdout);
1299 /*--------------------------------------------------------*/
1300 void printCompNativeCall(nativeCompCall nc) {
1302 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1303 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1304 utf_display(nc.classname); fflush(stdout);
1306 for (i=0; i<nc.methCnt; i++) {
1307 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1308 utf_display(nc.methods[i].methodname); fflush(stdout);
1309 utf_display(nc.methods[i].descriptor);fflush(stdout);
1310 printf("\n");fflush(stdout);
1312 for (j=0; j<nc.callCnt[i]; j++) {
1313 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1314 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1315 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1316 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1317 printf("\n");fflush(stdout);
1320 printf("---------------------\n");fflush(stdout);
1324 /*--------------------------------------------------------*/
1325 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1338 /*--------------------------------------------------------*/
1339 nativeCall* findNativeClassCalls(char *aclassname ) {
1342 for (i=0;i<NATIVECALLSSIZE; i++) {
1343 /* convert table to utf later to speed up search */
1344 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1345 return &nativeCalls[i];
1350 /*--------------------------------------------------------*/
1351 /*--------------------------------------------------------*/
1352 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1356 ncc->classname = utf_new_char(nc.classname);
1357 ncc->methCnt = nc.methCnt;
1359 for (i=0; i<nc.methCnt; i++) {
1360 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1361 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1362 ncc->callCnt[i] = nc.callCnt[i];
1364 for (j=0; j<nc.callCnt[i]; j++) {
1366 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1368 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1369 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1370 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1373 ncc->methods[i].methodCalls[j].methodname = NULL;
1374 ncc->methods[i].methodCalls[j].descriptor = NULL;
1382 /*--------------------------------------------------------*/
1384 bool natcall2utf(bool natcallcompdone) {
1387 if (natcallcompdone)
1390 for (i=0;i<NATIVECALLSSIZE; i++) {
1391 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1397 /*--------------------------------------------------------*/
1400 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start)
1402 #if defined(__GNUC__)
1403 #warning platform dependend
1405 java_objectarray *tmpArray;
1407 classinfo **current;
1409 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1410 /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1411 if (!class_java_lang_Class)
1412 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1413 if (!class_java_lang_SecurityManager)
1414 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1416 if (start==class_java_lang_SecurityManager) {
1421 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1423 for(i=0,current=start;i<size;i++,current--) {
1425 /* printf("%d\n",i);
1426 utf_display(c->name);*/
1427 use_class_as_object(c);
1428 tmpArray->data[i]=c;
1434 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start)
1436 #if defined(__GNUC__)
1437 #warning platform dependend
1440 classinfo **current;
1442 classinfo *privilegedAction;
1443 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1444 /* log_text("builtin_asm_getclassloader");
1445 printf("end %p, start %p, size %ld\n",end,start,size);*/
1447 if (!class_java_lang_SecurityManager)
1448 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1450 if (start==class_java_lang_SecurityManager) {
1456 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1458 for(i=0,current=start;i<size;i++,current--) {
1460 if (c==privilegedAction) return NULL;
1461 if (c->classloader) return c->classloader;
1469 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1470 init_systemclassloader();
1472 return SystemClassLoader;*/
1476 * These are local overrides for various environment variables in Emacs.
1477 * Please do not remove this and leave it at the end of the file, where
1478 * Emacs will automagically detect them.
1479 * ---------------------------------------------------------------------
1482 * indent-tabs-mode: t