* src/native/vm/sun/jvm.c (JVM_GetEnclosingMethodInfo): Implemented.
[cacao.git] / src / native / vm / sun / jvm.c
1 /* src/native/vm/sun/jvm.c - HotSpot JVM interface functions
2
3    Copyright (C) 2007 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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #define PRINTJVM 0
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <ltdl.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #if defined(HAVE_SYS_IOCTL_H)
42 #define BSD_COMP /* Get FIONREAD on Solaris2 */
43 #include <sys/ioctl.h>
44 #endif
45
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/types.h>
49
50 #include "vm/types.h"
51
52 #include "mm/memory.h"
53
54 #include "native/jni.h"
55 #include "native/llni.h"
56 #include "native/native.h"
57
58 #include "native/include/java_lang_AssertionStatusDirectives.h"
59 #include "native/include/java_lang_String.h"            /* required by j.l.CL */
60 #include "native/include/java_nio_ByteBuffer.h"         /* required by j.l.CL */
61 #include "native/include/java_lang_ClassLoader.h"        /* required by j.l.C */
62 #include "native/include/java_lang_StackTraceElement.h"
63 #include "native/include/java_lang_Throwable.h"
64 #include "native/include/java_security_ProtectionDomain.h"
65 #include "native/include/java_lang_Integer.h"
66 #include "native/include/java_lang_Long.h"
67 #include "native/include/java_lang_Short.h"
68 #include "native/include/java_lang_Byte.h"
69 #include "native/include/java_lang_Character.h"
70 #include "native/include/java_lang_Boolean.h"
71 #include "native/include/java_lang_Float.h"
72 #include "native/include/java_lang_Double.h"
73
74 #if defined(ENABLE_ANNOTATIONS)
75 #include "native/include/sun_reflect_ConstantPool.h"
76 #endif
77
78 #include "native/vm/java_lang_Class.h"
79 #include "native/vm/java_lang_ClassLoader.h"
80 #include "native/vm/java_lang_Runtime.h"
81 #include "native/vm/java_lang_Thread.h"
82 #include "native/vm/java_lang_reflect_Constructor.h"
83 #include "native/vm/java_lang_reflect_Method.h"
84 #include "native/vm/reflect.h"
85
86 #include "threads/lock-common.h"
87 #include "threads/threads-common.h"
88
89 #include "toolbox/logging.h"
90 #include "toolbox/list.h"
91
92 #include "vm/array.h"
93
94 #if defined(ENABLE_ASSERTION)
95 #include "vm/assertion.h"
96 #endif
97
98 #include "vm/builtin.h"
99 #include "vm/exceptions.h"
100 #include "vm/global.h"
101 #include "vm/initialize.h"
102 #include "vm/package.h"
103 #include "vm/primitive.h"
104 #include "vm/properties.h"
105 #include "vm/resolve.h"
106 #include "vm/signallocal.h"
107 #include "vm/stringlocal.h"
108 #include "vm/vm.h"
109
110 #include "vm/jit/stacktrace.h"
111
112 #include "vmcore/classcache.h"
113 #include "vmcore/options.h"
114 #include "vmcore/system.h"
115
116
117 /* debugging macros ***********************************************************/
118
119 #if !defined(NDEBUG)
120
121 # define TRACEJVMCALLS(...)                                             \
122     do {                                                                                \
123         if (opt_TraceJVMCalls) {                                \
124             log_println(__VA_ARGS__);                   \
125         }                                                                               \
126     } while (0)
127
128 # define PRINTJVMWARNINGS(...)
129 /*     do { \ */
130 /*         if (opt_PrintJVMWarnings) { \ */
131 /*             log_println(__VA_ARGS__); \ */
132 /*         } \ */
133 /*     } while (0) */
134
135 #else
136
137 # define TRACEJVMCALLS(...)
138 # define PRINTJVMWARNINGS(...)
139
140 #endif
141
142
143 typedef struct {
144     /* Naming convention of RE build version string: n.n.n[_uu[c]][-<identifier>]-bxx */
145     unsigned int jvm_version;   /* Consists of major, minor, micro (n.n.n) */
146                                 /* and build number (xx) */
147     unsigned int update_version : 8;         /* Update release version (uu) */
148     unsigned int special_update_version : 8; /* Special update release version (c) */
149     unsigned int reserved1 : 16; 
150     unsigned int reserved2; 
151
152     /* The following bits represents JVM supports that JDK has dependency on.
153      * JDK can use these bits to determine which JVM version
154      * and support it has to maintain runtime compatibility.
155      *
156      * When a new bit is added in a minor or update release, make sure
157      * the new bit is also added in the main/baseline.
158      */
159     unsigned int is_attachable : 1;
160     unsigned int : 31;
161     unsigned int : 32;
162     unsigned int : 32;
163 } jvm_version_info;
164
165
166 /*
167  * A structure used to a capture exception table entry in a Java method.
168  */
169 typedef struct {
170     jint start_pc;
171     jint end_pc;
172     jint handler_pc;
173     jint catchType;
174 } JVM_ExceptionTableEntryType;
175
176
177 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
178 {
179         if ((intptr_t) count <= 0)
180                 return -1;
181
182         return vsnprintf(str, count, fmt, args);
183 }
184
185
186 int jio_snprintf(char *str, size_t count, const char *fmt, ...)
187 {
188         va_list ap;
189         int     len;
190
191         va_start(ap, fmt);
192         len = jio_vsnprintf(str, count, fmt, ap);
193         va_end(ap);
194
195         return len;
196 }
197
198
199 int jio_fprintf(FILE* f, const char *fmt, ...)
200 {
201         log_println("jio_fprintf: IMPLEMENT ME!");
202 }
203
204
205 int jio_vfprintf(FILE* f, const char *fmt, va_list args)
206 {
207         log_println("jio_vfprintf: IMPLEMENT ME!");
208 }
209
210
211 int jio_printf(const char *fmt, ...)
212 {
213         log_println("jio_printf: IMPLEMENT ME!");
214 }
215
216
217 /* JVM_GetInterfaceVersion */
218
219 jint JVM_GetInterfaceVersion()
220 {
221         /* This is defined in hotspot/src/share/vm/prims/jvm.h */
222
223 #define JVM_INTERFACE_VERSION 4
224
225         return JVM_INTERFACE_VERSION;
226 }
227
228
229 /* JVM_CurrentTimeMillis */
230
231 jlong JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored)
232 {
233 #if PRINTJVM
234         log_println("JVM_CurrentTimeMillis");
235 #endif
236         return (jlong) builtin_currenttimemillis();
237 }
238
239
240 /* JVM_NanoTime */
241
242 jlong JVM_NanoTime(JNIEnv *env, jclass ignored)
243 {
244 #if PRINTJVM
245         log_println("JVM_NanoTime");
246 #endif
247         return (jlong) builtin_nanotime();
248 }
249
250
251 /* JVM_ArrayCopy */
252
253 void JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, jobject dst, jint dst_pos, jint length)
254 {
255         java_handle_t *s;
256         java_handle_t *d;
257
258         s = (java_handle_t *) src;
259         d = (java_handle_t *) dst;
260
261 #if PRINTJVM
262         log_println("JVM_ArrayCopy: src=%p, src_pos=%d, dst=%p, dst_pos=%d, length=%d", src, src_pos, dst, dst_pos, length);
263 #endif
264
265         builtin_arraycopy(s, src_pos, d, dst_pos, length);
266 }
267
268
269 /* JVM_InitProperties */
270
271 jobject JVM_InitProperties(JNIEnv *env, jobject properties)
272 {
273         java_handle_t *h;
274
275         TRACEJVMCALLS("JVM_InitProperties(env=%p, properties=%p)", env, properties);
276
277         h = (java_handle_t *) properties;
278
279         properties_system_add_all(h);
280
281         return properties;
282 }
283
284
285 /* JVM_Exit */
286
287 void JVM_Exit(jint code)
288 {
289         log_println("JVM_Exit: IMPLEMENT ME!");
290 }
291
292
293 /* JVM_Halt */
294
295 void JVM_Halt(jint code)
296 {
297 #if PRINTJVM
298         log_println("JVM_Halt: code=%d", code);
299 #endif
300 /*      vm_exit(code); */
301         vm_shutdown(code);
302 }
303
304
305 /* JVM_OnExit(void (*func)) */
306
307 void JVM_OnExit(void (*func)(void))
308 {
309         log_println("JVM_OnExit(void (*func): IMPLEMENT ME!");
310 }
311
312
313 /* JVM_GC */
314
315 void JVM_GC(void)
316 {
317         TRACEJVMCALLS("JVM_GC()");
318
319         gc_call();
320 }
321
322
323 /* JVM_MaxObjectInspectionAge */
324
325 jlong JVM_MaxObjectInspectionAge(void)
326 {
327         log_println("JVM_MaxObjectInspectionAge: IMPLEMENT ME!");
328 }
329
330
331 /* JVM_TraceInstructions */
332
333 void JVM_TraceInstructions(jboolean on)
334 {
335         log_println("JVM_TraceInstructions: IMPLEMENT ME!");
336 }
337
338
339 /* JVM_TraceMethodCalls */
340
341 void JVM_TraceMethodCalls(jboolean on)
342 {
343         log_println("JVM_TraceMethodCalls: IMPLEMENT ME!");
344 }
345
346
347 /* JVM_TotalMemory */
348
349 jlong JVM_TotalMemory(void)
350 {
351         TRACEJVMCALLS("JVM_TotalMemory()");
352
353         return gc_get_heap_size();
354 }
355
356
357 /* JVM_FreeMemory */
358
359 jlong JVM_FreeMemory(void)
360 {
361         TRACEJVMCALLS("JVM_FreeMemory()");
362
363         return gc_get_free_bytes();
364 }
365
366
367 /* JVM_MaxMemory */
368
369 jlong JVM_MaxMemory(void)
370 {
371         TRACEJVMCALLS("JVM_MaxMemory()");
372
373         return gc_get_max_heap_size();
374 }
375
376
377 /* JVM_ActiveProcessorCount */
378
379 jint JVM_ActiveProcessorCount(void)
380 {
381         TRACEJVMCALLS("JVM_ActiveProcessorCount()");
382
383         return system_processors_online();
384 }
385
386
387 /* JVM_FillInStackTrace */
388
389 void JVM_FillInStackTrace(JNIEnv *env, jobject receiver)
390 {
391         java_lang_Throwable     *o;
392         java_handle_bytearray_t *ba;
393
394         TRACEJVMCALLS("JVM_FillInStackTrace(env=%p, receiver=%p)", env, receiver);
395
396         o = (java_lang_Throwable *) receiver;
397
398         ba = stacktrace_get();
399
400         if (ba == NULL)
401                 return;
402
403         LLNI_field_set_ref(o, backtrace, ba);
404 }
405
406
407 /* JVM_PrintStackTrace */
408
409 void JVM_PrintStackTrace(JNIEnv *env, jobject receiver, jobject printable)
410 {
411         log_println("JVM_PrintStackTrace: IMPLEMENT ME!");
412 }
413
414
415 /* JVM_GetStackTraceDepth */
416
417 jint JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)
418 {
419         java_lang_Throwable     *t;
420         java_handle_bytearray_t *ba;
421         stacktrace_t            *st;
422         int32_t                  depth;
423
424         TRACEJVMCALLS("JVM_GetStackTraceDepth(env=%p, throwable=%p)", env, throwable);
425
426         if (throwable == NULL) {
427                 exceptions_throw_nullpointerexception();
428                 return 0;
429         }
430
431         t = (java_lang_Throwable *) throwable;
432
433         LLNI_field_get_ref(t, backtrace, ba);
434
435         if (ba == NULL)
436                 return 0;
437
438         /* We need a critical section here as the stacktrace structure is
439            mapped onto a Java byte-array. */
440
441         LLNI_CRITICAL_START;
442
443         st = (stacktrace_t *) LLNI_array_data(ba);
444
445         depth = st->length;
446
447         LLNI_CRITICAL_END;
448
449         return depth;
450 }
451
452
453 /* JVM_GetStackTraceElement */
454
455 jobject JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)
456 {
457         java_lang_Throwable         *t;
458         java_handle_bytearray_t     *ba;
459         stacktrace_t                *st;
460         stacktrace_entry_t          *ste;
461         codeinfo                    *code;
462         methodinfo                  *m;
463         classinfo                   *c;
464         java_lang_StackTraceElement *o;
465         java_lang_String            *declaringclass;
466         java_lang_String            *filename;
467         int32_t                      linenumber;
468
469         TRACEJVMCALLS("JVM_GetStackTraceElement(env=%p, throwable=%p, index=%d)", env, throwable, index);
470
471         t = (java_lang_Throwable *) throwable;
472
473         LLNI_field_get_ref(t, backtrace, ba);
474
475         /* FIXME critical section */
476
477         st = (stacktrace_t *) LLNI_array_data(ba);
478
479         if ((index < 0) || (index >= st->length)) {
480                 /* XXX This should be an IndexOutOfBoundsException (check this
481                    again). */
482
483                 exceptions_throw_arrayindexoutofboundsexception();
484                 return NULL;
485         }
486
487         /* Get the stacktrace entry. */
488
489         ste = &(st->entries[index]);
490
491         /* Get the codeinfo, methodinfo and classinfo. */
492
493         code = ste->code;
494         m    = code->m;
495         c    = m->class;
496
497         /* allocate a new StackTraceElement */
498
499         o = (java_lang_StackTraceElement *)
500                 builtin_new(class_java_lang_StackTraceElement);
501
502         if (o == NULL)
503                 return NULL;
504
505         /* get filename */
506
507         if (!(m->flags & ACC_NATIVE)) {
508                 if (c->sourcefile != NULL)
509                         filename = (java_lang_String *) javastring_new(c->sourcefile);
510                 else
511                         filename = NULL;
512         }
513         else
514                 filename = NULL;
515
516         /* get line number */
517
518         if (m->flags & ACC_NATIVE) {
519                 linenumber = -2;
520         }
521         else {
522                 /* FIXME The linenumbertable_linenumber_for_pc could change
523                    the methodinfo pointer when hitting an inlined method. */
524
525                 linenumber = linenumbertable_linenumber_for_pc(&m, code, ste->pc);
526                 linenumber = (linenumber == 0) ? -1 : linenumber;
527         }
528
529         /* get declaring class name */
530
531         declaringclass = _Jv_java_lang_Class_getName(LLNI_classinfo_wrap(c));
532
533         /* fill the java.lang.StackTraceElement element */
534
535         /* FIXME critical section */
536
537         o->declaringClass = declaringclass;
538         o->methodName     = (java_lang_String *) javastring_new(m->name);
539         o->fileName       = filename;
540         o->lineNumber     = linenumber;
541
542         return (jobject) o;
543 }
544
545
546 /* JVM_IHashCode */
547
548 jint JVM_IHashCode(JNIEnv* env, jobject handle)
549 {
550         TRACEJVMCALLS("JVM_IHashCode(env=%p, jobject=%p)", env, handle);
551
552         return (jint) ((ptrint) handle);
553 }
554
555
556 /* JVM_MonitorWait */
557
558 void JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)
559 {
560 #if defined(ENABLE_THREADS)
561         java_handle_t *o;
562 #endif
563
564         TRACEJVMCALLS("JVM_MonitorWait(env=%p, handle=%p, ms=%ld)", env, handle, ms);
565     if (ms < 0) {
566 /*              exceptions_throw_illegalargumentexception("argument out of range"); */
567                 exceptions_throw_illegalargumentexception();
568                 return;
569         }
570
571 #if defined(ENABLE_THREADS)
572         o = (java_handle_t *) handle;
573
574         lock_wait_for_object(o, ms, 0);
575 #endif
576 }
577
578
579 /* JVM_MonitorNotify */
580
581 void JVM_MonitorNotify(JNIEnv* env, jobject handle)
582 {
583 #if defined(ENABLE_THREADS)
584         java_handle_t *o;
585 #endif
586
587         TRACEJVMCALLS("JVM_MonitorNotify(env=%p, handle=%p)", env, handle);
588
589 #if defined(ENABLE_THREADS)
590         o = (java_handle_t *) handle;
591
592         lock_notify_object(o);
593 #endif
594 }
595
596
597 /* JVM_MonitorNotifyAll */
598
599 void JVM_MonitorNotifyAll(JNIEnv* env, jobject handle)
600 {
601 #if defined(ENABLE_THREADS)
602         java_handle_t *o;
603 #endif
604
605         TRACEJVMCALLS("JVM_MonitorNotifyAll(env=%p, handle=%p)", env, handle);
606
607 #if defined(ENABLE_THREADS)
608         o = (java_handle_t *) handle;
609
610         lock_notify_all_object(o);
611 #endif
612 }
613
614
615 /* JVM_Clone */
616
617 jobject JVM_Clone(JNIEnv* env, jobject handle)
618 {
619 #if PRINTJVM
620         log_println("JVM_Clone: handle=%p", handle);
621 #endif
622         return (jobject) builtin_clone(env, (java_handle_t *) handle);
623 }
624
625
626 /* JVM_InitializeCompiler  */
627
628 void JVM_InitializeCompiler (JNIEnv *env, jclass compCls)
629 {
630         log_println("JVM_InitializeCompiler : IMPLEMENT ME!");
631 }
632
633
634 /* JVM_IsSilentCompiler */
635
636 jboolean JVM_IsSilentCompiler(JNIEnv *env, jclass compCls)
637 {
638         log_println("JVM_IsSilentCompiler: IMPLEMENT ME!");
639 }
640
641
642 /* JVM_CompileClass */
643
644 jboolean JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls)
645 {
646         log_println("JVM_CompileClass: IMPLEMENT ME!");
647 }
648
649
650 /* JVM_CompileClasses */
651
652 jboolean JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname)
653 {
654         log_println("JVM_CompileClasses: IMPLEMENT ME!");
655 }
656
657
658 /* JVM_CompilerCommand */
659
660 jobject JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg)
661 {
662         log_println("JVM_CompilerCommand: IMPLEMENT ME!");
663 }
664
665
666 /* JVM_EnableCompiler */
667
668 void JVM_EnableCompiler(JNIEnv *env, jclass compCls)
669 {
670         TRACEJVMCALLS("JVM_EnableCompiler(env=%p, compCls=%p)", env, compCls);
671         PRINTJVMWARNINGS("JVM_EnableCompiler not supported");
672 }
673
674
675 /* JVM_DisableCompiler */
676
677 void JVM_DisableCompiler(JNIEnv *env, jclass compCls)
678 {
679         TRACEJVMCALLS("JVM_DisableCompiler(env=%p, compCls=%p)", env, compCls);
680         PRINTJVMWARNINGS("JVM_DisableCompiler not supported");
681 }
682
683
684 /* JVM_GetLastErrorString */
685
686 jint JVM_GetLastErrorString(char *buf, int len)
687 {
688         const char *s;
689         int n;
690
691     if (errno == 0) {
692                 return 0;
693     }
694         else {
695                 s = strerror(errno);
696                 n = strlen(s);
697
698                 if (n >= len)
699                         n = len - 1;
700
701                 strncpy(buf, s, n);
702
703                 buf[n] = '\0';
704
705                 return n;
706     }
707 }
708
709
710 /* JVM_NativePath */
711
712 char *JVM_NativePath(char *path)
713 {
714         TRACEJVMCALLS("JVM_NativePath(path=%s)", path);
715
716         /* XXX is this correct? */
717
718         return path;
719 }
720
721
722 /* JVM_GetCallerClass */
723
724 jclass JVM_GetCallerClass(JNIEnv* env, int depth)
725 {
726         java_handle_objectarray_t *oa;
727
728         TRACEJVMCALLS("JVM_GetCallerClass(env=%p, depth=%d)", env, depth);
729
730         oa = stacktrace_getClassContext();
731
732         if (oa == NULL)
733                 return NULL;
734
735         if (oa->header.size < depth)
736                 return NULL;
737
738         return (jclass) oa->data[depth - 1];
739
740 }
741
742
743 /* JVM_FindPrimitiveClass */
744
745 jclass JVM_FindPrimitiveClass(JNIEnv* env, const char* s)
746 {
747         classinfo *c;
748         utf       *u;
749
750         TRACEJVMCALLS("JVM_FindPrimitiveClass(env=%p, s=%s)", env, s);
751
752         u = utf_new_char(s);
753         c = primitive_class_get_by_name(u);
754
755         return (jclass) LLNI_classinfo_wrap(c);
756 }
757
758
759 /* JVM_ResolveClass */
760
761 void JVM_ResolveClass(JNIEnv* env, jclass cls)
762 {
763         TRACEJVMCALLS("JVM_ResolveClass(env=%p, cls=%p)", env, cls);
764         PRINTJVMWARNINGS("JVM_ResolveClass not implemented");
765 }
766
767
768 /* JVM_FindClassFromClassLoader */
769
770 jclass JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, jboolean init, jobject loader, jboolean throwError)
771 {
772         classinfo   *c;
773         utf         *u;
774         classloader *cl;
775
776         TRACEJVMCALLS("JVM_FindClassFromClassLoader: name=%s, init=%d, loader=%p, throwError=%d", name, init, loader, throwError);
777
778         u  = utf_new_char(name);
779         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
780
781         c = load_class_from_classloader(u, cl);
782
783         if (c == NULL)
784                 return NULL;
785
786         if (init)
787                 if (!(c->state & CLASS_INITIALIZED))
788                         if (!initialize_class(c))
789                                 return NULL;
790
791         return (jclass) LLNI_classinfo_wrap(c);
792 }
793
794
795 /* JVM_FindClassFromClass */
796
797 jclass JVM_FindClassFromClass(JNIEnv *env, const char *name, jboolean init, jclass from)
798 {
799         log_println("JVM_FindClassFromClass: IMPLEMENT ME!");
800 }
801
802
803 /* JVM_DefineClass */
804
805 jclass JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd)
806 {
807         log_println("JVM_DefineClass: IMPLEMENT ME!");
808 }
809
810
811 /* JVM_DefineClassWithSource */
812
813 jclass JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source)
814 {
815         classinfo   *c;
816         utf         *u;
817         classloader *cl;
818
819         TRACEJVMCALLS("JVM_DefineClassWithSource(env=%p, name=%s, loader=%p, buf=%p, len=%d, pd=%p, source=%s)", env, name, loader, buf, len, pd, source);
820
821         if (name != NULL)
822                 u = utf_new_char(name);
823         else
824                 u = NULL;
825
826         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
827
828         /* XXX do something with source */
829
830         c = class_define(u, cl, len, (const uint8_t *) buf, (java_handle_t *) pd);
831
832         return (jclass) LLNI_classinfo_wrap(c);
833 }
834
835
836 /* JVM_FindLoadedClass */
837
838 jclass JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name)
839 {
840         classloader *cl;
841         utf         *u;
842         classinfo   *c;
843
844         TRACEJVMCALLS("JVM_FindLoadedClass(env=%p, loader=%p, name=%p)", env, loader, name);
845
846         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
847
848         u = javastring_toutf((java_handle_t *) name, true);
849         c = classcache_lookup(cl, u);
850
851         return (jclass) LLNI_classinfo_wrap(c);
852 }
853
854
855 /* JVM_GetClassName */
856
857 jstring JVM_GetClassName(JNIEnv *env, jclass cls)
858 {
859 #if PRINTJVM
860         log_println("JVM_GetClassName: cls=%p", cls);
861 #endif
862         return (jstring) _Jv_java_lang_Class_getName((java_lang_Class *) cls);
863 }
864
865
866 /* JVM_GetClassInterfaces */
867
868 jobjectArray JVM_GetClassInterfaces(JNIEnv *env, jclass cls)
869 {
870         classinfo                 *c;
871         java_handle_objectarray_t *oa;
872
873         TRACEJVMCALLS("JVM_GetClassInterfaces(env=%p, cls=%p)", env, cls);
874
875         c = LLNI_classinfo_unwrap(cls);
876
877         oa = class_get_interfaces(c);
878
879         return (jobjectArray) oa;
880 }
881
882
883 /* JVM_GetClassLoader */
884
885 jobject JVM_GetClassLoader(JNIEnv *env, jclass cls)
886 {
887         classinfo   *c;
888         classloader *cl;
889
890         TRACEJVMCALLS("JVM_GetClassLoader(env=%p, cls=%p)", env, cls);
891
892         c  = LLNI_classinfo_unwrap(cls);
893         cl = class_get_classloader(c);
894
895         return (jobject) cl;
896 }
897
898
899 /* JVM_IsInterface */
900
901 jboolean JVM_IsInterface(JNIEnv *env, jclass cls)
902 {
903         classinfo *c;
904
905 #if PRINTJVM
906         log_println("JVM_IsInterface: cls=%p", cls);
907 #endif
908
909         c = LLNI_classinfo_unwrap(cls);
910
911         return class_is_interface(c);
912 }
913
914
915 /* JVM_GetClassSigners */
916
917 jobjectArray JVM_GetClassSigners(JNIEnv *env, jclass cls)
918 {
919         log_println("JVM_GetClassSigners: IMPLEMENT ME!");
920 }
921
922
923 /* JVM_SetClassSigners */
924
925 void JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)
926 {
927         classinfo                 *c;
928         java_handle_objectarray_t *hoa;
929
930         TRACEJVMCALLS("JVM_SetClassSigners(env=%p, cls=%p, signers=%p)", env, cls, signers);
931
932         c = LLNI_classinfo_unwrap(cls);
933
934         hoa = (java_handle_objectarray_t *) signers;
935
936     /* This call is ignored for primitive types and arrays.  Signers
937            are only set once, ClassLoader.java, and thus shouldn't be
938            called with an array.  Only the bootstrap loader creates
939            arrays. */
940
941         if (class_is_primitive(c) || class_is_array(c))
942                 return;
943
944         LLNI_classinfo_field_set(c, signers, hoa);
945 }
946
947
948 /* JVM_GetProtectionDomain */
949
950 jobject JVM_GetProtectionDomain(JNIEnv *env, jclass cls)
951 {
952         classinfo *c;
953
954         TRACEJVMCALLS("JVM_GetProtectionDomain(env=%p, cls=%p)", env, cls);
955
956         c = LLNI_classinfo_unwrap(cls);
957
958         if (c == NULL) {
959                 exceptions_throw_nullpointerexception();
960                 return NULL;
961         }
962
963     /* Primitive types do not have a protection domain. */
964
965         if (class_is_primitive(c))
966                 return NULL;
967
968         return (jobject) c->protectiondomain;
969 }
970
971
972 /* JVM_SetProtectionDomain */
973
974 void JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain)
975 {
976         log_println("JVM_SetProtectionDomain: IMPLEMENT ME!");
977 }
978
979
980 /* JVM_DoPrivileged */
981
982 jobject JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException)
983 {
984         java_handle_t *o;
985         classinfo     *c;
986         methodinfo    *m;
987         java_handle_t *result;
988         java_handle_t *e;
989
990 #if PRINTJVM
991         log_println("JVM_DoPrivileged: action=%p, context=%p, wrapException=%d", action, context, wrapException);
992 #endif
993
994         o = (java_handle_t *) action;
995         c = o->vftbl->class;
996
997         if (action == NULL) {
998                 exceptions_throw_nullpointerexception();
999                 return NULL;
1000         }
1001
1002         /* lookup run() method (throw no exceptions) */
1003
1004         m = class_resolveclassmethod(c, utf_run, utf_void__java_lang_Object, c,
1005                                                                  false);
1006
1007         if ((m == NULL) || !(m->flags & ACC_PUBLIC) || (m->flags & ACC_STATIC)) {
1008                 exceptions_throw_internalerror("No run method");
1009                 return NULL;
1010         }
1011
1012         /* XXX It seems something with a privileged stack needs to be done
1013            here. */
1014
1015         result = vm_call_method(m, o);
1016
1017         e = exceptions_get_and_clear_exception();
1018
1019         if (e != NULL) {
1020                 exceptions_throw_privilegedactionexception(e);
1021                 return NULL;
1022         }
1023
1024         return (jobject) result;
1025 }
1026
1027
1028 /* JVM_GetInheritedAccessControlContext */
1029
1030 jobject JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)
1031 {
1032         log_println("JVM_GetInheritedAccessControlContext: IMPLEMENT ME!");
1033 }
1034
1035
1036 /* JVM_GetStackAccessControlContext */
1037
1038 jobject JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)
1039 {
1040         TRACEJVMCALLS("JVM_GetStackAccessControlContext(env=%p, cls=%p): IMPLEMENT ME!", env, cls);
1041
1042         /* XXX All stuff I tested so far works without that function.  At
1043            some point we have to implement it, but I disable the output
1044            for now to make IcedTea happy. */
1045
1046         return NULL;
1047 }
1048
1049
1050 /* JVM_IsArrayClass */
1051
1052 jboolean JVM_IsArrayClass(JNIEnv *env, jclass cls)
1053 {
1054 #if PRINTJVM
1055         log_println("JVM_IsArrayClass: cls=%p", cls);
1056 #endif
1057         return class_is_array(LLNI_classinfo_unwrap(cls));
1058 }
1059
1060
1061 /* JVM_IsPrimitiveClass */
1062
1063 jboolean JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)
1064 {
1065         classinfo *c;
1066
1067         c = LLNI_classinfo_unwrap(cls);
1068
1069 #if PRINTJVM
1070         log_println("JVM_IsPrimitiveClass(cls=%p)", cls);
1071 #endif
1072
1073         return class_is_primitive(c);
1074 }
1075
1076
1077 /* JVM_GetComponentType */
1078
1079 jclass JVM_GetComponentType(JNIEnv *env, jclass cls)
1080 {
1081         classinfo *component;
1082         classinfo *c;
1083         
1084         TRACEJVMCALLS("JVM_GetComponentType(env=%p, cls=%p)", env, cls);
1085
1086         c = LLNI_classinfo_unwrap(cls);
1087         
1088         component = class_get_componenttype(c);
1089
1090         return (jclass) LLNI_classinfo_wrap(component);
1091 }
1092
1093
1094 /* JVM_GetClassModifiers */
1095
1096 jint JVM_GetClassModifiers(JNIEnv *env, jclass cls)
1097 {
1098         classinfo *c;
1099         int32_t    flags;
1100
1101         TRACEJVMCALLS("JVM_GetClassModifiers(env=%p, cls=%p)", env, cls);
1102
1103         c = LLNI_classinfo_unwrap(cls);
1104
1105         flags = class_get_modifiers(c, false);
1106
1107         return flags;
1108 }
1109
1110
1111 /* JVM_GetDeclaredClasses */
1112
1113 jobjectArray JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)
1114 {
1115         classinfo                 *c;
1116         java_handle_objectarray_t *oa;
1117
1118         TRACEJVMCALLS("JVM_GetDeclaredClasses(env=%p, ofClass=%p)", env, ofClass);
1119
1120         c = LLNI_classinfo_unwrap(ofClass);
1121
1122         oa = class_get_declaredclasses(c, false);
1123
1124         return (jobjectArray) oa;
1125 }
1126
1127
1128 /* JVM_GetDeclaringClass */
1129
1130 jclass JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)
1131 {
1132         classinfo *c;
1133         classinfo *dc;
1134
1135         TRACEJVMCALLS("JVM_GetDeclaringClass(env=%p, ofClass=%p)", env, ofClass);
1136
1137         c = LLNI_classinfo_unwrap(ofClass);
1138
1139         dc = class_get_declaringclass(c);
1140
1141         return (jclass) LLNI_classinfo_wrap(dc);
1142 }
1143
1144
1145 /* JVM_GetClassSignature */
1146
1147 jstring JVM_GetClassSignature(JNIEnv *env, jclass cls)
1148 {
1149         classinfo     *c;
1150         utf           *u;
1151         java_handle_t *s;
1152
1153         TRACEJVMCALLS("JVM_GetClassSignature(env=%p, cls=%p)", env, cls);
1154
1155         c = LLNI_classinfo_unwrap(cls);
1156
1157         /* Get the signature of the class. */
1158
1159         u = class_get_signature(c);
1160
1161         if (u == NULL)
1162                 return NULL;
1163
1164         /* Convert UTF-string to a Java-string. */
1165
1166         s = javastring_new(u);
1167
1168         return (jstring) s;
1169 }
1170
1171
1172 /* JVM_GetClassAnnotations */
1173
1174 jbyteArray JVM_GetClassAnnotations(JNIEnv *env, jclass cls)
1175 {
1176         classinfo               *c           = NULL; /* classinfo for 'cls'  */
1177         java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
1178
1179         TRACEJVMCALLS("JVM_GetClassAnnotations: cls=%p", cls);
1180
1181         if (cls == NULL) {
1182                 exceptions_throw_nullpointerexception();
1183                 return NULL;
1184         }
1185         
1186         c = LLNI_classinfo_unwrap(cls);
1187
1188         /* get annotations: */
1189         annotations = class_get_annotations(c);
1190
1191         return (jbyteArray)annotations;
1192 }
1193
1194
1195 /* JVM_GetFieldAnnotations */
1196
1197 jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
1198 {
1199         java_lang_reflect_Field *rf = NULL; /* java.lang.reflect.Field for 'field' */
1200         java_handle_bytearray_t *ba = NULL; /* unparsed annotations */
1201
1202         TRACEJVMCALLS("JVM_GetFieldAnnotations: field=%p", field);
1203
1204         if (field == NULL) {
1205                 exceptions_throw_nullpointerexception();
1206                 return NULL;
1207         }
1208
1209         rf = (java_lang_reflect_Field*)field;
1210
1211         LLNI_field_get_ref(rf, annotations, ba);
1212
1213         return (jbyteArray)ba;
1214 }
1215
1216
1217 /* JVM_GetMethodAnnotations */
1218
1219 jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
1220 {
1221         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1222         java_handle_bytearray_t  *ba = NULL; /* unparsed annotations */
1223
1224         TRACEJVMCALLS("JVM_GetMethodAnnotations: method=%p", method);
1225
1226         if (method == NULL) {
1227                 exceptions_throw_nullpointerexception();
1228                 return NULL;
1229         }
1230
1231         rm = (java_lang_reflect_Method*)method;
1232
1233         LLNI_field_get_ref(rm, annotations, ba);
1234
1235         return (jbyteArray)ba;
1236 }
1237
1238
1239 /* JVM_GetMethodDefaultAnnotationValue */
1240
1241 jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
1242 {
1243         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1244         java_handle_bytearray_t  *ba = NULL; /* unparsed annotation default value */
1245
1246         TRACEJVMCALLS("JVM_GetMethodDefaultAnnotationValue: method=%p", method);
1247
1248         if (method == NULL) {
1249                 exceptions_throw_nullpointerexception();
1250                 return NULL;
1251         }
1252
1253         rm = (java_lang_reflect_Method*)method;
1254
1255         LLNI_field_get_ref(rm, annotationDefault, ba);
1256
1257         return (jbyteArray)ba;
1258 }
1259
1260
1261 /* JVM_GetMethodParameterAnnotations */
1262
1263 jbyteArray JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)
1264 {
1265         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1266         java_handle_bytearray_t  *ba = NULL; /* unparsed parameter annotations */
1267
1268         TRACEJVMCALLS("JVM_GetMethodParameterAnnotations: method=%p", method);
1269
1270         if (method == NULL) {
1271                 exceptions_throw_nullpointerexception();
1272                 return NULL;
1273         }
1274
1275         rm = (java_lang_reflect_Method*)method;
1276
1277         LLNI_field_get_ref(rm, parameterAnnotations, ba);
1278
1279         return (jbyteArray)ba;
1280 }
1281
1282
1283 /* JVM_GetClassDeclaredFields */
1284
1285 jobjectArray JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1286 {
1287 #if PRINTJVM
1288         log_println("JVM_GetClassDeclaredFields: ofClass=%p, publicOnly=%d", ofClass, publicOnly);
1289 #endif
1290         return (jobjectArray) _Jv_java_lang_Class_getDeclaredFields((java_lang_Class *) ofClass, publicOnly);
1291 }
1292
1293
1294 /* JVM_GetClassDeclaredMethods */
1295
1296 jobjectArray JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1297 {
1298 #if PRINTJVM
1299         log_println("JVM_GetClassDeclaredMethods: ofClass=%p, publicOnly=%d", ofClass, publicOnly);
1300 #endif
1301         return (jobjectArray) _Jv_java_lang_Class_getDeclaredMethods((java_lang_Class *) ofClass, publicOnly);
1302 }
1303
1304
1305 /* JVM_GetClassDeclaredConstructors */
1306
1307 jobjectArray JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1308 {
1309 #if PRINTJVM
1310         log_println("JVM_GetClassDeclaredConstructors: ofClass=%p, publicOnly=%d", ofClass, publicOnly);
1311 #endif
1312         return (jobjectArray) _Jv_java_lang_Class_getDeclaredConstructors((java_lang_Class *) ofClass, publicOnly);
1313 }
1314
1315
1316 /* JVM_GetClassAccessFlags */
1317
1318 jint JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)
1319 {
1320         classinfo *c;
1321
1322         TRACEJVMCALLS("JVM_GetClassAccessFlags(env=%p, cls=%p)", env, cls);
1323
1324         c = LLNI_classinfo_unwrap(cls);
1325
1326         /* Primitive type classes have the correct access flags. */
1327
1328         return c->flags & ACC_CLASS_REFLECT_MASK;
1329 }
1330
1331
1332 /* JVM_GetClassConstantPool */
1333
1334 jobject JVM_GetClassConstantPool(JNIEnv *env, jclass cls)
1335 {
1336 #if defined(ENABLE_ANNOTATIONS)
1337         sun_reflect_ConstantPool *constantPool    = NULL;
1338                       /* constant pool object for the class refered by 'cls' */
1339         java_lang_Object         *constantPoolOop = (java_lang_Object*)cls;
1340                       /* constantPoolOop field of the constant pool object   */
1341
1342         TRACEJVMCALLS("JVM_GetClassConstantPool(env=%p, cls=%p)", env, cls);
1343
1344         constantPool = 
1345                 (sun_reflect_ConstantPool*)native_new_and_init(
1346                         class_sun_reflect_ConstantPool);
1347         
1348         if (constantPool == NULL) {
1349                 /* out of memory */
1350                 return NULL;
1351         }
1352         
1353         LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
1354
1355         return (jobject)constantPool;
1356 #else
1357         log_println("JVM_GetClassConstantPool(env=%p, cls=%p): not implemented in this configuration!", env, cls);
1358         return NULL;
1359 #endif
1360 }
1361
1362
1363 /* JVM_ConstantPoolGetSize */
1364
1365 jint JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool)
1366 {
1367         classinfo *c; /* classinfo of the class for which 'this' is the constant pool */
1368
1369         TRACEJVMCALLS("JVM_ConstantPoolGetSize(env=%p, unused=%p, jcpool=%p)", env, unused, jcpool);
1370
1371         c = LLNI_classinfo_unwrap(jcpool);
1372
1373         return c->cpcount;
1374 }
1375
1376
1377 /* JVM_ConstantPoolGetClassAt */
1378
1379 jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1380 {
1381         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1382         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1383         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1384
1385         TRACEJVMCALLS("JVM_ConstantPoolGetClassAt(env=%p, jcpool=%p, index=%d)", env, jcpool, index);
1386
1387         c = LLNI_classinfo_unwrap(jcpool);
1388
1389         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1390
1391         if (ref == NULL) {
1392                 exceptions_throw_illegalargumentexception();
1393                 return NULL;
1394         }
1395
1396         result = resolve_classref_eager(ref);
1397
1398         return (jclass) LLNI_classinfo_wrap(result);
1399 }
1400
1401
1402 /* JVM_ConstantPoolGetClassAtIfLoaded */
1403
1404 jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1405 {
1406         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1407         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1408         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1409
1410         TRACEJVMCALLS("JVM_ConstantPoolGetClassAtIfLoaded(env=%p, unused=%p, jcpool=%p, index=%d)", env, unused, jcpool, index);
1411
1412         c = LLNI_classinfo_unwrap(jcpool);
1413
1414         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1415
1416         if (ref == NULL) {
1417                 exceptions_throw_illegalargumentexception();
1418                 return NULL;
1419         }
1420         
1421         if (!resolve_classref(NULL, ref, resolveLazy, true, true, &result)) {
1422                 return NULL;
1423         }
1424
1425         if ((result == NULL) || !(result->state & CLASS_LOADED)) {
1426                 return NULL;
1427         }
1428         
1429         return (jclass) LLNI_classinfo_wrap(result);
1430 }
1431
1432
1433 /* JVM_ConstantPoolGetMethodAt */
1434
1435 jobject JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1436 {
1437         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1438         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1439         
1440         TRACEJVMCALLS("JVM_ConstantPoolGetMethodAt: jcpool=%p, index=%d", jcpool, index);
1441         
1442         cls = LLNI_classinfo_unwrap(jcpool);
1443         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1444         
1445         if (ref == NULL) {
1446                 exceptions_throw_illegalargumentexception();
1447                 return NULL;
1448         }
1449
1450         /* XXX: is that right? or do I have to use resolve_method_*? */
1451         return (jobject)reflect_method_new(ref->p.method);
1452 }
1453
1454
1455 /* JVM_ConstantPoolGetMethodAtIfLoaded */
1456
1457 jobject JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1458 {
1459         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1460         classinfo *c = NULL;  /* resolved declaring class of the method */
1461         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1462
1463         TRACEJVMCALLS("JVM_ConstantPoolGetMethodAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
1464
1465         cls = LLNI_classinfo_unwrap(jcpool);
1466         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1467
1468         if (ref == NULL) {
1469                 exceptions_throw_illegalargumentexception();
1470                 return NULL;
1471         }
1472
1473         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1474                 return NULL;
1475         }
1476
1477         if (c == NULL || !(c->state & CLASS_LOADED)) {
1478                 return NULL;
1479         }
1480
1481         return (jobject)reflect_method_new(ref->p.method);
1482 }
1483
1484
1485 /* JVM_ConstantPoolGetFieldAt */
1486
1487 jobject JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1488 {
1489         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1490         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1491         
1492         TRACEJVMCALLS("JVM_ConstantPoolGetFieldAt: jcpool=%p, index=%d", jcpool, index);
1493
1494         cls = LLNI_classinfo_unwrap(jcpool);
1495         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1496
1497         if (ref == NULL) {
1498                 exceptions_throw_illegalargumentexception();
1499                 return NULL;
1500         }
1501
1502         return (jobject)reflect_field_new(ref->p.field);
1503 }
1504
1505
1506 /* JVM_ConstantPoolGetFieldAtIfLoaded */
1507
1508 jobject JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1509 {
1510         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1511         classinfo *c;         /* resolved declaring class for the field */
1512         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1513
1514         TRACEJVMCALLS("JVM_ConstantPoolGetFieldAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
1515
1516         cls = LLNI_classinfo_unwrap(jcpool);
1517         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1518
1519         if (ref == NULL) {
1520                 exceptions_throw_illegalargumentexception();
1521                 return NULL;
1522         }
1523
1524         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1525                 return NULL;
1526         }
1527
1528         if (c == NULL || !(c->state & CLASS_LOADED)) {
1529                 return NULL;
1530         }
1531
1532         return (jobject)reflect_field_new(ref->p.field);
1533 }
1534
1535
1536 /* JVM_ConstantPoolGetMemberRefInfoAt */
1537
1538 jobjectArray JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1539 {
1540         log_println("JVM_ConstantPoolGetMemberRefInfoAt: jcpool=%p, index=%d, IMPLEMENT ME!", jcpool, index);
1541
1542         /* TODO: implement. (this is yet unused be OpenJDK but, so very low priority) */
1543
1544         return NULL;
1545 }
1546
1547
1548 /* JVM_ConstantPoolGetIntAt */
1549
1550 jint JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1551 {
1552         constant_integer *ref; /* reference to the int value in constant pool at index 'index' */
1553         classinfo *cls;        /* classinfo of the class for which 'this' is the constant pool */
1554
1555         TRACEJVMCALLS("JVM_ConstantPoolGetIntAt: jcpool=%p, index=%d", jcpool, index);
1556
1557         cls = LLNI_classinfo_unwrap(jcpool);
1558         ref = (constant_integer*)class_getconstant(cls, index, CONSTANT_Integer);
1559
1560         if (ref == NULL) {
1561                 exceptions_throw_illegalargumentexception();
1562                 return 0;
1563         }
1564
1565         return ref->value;
1566 }
1567
1568
1569 /* JVM_ConstantPoolGetLongAt */
1570
1571 jlong JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1572 {
1573         constant_long *ref; /* reference to the long value in constant pool at index 'index' */
1574         classinfo *cls;     /* classinfo of the class for which 'this' is the constant pool */
1575
1576         TRACEJVMCALLS("JVM_ConstantPoolGetLongAt: jcpool=%p, index=%d", jcpool, index);
1577
1578         cls = LLNI_classinfo_unwrap(jcpool);
1579         ref = (constant_long*)class_getconstant(cls, index, CONSTANT_Long);
1580
1581         if (ref == NULL) {
1582                 exceptions_throw_illegalargumentexception();
1583                 return 0;
1584         }
1585
1586         return ref->value;
1587 }
1588
1589
1590 /* JVM_ConstantPoolGetFloatAt */
1591
1592 jfloat JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1593 {
1594         constant_float *ref; /* reference to the float value in constant pool at index 'index' */
1595         classinfo *cls;      /* classinfo of the class for which 'this' is the constant pool */
1596
1597         TRACEJVMCALLS("JVM_ConstantPoolGetFloatAt: jcpool=%p, index=%d", jcpool, index);
1598
1599         cls = LLNI_classinfo_unwrap(jcpool);
1600         ref = (constant_float*)class_getconstant(cls, index, CONSTANT_Float);
1601
1602         if (ref == NULL) {
1603                 exceptions_throw_illegalargumentexception();
1604                 return 0;
1605         }
1606
1607         return ref->value;
1608 }
1609
1610
1611 /* JVM_ConstantPoolGetDoubleAt */
1612
1613 jdouble JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1614 {
1615         constant_double *ref; /* reference to the double value in constant pool at index 'index' */
1616         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1617
1618         TRACEJVMCALLS("JVM_ConstantPoolGetDoubleAt: jcpool=%p, index=%d", jcpool, index);
1619
1620         cls = LLNI_classinfo_unwrap(jcpool);
1621         ref = (constant_double*)class_getconstant(cls, index, CONSTANT_Double);
1622
1623         if (ref == NULL) {
1624                 exceptions_throw_illegalargumentexception();
1625                 return 0;
1626         }
1627
1628         return ref->value;
1629 }
1630
1631
1632 /* JVM_ConstantPoolGetStringAt */
1633
1634 jstring JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1635 {
1636         utf *ref;       /* utf object for the string in constant pool at index 'index' */
1637         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1638
1639         TRACEJVMCALLS("JVM_ConstantPoolGetStringAt: jcpool=%p, index=%d", jcpool, index);
1640         
1641         cls = LLNI_classinfo_unwrap(jcpool);
1642         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
1643
1644         if (ref == NULL) {
1645                 exceptions_throw_illegalargumentexception();
1646                 return NULL;
1647         }
1648
1649         /* XXX: I hope literalstring_new is the right Function. */
1650         return (jstring)literalstring_new(ref);
1651 }
1652
1653
1654 /* JVM_ConstantPoolGetUTF8At */
1655
1656 jstring JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1657 {
1658         utf *ref; /* utf object for the utf8 data in constant pool at index 'index' */
1659         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1660
1661         TRACEJVMCALLS("JVM_ConstantPoolGetUTF8At: jcpool=%p, index=%d", jcpool, index);
1662
1663         cls = LLNI_classinfo_unwrap(jcpool);
1664         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
1665
1666         if (ref == NULL) {
1667                 exceptions_throw_illegalargumentexception();
1668                 return NULL;
1669         }
1670
1671         /* XXX: I hope literalstring_new is the right Function. */
1672         return (jstring)literalstring_new(ref);
1673 }
1674
1675
1676 /* JVM_DesiredAssertionStatus */
1677
1678 jboolean JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)
1679 {
1680 #if defined(ENABLE_ASSERTION)
1681         assertion_name_t  *item;
1682         classinfo         *c;
1683         jboolean           status;
1684         utf               *name;
1685
1686         TRACEJVMCALLS("JVM_DesiredAssertionStatus(env=%p, unused=%p, cls=%p)", env, unused, cls);
1687
1688         c = LLNI_classinfo_unwrap(cls);
1689
1690         if (c->classloader == NULL) {
1691                 status = (jboolean)assertion_system_enabled;
1692         }
1693         else {
1694                 status = (jboolean)assertion_user_enabled;
1695         }
1696
1697         if (list_assertion_names != NULL) {
1698                 item = (assertion_name_t *)list_first(list_assertion_names);
1699                 while (item != NULL) {
1700                         name = utf_new_char(item->name);
1701                         if (name == c->packagename) {
1702                                 status = (jboolean)item->enabled;
1703                         }
1704                         else if (name == c->name) {
1705                                 status = (jboolean)item->enabled;
1706                         }
1707
1708                         item = (assertion_name_t *)list_next(list_assertion_names, item);
1709                 }
1710         }
1711
1712         return status;
1713 #else
1714         return (jboolean)false;
1715 #endif
1716 }
1717
1718
1719 /* JVM_AssertionStatusDirectives */
1720
1721 jobject JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)
1722 {
1723         classinfo                             *c;
1724         java_lang_AssertionStatusDirectives   *o;
1725         java_handle_objectarray_t             *classes;
1726         java_handle_objectarray_t             *packages;
1727         java_booleanarray_t                   *classEnabled;
1728         java_booleanarray_t                   *packageEnabled;
1729 #if defined(ENABLE_ASSERTION)
1730         assertion_name_t                      *item;
1731         java_handle_t                         *js;
1732         s4                                     i, j;
1733 #endif
1734
1735         TRACEJVMCALLS("JVM_AssertionStatusDirectives(env=%p, unused=%p)", env, unused);
1736
1737         c = load_class_bootstrap(utf_new_char("java/lang/AssertionStatusDirectives"));
1738
1739         if (c == NULL)
1740                 return NULL;
1741
1742         o = (java_lang_AssertionStatusDirectives *) builtin_new(c);
1743
1744         if (o == NULL)
1745                 return NULL;
1746
1747 #if defined(ENABLE_ASSERTION)
1748         classes = builtin_anewarray(assertion_class_count, class_java_lang_Object);
1749 #else
1750         classes = builtin_anewarray(0, class_java_lang_Object);
1751 #endif
1752         if (classes == NULL)
1753                 return NULL;
1754
1755 #if defined(ENABLE_ASSERTION)
1756         packages = builtin_anewarray(assertion_package_count, class_java_lang_Object);
1757 #else
1758         packages = builtin_anewarray(0, class_java_lang_Object);
1759 #endif
1760         if (packages == NULL)
1761                 return NULL;
1762         
1763 #if defined(ENABLE_ASSERTION)
1764         classEnabled = builtin_newarray_boolean(assertion_class_count);
1765 #else
1766         classEnabled = builtin_newarray_boolean(0);
1767 #endif
1768         if (classEnabled == NULL)
1769                 return NULL;
1770
1771 #if defined(ENABLE_ASSERTION)
1772         packageEnabled = builtin_newarray_boolean(assertion_package_count);
1773 #else
1774         packageEnabled = builtin_newarray_boolean(0);
1775 #endif
1776         if (packageEnabled == NULL)
1777                 return NULL;
1778
1779 #if defined(ENABLE_ASSERTION)
1780         /* initialize arrays */
1781
1782         if (list_assertion_names != NULL) {
1783                 i = 0;
1784                 j = 0;
1785                 
1786                 item = (assertion_name_t *)list_first(list_assertion_names);
1787                 while (item != NULL) {
1788                         js = javastring_new_from_ascii(item->name);
1789                         if (js == NULL) {
1790                                 return NULL;
1791                         }
1792
1793                         if (item->package == false) {
1794                                 classes->data[i] = js;
1795                                 classEnabled->data[i] = (jboolean) item->enabled;
1796                                 i += 1;
1797                         }
1798                         else {
1799                                 packages->data[j] = js;
1800                                 packageEnabled->data[j] = (jboolean) item->enabled;
1801                                 j += 1;
1802                         }
1803
1804                         item = (assertion_name_t *)list_next(list_assertion_names, item);
1805                 }
1806         }
1807 #endif
1808
1809         /* set instance fields */
1810
1811         o->classes  = classes;
1812         o->packages = packages;
1813         o->classEnabled = classEnabled;
1814         o->packageEnabled = packageEnabled;
1815
1816         return (jobject) o;
1817 }
1818
1819
1820 /* JVM_GetClassNameUTF */
1821
1822 const char* JVM_GetClassNameUTF(JNIEnv *env, jclass cls)
1823 {
1824         log_println("JVM_GetClassNameUTF: IMPLEMENT ME!");
1825 }
1826
1827
1828 /* JVM_GetClassCPTypes */
1829
1830 void JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)
1831 {
1832         log_println("JVM_GetClassCPTypes: IMPLEMENT ME!");
1833 }
1834
1835
1836 /* JVM_GetClassCPEntriesCount */
1837
1838 jint JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)
1839 {
1840         log_println("JVM_GetClassCPEntriesCount: IMPLEMENT ME!");
1841 }
1842
1843
1844 /* JVM_GetClassFieldsCount */
1845
1846 jint JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)
1847 {
1848         log_println("JVM_GetClassFieldsCount: IMPLEMENT ME!");
1849 }
1850
1851
1852 /* JVM_GetClassMethodsCount */
1853
1854 jint JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)
1855 {
1856         log_println("JVM_GetClassMethodsCount: IMPLEMENT ME!");
1857 }
1858
1859
1860 /* JVM_GetMethodIxExceptionIndexes */
1861
1862 void JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)
1863 {
1864         log_println("JVM_GetMethodIxExceptionIndexes: IMPLEMENT ME!");
1865 }
1866
1867
1868 /* JVM_GetMethodIxExceptionsCount */
1869
1870 jint JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)
1871 {
1872         log_println("JVM_GetMethodIxExceptionsCount: IMPLEMENT ME!");
1873 }
1874
1875
1876 /* JVM_GetMethodIxByteCode */
1877
1878 void JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)
1879 {
1880         log_println("JVM_GetMethodIxByteCode: IMPLEMENT ME!");
1881 }
1882
1883
1884 /* JVM_GetMethodIxByteCodeLength */
1885
1886 jint JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)
1887 {
1888         log_println("JVM_GetMethodIxByteCodeLength: IMPLEMENT ME!");
1889 }
1890
1891
1892 /* JVM_GetMethodIxExceptionTableEntry */
1893
1894 void JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)
1895 {
1896         log_println("JVM_GetMethodIxExceptionTableEntry: IMPLEMENT ME!");
1897 }
1898
1899
1900 /* JVM_GetMethodIxExceptionTableLength */
1901
1902 jint JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)
1903 {
1904         log_println("JVM_GetMethodIxExceptionTableLength: IMPLEMENT ME!");
1905 }
1906
1907
1908 /* JVM_GetMethodIxModifiers */
1909
1910 jint JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)
1911 {
1912         log_println("JVM_GetMethodIxModifiers: IMPLEMENT ME!");
1913 }
1914
1915
1916 /* JVM_GetFieldIxModifiers */
1917
1918 jint JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)
1919 {
1920         log_println("JVM_GetFieldIxModifiers: IMPLEMENT ME!");
1921 }
1922
1923
1924 /* JVM_GetMethodIxLocalsCount */
1925
1926 jint JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)
1927 {
1928         log_println("JVM_GetMethodIxLocalsCount: IMPLEMENT ME!");
1929 }
1930
1931
1932 /* JVM_GetMethodIxArgsSize */
1933
1934 jint JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)
1935 {
1936         log_println("JVM_GetMethodIxArgsSize: IMPLEMENT ME!");
1937 }
1938
1939
1940 /* JVM_GetMethodIxMaxStack */
1941
1942 jint JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)
1943 {
1944         log_println("JVM_GetMethodIxMaxStack: IMPLEMENT ME!");
1945 }
1946
1947
1948 /* JVM_IsConstructorIx */
1949
1950 jboolean JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)
1951 {
1952         log_println("JVM_IsConstructorIx: IMPLEMENT ME!");
1953 }
1954
1955
1956 /* JVM_GetMethodIxNameUTF */
1957
1958 const char* JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)
1959 {
1960         log_println("JVM_GetMethodIxNameUTF: IMPLEMENT ME!");
1961 }
1962
1963
1964 /* JVM_GetMethodIxSignatureUTF */
1965
1966 const char* JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)
1967 {
1968         log_println("JVM_GetMethodIxSignatureUTF: IMPLEMENT ME!");
1969 }
1970
1971
1972 /* JVM_GetCPFieldNameUTF */
1973
1974 const char* JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1975 {
1976         log_println("JVM_GetCPFieldNameUTF: IMPLEMENT ME!");
1977 }
1978
1979
1980 /* JVM_GetCPMethodNameUTF */
1981
1982 const char* JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1983 {
1984         log_println("JVM_GetCPMethodNameUTF: IMPLEMENT ME!");
1985 }
1986
1987
1988 /* JVM_GetCPMethodSignatureUTF */
1989
1990 const char* JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1991 {
1992         log_println("JVM_GetCPMethodSignatureUTF: IMPLEMENT ME!");
1993 }
1994
1995
1996 /* JVM_GetCPFieldSignatureUTF */
1997
1998 const char* JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1999 {
2000         log_println("JVM_GetCPFieldSignatureUTF: IMPLEMENT ME!");
2001 }
2002
2003
2004 /* JVM_GetCPClassNameUTF */
2005
2006 const char* JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2007 {
2008         log_println("JVM_GetCPClassNameUTF: IMPLEMENT ME!");
2009 }
2010
2011
2012 /* JVM_GetCPFieldClassNameUTF */
2013
2014 const char* JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2015 {
2016         log_println("JVM_GetCPFieldClassNameUTF: IMPLEMENT ME!");
2017 }
2018
2019
2020 /* JVM_GetCPMethodClassNameUTF */
2021
2022 const char* JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2023 {
2024         log_println("JVM_GetCPMethodClassNameUTF: IMPLEMENT ME!");
2025 }
2026
2027
2028 /* JVM_GetCPFieldModifiers */
2029
2030 jint JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
2031 {
2032         log_println("JVM_GetCPFieldModifiers: IMPLEMENT ME!");
2033 }
2034
2035
2036 /* JVM_GetCPMethodModifiers */
2037
2038 jint JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
2039 {
2040         log_println("JVM_GetCPMethodModifiers: IMPLEMENT ME!");
2041 }
2042
2043
2044 /* JVM_ReleaseUTF */
2045
2046 void JVM_ReleaseUTF(const char *utf)
2047 {
2048         log_println("JVM_ReleaseUTF: IMPLEMENT ME!");
2049 }
2050
2051
2052 /* JVM_IsSameClassPackage */
2053
2054 jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)
2055 {
2056         log_println("JVM_IsSameClassPackage: IMPLEMENT ME!");
2057 }
2058
2059
2060 /* JVM_Open */
2061
2062 jint JVM_Open(const char *fname, jint flags, jint mode)
2063 {
2064         int result;
2065
2066 #if PRINTJVM
2067         log_println("JVM_Open: fname=%s, flags=%d, mode=%d", fname, flags, mode);
2068 #endif
2069
2070         result = open(fname, flags, mode);
2071
2072         if (result >= 0) {
2073                 return result;
2074         }
2075         else {
2076                 switch(errno) {
2077                 case EEXIST:
2078                         /* XXX don't know what to do here */
2079 /*                      return JVM_EEXIST; */
2080                         return -1;
2081                 default:
2082                         return -1;
2083                 }
2084         }
2085 }
2086
2087
2088 /* JVM_Close */
2089
2090 jint JVM_Close(jint fd)
2091 {
2092 #if PRINTJVM
2093         log_println("JVM_Close: fd=%d", fd);
2094 #endif
2095         return close(fd);
2096 }
2097
2098
2099 /* JVM_Read */
2100
2101 jint JVM_Read(jint fd, char *buf, jint nbytes)
2102 {
2103 #if PRINTJVM
2104         log_println("JVM_Read: fd=%d, buf=%p, nbytes=%d", fd, buf, nbytes);
2105 #endif
2106         return read(fd, buf, nbytes);
2107 }
2108
2109
2110 /* JVM_Write */
2111
2112 jint JVM_Write(jint fd, char *buf, jint nbytes)
2113 {
2114 #if PRINTJVM
2115         log_println("JVM_Write: fd=%d, buf=%s, nbytes=%d", fd, buf, nbytes);
2116 #endif
2117         return write(fd, buf, nbytes);
2118 }
2119
2120
2121 /* JVM_Available */
2122
2123 jint JVM_Available(jint fd, jlong *pbytes)
2124 {
2125 #if defined(FIONREAD)
2126         int bytes;
2127
2128         TRACEJVMCALLS("JVM_Available(fd=%d, pbytes=%p)", fd, pbytes);
2129
2130         *pbytes = 0;
2131
2132         if (ioctl(fd, FIONREAD, &bytes) < 0)
2133                 return 0;
2134
2135         *pbytes = bytes;
2136
2137         return 1;
2138 #else
2139 # error FIONREAD not defined
2140 #endif
2141 }
2142
2143
2144 /* JVM_Lseek */
2145
2146 jlong JVM_Lseek(jint fd, jlong offset, jint whence)
2147 {
2148         TRACEJVMCALLS("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence);
2149
2150         return (jlong) lseek(fd, (off_t) offset, whence);
2151 }
2152
2153
2154 /* JVM_SetLength */
2155
2156 jint JVM_SetLength(jint fd, jlong length)
2157 {
2158         TRACEJVMCALLS("JVM_SetLength(fd=%d, length=%ld)", length);
2159
2160         return ftruncate(fd, length);
2161 }
2162
2163
2164 /* JVM_Sync */
2165
2166 jint JVM_Sync(jint fd)
2167 {
2168         TRACEJVMCALLS("JVM_Sync(fd=%d)", fd);
2169
2170         return fsync(fd);
2171 }
2172
2173
2174 /* JVM_StartThread */
2175
2176 void JVM_StartThread(JNIEnv* env, jobject jthread)
2177 {
2178 #if PRINTJVM
2179         log_println("JVM_StartThread: jthread=%p", jthread);
2180 #endif
2181         _Jv_java_lang_Thread_start((java_lang_Thread *) jthread, 0);
2182 }
2183
2184
2185 /* JVM_StopThread */
2186
2187 void JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)
2188 {
2189         log_println("JVM_StopThread: IMPLEMENT ME!");
2190 }
2191
2192
2193 /* JVM_IsThreadAlive */
2194
2195 jboolean JVM_IsThreadAlive(JNIEnv* env, jobject jthread)
2196 {
2197         threadobject *t;
2198         bool          equal;
2199         bool          result;
2200
2201         TRACEJVMCALLS("JVM_IsThreadAlive(env=%p, jthread=%p)", env, jthread);
2202
2203         /* XXX this is just a quick hack */
2204
2205         for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
2206                 LLNI_equals(t->object, jthread, equal);
2207
2208                 if (equal == true)
2209                         break;
2210         }
2211
2212         /* The threadobject is null when a thread is created in Java. The
2213            priority is set later during startup. */
2214
2215         if (t == NULL)
2216                 return 0;
2217
2218         result = threads_thread_is_alive(t);
2219
2220         return result;
2221 }
2222
2223
2224 /* JVM_SuspendThread */
2225
2226 void JVM_SuspendThread(JNIEnv* env, jobject jthread)
2227 {
2228         log_println("JVM_SuspendThread: IMPLEMENT ME!");
2229 }
2230
2231
2232 /* JVM_ResumeThread */
2233
2234 void JVM_ResumeThread(JNIEnv* env, jobject jthread)
2235 {
2236         log_println("JVM_ResumeThread: IMPLEMENT ME!");
2237 }
2238
2239
2240 /* JVM_SetThreadPriority */
2241
2242 void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)
2243 {
2244 #if PRINTJVM
2245         log_println("JVM_SetThreadPriority: jthread=%p, prio=%d", jthread, prio);
2246 #endif
2247         _Jv_java_lang_Thread_setPriority((java_lang_Thread *) jthread, prio);
2248 }
2249
2250
2251 /* JVM_Yield */
2252
2253 void JVM_Yield(JNIEnv *env, jclass threadClass)
2254 {
2255         TRACEJVMCALLS("JVM_Yield(env=%p, threadClass=%p)", env, threadClass);
2256
2257         threads_yield();
2258 }
2259
2260
2261 /* JVM_Sleep */
2262
2263 void JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)
2264 {
2265 #if PRINTJVM
2266         log_println("JVM_Sleep: threadClass=%p, millis=%ld", threadClass, millis);
2267 #endif
2268         _Jv_java_lang_Thread_sleep(millis);
2269 }
2270
2271
2272 /* JVM_CurrentThread */
2273
2274 jobject JVM_CurrentThread(JNIEnv* env, jclass threadClass)
2275 {
2276 #if PRINTJVM
2277         log_println("JVM_CurrentThread: threadClass=%p", threadClass);
2278 #endif
2279         return (jobject) _Jv_java_lang_Thread_currentThread();
2280 }
2281
2282
2283 /* JVM_CountStackFrames */
2284
2285 jint JVM_CountStackFrames(JNIEnv* env, jobject jthread)
2286 {
2287         log_println("JVM_CountStackFrames: IMPLEMENT ME!");
2288 }
2289
2290
2291 /* JVM_Interrupt */
2292
2293 void JVM_Interrupt(JNIEnv* env, jobject jthread)
2294 {
2295         log_println("JVM_Interrupt: IMPLEMENT ME!");
2296 }
2297
2298
2299 /* JVM_IsInterrupted */
2300
2301 jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)
2302 {
2303 #if PRINTJVM
2304         log_println("JVM_IsInterrupted: jthread=%p, clear_interrupted=%d", jthread, clear_interrupted);
2305 #endif
2306         /* XXX do something with clear_interrupted */
2307         return _Jv_java_lang_Thread_isInterrupted((java_lang_Thread *) jthread);
2308 }
2309
2310
2311 /* JVM_HoldsLock */
2312
2313 jboolean JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)
2314 {
2315         java_handle_t *h;
2316         bool           result;
2317
2318         TRACEJVMCALLS("JVM_HoldsLock(env=%p, threadClass=%p, obj=%p)", env, threadClass, obj);
2319
2320         h = (java_handle_t *) obj;
2321
2322         if (h == NULL) {
2323                 exceptions_throw_nullpointerexception();
2324                 return JNI_FALSE;
2325         }
2326
2327         result = lock_is_held_by_current_thread(h);
2328
2329         return result;
2330 }
2331
2332
2333 /* JVM_DumpAllStacks */
2334
2335 void JVM_DumpAllStacks(JNIEnv* env, jclass unused)
2336 {
2337         log_println("JVM_DumpAllStacks: IMPLEMENT ME!");
2338 }
2339
2340
2341 /* JVM_CurrentLoadedClass */
2342
2343 jclass JVM_CurrentLoadedClass(JNIEnv *env)
2344 {
2345         log_println("JVM_CurrentLoadedClass: IMPLEMENT ME!");
2346 }
2347
2348
2349 /* JVM_CurrentClassLoader */
2350
2351 jobject JVM_CurrentClassLoader(JNIEnv *env)
2352 {
2353     /* XXX if a method in a class in a trusted loader is in a
2354            doPrivileged, return NULL */
2355
2356         log_println("JVM_CurrentClassLoader: IMPLEMENT ME!");
2357 }
2358
2359
2360 /* JVM_GetClassContext */
2361
2362 jobjectArray JVM_GetClassContext(JNIEnv *env)
2363 {
2364 #if PRINTJVM
2365         log_println("JVM_GetClassContext");
2366 #endif
2367         return (jobjectArray) stacktrace_getClassContext();
2368 }
2369
2370
2371 /* JVM_ClassDepth */
2372
2373 jint JVM_ClassDepth(JNIEnv *env, jstring name)
2374 {
2375         log_println("JVM_ClassDepth: IMPLEMENT ME!");
2376 }
2377
2378
2379 /* JVM_ClassLoaderDepth */
2380
2381 jint JVM_ClassLoaderDepth(JNIEnv *env)
2382 {
2383         log_println("JVM_ClassLoaderDepth: IMPLEMENT ME!");
2384 }
2385
2386
2387 /* JVM_GetSystemPackage */
2388
2389 jstring JVM_GetSystemPackage(JNIEnv *env, jstring name)
2390 {
2391         java_handle_t *s;
2392         utf *u;
2393         utf *result;
2394
2395         TRACEJVMCALLS("JVM_GetSystemPackage(env=%p, name=%p)", env, name);
2396
2397 /*      s = package_find(name); */
2398         u = javastring_toutf(name, false);
2399         result = package_find(u);
2400         if (result != NULL)
2401                 s = javastring_new(result);
2402         else
2403                 s = NULL;
2404
2405         return (jstring) s;
2406 }
2407
2408
2409 /* JVM_GetSystemPackages */
2410
2411 jobjectArray JVM_GetSystemPackages(JNIEnv *env)
2412 {
2413         log_println("JVM_GetSystemPackages: IMPLEMENT ME!");
2414 }
2415
2416
2417 /* JVM_AllocateNewObject */
2418
2419 jobject JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass)
2420 {
2421         log_println("JVM_AllocateNewObject: IMPLEMENT ME!");
2422 }
2423
2424
2425 /* JVM_AllocateNewArray */
2426
2427 jobject JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length)
2428 {
2429         log_println("JVM_AllocateNewArray: IMPLEMENT ME!");
2430 }
2431
2432
2433 /* JVM_LatestUserDefinedLoader */
2434
2435 jobject JVM_LatestUserDefinedLoader(JNIEnv *env)
2436 {
2437         classloader *cl;
2438
2439         TRACEJVMCALLS("JVM_LatestUserDefinedLoader(env=%p)", env);
2440
2441         cl = stacktrace_first_nonnull_classloader();
2442
2443         return (jobject) cl;
2444 }
2445
2446
2447 /* JVM_LoadClass0 */
2448
2449 jclass JVM_LoadClass0(JNIEnv *env, jobject receiver, jclass currClass, jstring currClassName)
2450 {
2451         log_println("JVM_LoadClass0: IMPLEMENT ME!");
2452 }
2453
2454
2455 /* JVM_GetArrayLength */
2456
2457 jint JVM_GetArrayLength(JNIEnv *env, jobject arr)
2458 {
2459         java_handle_t *a;
2460
2461         TRACEJVMCALLS("JVM_GetArrayLength(arr=%p)", arr);
2462
2463         a = (java_handle_t *) arr;
2464
2465         return array_length_get(a);
2466 }
2467
2468
2469 /* JVM_GetArrayElement */
2470
2471 jobject JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)
2472 {
2473         java_handle_t *a;
2474         java_handle_t *o;
2475
2476         TRACEJVMCALLS("JVM_GetArrayElement(env=%p, arr=%p, index=%d)", env, arr, index);
2477
2478         a = (java_handle_t *) arr;
2479
2480 /*      if (!class_is_array(a->objheader.vftbl->class)) { */
2481 /*              exceptions_throw_illegalargumentexception(); */
2482 /*              return NULL; */
2483 /*      } */
2484
2485         o = array_element_get(a, index);
2486
2487         return (jobject) o;
2488 }
2489
2490
2491 /* JVM_GetPrimitiveArrayElement */
2492
2493 jvalue JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)
2494 {
2495         log_println("JVM_GetPrimitiveArrayElement: IMPLEMENT ME!");
2496 }
2497
2498
2499 /* JVM_SetArrayElement */
2500
2501 void JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)
2502 {
2503         java_handle_t *a;
2504         java_handle_t *value;
2505
2506         TRACEJVMCALLS("JVM_SetArrayElement(env=%p, arr=%p, index=%d, val=%p)", env, arr, index, val);
2507
2508         a     = (java_handle_t *) arr;
2509         value = (java_handle_t *) val;
2510
2511         array_element_set(a, index, value);
2512 }
2513
2514
2515 /* JVM_SetPrimitiveArrayElement */
2516
2517 void JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)
2518 {
2519         log_println("JVM_SetPrimitiveArrayElement: IMPLEMENT ME!");
2520 }
2521
2522
2523 /* JVM_NewArray */
2524
2525 jobject JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)
2526 {
2527         classinfo                 *c;
2528         classinfo                 *pc;
2529         java_handle_t             *a;
2530         java_handle_objectarray_t *oa;
2531
2532         TRACEJVMCALLS("JVM_NewArray(env=%p, eltClass=%p, length=%d)", env, eltClass, length);
2533
2534         if (eltClass == NULL) {
2535                 exceptions_throw_nullpointerexception();
2536                 return NULL;
2537         }
2538
2539         /* NegativeArraySizeException is checked in builtin_newarray. */
2540
2541         c = LLNI_classinfo_unwrap(eltClass);
2542
2543         /* create primitive or object array */
2544
2545         if (class_is_primitive(c)) {
2546                 pc = primitive_arrayclass_get_by_name(c->name);
2547                 a = builtin_newarray(length, pc);
2548
2549                 return (jobject) a;
2550         }
2551         else {
2552                 oa = builtin_anewarray(length, c);
2553
2554                 return (jobject) oa;
2555         }
2556 }
2557
2558
2559 /* JVM_NewMultiArray */
2560
2561 jobject JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)
2562 {
2563         classinfo                 *c;
2564         java_handle_intarray_t    *ia;
2565         int32_t                    length;
2566         long                      *dims;
2567         int32_t                    value;
2568         int32_t                    i;
2569         classinfo                 *ac;
2570         java_handle_objectarray_t *a;
2571
2572         TRACEJVMCALLS("JVM_NewMultiArray(env=%p, eltClass=%p, dim=%p)", env, eltClass, dim);
2573
2574         if (eltClass == NULL) {
2575                 exceptions_throw_nullpointerexception();
2576                 return NULL;
2577         }
2578
2579         /* NegativeArraySizeException is checked in builtin_newarray. */
2580
2581         c = LLNI_classinfo_unwrap(eltClass);
2582
2583         /* XXX This is just a quick hack to get it working. */
2584
2585         ia = (java_handle_intarray_t *) dim;
2586
2587         length = array_length_get(ia);
2588
2589         dims = MNEW(long, length);
2590
2591         for (i = 0; i < length; i++) {
2592                 value = LLNI_array_direct(ia, i);
2593                 dims[i] = (long) value;
2594         }
2595
2596         /* Create an array-class if necessary. */
2597
2598         if (class_is_primitive(c))
2599                 ac = primitive_arrayclass_get_by_name(c->name);
2600         else
2601                 ac = class_array_of(c, true);
2602
2603         if (ac == NULL)
2604                 return NULL;
2605
2606         a = builtin_multianewarray(length, ac, dims);
2607
2608         return (jobject) a;
2609 }
2610
2611
2612 /* JVM_InitializeSocketLibrary */
2613
2614 jint JVM_InitializeSocketLibrary()
2615 {
2616         log_println("JVM_InitializeSocketLibrary: IMPLEMENT ME!");
2617 }
2618
2619
2620 /* JVM_Socket */
2621
2622 jint JVM_Socket(jint domain, jint type, jint protocol)
2623 {
2624         TRACEJVMCALLS("JVM_Socket(domain=%d, type=%d, protocol=%d)", domain, type, protocol);
2625
2626         return socket(domain, type, protocol);
2627 }
2628
2629
2630 /* JVM_SocketClose */
2631
2632 jint JVM_SocketClose(jint fd)
2633 {
2634         TRACEJVMCALLS("JVM_SocketClose(fd=%d)", fd);
2635
2636         return close(fd);
2637 }
2638
2639
2640 /* JVM_SocketShutdown */
2641
2642 jint JVM_SocketShutdown(jint fd, jint howto)
2643 {
2644         TRACEJVMCALLS("JVM_SocketShutdown(fd=%d, howto=%d)", fd, howto);
2645
2646         return shutdown(fd, howto);
2647 }
2648
2649
2650 /* JVM_Recv */
2651
2652 jint JVM_Recv(jint fd, char *buf, jint nBytes, jint flags)
2653 {
2654         log_println("JVM_Recv: IMPLEMENT ME!");
2655 }
2656
2657
2658 /* JVM_Send */
2659
2660 jint JVM_Send(jint fd, char *buf, jint nBytes, jint flags)
2661 {
2662         log_println("JVM_Send: IMPLEMENT ME!");
2663 }
2664
2665
2666 /* JVM_Timeout */
2667
2668 jint JVM_Timeout(int fd, long timeout)
2669 {
2670         log_println("JVM_Timeout: IMPLEMENT ME!");
2671 }
2672
2673
2674 /* JVM_Listen */
2675
2676 jint JVM_Listen(jint fd, jint count)
2677 {
2678         TRACEJVMCALLS("JVM_Listen(fd=%d, count=%d)", fd, count);
2679
2680         return listen(fd, count);
2681 }
2682
2683
2684 /* JVM_Connect */
2685
2686 jint JVM_Connect(jint fd, struct sockaddr *him, jint len)
2687 {
2688         TRACEJVMCALLS("JVM_Connect(fd=%d, him=%p, len=%d)", fd, him, len);
2689
2690         return connect(fd, him, len);
2691 }
2692
2693
2694 /* JVM_Bind */
2695
2696 jint JVM_Bind(jint fd, struct sockaddr *him, jint len)
2697 {
2698         log_println("JVM_Bind: IMPLEMENT ME!");
2699 }
2700
2701
2702 /* JVM_Accept */
2703
2704 jint JVM_Accept(jint fd, struct sockaddr *him, jint *len)
2705 {
2706         TRACEJVMCALLS("JVM_Accept(fd=%d, him=%p, len=%p)", fd, him, len);
2707
2708         return accept(fd, him, (socklen_t *) len);
2709 }
2710
2711
2712 /* JVM_RecvFrom */
2713
2714 jint JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen)
2715 {
2716         log_println("JVM_RecvFrom: IMPLEMENT ME!");
2717 }
2718
2719
2720 /* JVM_GetSockName */
2721
2722 jint JVM_GetSockName(jint fd, struct sockaddr *him, int *len)
2723 {
2724         TRACEJVMCALLS("JVM_GetSockName(fd=%d, him=%p, len=%p)", fd, him, len);
2725
2726         return getsockname(fd, him, (socklen_t *) len);
2727 }
2728
2729
2730 /* JVM_SendTo */
2731
2732 jint JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen)
2733 {
2734         log_println("JVM_SendTo: IMPLEMENT ME!");
2735 }
2736
2737
2738 /* JVM_SocketAvailable */
2739
2740 jint JVM_SocketAvailable(jint fd, jint *pbytes)
2741 {
2742 #if defined(FIONREAD)
2743         int bytes;
2744
2745         TRACEJVMCALLS("JVM_SocketAvailable(fd=%d, pbytes=%p)", fd, pbytes);
2746
2747         *pbytes = 0;
2748
2749         if (ioctl(fd, FIONREAD, &bytes) < 0)
2750                 return 0;
2751
2752         *pbytes = bytes;
2753
2754         return 1;
2755 #else
2756 # error FIONREAD not defined
2757 #endif
2758 }
2759
2760
2761 /* JVM_GetSockOpt */
2762
2763 jint JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)
2764 {
2765 #if defined(HAVE_GETSOCKOPT)
2766         TRACEJVMCALLS("JVM_GetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%p)", fd, level, optname, optval, optlen);
2767
2768         return getsockopt(fd, level, optname, optval, (socklen_t *) optlen);
2769 #else
2770 # error getsockopt not available
2771 #endif
2772 }
2773
2774
2775 /* JVM_SetSockOpt */
2776
2777 jint JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)
2778 {
2779 #if defined(HAVE_SETSOCKOPT)
2780         TRACEJVMCALLS("JVM_SetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%d)", fd, level, optname, optval, optlen);
2781
2782         return setsockopt(fd, level, optname, optval, optlen);
2783 #else
2784 # error setsockopt not available
2785 #endif
2786 }
2787
2788
2789 /* JVM_GetHostName */
2790
2791 int JVM_GetHostName(char* name, int namelen)
2792 {
2793         TRACEJVMCALLS("JVM_GetHostName(name=%s, namelen=%d)", name, namelen);
2794
2795         return gethostname(name, namelen);
2796 }
2797
2798
2799 /* JVM_GetHostByAddr */
2800
2801 struct hostent* JVM_GetHostByAddr(const char* name, int len, int type)
2802 {
2803         log_println("JVM_GetHostByAddr: IMPLEMENT ME!");
2804 }
2805
2806
2807 /* JVM_GetHostByName */
2808
2809 struct hostent* JVM_GetHostByName(char* name)
2810 {
2811         log_println("JVM_GetHostByName: IMPLEMENT ME!");
2812 }
2813
2814
2815 /* JVM_GetProtoByName */
2816
2817 struct protoent* JVM_GetProtoByName(char* name)
2818 {
2819         log_println("JVM_GetProtoByName: IMPLEMENT ME!");
2820 }
2821
2822
2823 /* JVM_LoadLibrary */
2824
2825 void *JVM_LoadLibrary(const char *name)
2826 {
2827         utf *u;
2828
2829         TRACEJVMCALLS("JVM_LoadLibrary(name=%s)", name);
2830
2831         u = utf_new_char(name);
2832
2833         return native_library_open(u);
2834 }
2835
2836
2837 /* JVM_UnloadLibrary */
2838
2839 void JVM_UnloadLibrary(void* handle)
2840 {
2841         log_println("JVM_UnloadLibrary: IMPLEMENT ME!");
2842 }
2843
2844
2845 /* JVM_FindLibraryEntry */
2846
2847 void *JVM_FindLibraryEntry(void *handle, const char *name)
2848 {
2849         lt_ptr symbol;
2850
2851         TRACEJVMCALLS("JVM_FindLibraryEntry(handle=%p, name=%s)", handle, name);
2852
2853         symbol = lt_dlsym(handle, name);
2854
2855         return symbol;
2856 }
2857
2858
2859 /* JVM_IsNaN */
2860
2861 jboolean JVM_IsNaN(jdouble a)
2862 {
2863         log_println("JVM_IsNaN: IMPLEMENT ME!");
2864 }
2865
2866
2867 /* JVM_IsSupportedJNIVersion */
2868
2869 jboolean JVM_IsSupportedJNIVersion(jint version)
2870 {
2871         TRACEJVMCALLS("JVM_IsSupportedJNIVersion(version=%d)", version);
2872
2873         return jni_version_check(version);
2874 }
2875
2876
2877 /* JVM_InternString */
2878
2879 jstring JVM_InternString(JNIEnv *env, jstring str)
2880 {
2881         TRACEJVMCALLS("JVM_InternString(env=%p, str=%p)", env, str);
2882
2883         return (jstring) javastring_intern((java_handle_t *) str);
2884 }
2885
2886
2887 /* JVM_RawMonitorCreate */
2888
2889 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void)
2890 {
2891         java_object_t *o;
2892
2893 #if PRINTJVM
2894         log_println("JVM_RawMonitorCreate");
2895 #endif
2896
2897         o = NEW(java_object_t);
2898
2899         lock_init_object_lock(o);
2900
2901         return o;
2902 }
2903
2904
2905 /* JVM_RawMonitorDestroy */
2906
2907 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon)
2908 {
2909 #if PRINTJVM
2910         log_println("JVM_RawMonitorDestroy: mon=%p", mon);
2911 #endif
2912         FREE(mon, java_object_t);
2913 }
2914
2915
2916 /* JVM_RawMonitorEnter */
2917
2918 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon)
2919 {
2920 #if PRINTJVM
2921         log_println("JVM_RawMonitorEnter: mon=%p", mon);
2922 #endif
2923         (void) lock_monitor_enter((java_object_t *) mon);
2924
2925         return 0;
2926 }
2927
2928
2929 /* JVM_RawMonitorExit */
2930
2931 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon)
2932 {
2933 #if PRINTJVM
2934         log_println("JVM_RawMonitorExit: mon=%p", mon);
2935 #endif
2936         (void) lock_monitor_exit((java_object_t *) mon);
2937 }
2938
2939
2940 /* JVM_SetPrimitiveFieldValues */
2941
2942 void JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
2943 {
2944         log_println("JVM_SetPrimitiveFieldValues: IMPLEMENT ME!");
2945 }
2946
2947
2948 /* JVM_GetPrimitiveFieldValues */
2949
2950 void JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
2951 {
2952         log_println("JVM_GetPrimitiveFieldValues: IMPLEMENT ME!");
2953 }
2954
2955
2956 /* JVM_AccessVMBooleanFlag */
2957
2958 jboolean JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get)
2959 {
2960         log_println("JVM_AccessVMBooleanFlag: IMPLEMENT ME!");
2961 }
2962
2963
2964 /* JVM_AccessVMIntFlag */
2965
2966 jboolean JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get)
2967 {
2968         log_println("JVM_AccessVMIntFlag: IMPLEMENT ME!");
2969 }
2970
2971
2972 /* JVM_VMBreakPoint */
2973
2974 void JVM_VMBreakPoint(JNIEnv *env, jobject obj)
2975 {
2976         log_println("JVM_VMBreakPoint: IMPLEMENT ME!");
2977 }
2978
2979
2980 /* JVM_GetClassFields */
2981
2982 jobjectArray JVM_GetClassFields(JNIEnv *env, jclass cls, jint which)
2983 {
2984         log_println("JVM_GetClassFields: IMPLEMENT ME!");
2985 }
2986
2987
2988 /* JVM_GetClassMethods */
2989
2990 jobjectArray JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which)
2991 {
2992         log_println("JVM_GetClassMethods: IMPLEMENT ME!");
2993 }
2994
2995
2996 /* JVM_GetClassConstructors */
2997
2998 jobjectArray JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which)
2999 {
3000         log_println("JVM_GetClassConstructors: IMPLEMENT ME!");
3001 }
3002
3003
3004 /* JVM_GetClassField */
3005
3006 jobject JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which)
3007 {
3008         log_println("JVM_GetClassField: IMPLEMENT ME!");
3009 }
3010
3011
3012 /* JVM_GetClassMethod */
3013
3014 jobject JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which)
3015 {
3016         log_println("JVM_GetClassMethod: IMPLEMENT ME!");
3017 }
3018
3019
3020 /* JVM_GetClassConstructor */
3021
3022 jobject JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which)
3023 {
3024         log_println("JVM_GetClassConstructor: IMPLEMENT ME!");
3025 }
3026
3027
3028 /* JVM_NewInstance */
3029
3030 jobject JVM_NewInstance(JNIEnv *env, jclass cls)
3031 {
3032         log_println("JVM_NewInstance: IMPLEMENT ME!");
3033 }
3034
3035
3036 /* JVM_GetField */
3037
3038 jobject JVM_GetField(JNIEnv *env, jobject field, jobject obj)
3039 {
3040         log_println("JVM_GetField: IMPLEMENT ME!");
3041 }
3042
3043
3044 /* JVM_GetPrimitiveField */
3045
3046 jvalue JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode)
3047 {
3048         log_println("JVM_GetPrimitiveField: IMPLEMENT ME!");
3049 }
3050
3051
3052 /* JVM_SetField */
3053
3054 void JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val)
3055 {
3056         log_println("JVM_SetField: IMPLEMENT ME!");
3057 }
3058
3059
3060 /* JVM_SetPrimitiveField */
3061
3062 void JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode)
3063 {
3064         log_println("JVM_SetPrimitiveField: IMPLEMENT ME!");
3065 }
3066
3067
3068 /* JVM_InvokeMethod */
3069
3070 jobject JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)
3071 {
3072 #if PRINTJVM
3073         log_println("JVM_InvokeMethod: method=%p, obj=%p, args0=%p", method, obj, args0);
3074 #endif
3075         return (jobject) _Jv_java_lang_reflect_Method_invoke((java_lang_reflect_Method *) method, (java_lang_Object *) obj, (java_handle_objectarray_t *) args0);
3076 }
3077
3078
3079 /* JVM_NewInstanceFromConstructor */
3080
3081 jobject JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0)
3082 {
3083 #if PRINTJVM
3084         log_println("JVM_NewInstanceFromConstructor: c=%p, args0=%p", c, args0);
3085 #endif
3086         return (jobject) _Jv_java_lang_reflect_Constructor_newInstance(env, (java_lang_reflect_Constructor *) c, (java_handle_objectarray_t *) args0);
3087 }
3088
3089
3090 /* JVM_SupportsCX8 */
3091
3092 jboolean JVM_SupportsCX8()
3093 {
3094         TRACEJVMCALLS("JVM_SupportsCX8()");
3095
3096         /* IMPLEMENT ME */
3097
3098         return 0;
3099 }
3100
3101
3102 /* JVM_CX8Field */
3103
3104 jboolean JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal)
3105 {
3106         log_println("JVM_CX8Field: IMPLEMENT ME!");
3107 }
3108
3109
3110 /* JVM_GetAllThreads */
3111
3112 jobjectArray JVM_GetAllThreads(JNIEnv *env, jclass dummy)
3113 {
3114         log_println("JVM_GetAllThreads: IMPLEMENT ME!");
3115 }
3116
3117
3118 /* JVM_DumpThreads */
3119
3120 jobjectArray JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)
3121 {
3122         log_println("JVM_DumpThreads: IMPLEMENT ME!");
3123 }
3124
3125
3126 /* JVM_GetManagement */
3127
3128 void* JVM_GetManagement(jint version)
3129 {
3130         log_println("JVM_GetManagement: IMPLEMENT ME!");
3131 }
3132
3133
3134 /* JVM_InitAgentProperties */
3135
3136 jobject JVM_InitAgentProperties(JNIEnv *env, jobject properties)
3137 {
3138         log_println("JVM_InitAgentProperties: IMPLEMENT ME!");
3139 }
3140
3141
3142 /* JVM_GetEnclosingMethodInfo */
3143
3144 jobjectArray JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)
3145 {
3146         classinfo                 *c;
3147         methodinfo                *m;
3148         java_handle_objectarray_t *oa;
3149
3150         TRACEJVMCALLS("JVM_GetEnclosingMethodInfo(env=%p, ofClass=%p)", env, ofClass);
3151
3152         c = LLNI_classinfo_unwrap(ofClass);
3153
3154         if ((c == NULL) || class_is_primitive(c))
3155                 return NULL;
3156
3157         m = class_get_enclosingmethod(c);
3158
3159         if (m == NULL)
3160                 return NULL;
3161
3162         oa = builtin_anewarray(3, class_java_lang_Object);
3163
3164         if (oa == NULL)
3165                 return NULL;
3166
3167         array_objectarray_element_set(oa, 0, (java_handle_t *) LLNI_classinfo_wrap(m->class));
3168         array_objectarray_element_set(oa, 1, javastring_new(m->name));
3169         array_objectarray_element_set(oa, 2, javastring_new(m->descriptor));
3170
3171         return (jobjectArray) oa;
3172 }
3173
3174
3175 /* JVM_GetThreadStateValues */
3176
3177 jintArray JVM_GetThreadStateValues(JNIEnv* env, jint javaThreadState)
3178 {
3179         java_handle_intarray_t *ia;
3180
3181         TRACEJVMCALLS("JVM_GetThreadStateValues(env=%p, javaThreadState=%d)",
3182                                   env, javaThreadState);
3183
3184         /* If new thread states are added in future JDK and VM versions,
3185            this should check if the JDK version is compatible with thread
3186            states supported by the VM.  Return NULL if not compatible.
3187         
3188            This function must map the VM java_lang_Thread::ThreadStatus
3189            to the Java thread state that the JDK supports. */
3190
3191         switch (javaThreadState) {
3192     case THREAD_STATE_NEW:
3193                 ia = builtin_newarray_int(1);
3194
3195                 if (ia == NULL)
3196                         return NULL;
3197
3198                 array_intarray_element_set(ia, 0, THREAD_STATE_NEW);
3199                 break; 
3200
3201     case THREAD_STATE_RUNNABLE:
3202                 ia = builtin_newarray_int(1);
3203
3204                 if (ia == NULL)
3205                         return NULL;
3206
3207                 array_intarray_element_set(ia, 0, THREAD_STATE_RUNNABLE);
3208                 break; 
3209
3210     case THREAD_STATE_BLOCKED:
3211                 ia = builtin_newarray_int(1);
3212
3213                 if (ia == NULL)
3214                         return NULL;
3215
3216                 array_intarray_element_set(ia, 0, THREAD_STATE_BLOCKED);
3217                 break; 
3218
3219     case THREAD_STATE_WAITING:
3220                 ia = builtin_newarray_int(2);
3221
3222                 if (ia == NULL)
3223                         return NULL;
3224
3225                 array_intarray_element_set(ia, 0, THREAD_STATE_WAITING);
3226                 /* XXX Implement parked stuff. */
3227 /*              array_intarray_element_set(ia, 1, PARKED); */
3228                 break; 
3229
3230     case THREAD_STATE_TIMED_WAITING:
3231                 ia = builtin_newarray_int(3);
3232
3233                 if (ia == NULL)
3234                         return NULL;
3235
3236                 /* XXX Not sure about that one. */
3237 /*              array_intarray_element_set(ia, 0, SLEEPING); */
3238                 array_intarray_element_set(ia, 0, THREAD_STATE_TIMED_WAITING);
3239                 /* XXX Implement parked stuff. */
3240 /*              array_intarray_element_set(ia, 2, PARKED); */
3241                 break; 
3242
3243     case THREAD_STATE_TERMINATED:
3244                 ia = builtin_newarray_int(1);
3245
3246                 if (ia == NULL)
3247                         return NULL;
3248
3249                 array_intarray_element_set(ia, 0, THREAD_STATE_TERMINATED);
3250                 break; 
3251
3252     default:
3253                 /* Unknown state - probably incompatible JDK version */
3254                 return NULL;
3255         }
3256
3257         return (jintArray) ia;
3258 }
3259
3260
3261 /* JVM_GetThreadStateNames */
3262
3263 jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArray values)
3264 {
3265         java_handle_intarray_t    *ia;
3266         java_handle_objectarray_t *oa;
3267         java_object_t             *s;
3268
3269         TRACEJVMCALLS("JVM_GetThreadStateNames(env=%p, javaThreadState=%d, values=%p)",
3270                                   env, javaThreadState, values);
3271
3272         ia = (java_handle_intarray_t *) values;
3273
3274         /* If new thread states are added in future JDK and VM versions,
3275            this should check if the JDK version is compatible with thread
3276            states supported by the VM.  Return NULL if not compatible.
3277         
3278            This function must map the VM java_lang_Thread::ThreadStatus
3279            to the Java thread state that the JDK supports. */
3280
3281         if (values == NULL) {
3282                 exceptions_throw_nullpointerexception();
3283                 return NULL;
3284         }
3285
3286         switch (javaThreadState) {
3287     case THREAD_STATE_NEW:
3288                 assert(ia->header.size == 1 && ia->data[0] == THREAD_STATE_NEW);
3289
3290                 oa = builtin_anewarray(1, class_java_lang_String);
3291
3292                 if (oa == NULL)
3293                         return NULL;
3294
3295                 s = javastring_new(utf_new_char("NEW"));
3296
3297                 if (s == NULL)
3298                         return NULL;
3299
3300                 array_objectarray_element_set(oa, 0, s);
3301                 break; 
3302
3303     case THREAD_STATE_RUNNABLE:
3304                 oa = builtin_anewarray(1, class_java_lang_String);
3305
3306                 if (oa == NULL)
3307                         return NULL;
3308
3309                 s = javastring_new(utf_new_char("RUNNABLE"));
3310
3311                 if (s == NULL)
3312                         return NULL;
3313
3314                 array_objectarray_element_set(oa, 0, s);
3315                 break; 
3316
3317     case THREAD_STATE_BLOCKED:
3318                 oa = builtin_anewarray(1, class_java_lang_String);
3319
3320                 if (oa == NULL)
3321                         return NULL;
3322
3323                 s = javastring_new(utf_new_char("BLOCKED"));
3324
3325                 if (s == NULL)
3326                         return NULL;
3327
3328                 array_objectarray_element_set(oa, 0, s);
3329                 break; 
3330
3331     case THREAD_STATE_WAITING:
3332                 oa = builtin_anewarray(2, class_java_lang_String);
3333
3334                 if (oa == NULL)
3335                         return NULL;
3336
3337                 s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
3338 /*              s = javastring_new(utf_new_char("WAITING.PARKED")); */
3339
3340                 if (s == NULL)
3341                         return NULL;
3342
3343                 array_objectarray_element_set(oa, 0, s);
3344 /*              array_objectarray_element_set(oa, 1, s); */
3345                 break; 
3346
3347     case THREAD_STATE_TIMED_WAITING:
3348                 oa = builtin_anewarray(3, class_java_lang_String);
3349
3350                 if (oa == NULL)
3351                         return NULL;
3352
3353 /*              s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
3354                 s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
3355 /*              s = javastring_new(utf_new_char("TIMED_WAITING.PARKED")); */
3356
3357                 if (s == NULL)
3358                         return NULL;
3359
3360 /*              array_objectarray_element_set(oa, 0, s); */
3361                 array_objectarray_element_set(oa, 0, s);
3362 /*              array_objectarray_element_set(oa, 2, s); */
3363                 break; 
3364
3365     case THREAD_STATE_TERMINATED:
3366                 oa = builtin_anewarray(1, class_java_lang_String);
3367
3368                 if (oa == NULL)
3369                         return NULL;
3370
3371                 s = javastring_new(utf_new_char("TERMINATED"));
3372
3373                 if (s == NULL)
3374                         return NULL;
3375
3376                 array_objectarray_element_set(oa, 0, s);
3377                 break; 
3378
3379         default:
3380                 /* Unknown state - probably incompatible JDK version */
3381                 return NULL;
3382         }
3383
3384         return (jobjectArray) oa;
3385 }
3386
3387
3388 /* JVM_GetVersionInfo */
3389
3390 void JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)
3391 {
3392         log_println("JVM_GetVersionInfo: IMPLEMENT ME!");
3393 }
3394
3395
3396 /* OS: JVM_RegisterSignal */
3397
3398 void *JVM_RegisterSignal(jint sig, void *handler)
3399 {
3400         functionptr newHandler;
3401
3402         TRACEJVMCALLS("JVM_RegisterSignal(sig=%d, handler=%p)", sig, handler);
3403
3404         if (handler == (void *) 2)
3405                 newHandler = (functionptr) signal_thread_handler;
3406         else
3407                 newHandler = (functionptr) (uintptr_t) handler;
3408
3409         switch (sig) {
3410     case SIGILL:
3411     case SIGFPE:
3412     case SIGUSR1:
3413     case SIGSEGV:
3414                 /* These signals are already used by the VM. */
3415                 return (void *) -1;
3416
3417     case SIGQUIT:
3418                 /* This signal is used by the VM to dump thread stacks unless
3419                    ReduceSignalUsage is set, in which case the user is allowed
3420                    to set his own _native_ handler for this signal; thus, in
3421                    either case, we do not allow JVM_RegisterSignal to change
3422                    the handler. */
3423                 return (void *) -1;
3424
3425         case SIGHUP:
3426         case SIGINT:
3427         case SIGTERM:
3428                 break;
3429         }
3430
3431         signal_register_signal(sig, newHandler, 0);
3432
3433         /* XXX Should return old handler. */
3434
3435         return (void *) 2;
3436 }
3437
3438
3439 /* OS: JVM_RaiseSignal */
3440
3441 jboolean JVM_RaiseSignal(jint sig)
3442 {
3443         log_println("JVM_RaiseSignal: IMPLEMENT ME! sig=%s", sig);
3444         return false;
3445 }
3446
3447
3448 /* OS: JVM_FindSignal */
3449
3450 jint JVM_FindSignal(const char *name)
3451 {
3452         TRACEJVMCALLS("JVM_FindSignal(name=%s)", name);
3453
3454 #if defined(__LINUX__)
3455         if (strcmp(name, "HUP") == 0)
3456                 return SIGHUP;
3457
3458         if (strcmp(name, "INT") == 0)
3459                 return SIGINT;
3460
3461         if (strcmp(name, "TERM") == 0)
3462                 return SIGTERM;
3463 #else
3464 # error not implemented for this OS
3465 #endif
3466
3467         return -1;
3468 }
3469
3470
3471 /*
3472  * These are local overrides for various environment variables in Emacs.
3473  * Please do not remove this and leave it at the end of the file, where
3474  * Emacs will automagically detect them.
3475  * ---------------------------------------------------------------------
3476  * Local variables:
3477  * mode: c
3478  * indent-tabs-mode: t
3479  * c-basic-offset: 4
3480  * tab-width: 4
3481  * End:
3482  */