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