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 595 2003-11-09 20:04:01Z twisti $
47 #include <sys/utsname.h>
51 #include "nativetypes.hh"
56 #include "toolbox/loging.h"
57 #include "threads/thread.h"
58 #include "threads/threadio.h"
59 #include "threads/locks.h"
61 /* Include files for IO functions */
65 #include <sys/types.h>
72 /* searchpath for classfiles */
73 static char *classpath;
75 /* for java-string to char conversion */
76 #define MAXSTRINGSIZE 1000
78 /******************** systemclasses required for native methods ***************/
80 static classinfo *class_java_lang_Class;
81 static classinfo *class_java_lang_Cloneable;
82 static classinfo *class_java_lang_CloneNotSupportedException;
83 static classinfo *class_java_lang_System;
84 static classinfo *class_java_lang_ClassLoader;
85 static classinfo *class_java_lang_ClassNotFoundException;
86 static classinfo *class_java_lang_InstantiationException;
87 static classinfo *class_java_lang_NoSuchMethodError;
88 static classinfo *class_java_lang_NoSuchFieldError;
89 static classinfo *class_java_lang_ClassFormatError;
90 static classinfo *class_java_lang_IllegalArgumentException;
91 static classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
92 static classinfo *class_java_lang_NoSuchFieldException;
93 static classinfo *class_java_io_SyncFailedException;
94 static classinfo *class_java_io_IOException;
95 static classinfo *class_java_io_FileNotFoundException;
96 static classinfo *class_java_io_UnixFileSystem;
97 static classinfo *class_java_security_PrivilegedActionException;
98 static classinfo *class_java_net_UnknownHostException;
99 static classinfo *class_java_net_SocketException;
100 static classinfo *class_java_lang_NoSuchMethodException;
101 static classinfo *class_java_lang_Double;
102 static classinfo *class_java_lang_Float;
103 static classinfo *class_java_lang_Long;
104 static classinfo *class_java_lang_Byte;
105 static classinfo *class_java_lang_Short;
106 static classinfo *class_java_lang_Boolean;
107 static classinfo *class_java_lang_Void;
108 static classinfo *class_java_lang_Character;
109 static classinfo *class_java_lang_Integer;
111 /* the system classloader object */
112 struct java_lang_ClassLoader *SystemClassLoader = NULL;
114 /* for raising exceptions from native methods */
115 java_objectheader* exceptionptr = NULL;
117 /************* use classinfo structure as java.lang.Class object **************/
119 void use_class_as_object(classinfo *c)
121 vftbl *vt = class_java_lang_Class->vftbl;
123 if (!c->classvftbl) {
124 c->classvftbl = true;
125 copy_vftbl(&newtbl, vt);
126 newtbl->class = c->header.vftbl->class;
127 newtbl->baseval = c->header.vftbl->baseval;
128 newtbl->diffval = c->header.vftbl->diffval;
129 c->header.vftbl = newtbl;
133 /*********************** include Java Native Interface ************************/
137 /*************************** include native methods ***************************/
139 #include "nat/Object.c"
140 #include "nat/String.c"
141 #include "nat/ClassLoader.c"
142 #include "nat/Class.c"
143 #include "nat/Compiler.c"
144 #include "nat/Double.c"
145 #include "nat/Float.c"
146 #include "nat/Math.c"
147 #include "nat/Package.c"
148 #include "nat/Runtime.c"
149 #include "nat/SecurityManager.c"
150 #include "nat/System.c"
151 #include "nat/Thread.c"
152 #include "nat/Throwable.c"
153 #include "nat/Finalizer.c"
154 #include "nat/Array.c"
155 #include "nat/Constructor.c"
156 #include "nat/Field.c"
157 #include "nat/Method.c"
158 #include "nat/FileDescriptor.c"
159 #include "nat/FileInputStream.c"
160 #include "nat/FileOutputStream.c"
161 #include "nat/FileSystem.c"
162 #include "nat/ObjectInputStream.c"
163 #include "nat/ObjectStreamClass.c"
164 #include "nat/RandomAccessFile.c"
165 #include "nat/ResourceBundle.c"
166 #include "nat/JarFile.c"
167 #include "nat/Adler32.c"
168 #include "nat/CRC32.c"
169 #include "nat/Deflater.c"
170 #include "nat/Inflater.c"
171 #include "nat/ZipEntry.c"
172 #include "nat/ZipFile.c"
173 #include "nat/BigInteger.c"
174 #include "nat/InetAddress.c"
175 #include "nat/InetAddressImpl.c"
176 #include "nat/DatagramPacket.c"
177 #include "nat/PlainDatagramSocketImpl.c"
178 #include "nat/PlainSocketImpl.c"
179 #include "nat/SocketInputStream.c"
180 #include "nat/SocketOutputStream.c"
181 #include "nat/AccessController.c"
182 #include "nat/ClassLoader_NativeLibrary.c"
183 #include "nat/UnixFileSystem.c"
185 /************************** tables for methods ********************************/
187 /* table for locating native methods */
188 static struct nativeref {
196 #include "nativetable.hh"
201 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
203 /* table for fast string comparison */
204 static struct nativecompref {
210 } nativecomptable [NATIVETABLESIZE];
212 /* string comparsion table initialized */
213 static bool nativecompdone = false;
216 /******************************************************************************/
217 /******************************************************************************/
218 #include "natcalls.h"
220 /* string call comparison table initialized */
222 /******************************************************************************/
223 /******************************************************************************/
225 /*--------------- native method calls & classes used -------------------------*/
230 /*********************** function: native_loadclasses **************************
232 load classes required for native methods
234 *******************************************************************************/
236 void native_loadclasses()
238 /* class_new adds the class to the list of classes to be loaded */
239 class_java_lang_Cloneable =
240 class_new(utf_new_char("java/lang/Cloneable"));
241 class_java_lang_CloneNotSupportedException =
242 class_new(utf_new_char("java/lang/CloneNotSupportedException"));
243 class_java_lang_Class =
244 class_new(utf_new_char("java/lang/Class"));
245 class_java_io_IOException =
246 class_new(utf_new_char("java/io/IOException"));
247 class_java_io_FileNotFoundException =
248 class_new(utf_new_char("java/io/FileNotFoundException"));
249 class_java_lang_ClassNotFoundException =
250 class_new(utf_new_char("java/lang/ClassNotFoundException"));
251 class_java_lang_InstantiationException =
252 class_new(utf_new_char("java/lang/InstantiationException"));
253 class_java_lang_NoSuchMethodError =
254 class_new(utf_new_char("java/lang/NoSuchMethodError"));
255 class_java_lang_NoSuchFieldError =
256 class_new(utf_new_char("java/lang/NoSuchFieldError"));
257 class_java_lang_ClassFormatError =
258 class_new(utf_new_char("java/lang/ClassFormatError"));
259 class_java_io_SyncFailedException =
260 class_new(utf_new_char("java/io/SyncFailedException"));
261 class_java_io_UnixFileSystem =
262 class_new(utf_new_char("java/io/UnixFileSystem"));
263 class_java_lang_System =
264 class_new(utf_new_char("java/lang/System"));
265 class_java_lang_ClassLoader =
266 class_new(utf_new_char("java/lang/ClassLoader"));
267 class_java_security_PrivilegedActionException =
268 class_new(utf_new_char("java/security/PrivilegedActionException"));
270 class_java_net_UnknownHostException =
271 loader_load(utf_new_char("java/net/UnknownHostException"));
272 class_java_net_SocketException =
273 loader_load(utf_new_char("java/net/SocketException"));
275 class_java_lang_IllegalArgumentException =
276 class_new(utf_new_char("java/lang/IllegalArgumentException"));
277 class_java_lang_ArrayIndexOutOfBoundsException =
278 class_new(utf_new_char("java/lang/ArrayIndexOutOfBoundsException"));
279 class_java_lang_NoSuchFieldException =
280 class_new(utf_new_char("java/lang/NoSuchFieldException"));
281 class_java_lang_NoSuchMethodException =
282 class_new(utf_new_char("java/lang/NoSuchMethodException"));
284 /* load classes for wrapping primitive types */
285 class_java_lang_Double =
286 class_new(utf_new_char("java/lang/Double"));
287 class_java_lang_Float =
288 class_new(utf_new_char("java/lang/Float"));
289 class_java_lang_Character =
290 class_new(utf_new_char("java/lang/Character"));
291 class_java_lang_Integer =
292 class_new(utf_new_char("java/lang/Integer"));
293 class_java_lang_Long =
294 class_new(utf_new_char("java/lang/Long"));
295 class_java_lang_Byte =
296 class_new(utf_new_char("java/lang/Byte"));
297 class_java_lang_Short =
298 class_new(utf_new_char("java/lang/Short"));
299 class_java_lang_Boolean =
300 class_new(utf_new_char("java/lang/Boolean"));
301 class_java_lang_Void =
302 class_new(utf_new_char("java/lang/Void"));
304 /* load to avoid dynamic classloading */
305 class_new(utf_new_char("sun/net/www/protocol/file/Handler"));
306 class_new(utf_new_char("sun/net/www/protocol/jar/Handler"));
307 class_new(utf_new_char("sun/io/CharToByteISO8859_1"));
309 /* start classloader */
310 loader_load(utf_new_char("sun/io/ByteToCharISO8859_1"));
314 /*************** adds a class to the vector of loaded classes ****************/
316 void systemclassloader_addclass(classinfo *c)
320 /* find method addClass of java.lang.ClassLoader */
321 m = class_resolvemethod(
322 class_java_lang_ClassLoader,
323 utf_new_char("addClass"),
324 utf_new_char("(Ljava/lang/Class;)")
327 if (!m) panic("warning: cannot initialize classloader");
329 /* prepare class to be passed as argument */
330 use_class_as_object (c);
332 /* call 'addClass' */
333 asm_calljavamethod(m,
334 (java_objectheader*) SystemClassLoader,
335 (java_objectheader*) c,
341 /*************** adds a library to the vector of loaded libraries *************/
343 void systemclassloader_addlibrary(java_objectheader *o)
347 /* find method addElement of java.util.Vector */
348 m = class_resolvemethod(
349 loader_load ( utf_new_char ("java/util/Vector") ),
350 utf_new_char("addElement"),
351 utf_new_char("(Ljava/lang/Object;)V")
354 if (!m) panic("cannot initialize classloader");
356 /* call 'addElement' */
357 asm_calljavamethod(m,
358 SystemClassLoader->nativeLibraries,
365 /*****************************************************************************
367 create systemclassloader object and initialize instance fields
369 ******************************************************************************/
371 void init_systemclassloader()
373 if (!SystemClassLoader) {
374 /* create object and call initializer */
375 SystemClassLoader = (java_lang_ClassLoader*) native_new_and_init(class_java_lang_ClassLoader);
376 heap_addreference((void**) &SystemClassLoader);
378 /* systemclassloader has no parent */
379 SystemClassLoader->parent = NULL;
380 SystemClassLoader->initialized = true;
385 /********************* add loaded library name *******************************/
387 void systemclassloader_addlibname(java_objectheader *o)
392 m = class_resolvemethod(loader_load(utf_new_char ("java/util/Vector")),
393 utf_new_char("addElement"),
394 utf_new_char("(Ljava/lang/Object;)V")
397 if (!m) panic("cannot initialize classloader");
399 id = env.GetStaticFieldID(&env,class_java_lang_ClassLoader,"loadedLibraryNames","Ljava/util/Vector;");
400 if (!id) panic("can not access ClassLoader");
402 asm_calljavamethod(m,
403 GetStaticObjectField(&env,class_java_lang_ClassLoader,id),
411 /********************* function: native_setclasspath **************************/
413 void native_setclasspath (char *path)
415 /* set searchpath for classfiles */
419 /***************** function: throw_classnotfoundexception *********************/
421 void throw_classnotfoundexception()
423 if (!class_java_lang_ClassNotFoundException) {
424 panic("java.lang.ClassNotFoundException not found. Maybe wrong classpath?");
427 /* throws a ClassNotFoundException */
428 exceptionptr = native_new_and_init (class_java_lang_ClassNotFoundException);
432 /*********************** Function: native_findfunction *************************
434 Looks up a method (must have the same class name, method name, descriptor
435 and 'static'ness) and returns a function pointer to it.
436 Returns: function pointer or NULL (if there is no such method)
438 Remark: For faster operation, the names/descriptors are converted from C
439 strings to Unicode the first time this function is called.
441 *******************************************************************************/
443 functionptr native_findfunction(utf *cname, utf *mname,
444 utf *desc, bool isstatic)
447 /* entry of table for fast string comparison */
448 struct nativecompref *n;
449 /* for warning message if no function is found */
453 isstatic = isstatic ? true : false;
455 if (!nativecompdone) {
456 for (i = 0; i < NATIVETABLESIZE; i++) {
457 nativecomptable[i].classname =
458 utf_new_char(nativetable[i].classname);
459 nativecomptable[i].methodname =
460 utf_new_char(nativetable[i].methodname);
461 nativecomptable[i].descriptor =
462 utf_new_char(nativetable[i].descriptor);
463 nativecomptable[i].isstatic =
464 nativetable[i].isstatic;
465 nativecomptable[i].func =
468 nativecompdone = true;
471 for (i = 0; i < NATIVETABLESIZE; i++) {
472 n = &(nativecomptable[i]);
474 if (cname == n->classname && mname == n->methodname &&
475 desc == n->descriptor && isstatic == n->isstatic)
479 /* no function was found, display warning */
482 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
484 buffer = MNEW(char, buffer_len);
486 strcpy(buffer, "warning: native function ");
487 utf_sprint(buffer + strlen(buffer), mname);
488 strcpy(buffer + strlen(buffer), ": ");
489 utf_sprint(buffer + strlen(buffer), desc);
490 strcpy(buffer + strlen(buffer), " not found in class ");
491 utf_sprint(buffer + strlen(buffer), cname);
495 MFREE(buffer, char, buffer_len);
501 /********************** function: javastring_new *******************************
503 creates a new object of type java/lang/String with the text of
504 the specified utf8-string
506 return: pointer to the string or NULL if memory is exhausted.
508 *******************************************************************************/
510 java_objectheader *javastring_new (utf *u)
512 char *utf_ptr = u->text; /* current utf character in utf string */
513 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
514 java_lang_String *s; /* result-string */
518 s = (java_lang_String*) builtin_new (class_java_lang_String);
519 a = builtin_newarray_char (utflength);
521 /* javastring or character-array could not be created */
525 /* decompress utf-string */
526 for (i = 0; i < utflength; i++)
527 a->data[i] = utf_nextu2(&utf_ptr);
529 /* set fields of the javastring-object */
532 s->count = utflength;
534 return (java_objectheader*) s;
537 /********************** function: javastring_new_char **************************
539 creates a new java/lang/String object which contains the convertet
540 C-string passed via text.
542 return: the object pointer or NULL if memory is exhausted.
544 *******************************************************************************/
546 java_objectheader *javastring_new_char (char *text)
549 s4 len = strlen(text); /* length of the string */
550 java_lang_String *s; /* result-string */
553 s = (java_lang_String*) builtin_new(class_java_lang_String);
554 a = builtin_newarray_char(len);
556 /* javastring or character-array could not be created */
557 if ((!a) || (!s)) return NULL;
560 for (i = 0; i < len; i++)
561 a->data[i] = text[i];
563 /* set fields of the javastring-object */
568 return (java_objectheader*) s;
572 /************************* function javastring_tochar **************************
574 converts a Java string into a C string.
576 return: pointer to C string
578 Caution: every call of this function overwrites the previous string !!!
580 *******************************************************************************/
582 static char stringbuffer[MAXSTRINGSIZE];
584 char *javastring_tochar (java_objectheader *so)
586 java_lang_String *s = (java_lang_String*) so;
595 if (s->count > MAXSTRINGSIZE)
597 for (i = 0; i < s->count; i++)
598 stringbuffer[i] = a->data[s->offset+i];
599 stringbuffer[i] = '\0';
604 /****************** function class_findfield_approx ****************************
606 searches in 'classinfo'-structure for a field with the
609 *******************************************************************************/
611 fieldinfo *class_findfield_approx (classinfo *c, utf *name)
614 for (i = 0; i < c->fieldscount; i++) {
615 /* compare field names */
616 if ((c->fields[i].name == name))
617 return &(c->fields[i]);
620 /* field was not found, raise exception */
621 exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
626 /********************** function: native_new_and_init *************************
628 Creates a new object on the heap and calls the initializer.
629 Returns the object pointer or NULL if memory is exhausted.
631 *******************************************************************************/
633 java_objectheader *native_new_and_init(classinfo *c)
636 java_objectheader *o = builtin_new(c); /* create object */
640 /* find initializer */
642 m = class_findmethod(c, utf_new_char("<init>"), utf_new_char("()V"));
644 if (!m) { /* initializer not found */
646 sprintf(logtext, "Warning: class has no instance-initializer: ");
647 utf_sprint(logtext + strlen(logtext), c->name);
653 /* call initializer */
655 asm_calljavamethod(m, o, NULL, NULL, NULL);
660 /******************** function: stringtable_update ****************************
662 traverses the javastring hashtable and sets the vftbl-entries of
663 javastrings which were temporarily set to NULL, because
664 java.lang.Object was not yet loaded
666 *******************************************************************************/
668 void stringtable_update ()
670 java_lang_String *js;
672 literalstring *s; /* hashtable entry */
675 for (i = 0; i < string_hash.size; i++) {
676 s = string_hash.ptr[i];
680 js = (java_lang_String *) s->string;
682 if (!js || !(a = js->value)) {
683 /* error in hashtable found */
684 panic("invalid literalstring in hashtable");
687 if (!js->header.vftbl)
688 /* vftbl of javastring is NULL */
689 js->header.vftbl = class_java_lang_String -> vftbl;
691 if (!a->header.objheader.vftbl)
692 /* vftbl of character-array is NULL */
693 a->header.objheader.vftbl = class_array -> vftbl;
695 /* follow link in external hash chain */
704 /************************* function: u2_utflength ***************************
706 returns the utf length in bytes of a u2 array
708 *****************************************************************************/
711 u4 u2_utflength(u2 *text, u4 u2_length)
713 u4 result_len = 0; /* utf length in bytes */
714 u2 ch; /* current unicode character */
717 for (len = 0; len < u2_length; len++) {
719 /* next unicode character */
722 /* determine bytes required to store unicode character as utf */
723 if (ch && (ch < 0x80))
734 /********************* function: utf_new_u2 ***********************************
736 make utf symbol from u2 array,
737 if isclassname is true '.' is replaced by '/'
739 *******************************************************************************/
741 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
743 char *buffer; /* memory buffer for unicode characters */
744 char *pos; /* pointer to current position in buffer */
745 u4 left; /* unicode characters left */
746 u4 buflength; /* utf length in bytes of the u2 array */
747 utf *result; /* resulting utf-string */
750 /* determine utf length in bytes and allocate memory */
751 buflength = u2_utflength(unicode_pos, unicode_length);
752 buffer = MNEW(char,buflength);
754 /* memory allocation failed */
755 if (!buffer) return NULL;
760 for (i = 0; i++ < unicode_length; unicode_pos++) {
761 /* next unicode character */
764 if ((c != 0) && (c < 0x80)) {
767 if ((int) left < 0) break;
768 /* convert classname */
769 if (isclassname && c=='.')
773 } else if (c < 0x800) {
775 unsigned char high = c >> 6;
776 unsigned char low = c & 0x3F;
778 if ((int) left < 0) break;
779 *pos++ = high | 0xC0;
784 char mid = (c >> 6) & 0x3F;
787 if ((int) left < 0) break;
788 *pos++ = high | 0xE0;
794 /* insert utf-string into symbol-table */
795 result = utf_new(buffer,buflength);
796 MFREE(buffer, char, buflength);
800 /********************* function: javastring_toutf *****************************
802 make utf symbol from javastring
804 *******************************************************************************/
806 utf *javastring_toutf(java_lang_String *string, bool isclassname)
808 java_lang_String *str = (java_lang_String *) string;
809 return utf_new_u2(str->value->data,str->count, isclassname);
812 /********************* function: literalstring_u2 *****************************
814 searches for the javastring with the specified u2-array in
815 the string hashtable, if there is no such string a new one is
818 if copymode is true a copy of the u2-array is made
820 *******************************************************************************/
822 java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode )
824 literalstring *s; /* hashtable element */
825 java_lang_String *js; /* u2-array wrapped in javastring */
826 java_chararray *stringdata; /* copy of u2-array */
831 /* find location in hashtable */
832 key = unicode_hashkey (a->data, length);
833 slot = key & (string_hash.size-1);
834 s = string_hash.ptr[slot];
838 js = (java_lang_String *) s->string;
840 if (js->count == length) {
842 for (i=0; i<length; i++)
843 if (js->value->data[i] != a->data[i]) goto nomatch;
845 /* string already in hashtable, free memory */
847 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2)*(length-1)+10);
849 return (java_objectheader *) js;
853 /* follow link in external hash chain */
858 /* create copy of u2-array for new javastring */
859 u4 arraysize = sizeof(java_chararray) + sizeof(u2)*(length-1)+10;
860 stringdata = lit_mem_alloc ( arraysize );
861 memcpy(stringdata, a, arraysize );
866 /* location in hashtable found, complete arrayheader */
867 if (class_array==NULL) panic("class_array not initialized");
868 stringdata -> header.objheader.vftbl = class_array -> vftbl;
869 stringdata -> header.size = length;
870 stringdata -> header.arraytype = ARRAYTYPE_CHAR;
872 /* create new javastring */
873 js = LNEW (java_lang_String);
874 js -> header.vftbl = class_java_lang_String -> vftbl;
875 js -> value = stringdata;
877 js -> count = length;
879 /* create new literalstring */
880 s = NEW (literalstring);
881 s->hashlink = string_hash.ptr[slot];
882 s->string = (java_objectheader *) js;
883 string_hash.ptr[slot] = s;
885 /* update numbe of hashtable entries */
886 string_hash.entries++;
888 /* reorganization of hashtable */
889 if ( string_hash.entries > (string_hash.size*2)) {
891 /* reorganization of hashtable, average length of
892 the external chains is approx. 2 */
896 hashtable newhash; /* the new hashtable */
898 /* create new hashtable, double the size */
899 init_hashtable(&newhash, string_hash.size*2);
900 newhash.entries=string_hash.entries;
902 /* transfer elements to new hashtable */
903 for (i=0; i<string_hash.size; i++) {
904 s = string_hash.ptr[i];
906 literalstring *nexts = s -> hashlink;
907 js = (java_lang_String*) s->string;
908 slot = (unicode_hashkey(js->value->data,js->count)) & (newhash.size-1);
910 s->hashlink = newhash.ptr[slot];
911 newhash.ptr[slot] = s;
913 /* follow link in external hash chain */
918 /* dispose old table */
919 MFREE (string_hash.ptr, void*, string_hash.size);
920 string_hash = newhash;
923 return (java_objectheader *) js;
926 /******************** Function: literalstring_new *****************************
928 creates a new javastring with the text of the utf-symbol
929 and inserts it into the string hashtable
931 *******************************************************************************/
933 java_objectheader *literalstring_new (utf *u)
935 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
936 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
937 java_chararray *a; /* u2-array constructed from utf string */
940 /* allocate memory */
941 a = lit_mem_alloc (sizeof(java_chararray) + sizeof(u2)*(utflength-1)+10 );
942 /* convert utf-string to u2-array */
943 for (i=0; i<utflength; i++) a->data[i] = utf_nextu2(&utf_ptr);
945 return literalstring_u2(a, utflength, false);
949 /********************** function: literalstring_free **************************
951 removes a javastring from memory
953 ******************************************************************************/
955 void literalstring_free (java_objectheader* sobj)
957 java_lang_String *s = (java_lang_String*) sobj;
958 java_chararray *a = s->value;
960 log_text("literalstring_free called");
962 /* dispose memory of java.lang.String object */
963 LFREE (s, java_lang_String);
964 /* dispose memory of java-characterarray */
965 LFREE (a, sizeof(java_chararray) + sizeof(u2)*(a->header.size-1)); /* +10 ?? */
971 void copy_vftbl(vftbl **dest, vftbl *src)
973 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr) * (src->vftbllength - 1));
974 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
975 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
978 /*****************************************************************************/
979 /*****************************************************************************/
982 /*--------------------------------------------------------*/
983 void printNativeCall(nativeCall nc) {
986 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
987 for (i=0; i<nc.methCnt; i++) {
988 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
990 for (j=0; j<nc.callCnt[i]; j++) {
991 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
992 nc.methods[i].methodCalls[j].classname,
993 nc.methods[i].methodCalls[j].methodname,
994 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
997 printf("-+++++--------------------\n");fflush(stdout);
1000 /*--------------------------------------------------------*/
1001 void printCompNativeCall(nativeCompCall nc) {
1003 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1004 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1005 utf_display(nc.classname); fflush(stdout);
1007 for (i=0; i<nc.methCnt; i++) {
1008 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1009 utf_display(nc.methods[i].methodname); fflush(stdout);
1010 utf_display(nc.methods[i].descriptor);fflush(stdout);
1011 printf("\n");fflush(stdout);
1013 for (j=0; j<nc.callCnt[i]; j++) {
1014 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1015 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1016 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1017 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1018 printf("\n");fflush(stdout);
1021 printf("---------------------\n");fflush(stdout);
1025 /*--------------------------------------------------------*/
1026 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1039 /*--------------------------------------------------------*/
1040 nativeCall* findNativeClassCalls(char *aclassname ) {
1043 for (i=0;i<NATIVECALLSSIZE; i++) {
1044 /* convert table to utf later to speed up search */
1045 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1046 return &nativeCalls[i];
1051 /*--------------------------------------------------------*/
1052 /*--------------------------------------------------------*/
1053 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1057 ncc->classname = utf_new_char(nc.classname);
1058 ncc->methCnt = nc.methCnt;
1060 for (i=0; i<nc.methCnt; i++) {
1061 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1062 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1063 ncc->callCnt[i] = nc.callCnt[i];
1065 for (j=0; j<nc.callCnt[i]; j++) {
1067 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1069 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1070 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1071 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1074 ncc->methods[i].methodCalls[j].methodname = NULL;
1075 ncc->methods[i].methodCalls[j].descriptor = NULL;
1083 /*--------------------------------------------------------*/
1085 bool natcall2utf(bool natcallcompdone) {
1088 if (natcallcompdone)
1091 for (i=0;i<NATIVECALLSSIZE; i++) {
1092 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1097 /*--------------------------------------------------------*/
1101 * These are local overrides for various environment variables in Emacs.
1102 * Please do not remove this and leave it at the end of the file, where
1103 * Emacs will automagically detect them.
1104 * ---------------------------------------------------------------------
1107 * indent-tabs-mode: t