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