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