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