1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
32 $Id: native.c 6251 2006-12-27 23:15:56Z twisti $
42 #if !defined(WITH_STATIC_CLASSPATH)
48 #include "mm/memory.h"
49 #include "native/jni.h"
50 #include "native/native.h"
51 #include "native/include/java_lang_Throwable.h"
53 #if defined(ENABLE_THREADS)
54 # include "threads/native/lock.h"
56 # include "threads/none/lock.h"
59 #include "toolbox/logging.h"
60 #include "vm/builtin.h"
61 #include "vm/exceptions.h"
62 #include "vm/global.h"
63 #include "vm/hashtable.h"
64 #include "vm/loader.h"
65 #include "vm/options.h"
66 #include "vm/resolve.h"
67 #include "vm/stringlocal.h"
69 #include "vm/jit/asmpart.h"
70 #include "vm/jit/jit.h"
72 #if defined(ENABLE_JVMTI)
73 #include "native/jvmti/cacaodbg.h"
77 /* include table of native functions ******************************************/
79 #if defined(ENABLE_JAVASE)
81 #include "native/include/java_io_InputStream.h"
82 #include "native/include/java_io_PrintStream.h"
84 #include "native/include/java_lang_Cloneable.h"
85 #include "native/include/java_util_Properties.h"
87 #include "native/include/java_lang_Object.h"
89 #include "native/include/gnu_classpath_VMStackWalker.h"
90 #include "native/include/gnu_classpath_VMSystemProperties.h"
91 #include "native/include/gnu_java_lang_management_VMClassLoadingMXBeanImpl.h"
92 #include "native/include/gnu_java_lang_management_VMMemoryMXBeanImpl.h"
93 #include "native/include/gnu_java_lang_management_VMRuntimeMXBeanImpl.h"
94 #include "native/include/java_lang_VMClass.h"
95 #include "native/include/java_lang_VMClassLoader.h"
96 #include "native/include/java_lang_VMObject.h"
97 #include "native/include/java_lang_VMRuntime.h"
98 #include "native/include/java_lang_VMString.h"
99 #include "native/include/java_lang_VMSystem.h"
100 #include "native/include/java_lang_VMThread.h"
101 #include "native/include/java_lang_VMThrowable.h"
102 #include "native/include/java_lang_management_VMManagementFactory.h"
103 #include "native/include/java_lang_reflect_Constructor.h"
104 #include "native/include/java_lang_reflect_Field.h"
105 #include "native/include/java_lang_reflect_Method.h"
106 #include "native/include/java_lang_reflect_VMProxy.h"
107 #include "native/include/java_security_VMAccessController.h"
109 #if defined(ENABLE_JVMTI)
110 #include "native/include/gnu_classpath_jdwp_event_EventRequest.h"
111 #include "native/include/java_nio_ByteBuffer.h"
112 #include "native/include/gnu_classpath_jdwp_VMVirtualMachine.h"
113 #include "native/include/gnu_classpath_jdwp_VMFrame.h"
114 #include "native/include/gnu_classpath_jdwp_VMMethod.h"
117 #elif defined(ENABLE_JAVAME_CLDC1_1)
119 #include "native/include/com_sun_cldchi_io_ConsoleOutputStream.h"
120 #include "native/include/java_lang_Class.h"
121 #include "native/include/java_lang_Double.h"
122 #include "native/include/java_lang_Float.h"
123 #include "native/include/java_lang_Math.h"
124 #include "native/include/java_lang_Runtime.h"
125 #include "native/include/java_lang_System.h"
126 #include "native/include/java_lang_Thread.h"
130 #if defined(WITH_STATIC_CLASSPATH)
132 /* these are required to prevent compiler warnings */
134 #include "native/include/java_net_DatagramPacket.h"
135 #include "native/include/java_net_InetAddress.h"
136 #include "native/include/java_net_SocketImpl.h"
138 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
139 #include "native/include/gnu_java_net_PlainSocketImpl.h"
140 #include "native/include/gnu_java_nio_PipeImpl.h"
141 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
142 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
143 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
144 #include "native/include/java_lang_VMProcess.h"
145 #include "native/include/java_nio_MappedByteBufferImpl.h"
146 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
148 /* now include the native table */
150 #include "native/nativetable.inc"
152 #elif !defined(ENABLE_LIBJVM)
154 /* dummynativetable ************************************************************
156 Ensure that symbols for functions implemented within CACAO are used
157 and exported to dlopen.
159 ATTENTION: Don't make this table static!!! Otherwise the compiler
160 may optimize it away!
162 *******************************************************************************/
164 functionptr dummynativetable[] = {
165 #if defined(ENABLE_JAVASE)
166 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
167 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClass,
168 (functionptr) Java_gnu_classpath_VMStackWalker_getCallingClassLoader,
169 (functionptr) Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader,
171 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
173 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getLoadedClassCount,
174 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getUnloadedClassCount,
175 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_isVerbose,
176 (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_setVerbose,
178 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getHeapMemoryUsage,
179 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getNonHeapMemoryUsage,
180 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getObjectPendingFinalizationCount,
181 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_isVerbose,
182 (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_setVerbose,
184 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getInputArguments,
185 (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getStartTime,
187 (functionptr) Java_java_lang_VMClass_isInstance,
188 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
189 (functionptr) Java_java_lang_VMClass_isInterface,
190 (functionptr) Java_java_lang_VMClass_isPrimitive,
191 (functionptr) Java_java_lang_VMClass_getName,
192 (functionptr) Java_java_lang_VMClass_getSuperclass,
193 (functionptr) Java_java_lang_VMClass_getInterfaces,
194 (functionptr) Java_java_lang_VMClass_getComponentType,
195 (functionptr) Java_java_lang_VMClass_getModifiers,
196 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
197 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
198 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
199 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
200 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
201 (functionptr) Java_java_lang_VMClass_getClassLoader,
202 (functionptr) Java_java_lang_VMClass_forName,
203 (functionptr) Java_java_lang_VMClass_isArray,
204 (functionptr) Java_java_lang_VMClass_throwException,
206 (functionptr) Java_java_lang_VMClassLoader_defineClass,
207 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
208 (functionptr) Java_java_lang_VMClassLoader_loadClass,
209 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
210 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
211 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
213 (functionptr) Java_java_lang_VMObject_getClass,
214 (functionptr) Java_java_lang_VMObject_clone,
215 (functionptr) Java_java_lang_VMObject_notify,
216 (functionptr) Java_java_lang_VMObject_notifyAll,
217 (functionptr) Java_java_lang_VMObject_wait,
219 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
220 (functionptr) Java_java_lang_VMRuntime_freeMemory,
221 (functionptr) Java_java_lang_VMRuntime_totalMemory,
222 (functionptr) Java_java_lang_VMRuntime_maxMemory,
223 (functionptr) Java_java_lang_VMRuntime_gc,
224 (functionptr) Java_java_lang_VMRuntime_runFinalization,
225 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
226 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
227 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
228 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
229 (functionptr) Java_java_lang_VMRuntime_exit,
230 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
231 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
233 (functionptr) Java_java_lang_VMString_intern,
235 (functionptr) Java_java_lang_VMSystem_arraycopy,
236 (functionptr) Java_java_lang_VMSystem_identityHashCode,
238 (functionptr) Java_java_lang_VMThread_start,
239 (functionptr) Java_java_lang_VMThread_interrupt,
240 (functionptr) Java_java_lang_VMThread_isInterrupted,
241 (functionptr) Java_java_lang_VMThread_suspend,
242 (functionptr) Java_java_lang_VMThread_resume,
243 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
244 (functionptr) Java_java_lang_VMThread_nativeStop,
245 (functionptr) Java_java_lang_VMThread_currentThread,
246 (functionptr) Java_java_lang_VMThread_yield,
247 (functionptr) Java_java_lang_VMThread_interrupted,
248 (functionptr) Java_java_lang_VMThread_holdsLock,
250 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
251 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
253 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
254 (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
255 (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
257 (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
258 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
260 (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
261 (functionptr) Java_java_lang_reflect_Field_getType,
262 (functionptr) Java_java_lang_reflect_Field_get,
263 (functionptr) Java_java_lang_reflect_Field_getBoolean,
264 (functionptr) Java_java_lang_reflect_Field_getByte,
265 (functionptr) Java_java_lang_reflect_Field_getChar,
266 (functionptr) Java_java_lang_reflect_Field_getShort,
267 (functionptr) Java_java_lang_reflect_Field_getInt,
268 (functionptr) Java_java_lang_reflect_Field_getLong,
269 (functionptr) Java_java_lang_reflect_Field_getFloat,
270 (functionptr) Java_java_lang_reflect_Field_getDouble,
271 (functionptr) Java_java_lang_reflect_Field_set,
272 (functionptr) Java_java_lang_reflect_Field_setBoolean,
273 (functionptr) Java_java_lang_reflect_Field_setByte,
274 (functionptr) Java_java_lang_reflect_Field_setChar,
275 (functionptr) Java_java_lang_reflect_Field_setShort,
276 (functionptr) Java_java_lang_reflect_Field_setInt,
277 (functionptr) Java_java_lang_reflect_Field_setLong,
278 (functionptr) Java_java_lang_reflect_Field_setFloat,
279 (functionptr) Java_java_lang_reflect_Field_setDouble,
281 (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
282 (functionptr) Java_java_lang_reflect_Method_getReturnType,
283 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
284 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
285 (functionptr) Java_java_lang_reflect_Method_invokeNative,
287 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
288 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
289 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
291 (functionptr) Java_java_security_VMAccessController_getStack,
293 #if defined(ENABLE_JVMTI)
294 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
295 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
296 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
297 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
298 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
299 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
300 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
301 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
302 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
303 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
304 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
305 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
306 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
307 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
308 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
309 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
310 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
311 (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
312 (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
313 (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
314 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
315 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
316 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
317 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
318 (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
321 #elif defined(ENABLE_JAVAME_CLDC1_1)
322 (functionptr) Java_com_sun_cldchi_io_ConsoleOutputStream_write,
324 (functionptr) Java_java_lang_Class_forName,
325 (functionptr) Java_java_lang_Class_newInstance,
326 (functionptr) Java_java_lang_Class_getName,
328 (functionptr) Java_java_lang_Double_doubleToLongBits,
330 (functionptr) Java_java_lang_Float_floatToIntBits,
332 (functionptr) Java_java_lang_Math_ceil,
333 (functionptr) Java_java_lang_Math_cos,
334 (functionptr) Java_java_lang_Math_floor,
335 (functionptr) Java_java_lang_Math_sin,
336 (functionptr) Java_java_lang_Math_sqrt,
337 (functionptr) Java_java_lang_Math_tan,
339 (functionptr) Java_java_lang_Runtime_exitInternal,
341 (functionptr) Java_java_lang_System_getProperty0,
343 (functionptr) Java_java_lang_Thread_currentThread,
344 (functionptr) Java_java_lang_Thread_setPriority0,
345 (functionptr) Java_java_lang_Thread_start0,
346 (functionptr) Java_java_lang_Thread_yield,
348 (functionptr) Java_java_lang_Throwable_printStackTrace,
349 (functionptr) Java_java_lang_Throwable_fillInStackTrace
353 #endif /* defined(ENABLE_LIBJVM) */
356 /* tables for methods *********************************************************/
358 #if defined(WITH_STATIC_CLASSPATH)
359 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
361 /* table for fast string comparison */
362 static nativecompref nativecomptable[NATIVETABLESIZE];
364 /* string comparsion table initialized */
365 static bool nativecompdone = false;
369 /* global variables ***********************************************************/
371 #if !defined(WITH_STATIC_CLASSPATH)
372 static hashtable *hashtable_library;
373 static lt_dlhandle mainhandle;
377 /* native_init *****************************************************************
379 Initializes the native subsystem.
381 *******************************************************************************/
383 bool native_init(void)
385 #if !defined(WITH_STATIC_CLASSPATH)
386 /* initialize libltdl */
389 vm_abort("lt_dlinit failed: %s\n", lt_dlerror());
391 /* Get the handle for the main program or for the libjvm.so,
392 depends on the configuration. */
394 # if defined(ENABLE_LIBJVM)
395 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
396 If not found, try the absolute path. */
398 if (!(mainhandle = lt_dlopenext("libjvm")))
399 if (!(mainhandle = lt_dlopenext(cacao_libjvm)))
400 vm_abort("lt_dlopenext failed: %s\n", lt_dlerror());
402 if (!(mainhandle = lt_dlopen(NULL)))
403 vm_abort("lt_dlopen failed: %s\n", lt_dlerror());
406 /* initialize library hashtable, 10 entries should be enough */
408 hashtable_library = NEW(hashtable);
410 hashtable_create(hashtable_library, 10);
413 /* everything's ok */
419 /* native_hashtable_library_add ************************************************
421 Adds an entry to the native library hashtable.
423 *******************************************************************************/
425 #if !defined(WITH_STATIC_CLASSPATH)
426 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
429 hashtable_library_loader_entry *le;
430 hashtable_library_name_entry *ne; /* library name */
431 u4 key; /* hashkey */
432 u4 slot; /* slot in hashtable */
434 LOCK_MONITOR_ENTER(hashtable_library->header);
436 /* normally addresses are aligned to 4, 8 or 16 bytes */
438 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
439 slot = key & (hashtable_library->size - 1);
440 le = hashtable_library->ptr[slot];
442 /* search external hash chain for the entry */
445 if (le->loader == loader)
448 le = le->hashlink; /* next element in external chain */
451 /* no loader found? create a new entry */
454 le = NEW(hashtable_library_loader_entry);
459 /* insert entry into hashtable */
462 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
463 hashtable_library->ptr[slot] = le;
465 /* update number of hashtable-entries */
467 hashtable_library->entries++;
471 /* search for library name */
476 if (ne->name == filename) {
477 LOCK_MONITOR_EXIT(hashtable_library->header);
482 ne = ne->hashlink; /* next element in external chain */
485 /* not found? add the library name to the classloader */
487 ne = NEW(hashtable_library_name_entry);
492 /* insert entry into external chain */
494 ne->hashlink = le->namelink;
497 LOCK_MONITOR_EXIT(hashtable_library->header);
499 #endif /* !defined(WITH_STATIC_CLASSPATH) */
502 /* native_hashtable_library_find ***********************************************
504 Find an entry in the native library hashtable.
506 *******************************************************************************/
508 #if !defined(WITH_STATIC_CLASSPATH)
509 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
510 java_objectheader *loader)
512 hashtable_library_loader_entry *le;
513 hashtable_library_name_entry *ne; /* library name */
514 u4 key; /* hashkey */
515 u4 slot; /* slot in hashtable */
517 /* normally addresses are aligned to 4, 8 or 16 bytes */
519 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
520 slot = key & (hashtable_library->size - 1);
521 le = hashtable_library->ptr[slot];
523 /* search external hash chain for the entry */
526 if (le->loader == loader)
529 le = le->hashlink; /* next element in external chain */
532 /* no loader found? return NULL */
537 /* search for library name */
542 if (ne->name == filename)
545 ne = ne->hashlink; /* next element in external chain */
548 /* return entry, if no entry was found, ne is NULL */
552 #endif /* !defined(WITH_STATIC_CLASSPATH) */
555 /* native_findfunction *********************************************************
557 Looks up a method (must have the same class name, method name,
558 descriptor and 'static'ness) and returns a function pointer to it.
559 Returns: function pointer or NULL (if there is no such method)
561 Remark: For faster operation, the names/descriptors are converted
562 from C strings to Unicode the first time this function is called.
564 *******************************************************************************/
566 #if defined(WITH_STATIC_CLASSPATH)
567 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
570 /* entry of table for fast string comparison */
571 struct nativecompref *n;
574 isstatic = isstatic ? true : false;
576 if (!nativecompdone) {
577 for (i = 0; i < NATIVETABLESIZE; i++) {
578 nativecomptable[i].classname =
579 utf_new_char(nativetable[i].classname);
581 nativecomptable[i].methodname =
582 utf_new_char(nativetable[i].methodname);
584 nativecomptable[i].descriptor =
585 utf_new_char(nativetable[i].descriptor);
587 nativecomptable[i].isstatic = nativetable[i].isstatic;
588 nativecomptable[i].func = nativetable[i].func;
591 nativecompdone = true;
594 for (i = 0; i < NATIVETABLESIZE; i++) {
595 n = &(nativecomptable[i]);
597 if (cname == n->classname && mname == n->methodname &&
598 desc == n->descriptor && isstatic == n->isstatic)
602 /* no function was found, throw exception */
605 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
610 #endif /* defined(WITH_STATIC_CLASSPATH) */
613 /* native_make_overloaded_function *********************************************
617 *******************************************************************************/
619 #if !defined(WITH_STATIC_CLASSPATH)
620 static char *native_make_overloaded_function(char *name, utf *desc)
628 utf_ptr = desc->text;
629 namelen = strlen(name) + strlen("__") + strlen("0");
631 /* calculate additional length */
633 while ((c = utf_nextu2(&utf_ptr)) != ')') {
650 while (utf_nextu2(&utf_ptr) != ';')
662 /* reallocate memory */
666 newname = DMNEW(char, namelen);
667 MCOPY(newname, name, char, i);
669 utf_ptr = desc->text;
674 while ((c = utf_nextu2(&utf_ptr)) != ')') {
692 while ((c = utf_nextu2(&utf_ptr)) != ';')
693 if (((c >= 'a') && (c <= 'z')) ||
694 ((c >= 'A') && (c <= 'Z')) ||
695 ((c >= '0') && (c <= '9')))
717 /* native_insert_char **********************************************************
719 Inserts the passed UTF character into the native method name. If
720 necessary it is escaped properly.
722 *******************************************************************************/
724 static s4 native_insert_char(char *name, u4 pos, u2 c)
732 /* replace '/' or '.' with '_' */
737 /* escape sequence for '_' is '_1' */
743 /* escape sequence for ';' is '_2' */
749 /* escape sequence for '[' is '_1' */
758 /* unicode character */
762 for (i = 0; i < 4; ++i) {
764 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
773 /* return the new buffer index */
779 /* native_resolve_function *****************************************************
781 Resolves a native function, maybe from a dynamic library.
783 *******************************************************************************/
785 functionptr native_resolve_function(methodinfo *m)
796 hashtable_library_loader_entry *le;
797 hashtable_library_name_entry *ne;
798 u4 key; /* hashkey */
799 u4 slot; /* slot in hashtable */
803 if (opt_verbosejni) {
804 printf("[Dynamic-linking native method ");
805 utf_display_printable_ascii_classname(m->class->name);
807 utf_display_printable_ascii(m->name);
811 /* Calculate length of native function name. We multiply the
812 class and method name length by 6 as this is the maxium
813 escape-sequence that can be generated (unicode). */
815 namelen = strlen("Java_") +
816 utf_get_number_of_u2s(m->class->name) * 6 +
818 utf_get_number_of_u2s(m->name) * 6 +
821 /* allocate memory */
823 dumpsize = dump_size();
825 name = DMNEW(char, namelen);
827 /* generate name of native functions */
829 strcpy(name, "Java_");
830 pos = strlen("Java_");
832 utf_ptr = m->class->name->text;
833 utf_endptr = UTF_END(m->class->name);
835 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
837 pos = native_insert_char(name, pos, c);
840 /* seperator between class and method */
844 utf_ptr = m->name->text;
845 utf_endptr = UTF_END(m->name);
847 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
849 pos = native_insert_char(name, pos, c);
856 /* check for an buffer overflow */
858 assert(pos <= namelen);
860 /* generate overloaded function (having the types in it's name) */
862 newname = native_make_overloaded_function(name, m->descriptor);
864 /* check the library hash entries of the classloader of the
869 /* normally addresses are aligned to 4, 8 or 16 bytes */
871 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
872 slot = key & (hashtable_library->size - 1);
873 le = hashtable_library->ptr[slot];
875 /* iterate through loaders in this hash slot */
877 while ((le != NULL) && (sym == NULL)) {
878 /* iterate through names in this loader */
882 while ((ne != NULL) && (sym == NULL)) {
883 sym = lt_dlsym(ne->handle, name);
886 sym = lt_dlsym(ne->handle, newname);
899 /* If not found, try to find the native function symbol in the
903 sym = lt_dlsym(mainhandle, name);
906 sym = lt_dlsym(mainhandle, newname);
910 printf("internal ]\n");
914 #if defined(ENABLE_JVMTI)
915 /* fire Native Method Bind event */
916 if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
919 /* no symbol found? throw exception */
923 printf("failed ]\n");
925 #if defined(ENABLE_JAVASE)
927 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
929 #elif defined(ENABLE_JAVAME_CLDC1_1)
931 new_exception_utfmessage(string_java_lang_VirtualMachineError,
940 dump_release(dumpsize);
942 return (functionptr) (ptrint) sym;
944 #endif /* !defined(WITH_STATIC_CLASSPATH) */
947 /* native_new_and_init *********************************************************
949 Creates a new object on the heap and calls the initializer.
950 Returns the object pointer or NULL if memory is exhausted.
952 *******************************************************************************/
954 java_objectheader *native_new_and_init(classinfo *c)
957 java_objectheader *o;
960 return *exceptionptr;
969 /* try to find the initializer */
971 m = class_findmethod(c, utf_init, utf_void__void);
973 /* ATTENTION: returning the object here is ok, since the class may
974 not have an initializer */
979 /* call initializer */
981 (void) vm_call_method(m, o);
987 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
990 java_objectheader *o;
993 return *exceptionptr;
1002 /* find initializer */
1004 m = class_resolveclassmethod(c,
1006 utf_java_lang_String__void,
1010 /* initializer not found */
1015 /* call initializer */
1017 (void) vm_call_method(m, o, s);
1023 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1026 java_objectheader *o;
1029 return *exceptionptr;
1038 /* find initializer */
1040 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
1042 /* initializer not found */
1047 /* call initializer */
1049 (void) vm_call_method(m, o, i);
1055 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
1058 java_objectheader *o;
1061 return *exceptionptr;
1070 /* find initializer */
1072 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
1074 /* initializer not found */
1079 /* call initializer */
1081 (void) vm_call_method(m, o, t);
1087 /* native_class_getdeclaredannotations *****************************************
1090 java.lang.Class.getDeclaredAnnotations(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;
1092 *******************************************************************************/
1094 #if defined(ENABLE_JAVASE)
1095 java_objectarray *native_class_getdeclaredannotations(classinfo *c)
1097 java_objectarray *oa;
1101 classinfo *class_java_lang_annotation_Annotation;
1103 /* create Annotation-array */
1105 /* XXX should we cache that class? */
1106 if (!(class_java_lang_annotation_Annotation =
1107 load_class_bootstrap(utf_new_char("java/lang/annotation/Annotation"))))
1110 count = c->runtimevisibleannotationscount;
1112 oa = builtin_anewarray(count, class_java_lang_annotation_Annotation);
1117 /* fill the annotations */
1119 for (i = 0; i < count; i++) {
1127 /* native_get_parametertypes ***************************************************
1129 Use the descriptor of a method to generate a java/lang/Class array
1130 which contains the classes of the parametertypes of the method.
1132 *******************************************************************************/
1134 java_objectarray *native_get_parametertypes(methodinfo *m)
1137 typedesc *paramtypes;
1139 java_objectarray *oa;
1144 /* is the descriptor fully parsed? */
1146 if (!m->parseddesc->params)
1147 if (!descriptor_params_from_paramtypes(md, m->flags))
1150 paramtypes = md->paramtypes;
1151 paramcount = md->paramcount;
1153 /* skip `this' pointer */
1155 if (!(m->flags & ACC_STATIC)) {
1160 /* create class-array */
1162 oa = builtin_anewarray(paramcount, class_java_lang_Class);
1169 for (i = 0; i < paramcount; i++)
1170 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
1171 (classinfo **) &oa->data[i]))
1178 /* native_get_exceptiontypes ***************************************************
1180 Get the exceptions which can be thrown by a method.
1182 *******************************************************************************/
1184 java_objectarray *native_get_exceptiontypes(methodinfo *m)
1186 java_objectarray *oa;
1190 /* create class-array */
1192 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
1197 for (i = 0; i < m->thrownexceptionscount; i++) {
1198 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
1199 resolveEager, true, false, &c))
1202 oa->data[i] = (java_objectheader *) c;
1209 /* native_get_returntype *******************************************************
1211 Get the returntype class of a method.
1213 *******************************************************************************/
1215 classinfo *native_get_returntype(methodinfo *m)
1219 if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
1228 * These are local overrides for various environment variables in Emacs.
1229 * Please do not remove this and leave it at the end of the file, where
1230 * Emacs will automagically detect them.
1231 * ---------------------------------------------------------------------
1234 * indent-tabs-mode: t