Merged branch subtype-trunk into default.
[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         java_handle_bytearray_t* ba = jlt.get_backtrace();
413
414         if (ba == 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 *) LLNI_array_data(ba);
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         java_handle_bytearray_t* 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 *) LLNI_array_data(ba);
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 (jobjectArray) 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         classinfo                 *c;
831         java_handle_objectarray_t *hoa;
832
833         TRACEJVMCALLS(("JVM_SetClassSigners(env=%p, cls=%p, signers=%p)", env, cls, signers));
834
835         c = LLNI_classinfo_unwrap(cls);
836
837         hoa = (java_handle_objectarray_t *) signers;
838
839     /* This call is ignored for primitive types and arrays.  Signers
840            are only set once, ClassLoader.java, and thus shouldn't be
841            called with an array.  Only the bootstrap loader creates
842            arrays. */
843
844         if (class_is_primitive(c) || class_is_array(c))
845                 return;
846
847         LLNI_classinfo_field_set(c, signers, hoa);
848 }
849
850
851 /* JVM_GetProtectionDomain */
852
853 jobject JVM_GetProtectionDomain(JNIEnv *env, jclass cls)
854 {
855         classinfo *c;
856
857         TRACEJVMCALLS(("JVM_GetProtectionDomain(env=%p, cls=%p)", env, cls));
858
859         c = LLNI_classinfo_unwrap(cls);
860
861         if (c == NULL) {
862                 exceptions_throw_nullpointerexception();
863                 return NULL;
864         }
865
866     /* Primitive types do not have a protection domain. */
867
868         if (class_is_primitive(c))
869                 return NULL;
870
871         return (jobject) c->protectiondomain;
872 }
873
874
875 /* JVM_SetProtectionDomain */
876
877 void JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain)
878 {
879         log_println("JVM_SetProtectionDomain: IMPLEMENT ME!");
880 }
881
882
883 /* JVM_DoPrivileged */
884
885 jobject JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException)
886 {
887         java_handle_t *h;
888         classinfo     *c;
889         methodinfo    *m;
890         java_handle_t *result;
891         java_handle_t *e;
892
893         TRACEJVMCALLS(("JVM_DoPrivileged(env=%p, cls=%p, action=%p, context=%p, wrapException=%d)", env, cls, action, context, wrapException));
894
895         h = (java_handle_t *) action;
896         LLNI_class_get(h, c);
897
898         if (action == NULL) {
899                 exceptions_throw_nullpointerexception();
900                 return NULL;
901         }
902
903         /* lookup run() method (throw no exceptions) */
904
905         m = class_resolveclassmethod(c, utf_run, utf_void__java_lang_Object, c,
906                                                                  false);
907
908         if ((m == NULL) || !(m->flags & ACC_PUBLIC) || (m->flags & ACC_STATIC)) {
909                 exceptions_throw_internalerror("No run method");
910                 return NULL;
911         }
912
913         /* XXX It seems something with a privileged stack needs to be done
914            here. */
915
916         result = vm_call_method(m, h);
917
918         e = exceptions_get_exception();
919
920         if (e != NULL) {
921                 if ( builtin_instanceof(e, class_java_lang_Exception) &&
922                         !builtin_instanceof(e, class_java_lang_RuntimeException)) {
923                         exceptions_clear_exception();
924                         exceptions_throw_privilegedactionexception(e);
925                 }
926
927                 return NULL;
928         }
929
930         return (jobject) result;
931 }
932
933
934 /* JVM_GetInheritedAccessControlContext */
935
936 jobject JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)
937 {
938         log_println("JVM_GetInheritedAccessControlContext: IMPLEMENT ME!");
939
940         return NULL;
941 }
942
943
944 /* JVM_GetStackAccessControlContext */
945
946 jobject JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)
947 {
948         TRACEJVMCALLS(("JVM_GetStackAccessControlContext(env=%p, cls=%p): IMPLEMENT ME!", env, cls));
949
950         /* XXX All stuff I tested so far works without that function.  At
951            some point we have to implement it, but I disable the output
952            for now to make IcedTea happy. */
953
954         return NULL;
955 }
956
957
958 /* JVM_IsArrayClass */
959
960 jboolean JVM_IsArrayClass(JNIEnv *env, jclass cls)
961 {
962         classinfo *c;
963
964         TRACEJVMCALLS(("JVM_IsArrayClass(env=%p, cls=%p)", env, cls));
965
966         c = LLNI_classinfo_unwrap(cls);
967
968         return class_is_array(c);
969 }
970
971
972 /* JVM_IsPrimitiveClass */
973
974 jboolean JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)
975 {
976         classinfo *c;
977
978         TRACEJVMCALLS(("JVM_IsPrimitiveClass(env=%p, cls=%p)", env, cls));
979
980         c = LLNI_classinfo_unwrap(cls);
981
982         return class_is_primitive(c);
983 }
984
985
986 /* JVM_GetComponentType */
987
988 jclass JVM_GetComponentType(JNIEnv *env, jclass cls)
989 {
990         classinfo *component;
991         classinfo *c;
992         
993         TRACEJVMCALLS(("JVM_GetComponentType(env=%p, cls=%p)", env, cls));
994
995         c = LLNI_classinfo_unwrap(cls);
996         
997         component = class_get_componenttype(c);
998
999         return (jclass) LLNI_classinfo_wrap(component);
1000 }
1001
1002
1003 /* JVM_GetClassModifiers */
1004
1005 jint JVM_GetClassModifiers(JNIEnv *env, jclass cls)
1006 {
1007         classinfo *c;
1008         int32_t    flags;
1009
1010         TRACEJVMCALLS(("JVM_GetClassModifiers(env=%p, cls=%p)", env, cls));
1011
1012         c = LLNI_classinfo_unwrap(cls);
1013
1014         flags = class_get_modifiers(c, false);
1015
1016         return flags;
1017 }
1018
1019
1020 /* JVM_GetDeclaredClasses */
1021
1022 jobjectArray JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)
1023 {
1024         classinfo                 *c;
1025         java_handle_objectarray_t *oa;
1026
1027         TRACEJVMCALLS(("JVM_GetDeclaredClasses(env=%p, ofClass=%p)", env, ofClass));
1028
1029         c = LLNI_classinfo_unwrap(ofClass);
1030
1031         oa = class_get_declaredclasses(c, false);
1032
1033         return (jobjectArray) oa;
1034 }
1035
1036
1037 /* JVM_GetDeclaringClass */
1038
1039 jclass JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)
1040 {
1041         classinfo *c;
1042         classinfo *dc;
1043
1044         TRACEJVMCALLS(("JVM_GetDeclaringClass(env=%p, ofClass=%p)", env, ofClass));
1045
1046         c = LLNI_classinfo_unwrap(ofClass);
1047
1048         dc = class_get_declaringclass(c);
1049
1050         return (jclass) LLNI_classinfo_wrap(dc);
1051 }
1052
1053
1054 /* JVM_GetClassSignature */
1055
1056 jstring JVM_GetClassSignature(JNIEnv *env, jclass cls)
1057 {
1058         classinfo     *c;
1059         utf           *u;
1060         java_handle_t *s;
1061
1062         TRACEJVMCALLS(("JVM_GetClassSignature(env=%p, cls=%p)", env, cls));
1063
1064         c = LLNI_classinfo_unwrap(cls);
1065
1066         /* Get the signature of the class. */
1067
1068         u = class_get_signature(c);
1069
1070         if (u == NULL)
1071                 return NULL;
1072
1073         /* Convert UTF-string to a Java-string. */
1074
1075         s = javastring_new(u);
1076
1077         return (jstring) s;
1078 }
1079
1080
1081 /* JVM_GetClassAnnotations */
1082
1083 jbyteArray JVM_GetClassAnnotations(JNIEnv *env, jclass cls)
1084 {
1085         TRACEJVMCALLS(("JVM_GetClassAnnotations(env=%p, cls=%p)", env, cls));
1086
1087         if (cls == NULL) {
1088                 exceptions_throw_nullpointerexception();
1089                 return NULL;
1090         }
1091         
1092         classinfo* c = LLNI_classinfo_unwrap(cls);
1093
1094         /* get annotations: */
1095         java_handle_bytearray_t* annotations = class_get_annotations(c);
1096
1097         return (jbyteArray) annotations;
1098 }
1099
1100
1101 /* JVM_GetFieldAnnotations */
1102
1103 jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
1104 {
1105         TRACEJVMCALLS(("JVM_GetFieldAnnotations(env=%p, field=%p)", env, field));
1106
1107         java_lang_reflect_Field jlrf(field);
1108
1109         if (jlrf.is_null()) {
1110                 exceptions_throw_nullpointerexception();
1111                 return NULL;
1112         }
1113
1114         return (jbyteArray) jlrf.get_annotations();
1115 }
1116
1117
1118 /* JVM_GetMethodAnnotations */
1119
1120 jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
1121 {
1122         TRACEJVMCALLS(("JVM_GetMethodAnnotations(env=%p, method=%p)", env, method));
1123
1124         java_lang_reflect_Method jlrm(method);
1125
1126         if (jlrm.is_null()) {
1127                 exceptions_throw_nullpointerexception();
1128                 return NULL;
1129         }
1130
1131         return (jbyteArray) jlrm.get_annotations();
1132 }
1133
1134
1135 /* JVM_GetMethodDefaultAnnotationValue */
1136
1137 jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
1138 {
1139         TRACEJVMCALLS(("JVM_GetMethodDefaultAnnotationValue(env=%p, method=%p)", env, method));
1140
1141         java_lang_reflect_Method jlrm(method);
1142
1143         if (jlrm.is_null()) {
1144                 exceptions_throw_nullpointerexception();
1145                 return NULL;
1146         }
1147
1148         return (jbyteArray) jlrm.get_annotationDefault();
1149 }
1150
1151
1152 /* JVM_GetMethodParameterAnnotations */
1153
1154 jbyteArray JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)
1155 {
1156         TRACEJVMCALLS(("JVM_GetMethodParameterAnnotations(env=%p, method=%p)", env, method));
1157
1158         java_lang_reflect_Method jlrm(method);
1159
1160         if (jlrm.is_null()) {
1161                 exceptions_throw_nullpointerexception();
1162                 return NULL;
1163         }
1164
1165         return (jbyteArray) jlrm.get_parameterAnnotations();
1166 }
1167
1168
1169 /* JVM_GetClassDeclaredFields */
1170
1171 jobjectArray JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1172 {
1173         classinfo                 *c;
1174         java_handle_objectarray_t *oa;
1175
1176         TRACEJVMCALLS(("JVM_GetClassDeclaredFields(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly));
1177
1178         c = LLNI_classinfo_unwrap(ofClass);
1179
1180         oa = class_get_declaredfields(c, publicOnly);
1181
1182         return (jobjectArray) oa;
1183 }
1184
1185
1186 /* JVM_GetClassDeclaredMethods */
1187
1188 jobjectArray JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1189 {
1190         TRACEJVMCALLS(("JVM_GetClassDeclaredMethods(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly));
1191
1192         classinfo* c = LLNI_classinfo_unwrap(ofClass);
1193
1194         java_handle_objectarray_t* oa = class_get_declaredmethods(c, publicOnly);
1195
1196         return (jobjectArray) oa;
1197 }
1198
1199
1200 /* JVM_GetClassDeclaredConstructors */
1201
1202 jobjectArray JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1203 {
1204         classinfo                 *c;
1205         java_handle_objectarray_t *oa;
1206
1207         TRACEJVMCALLS(("JVM_GetClassDeclaredConstructors(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly));
1208
1209         c = LLNI_classinfo_unwrap(ofClass);
1210
1211         oa = class_get_declaredconstructors(c, publicOnly);
1212
1213         return (jobjectArray) oa;
1214 }
1215
1216
1217 /* JVM_GetClassAccessFlags */
1218
1219 jint JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)
1220 {
1221         classinfo *c;
1222
1223         TRACEJVMCALLS(("JVM_GetClassAccessFlags(env=%p, cls=%p)", env, cls));
1224
1225         c = LLNI_classinfo_unwrap(cls);
1226
1227         /* Primitive type classes have the correct access flags. */
1228
1229         return c->flags & ACC_CLASS_REFLECT_MASK;
1230 }
1231
1232
1233 /* JVM_GetClassConstantPool */
1234
1235 jobject JVM_GetClassConstantPool(JNIEnv *env, jclass cls)
1236 {
1237 #if defined(ENABLE_ANNOTATIONS)
1238         TRACEJVMCALLS(("JVM_GetClassConstantPool(env=%p, cls=%p)", env, cls));
1239
1240         java_handle_t* h = native_new_and_init(class_sun_reflect_ConstantPool);
1241         sun_reflect_ConstantPool cp(h, cls);
1242         
1243         if (cp.is_null()) {
1244                 return NULL;
1245         }
1246         
1247         return (jobject) cp.get_handle();
1248 #else
1249         log_println("JVM_GetClassConstantPool(env=%p, cls=%p): not implemented in this configuration!", env, cls);
1250         return NULL;
1251 #endif
1252 }
1253
1254
1255 /* JVM_ConstantPoolGetSize */
1256
1257 jint JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool)
1258 {
1259         classinfo *c; /* classinfo of the class for which 'this' is the constant pool */
1260
1261         TRACEJVMCALLS(("JVM_ConstantPoolGetSize(env=%p, unused=%p, jcpool=%p)", env, unused, jcpool));
1262
1263         c = LLNI_classinfo_unwrap(jcpool);
1264
1265         return c->cpcount;
1266 }
1267
1268
1269 /* JVM_ConstantPoolGetClassAt */
1270
1271 jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1272 {
1273         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1274         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1275         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1276
1277         TRACEJVMCALLS(("JVM_ConstantPoolGetClassAt(env=%p, jcpool=%p, index=%d)", env, jcpool, index));
1278
1279         c = LLNI_classinfo_unwrap(jcpool);
1280
1281         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1282
1283         if (ref == NULL) {
1284                 exceptions_throw_illegalargumentexception();
1285                 return NULL;
1286         }
1287
1288         result = resolve_classref_eager(ref);
1289
1290         return (jclass) LLNI_classinfo_wrap(result);
1291 }
1292
1293
1294 /* JVM_ConstantPoolGetClassAtIfLoaded */
1295
1296 jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1297 {
1298         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1299         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1300         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1301
1302         TRACEJVMCALLS(("JVM_ConstantPoolGetClassAtIfLoaded(env=%p, unused=%p, jcpool=%p, index=%d)", env, unused, jcpool, index));
1303
1304         c = LLNI_classinfo_unwrap(jcpool);
1305
1306         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1307
1308         if (ref == NULL) {
1309                 exceptions_throw_illegalargumentexception();
1310                 return NULL;
1311         }
1312         
1313         if (!resolve_classref(NULL, ref, resolveLazy, true, true, &result)) {
1314                 return NULL;
1315         }
1316
1317         if ((result == NULL) || !(result->state & CLASS_LOADED)) {
1318                 return NULL;
1319         }
1320         
1321         return (jclass) LLNI_classinfo_wrap(result);
1322 }
1323
1324
1325 /* JVM_ConstantPoolGetMethodAt */
1326
1327 jobject JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1328 {
1329         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1330         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1331         
1332         TRACEJVMCALLS(("JVM_ConstantPoolGetMethodAt: jcpool=%p, index=%d", jcpool, index));
1333         
1334         cls = LLNI_classinfo_unwrap(jcpool);
1335         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1336         
1337         if (ref == NULL) {
1338                 exceptions_throw_illegalargumentexception();
1339                 return NULL;
1340         }
1341
1342         // Create a new java.lang.reflect.Method Java object.
1343         /* XXX: is that right? or do I have to use resolve_method_*? */
1344         java_lang_reflect_Method jlrm(ref->p.method);
1345
1346         return (jobject) jlrm.get_handle();
1347 }
1348
1349
1350 /* JVM_ConstantPoolGetMethodAtIfLoaded */
1351
1352 jobject JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1353 {
1354         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1355         classinfo *c = NULL;  /* resolved declaring class of the method */
1356         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1357
1358         TRACEJVMCALLS(("JVM_ConstantPoolGetMethodAtIfLoaded: jcpool=%p, index=%d", jcpool, index));
1359
1360         cls = LLNI_classinfo_unwrap(jcpool);
1361         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1362
1363         if (ref == NULL) {
1364                 exceptions_throw_illegalargumentexception();
1365                 return NULL;
1366         }
1367
1368         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1369                 return NULL;
1370         }
1371
1372         if (c == NULL || !(c->state & CLASS_LOADED)) {
1373                 return NULL;
1374         }
1375
1376         // Create a new java.lang.reflect.Method Java object.
1377         java_lang_reflect_Method jlrm(ref->p.method);
1378
1379         return (jobject) jlrm.get_handle();
1380 }
1381
1382
1383 /* JVM_ConstantPoolGetFieldAt */
1384
1385 jobject JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1386 {
1387         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1388         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1389         
1390         TRACEJVMCALLS(("JVM_ConstantPoolGetFieldAt: jcpool=%p, index=%d", jcpool, index));
1391
1392         cls = LLNI_classinfo_unwrap(jcpool);
1393         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1394
1395         if (ref == NULL) {
1396                 exceptions_throw_illegalargumentexception();
1397                 return NULL;
1398         }
1399
1400         // Create a new java.lang.reflect.Field Java object.
1401         java_lang_reflect_Field jlrf(ref->p.field);
1402
1403         return (jobject) jlrf.get_handle();
1404 }
1405
1406
1407 /* JVM_ConstantPoolGetFieldAtIfLoaded */
1408
1409 jobject JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1410 {
1411         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1412         classinfo *c;         /* resolved declaring class for the field */
1413         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1414
1415         TRACEJVMCALLS(("JVM_ConstantPoolGetFieldAtIfLoaded: jcpool=%p, index=%d", jcpool, index));
1416
1417         cls = LLNI_classinfo_unwrap(jcpool);
1418         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1419
1420         if (ref == NULL) {
1421                 exceptions_throw_illegalargumentexception();
1422                 return NULL;
1423         }
1424
1425         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1426                 return NULL;
1427         }
1428
1429         if (c == NULL || !(c->state & CLASS_LOADED)) {
1430                 return NULL;
1431         }
1432
1433         // Create a new java.lang.reflect.Field Java object.
1434         java_lang_reflect_Field jlrf(ref->p.field);
1435
1436         return (jobject) jlrf.get_handle();
1437 }
1438
1439
1440 /* JVM_ConstantPoolGetMemberRefInfoAt */
1441
1442 jobjectArray JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1443 {
1444         log_println("JVM_ConstantPoolGetMemberRefInfoAt: jcpool=%p, index=%d, IMPLEMENT ME!", jcpool, index);
1445
1446         /* TODO: implement. (this is yet unused be OpenJDK but, so very low priority) */
1447
1448         return NULL;
1449 }
1450
1451
1452 /* JVM_ConstantPoolGetIntAt */
1453
1454 jint JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1455 {
1456         constant_integer *ref; /* reference to the int value in constant pool at index 'index' */
1457         classinfo *cls;        /* classinfo of the class for which 'this' is the constant pool */
1458
1459         TRACEJVMCALLS(("JVM_ConstantPoolGetIntAt: jcpool=%p, index=%d", jcpool, index));
1460
1461         cls = LLNI_classinfo_unwrap(jcpool);
1462         ref = (constant_integer*)class_getconstant(cls, index, CONSTANT_Integer);
1463
1464         if (ref == NULL) {
1465                 exceptions_throw_illegalargumentexception();
1466                 return 0;
1467         }
1468
1469         return ref->value;
1470 }
1471
1472
1473 /* JVM_ConstantPoolGetLongAt */
1474
1475 jlong JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1476 {
1477         constant_long *ref; /* reference to the long value in constant pool at index 'index' */
1478         classinfo *cls;     /* classinfo of the class for which 'this' is the constant pool */
1479
1480         TRACEJVMCALLS(("JVM_ConstantPoolGetLongAt: jcpool=%p, index=%d", jcpool, index));
1481
1482         cls = LLNI_classinfo_unwrap(jcpool);
1483         ref = (constant_long*)class_getconstant(cls, index, CONSTANT_Long);
1484
1485         if (ref == NULL) {
1486                 exceptions_throw_illegalargumentexception();
1487                 return 0;
1488         }
1489
1490         return ref->value;
1491 }
1492
1493
1494 /* JVM_ConstantPoolGetFloatAt */
1495
1496 jfloat JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1497 {
1498         constant_float *ref; /* reference to the float value in constant pool at index 'index' */
1499         classinfo *cls;      /* classinfo of the class for which 'this' is the constant pool */
1500
1501         TRACEJVMCALLS(("JVM_ConstantPoolGetFloatAt: jcpool=%p, index=%d", jcpool, index));
1502
1503         cls = LLNI_classinfo_unwrap(jcpool);
1504         ref = (constant_float*)class_getconstant(cls, index, CONSTANT_Float);
1505
1506         if (ref == NULL) {
1507                 exceptions_throw_illegalargumentexception();
1508                 return 0;
1509         }
1510
1511         return ref->value;
1512 }
1513
1514
1515 /* JVM_ConstantPoolGetDoubleAt */
1516
1517 jdouble JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1518 {
1519         constant_double *ref; /* reference to the double value in constant pool at index 'index' */
1520         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1521
1522         TRACEJVMCALLS(("JVM_ConstantPoolGetDoubleAt: jcpool=%p, index=%d", jcpool, index));
1523
1524         cls = LLNI_classinfo_unwrap(jcpool);
1525         ref = (constant_double*)class_getconstant(cls, index, CONSTANT_Double);
1526
1527         if (ref == NULL) {
1528                 exceptions_throw_illegalargumentexception();
1529                 return 0;
1530         }
1531
1532         return ref->value;
1533 }
1534
1535
1536 /* JVM_ConstantPoolGetStringAt */
1537
1538 jstring JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1539 {
1540         utf *ref;       /* utf object for the string in constant pool at index 'index' */
1541         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1542
1543         TRACEJVMCALLS(("JVM_ConstantPoolGetStringAt: jcpool=%p, index=%d", jcpool, index));
1544         
1545         cls = LLNI_classinfo_unwrap(jcpool);
1546         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
1547
1548         if (ref == NULL) {
1549                 exceptions_throw_illegalargumentexception();
1550                 return NULL;
1551         }
1552
1553         /* XXX: I hope literalstring_new is the right Function. */
1554         return (jstring)literalstring_new(ref);
1555 }
1556
1557
1558 /* JVM_ConstantPoolGetUTF8At */
1559
1560 jstring JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1561 {
1562         utf *ref; /* utf object for the utf8 data in constant pool at index 'index' */
1563         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1564
1565         TRACEJVMCALLS(("JVM_ConstantPoolGetUTF8At: jcpool=%p, index=%d", jcpool, index));
1566
1567         cls = LLNI_classinfo_unwrap(jcpool);
1568         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
1569
1570         if (ref == NULL) {
1571                 exceptions_throw_illegalargumentexception();
1572                 return NULL;
1573         }
1574
1575         /* XXX: I hope literalstring_new is the right Function. */
1576         return (jstring)literalstring_new(ref);
1577 }
1578
1579
1580 /* JVM_DesiredAssertionStatus */
1581
1582 jboolean JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)
1583 {
1584 #if defined(ENABLE_ASSERTION)
1585         classinfo         *c;
1586         jboolean           status;
1587         utf               *name;
1588
1589         TRACEJVMCALLS(("JVM_DesiredAssertionStatus(env=%p, unused=%p, cls=%p)", env, unused, cls));
1590
1591         c = LLNI_classinfo_unwrap(cls);
1592
1593         if (c->classloader == NULL) {
1594                 status = (jboolean)assertion_system_enabled;
1595         }
1596         else {
1597                 status = (jboolean)assertion_user_enabled;
1598         }
1599
1600         if (list_assertion_names != NULL) {
1601                 for (List<assertion_name_t*>::iterator it = list_assertion_names->begin();
1602                          it != list_assertion_names->end(); it++) {
1603                         assertion_name_t* item = *it;
1604
1605                         name = utf_new_char(item->name);
1606                         if (name == c->packagename) {
1607                                 status = (jboolean)item->enabled;
1608                         }
1609                         else if (name == c->name) {
1610                                 status = (jboolean)item->enabled;
1611                         }
1612                 }
1613         }
1614
1615         return status;
1616 #else
1617         return (jboolean)false;
1618 #endif
1619 }
1620
1621
1622 /* JVM_AssertionStatusDirectives */
1623
1624 jobject JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)
1625 {
1626         java_handle_objectarray_t             *classes;
1627         java_handle_objectarray_t             *packages;
1628         java_booleanarray_t                   *classEnabled;
1629         java_booleanarray_t                   *packageEnabled;
1630 #if defined(ENABLE_ASSERTION)
1631         java_handle_t                         *js;
1632         s4                                     i, j;
1633 #endif
1634
1635         TRACEJVMCALLS(("JVM_AssertionStatusDirectives(env=%p, unused=%p)", env, unused));
1636
1637 #if defined(ENABLE_ASSERTION)
1638         classes = builtin_anewarray(assertion_class_count, class_java_lang_Object);
1639 #else
1640         classes = builtin_anewarray(0, class_java_lang_Object);
1641 #endif
1642         if (classes == NULL)
1643                 return NULL;
1644
1645 #if defined(ENABLE_ASSERTION)
1646         packages = builtin_anewarray(assertion_package_count, class_java_lang_Object);
1647 #else
1648         packages = builtin_anewarray(0, class_java_lang_Object);
1649 #endif
1650         if (packages == NULL)
1651                 return NULL;
1652         
1653 #if defined(ENABLE_ASSERTION)
1654         classEnabled = builtin_newarray_boolean(assertion_class_count);
1655 #else
1656         classEnabled = builtin_newarray_boolean(0);
1657 #endif
1658         if (classEnabled == NULL)
1659                 return NULL;
1660
1661 #if defined(ENABLE_ASSERTION)
1662         packageEnabled = builtin_newarray_boolean(assertion_package_count);
1663 #else
1664         packageEnabled = builtin_newarray_boolean(0);
1665 #endif
1666         if (packageEnabled == NULL)
1667                 return NULL;
1668
1669 #if defined(ENABLE_ASSERTION)
1670         /* initialize arrays */
1671
1672         if (list_assertion_names != NULL) {
1673                 i = 0;
1674                 j = 0;
1675                 
1676                 for (List<assertion_name_t*>::iterator it = list_assertion_names->begin(); it != list_assertion_names->end(); it++) {
1677                         assertion_name_t* item = *it;
1678
1679                         js = javastring_new_from_ascii(item->name);
1680                         if (js == NULL) {
1681                                 return NULL;
1682                         }
1683
1684                         if (item->package == false) {
1685                                 classes->data[i] = js;
1686                                 classEnabled->data[i] = (jboolean) item->enabled;
1687                                 i += 1;
1688                         }
1689                         else {
1690                                 packages->data[j] = js;
1691                                 packageEnabled->data[j] = (jboolean) item->enabled;
1692                                 j += 1;
1693                         }
1694                 }
1695         }
1696 #endif
1697
1698         /* set instance fields */
1699
1700         java_lang_AssertionStatusDirectives jlasd(classes, classEnabled, packages, packageEnabled);
1701
1702         return (jobject) jlasd.get_handle();
1703 }
1704
1705
1706 /* JVM_GetClassNameUTF */
1707
1708 const char *JVM_GetClassNameUTF(JNIEnv *env, jclass cls)
1709 {
1710         log_println("JVM_GetClassNameUTF: IMPLEMENT ME!");
1711
1712         return NULL;
1713 }
1714
1715
1716 /* JVM_GetClassCPTypes */
1717
1718 void JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)
1719 {
1720         log_println("JVM_GetClassCPTypes: IMPLEMENT ME!");
1721 }
1722
1723
1724 /* JVM_GetClassCPEntriesCount */
1725
1726 jint JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)
1727 {
1728         log_println("JVM_GetClassCPEntriesCount: IMPLEMENT ME!");
1729
1730         return 0;
1731 }
1732
1733
1734 /* JVM_GetClassFieldsCount */
1735
1736 jint JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)
1737 {
1738         log_println("JVM_GetClassFieldsCount: IMPLEMENT ME!");
1739
1740         return 0;
1741 }
1742
1743
1744 /* JVM_GetClassMethodsCount */
1745
1746 jint JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)
1747 {
1748         log_println("JVM_GetClassMethodsCount: IMPLEMENT ME!");
1749
1750         return 0;
1751 }
1752
1753
1754 /* JVM_GetMethodIxExceptionIndexes */
1755
1756 void JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)
1757 {
1758         log_println("JVM_GetMethodIxExceptionIndexes: IMPLEMENT ME!");
1759 }
1760
1761
1762 /* JVM_GetMethodIxExceptionsCount */
1763
1764 jint JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)
1765 {
1766         log_println("JVM_GetMethodIxExceptionsCount: IMPLEMENT ME!");
1767
1768         return 0;
1769 }
1770
1771
1772 /* JVM_GetMethodIxByteCode */
1773
1774 void JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)
1775 {
1776         log_println("JVM_GetMethodIxByteCode: IMPLEMENT ME!");
1777 }
1778
1779
1780 /* JVM_GetMethodIxByteCodeLength */
1781
1782 jint JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)
1783 {
1784         log_println("JVM_GetMethodIxByteCodeLength: IMPLEMENT ME!");
1785
1786         return 0;
1787 }
1788
1789
1790 /* JVM_GetMethodIxExceptionTableEntry */
1791
1792 void JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)
1793 {
1794         log_println("JVM_GetMethodIxExceptionTableEntry: IMPLEMENT ME!");
1795 }
1796
1797
1798 /* JVM_GetMethodIxExceptionTableLength */
1799
1800 jint JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)
1801 {
1802         log_println("JVM_GetMethodIxExceptionTableLength: IMPLEMENT ME!");
1803
1804         return 0;
1805 }
1806
1807
1808 /* JVM_GetMethodIxModifiers */
1809
1810 jint JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)
1811 {
1812         log_println("JVM_GetMethodIxModifiers: IMPLEMENT ME!");
1813
1814         return 0;
1815 }
1816
1817
1818 /* JVM_GetFieldIxModifiers */
1819
1820 jint JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)
1821 {
1822         log_println("JVM_GetFieldIxModifiers: IMPLEMENT ME!");
1823
1824         return 0;
1825 }
1826
1827
1828 /* JVM_GetMethodIxLocalsCount */
1829
1830 jint JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)
1831 {
1832         log_println("JVM_GetMethodIxLocalsCount: IMPLEMENT ME!");
1833
1834         return 0;
1835 }
1836
1837
1838 /* JVM_GetMethodIxArgsSize */
1839
1840 jint JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)
1841 {
1842         log_println("JVM_GetMethodIxArgsSize: IMPLEMENT ME!");
1843
1844         return 0;
1845 }
1846
1847
1848 /* JVM_GetMethodIxMaxStack */
1849
1850 jint JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)
1851 {
1852         log_println("JVM_GetMethodIxMaxStack: IMPLEMENT ME!");
1853
1854         return 0;
1855 }
1856
1857
1858 /* JVM_IsConstructorIx */
1859
1860 jboolean JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)
1861 {
1862         log_println("JVM_IsConstructorIx: IMPLEMENT ME!");
1863
1864         return 0;
1865 }
1866
1867
1868 /* JVM_GetMethodIxNameUTF */
1869
1870 const char *JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)
1871 {
1872         log_println("JVM_GetMethodIxNameUTF: IMPLEMENT ME!");
1873
1874         return NULL;
1875 }
1876
1877
1878 /* JVM_GetMethodIxSignatureUTF */
1879
1880 const char *JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)
1881 {
1882         log_println("JVM_GetMethodIxSignatureUTF: IMPLEMENT ME!");
1883
1884         return NULL;
1885 }
1886
1887
1888 /* JVM_GetCPFieldNameUTF */
1889
1890 const char *JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1891 {
1892         log_println("JVM_GetCPFieldNameUTF: IMPLEMENT ME!");
1893
1894         return NULL;
1895 }
1896
1897
1898 /* JVM_GetCPMethodNameUTF */
1899
1900 const char *JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1901 {
1902         log_println("JVM_GetCPMethodNameUTF: IMPLEMENT ME!");
1903
1904         return NULL;
1905 }
1906
1907
1908 /* JVM_GetCPMethodSignatureUTF */
1909
1910 const char *JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1911 {
1912         log_println("JVM_GetCPMethodSignatureUTF: IMPLEMENT ME!");
1913
1914         return NULL;
1915 }
1916
1917
1918 /* JVM_GetCPFieldSignatureUTF */
1919
1920 const char *JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1921 {
1922         log_println("JVM_GetCPFieldSignatureUTF: IMPLEMENT ME!");
1923
1924         return NULL;
1925 }
1926
1927
1928 /* JVM_GetCPClassNameUTF */
1929
1930 const char *JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1931 {
1932         log_println("JVM_GetCPClassNameUTF: IMPLEMENT ME!");
1933
1934         return NULL;
1935 }
1936
1937
1938 /* JVM_GetCPFieldClassNameUTF */
1939
1940 const char *JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1941 {
1942         log_println("JVM_GetCPFieldClassNameUTF: IMPLEMENT ME!");
1943
1944         return NULL;
1945 }
1946
1947
1948 /* JVM_GetCPMethodClassNameUTF */
1949
1950 const char *JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1951 {
1952         log_println("JVM_GetCPMethodClassNameUTF: IMPLEMENT ME!");
1953
1954         return NULL;
1955 }
1956
1957
1958 /* JVM_GetCPFieldModifiers */
1959
1960 jint JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
1961 {
1962         log_println("JVM_GetCPFieldModifiers: IMPLEMENT ME!");
1963
1964         return 0;
1965 }
1966
1967
1968 /* JVM_GetCPMethodModifiers */
1969
1970 jint JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
1971 {
1972         log_println("JVM_GetCPMethodModifiers: IMPLEMENT ME!");
1973
1974         return 0;
1975 }
1976
1977
1978 /* JVM_ReleaseUTF */
1979
1980 void JVM_ReleaseUTF(const char *utf)
1981 {
1982         log_println("JVM_ReleaseUTF: IMPLEMENT ME!");
1983 }
1984
1985
1986 /* JVM_IsSameClassPackage */
1987
1988 jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)
1989 {
1990         log_println("JVM_IsSameClassPackage: IMPLEMENT ME!");
1991
1992         return 0;
1993 }
1994
1995
1996 /* JVM_Open */
1997
1998 /* Taken from: hotspot/src/share/vm/prims/jvm.h */
1999
2000 /*
2001  * JVM I/O error codes
2002  */
2003 #define JVM_EEXIST       -100
2004
2005 jint JVM_Open(const char* fname, jint flags, jint mode)
2006 {
2007         int result;
2008
2009         TRACEJVMCALLS(("JVM_Open(fname=%s, flags=%d, mode=%d)", fname, flags, mode));
2010
2011         HPI& hpi = VM::get_current()->get_hpi();
2012         result = hpi.get_file().Open(fname, flags, mode);
2013
2014         if (result >= 0) {
2015                 return result;
2016         }
2017         else {
2018                 switch (errno) {
2019                 case EEXIST:
2020                         return JVM_EEXIST;
2021                 default:
2022                         return -1;
2023                 }
2024         }
2025 }
2026
2027
2028 /* JVM_Close */
2029
2030 jint JVM_Close(jint fd)
2031 {
2032         TRACEJVMCALLS(("JVM_Close(fd=%d)", fd));
2033
2034         HPI& hpi = VM::get_current()->get_hpi();
2035         return hpi.get_file().Close(fd);
2036 }
2037
2038
2039 /* JVM_Read */
2040
2041 jint JVM_Read(jint fd, char* buf, jint nbytes)
2042 {
2043         TRACEJVMCALLS(("JVM_Read(fd=%d, buf=%p, nbytes=%d)", fd, buf, nbytes));
2044
2045         HPI& hpi = VM::get_current()->get_hpi();
2046         return (jint) hpi.get_file().Read(fd, buf, nbytes);
2047 }
2048
2049
2050 /* JVM_Write */
2051
2052 jint JVM_Write(jint fd, char* buf, jint nbytes)
2053 {
2054         TRACEJVMCALLS(("JVM_Write(fd=%d, buf=%s, nbytes=%d)", fd, buf, nbytes));
2055
2056         HPI& hpi = VM::get_current()->get_hpi();
2057         return (jint) hpi.get_file().Write(fd, buf, nbytes);
2058 }
2059
2060
2061 /* JVM_Available */
2062
2063 jint JVM_Available(jint fd, jlong* pbytes)
2064 {
2065         TRACEJVMCALLS(("JVM_Available(fd=%d, pbytes=%p)", fd, pbytes));
2066
2067         HPI& hpi = VM::get_current()->get_hpi();
2068         return hpi.get_file().Available(fd, pbytes);
2069 }
2070
2071
2072 /* JVM_Lseek */
2073
2074 jlong JVM_Lseek(jint fd, jlong offset, jint whence)
2075 {
2076         TRACEJVMCALLS(("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence));
2077
2078         HPI& hpi = VM::get_current()->get_hpi();
2079         return hpi.get_file().Seek(fd, (off_t) offset, whence);
2080 }
2081
2082
2083 /* JVM_SetLength */
2084
2085 jint JVM_SetLength(jint fd, jlong length)
2086 {
2087         TRACEJVMCALLS(("JVM_SetLength(fd=%d, length=%ld)", length));
2088
2089         HPI& hpi = VM::get_current()->get_hpi();
2090         return hpi.get_file().SetLength(fd, length);
2091 }
2092
2093
2094 /* JVM_Sync */
2095
2096 jint JVM_Sync(jint fd)
2097 {
2098         TRACEJVMCALLS(("JVM_Sync(fd=%d)", fd));
2099
2100         HPI& hpi = VM::get_current()->get_hpi();
2101         return hpi.get_file().Sync(fd);
2102 }
2103
2104
2105 /* JVM_StartThread */
2106
2107 void JVM_StartThread(JNIEnv* env, jobject jthread)
2108 {
2109         TRACEJVMCALLS(("JVM_StartThread(env=%p, jthread=%p)", env, jthread));
2110
2111         threads_thread_start((java_handle_t *) jthread);
2112 }
2113
2114
2115 /* JVM_StopThread */
2116
2117 void JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)
2118 {
2119         log_println("JVM_StopThread: Deprecated.  Not implemented.");
2120 }
2121
2122
2123 /* JVM_IsThreadAlive */
2124
2125 jboolean JVM_IsThreadAlive(JNIEnv* env, jobject jthread)
2126 {
2127         java_handle_t *h;
2128         threadobject  *t;
2129         bool           result;
2130
2131         TRACEJVMCALLS(("JVM_IsThreadAlive(env=%p, jthread=%p)", env, jthread));
2132
2133         h = (java_handle_t *) jthread;
2134         t = thread_get_thread(h);
2135
2136         /* The threadobject is null when a thread is created in Java. */
2137
2138         if (t == NULL)
2139                 return 0;
2140
2141         result = threads_thread_is_alive(t);
2142
2143         return result;
2144 }
2145
2146
2147 /* JVM_SuspendThread */
2148
2149 void JVM_SuspendThread(JNIEnv* env, jobject jthread)
2150 {
2151         log_println("JVM_SuspendThread: Deprecated.  Not implemented.");
2152 }
2153
2154
2155 /* JVM_ResumeThread */
2156
2157 void JVM_ResumeThread(JNIEnv* env, jobject jthread)
2158 {
2159         log_println("JVM_ResumeThread: Deprecated.  Not implemented.");
2160 }
2161
2162
2163 /* JVM_SetThreadPriority */
2164
2165 void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)
2166 {
2167         java_handle_t *h;
2168         threadobject  *t;
2169
2170         TRACEJVMCALLS(("JVM_SetThreadPriority(env=%p, jthread=%p, prio=%d)", env, jthread, prio));
2171
2172         h = (java_handle_t *) jthread;
2173         t = thread_get_thread(h);
2174
2175         /* The threadobject is null when a thread is created in Java. The
2176            priority is set later during startup. */
2177
2178         if (t == NULL)
2179                 return;
2180
2181         threads_set_thread_priority(t->tid, prio);
2182 }
2183
2184
2185 /* JVM_Yield */
2186
2187 void JVM_Yield(JNIEnv *env, jclass threadClass)
2188 {
2189         TRACEJVMCALLS(("JVM_Yield(env=%p, threadClass=%p)", env, threadClass));
2190
2191         threads_yield();
2192 }
2193
2194
2195 /* JVM_Sleep */
2196
2197 void JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)
2198 {
2199         TRACEJVMCALLS(("JVM_Sleep(env=%p, threadClass=%p, millis=%ld)", env, threadClass, millis));
2200
2201         threads_sleep(millis, 0);
2202 }
2203
2204
2205 /* JVM_CurrentThread */
2206
2207 jobject JVM_CurrentThread(JNIEnv* env, jclass threadClass)
2208 {
2209         java_object_t *o;
2210
2211         TRACEJVMCALLSVERBOSE(("JVM_CurrentThread(env=%p, threadClass=%p)", env, threadClass));
2212
2213         o = thread_get_current_object();
2214
2215         return (jobject) o;
2216 }
2217
2218
2219 /* JVM_CountStackFrames */
2220
2221 jint JVM_CountStackFrames(JNIEnv* env, jobject jthread)
2222 {
2223         log_println("JVM_CountStackFrames: Deprecated.  Not implemented.");
2224
2225         return 0;
2226 }
2227
2228
2229 /* JVM_Interrupt */
2230
2231 void JVM_Interrupt(JNIEnv* env, jobject jthread)
2232 {
2233         java_handle_t *h;
2234         threadobject  *t;
2235
2236         TRACEJVMCALLS(("JVM_Interrupt(env=%p, jthread=%p)", env, jthread));
2237
2238         h = (java_handle_t *) jthread;
2239         t = thread_get_thread(h);
2240
2241         /* The threadobject is null when a thread is created in Java. */
2242
2243         if (t == NULL)
2244                 return;
2245
2246         threads_thread_interrupt(t);
2247 }
2248
2249
2250 /* JVM_IsInterrupted */
2251
2252 jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)
2253 {
2254         java_handle_t *h;
2255         threadobject  *t;
2256         jboolean       interrupted;
2257
2258         TRACEJVMCALLS(("JVM_IsInterrupted(env=%p, jthread=%p, clear_interrupted=%d)", env, jthread, clear_interrupted));
2259
2260         h = (java_handle_t *) jthread;
2261         t = thread_get_thread(h);
2262
2263         /* The threadobject is null when a thread is created in Java. */
2264
2265         if (t == NULL)
2266                 return JNI_FALSE;
2267
2268         interrupted = thread_is_interrupted(t);
2269
2270         if (interrupted && clear_interrupted)
2271                 thread_set_interrupted(t, false);
2272
2273         return interrupted;
2274 }
2275
2276
2277 /* JVM_HoldsLock */
2278
2279 jboolean JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)
2280 {
2281         java_handle_t *h;
2282         bool           result;
2283
2284         TRACEJVMCALLS(("JVM_HoldsLock(env=%p, threadClass=%p, obj=%p)", env, threadClass, obj));
2285
2286         h = (java_handle_t *) obj;
2287
2288         if (h == NULL) {
2289                 exceptions_throw_nullpointerexception();
2290                 return JNI_FALSE;
2291         }
2292
2293         result = lock_is_held_by_current_thread(h);
2294
2295         return result;
2296 }
2297
2298
2299 /* JVM_DumpAllStacks */
2300
2301 void JVM_DumpAllStacks(JNIEnv* env, jclass unused)
2302 {
2303         log_println("JVM_DumpAllStacks: IMPLEMENT ME!");
2304 }
2305
2306
2307 /* JVM_CurrentLoadedClass */
2308
2309 jclass JVM_CurrentLoadedClass(JNIEnv *env)
2310 {
2311         log_println("JVM_CurrentLoadedClass: IMPLEMENT ME!");
2312
2313         return NULL;
2314 }
2315
2316
2317 /* JVM_CurrentClassLoader */
2318
2319 jobject JVM_CurrentClassLoader(JNIEnv *env)
2320 {
2321     /* XXX if a method in a class in a trusted loader is in a
2322            doPrivileged, return NULL */
2323
2324         log_println("JVM_CurrentClassLoader: IMPLEMENT ME!");
2325
2326         return NULL;
2327 }
2328
2329
2330 /* JVM_GetClassContext */
2331
2332 jobjectArray JVM_GetClassContext(JNIEnv *env)
2333 {
2334         TRACEJVMCALLS(("JVM_GetClassContext(env=%p)", env));
2335
2336         return (jobjectArray) stacktrace_getClassContext();
2337 }
2338
2339
2340 /* JVM_ClassDepth */
2341
2342 jint JVM_ClassDepth(JNIEnv *env, jstring name)
2343 {
2344         log_println("JVM_ClassDepth: IMPLEMENT ME!");
2345
2346         return 0;
2347 }
2348
2349
2350 /* JVM_ClassLoaderDepth */
2351
2352 jint JVM_ClassLoaderDepth(JNIEnv *env)
2353 {
2354         log_println("JVM_ClassLoaderDepth: IMPLEMENT ME!");
2355
2356         return 0;
2357 }
2358
2359
2360 /* JVM_GetSystemPackage */
2361
2362 jstring JVM_GetSystemPackage(JNIEnv *env, jstring name)
2363 {
2364         java_handle_t *s;
2365         utf *u;
2366         utf *result;
2367
2368         TRACEJVMCALLS(("JVM_GetSystemPackage(env=%p, name=%p)", env, name));
2369
2370 /*      s = Package::find(name); */
2371         u = javastring_toutf((java_handle_t *) name, false);
2372
2373         result = Package::find(u);
2374
2375         if (result != NULL)
2376                 s = javastring_new(result);
2377         else
2378                 s = NULL;
2379
2380         return (jstring) s;
2381 }
2382
2383
2384 /* JVM_GetSystemPackages */
2385
2386 jobjectArray JVM_GetSystemPackages(JNIEnv *env)
2387 {
2388         log_println("JVM_GetSystemPackages: IMPLEMENT ME!");
2389
2390         return NULL;
2391 }
2392
2393
2394 /* JVM_AllocateNewObject */
2395
2396 jobject JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass)
2397 {
2398         log_println("JVM_AllocateNewObject: IMPLEMENT ME!");
2399
2400         return NULL;
2401 }
2402
2403
2404 /* JVM_AllocateNewArray */
2405
2406 jobject JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length)
2407 {
2408         log_println("JVM_AllocateNewArray: IMPLEMENT ME!");
2409
2410         return NULL;
2411 }
2412
2413
2414 /* JVM_LatestUserDefinedLoader */
2415
2416 jobject JVM_LatestUserDefinedLoader(JNIEnv *env)
2417 {
2418         classloader_t *cl;
2419
2420         TRACEJVMCALLS(("JVM_LatestUserDefinedLoader(env=%p)", env));
2421
2422         cl = stacktrace_first_nonnull_classloader();
2423
2424         return (jobject) cl;
2425 }
2426
2427
2428 /* JVM_LoadClass0 */
2429
2430 jclass JVM_LoadClass0(JNIEnv *env, jobject receiver, jclass currClass, jstring currClassName)
2431 {
2432         log_println("JVM_LoadClass0: IMPLEMENT ME!");
2433
2434         return NULL;
2435 }
2436
2437
2438 /* JVM_GetArrayLength */
2439
2440 jint JVM_GetArrayLength(JNIEnv *env, jobject arr)
2441 {
2442         java_handle_t *a;
2443
2444         TRACEJVMCALLS(("JVM_GetArrayLength(arr=%p)", arr));
2445
2446         a = (java_handle_t *) arr;
2447
2448         return array_length_get(a);
2449 }
2450
2451
2452 /* JVM_GetArrayElement */
2453
2454 jobject JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)
2455 {
2456         java_handle_t *a;
2457         java_handle_t *o;
2458
2459         TRACEJVMCALLS(("JVM_GetArrayElement(env=%p, arr=%p, index=%d)", env, arr, index));
2460
2461         a = (java_handle_t *) arr;
2462
2463 /*      if (!class_is_array(a->objheader.vftbl->class)) { */
2464 /*              exceptions_throw_illegalargumentexception(); */
2465 /*              return NULL; */
2466 /*      } */
2467
2468         o = array_element_get(a, index);
2469
2470         return (jobject) o;
2471 }
2472
2473
2474 /* JVM_GetPrimitiveArrayElement */
2475
2476 jvalue JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)
2477 {
2478         jvalue jv;
2479
2480         log_println("JVM_GetPrimitiveArrayElement: IMPLEMENT ME!");
2481
2482         jv.l = NULL;
2483
2484         return jv;
2485 }
2486
2487
2488 /* JVM_SetArrayElement */
2489
2490 void JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)
2491 {
2492         java_handle_t *a;
2493         java_handle_t *value;
2494
2495         TRACEJVMCALLS(("JVM_SetArrayElement(env=%p, arr=%p, index=%d, val=%p)", env, arr, index, val));
2496
2497         a     = (java_handle_t *) arr;
2498         value = (java_handle_t *) val;
2499
2500         array_element_set(a, index, value);
2501 }
2502
2503
2504 /* JVM_SetPrimitiveArrayElement */
2505
2506 void JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)
2507 {
2508         log_println("JVM_SetPrimitiveArrayElement: IMPLEMENT ME!");
2509 }
2510
2511
2512 /* JVM_NewArray */
2513
2514 jobject JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)
2515 {
2516         classinfo                 *c;
2517         classinfo                 *pc;
2518         java_handle_t             *a;
2519         java_handle_objectarray_t *oa;
2520
2521         TRACEJVMCALLS(("JVM_NewArray(env=%p, eltClass=%p, length=%d)", env, eltClass, length));
2522
2523         if (eltClass == NULL) {
2524                 exceptions_throw_nullpointerexception();
2525                 return NULL;
2526         }
2527
2528         /* NegativeArraySizeException is checked in builtin_newarray. */
2529
2530         c = LLNI_classinfo_unwrap(eltClass);
2531
2532         /* Create primitive or object array. */
2533
2534         if (class_is_primitive(c)) {
2535                 pc = Primitive::get_arrayclass_by_name(c->name);
2536
2537                 /* void arrays are not allowed. */
2538
2539                 if (pc == NULL) {
2540                         exceptions_throw_illegalargumentexception();
2541                         return NULL;
2542                 }
2543
2544                 a = builtin_newarray(length, pc);
2545
2546                 return (jobject) a;
2547         }
2548         else {
2549                 oa = builtin_anewarray(length, c);
2550
2551                 return (jobject) oa;
2552         }
2553 }
2554
2555
2556 /* JVM_NewMultiArray */
2557
2558 jobject JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)
2559 {
2560         classinfo                 *c;
2561         java_handle_intarray_t    *ia;
2562         int32_t                    length;
2563         long                      *dims;
2564         int32_t                    value;
2565         int32_t                    i;
2566         classinfo                 *ac;
2567         java_handle_objectarray_t *a;
2568
2569         TRACEJVMCALLS(("JVM_NewMultiArray(env=%p, eltClass=%p, dim=%p)", env, eltClass, dim));
2570
2571         if (eltClass == NULL) {
2572                 exceptions_throw_nullpointerexception();
2573                 return NULL;
2574         }
2575
2576         /* NegativeArraySizeException is checked in builtin_newarray. */
2577
2578         c = LLNI_classinfo_unwrap(eltClass);
2579
2580         ia = (java_handle_intarray_t *) dim;
2581
2582         length = array_length_get((java_handle_t *) ia);
2583
2584         /* We check here for exceptions thrown in array_length_get,
2585            otherwise these exceptions get overwritten by the following
2586            IllegalArgumentException. */
2587
2588         if (length < 0)
2589                 return NULL;
2590
2591         if ((length <= 0) || (length > /* MAX_DIM */ 255)) {
2592                 exceptions_throw_illegalargumentexception();
2593                 return NULL;
2594         }
2595
2596         /* XXX This is just a quick hack to get it working. */
2597
2598         dims = MNEW(long, length);
2599
2600         for (i = 0; i < length; i++) {
2601                 value = LLNI_array_direct(ia, i);
2602                 dims[i] = (long) value;
2603         }
2604
2605         /* Create an array-class if necessary. */
2606
2607         if (class_is_primitive(c)) {
2608                 ac = Primitive::get_arrayclass_by_name(c->name);
2609
2610                 // Arrays of void are not allowed.
2611                 if (ac == NULL) {
2612                         exceptions_throw_illegalargumentexception();
2613                         return NULL;
2614                 }
2615
2616                 if (length > 1)
2617                         ac = class_multiarray_of((length - 1), ac, true);
2618         }
2619         else
2620                 ac = class_multiarray_of(length, c, true);
2621
2622         if (ac == NULL)
2623                 return NULL;
2624
2625         /* Allocate a new array on the heap. */
2626
2627         a = builtin_multianewarray(length, (java_handle_t *) ac, dims);
2628
2629         return (jobject) a;
2630 }
2631
2632
2633 /* JVM_InitializeSocketLibrary */
2634
2635 jint JVM_InitializeSocketLibrary()
2636 {
2637         TRACEJVMCALLS(("JVM_InitializeSocketLibrary()"));
2638
2639         HPI& hpi = VM::get_current()->get_hpi();
2640         return hpi.initialize_socket_library();
2641 }
2642
2643
2644 /* JVM_Socket */
2645
2646 jint JVM_Socket(jint domain, jint type, jint protocol)
2647 {
2648         TRACEJVMCALLS(("JVM_Socket(domain=%d, type=%d, protocol=%d)", domain, type, protocol));
2649
2650         return os::socket(domain, type, protocol);
2651 }
2652
2653
2654 /* JVM_SocketClose */
2655
2656 jint JVM_SocketClose(jint fd)
2657 {
2658         TRACEJVMCALLS(("JVM_SocketClose(fd=%d)", fd));
2659
2660         return os::close(fd);
2661 }
2662
2663
2664 /* JVM_SocketShutdown */
2665
2666 jint JVM_SocketShutdown(jint fd, jint howto)
2667 {
2668         TRACEJVMCALLS(("JVM_SocketShutdown(fd=%d, howto=%d)", fd, howto));
2669
2670         return os::shutdown(fd, howto);
2671 }
2672
2673
2674 /* JVM_Recv */
2675
2676 jint JVM_Recv(jint fd, char *buf, jint nBytes, jint flags)
2677 {
2678         log_println("JVM_Recv: IMPLEMENT ME!");
2679
2680         return 0;
2681 }
2682
2683
2684 /* JVM_Send */
2685
2686 jint JVM_Send(jint fd, char *buf, jint nBytes, jint flags)
2687 {
2688         TRACEJVMCALLSENTER(("JVM_Send(fd=%d, buf=%p, nBytes=%d, flags=%d", fd, buf, nBytes, flags));
2689
2690         int result = os::send(fd, buf, nBytes, flags);
2691
2692         TRACEJVMCALLSEXIT(("->%d", result));
2693
2694         return result;
2695 }
2696
2697
2698 /* JVM_Timeout */
2699
2700 jint JVM_Timeout(int fd, long timeout)
2701 {
2702         log_println("JVM_Timeout: IMPLEMENT ME!");
2703
2704         return 0;
2705 }
2706
2707
2708 /* JVM_Listen */
2709
2710 jint JVM_Listen(jint fd, jint count)
2711 {
2712         TRACEJVMCALLS(("JVM_Listen(fd=%d, count=%d)", fd, count));
2713
2714         return os::listen(fd, count);
2715 }
2716
2717
2718 /* JVM_Connect */
2719
2720 jint JVM_Connect(jint fd, struct sockaddr *him, jint len)
2721 {
2722         TRACEJVMCALLS(("JVM_Connect(fd=%d, him=%p, len=%d)", fd, him, len));
2723
2724         return os::connect(fd, him, len);
2725 }
2726
2727
2728 /* JVM_Bind */
2729
2730 jint JVM_Bind(jint fd, struct sockaddr *him, jint len)
2731 {
2732         log_println("JVM_Bind: IMPLEMENT ME!");
2733
2734         return 0;
2735 }
2736
2737
2738 /* JVM_Accept */
2739
2740 jint JVM_Accept(jint fd, struct sockaddr *him, jint *len)
2741 {
2742         TRACEJVMCALLS(("JVM_Accept(fd=%d, him=%p, len=%p)", fd, him, len));
2743
2744         return os::accept(fd, him, (socklen_t *) len);
2745 }
2746
2747
2748 /* JVM_RecvFrom */
2749
2750 jint JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen)
2751 {
2752         log_println("JVM_RecvFrom: IMPLEMENT ME!");
2753
2754         return 0;
2755 }
2756
2757
2758 /* JVM_GetSockName */
2759
2760 jint JVM_GetSockName(jint fd, struct sockaddr *him, int *len)
2761 {
2762         TRACEJVMCALLS(("JVM_GetSockName(fd=%d, him=%p, len=%p)", fd, him, len));
2763
2764         return os::getsockname(fd, him, (socklen_t *) len);
2765 }
2766
2767
2768 /* JVM_SendTo */
2769
2770 jint JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen)
2771 {
2772         log_println("JVM_SendTo: IMPLEMENT ME!");
2773
2774         return 0;
2775 }
2776
2777
2778 /* JVM_SocketAvailable */
2779
2780 jint JVM_SocketAvailable(jint fd, jint *pbytes)
2781 {
2782 #if defined(FIONREAD)
2783         int bytes;
2784         int result;
2785
2786         TRACEJVMCALLS(("JVM_SocketAvailable(fd=%d, pbytes=%p)", fd, pbytes));
2787
2788         *pbytes = 0;
2789
2790         result = ioctl(fd, FIONREAD, &bytes);
2791
2792         if (result < 0)
2793                 return 0;
2794
2795         *pbytes = bytes;
2796
2797         return 1;
2798 #else
2799 # error FIONREAD not defined
2800 #endif
2801 }
2802
2803
2804 /* JVM_GetSockOpt */
2805
2806 jint JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)
2807 {
2808         TRACEJVMCALLS(("JVM_GetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%p)", fd, level, optname, optval, optlen));
2809
2810         return os::getsockopt(fd, level, optname, optval, (socklen_t *) optlen);
2811 }
2812
2813
2814 /* JVM_SetSockOpt */
2815
2816 jint JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)
2817 {
2818         TRACEJVMCALLS(("JVM_SetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%d)", fd, level, optname, optval, optlen));
2819
2820         return os::setsockopt(fd, level, optname, optval, optlen);
2821 }
2822
2823
2824 /* JVM_GetHostName */
2825
2826 int JVM_GetHostName(char *name, int namelen)
2827 {
2828         int result;
2829
2830         TRACEJVMCALLSENTER(("JVM_GetHostName(name=%s, namelen=%d)", name, namelen));
2831
2832         result = os::gethostname(name, namelen);
2833
2834         TRACEJVMCALLSEXIT(("->%d (name=%s)", result, name));
2835
2836         return result;
2837 }
2838
2839
2840 /* JVM_GetHostByAddr */
2841
2842 struct hostent *JVM_GetHostByAddr(const char* name, int len, int type)
2843 {
2844         log_println("JVM_GetHostByAddr: IMPLEMENT ME!");
2845
2846         return NULL;
2847 }
2848
2849
2850 /* JVM_GetHostByName */
2851
2852 struct hostent *JVM_GetHostByName(char* name)
2853 {
2854         log_println("JVM_GetHostByName: IMPLEMENT ME!");
2855
2856         return NULL;
2857 }
2858
2859
2860 /* JVM_GetProtoByName */
2861
2862 struct protoent *JVM_GetProtoByName(char* name)
2863 {
2864         log_println("JVM_GetProtoByName: IMPLEMENT ME!");
2865
2866         return NULL;
2867 }
2868
2869
2870 /* JVM_LoadLibrary */
2871
2872 void* JVM_LoadLibrary(const char* name)
2873 {
2874         TRACEJVMCALLSENTER(("JVM_LoadLibrary(name=%s)", name));
2875
2876         utf* u = utf_new_char(name);
2877
2878         NativeLibrary nl(u);
2879         void* handle = nl.open();
2880         
2881         TRACEJVMCALLSEXIT(("->%p", handle));
2882
2883         return handle;
2884 }
2885
2886
2887 /* JVM_UnloadLibrary */
2888
2889 void JVM_UnloadLibrary(void* handle)
2890 {
2891         TRACEJVMCALLS(("JVM_UnloadLibrary(handle=%p)", handle));
2892
2893         NativeLibrary nl(handle);
2894         nl.close();
2895 }
2896
2897
2898 /* JVM_FindLibraryEntry */
2899
2900 void *JVM_FindLibraryEntry(void* handle, const char* name)
2901 {
2902         void* symbol;
2903
2904         TRACEJVMCALLSENTER(("JVM_FindLibraryEntry(handle=%p, name=%s)", handle, name));
2905
2906         HPI& hpi = VM::get_current()->get_hpi();
2907         symbol = hpi.get_library().FindLibraryEntry(handle, name);
2908
2909         TRACEJVMCALLSEXIT(("->%p", symbol));
2910
2911         return symbol;
2912 }
2913
2914
2915 /* JVM_IsNaN */
2916
2917 jboolean JVM_IsNaN(jdouble d)
2918 {
2919         bool result;
2920
2921         TRACEJVMCALLSENTER(("JVM_IsNaN(d=%f)", d));
2922
2923         result = isnan(d);
2924
2925         TRACEJVMCALLSEXIT(("->%d", result));
2926
2927         return result;
2928 }
2929
2930
2931 /* JVM_IsSupportedJNIVersion */
2932
2933 jboolean JVM_IsSupportedJNIVersion(jint version)
2934 {
2935         TRACEJVMCALLS(("JVM_IsSupportedJNIVersion(version=%d)", version));
2936
2937         return jni_version_check(version);
2938 }
2939
2940
2941 /* JVM_InternString */
2942
2943 jstring JVM_InternString(JNIEnv *env, jstring str)
2944 {
2945         TRACEJVMCALLS(("JVM_InternString(env=%p, str=%p)", env, str));
2946
2947         return (jstring) javastring_intern((java_handle_t *) str);
2948 }
2949
2950
2951 /* JVM_RawMonitorCreate */
2952
2953 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void)
2954 {
2955         TRACEJVMCALLS(("JVM_RawMonitorCreate()"));
2956
2957         Mutex* m = new Mutex();
2958
2959         return m;
2960 }
2961
2962
2963 /* JVM_RawMonitorDestroy */
2964
2965 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void* mon)
2966 {
2967         TRACEJVMCALLS(("JVM_RawMonitorDestroy(mon=%p)", mon));
2968
2969         delete ((Mutex*) mon);
2970 }
2971
2972
2973 /* JVM_RawMonitorEnter */
2974
2975 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void* mon)
2976 {
2977         TRACEJVMCALLS(("JVM_RawMonitorEnter(mon=%p)", mon));
2978
2979         ((Mutex*) mon)->lock();
2980
2981         return 0;
2982 }
2983
2984
2985 /* JVM_RawMonitorExit */
2986
2987 JNIEXPORT void JNICALL JVM_RawMonitorExit(void* mon)
2988 {
2989         TRACEJVMCALLS(("JVM_RawMonitorExit(mon=%p)", mon));
2990
2991         ((Mutex*) mon)->unlock();
2992 }
2993
2994
2995 /* JVM_SetPrimitiveFieldValues */
2996
2997 void JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
2998 {
2999         log_println("JVM_SetPrimitiveFieldValues: IMPLEMENT ME!");
3000 }
3001
3002
3003 /* JVM_GetPrimitiveFieldValues */
3004
3005 void JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
3006 {
3007         log_println("JVM_GetPrimitiveFieldValues: IMPLEMENT ME!");
3008 }
3009
3010
3011 /* JVM_AccessVMBooleanFlag */
3012
3013 jboolean JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get)
3014 {
3015         log_println("JVM_AccessVMBooleanFlag: IMPLEMENT ME!");
3016
3017         return 0;
3018 }
3019
3020
3021 /* JVM_AccessVMIntFlag */
3022
3023 jboolean JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get)
3024 {
3025         log_println("JVM_AccessVMIntFlag: IMPLEMENT ME!");
3026
3027         return 0;
3028 }
3029
3030
3031 /* JVM_VMBreakPoint */
3032
3033 void JVM_VMBreakPoint(JNIEnv *env, jobject obj)
3034 {
3035         log_println("JVM_VMBreakPoint: IMPLEMENT ME!");
3036 }
3037
3038
3039 /* JVM_GetClassFields */
3040
3041 jobjectArray JVM_GetClassFields(JNIEnv *env, jclass cls, jint which)
3042 {
3043         log_println("JVM_GetClassFields: IMPLEMENT ME!");
3044
3045         return NULL;
3046 }
3047
3048
3049 /* JVM_GetClassMethods */
3050
3051 jobjectArray JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which)
3052 {
3053         log_println("JVM_GetClassMethods: IMPLEMENT ME!");
3054
3055         return NULL;
3056 }
3057
3058
3059 /* JVM_GetClassConstructors */
3060
3061 jobjectArray JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which)
3062 {
3063         log_println("JVM_GetClassConstructors: IMPLEMENT ME!");
3064
3065         return NULL;
3066 }
3067
3068
3069 /* JVM_GetClassField */
3070
3071 jobject JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which)
3072 {
3073         log_println("JVM_GetClassField: IMPLEMENT ME!");
3074
3075         return NULL;
3076 }
3077
3078
3079 /* JVM_GetClassMethod */
3080
3081 jobject JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which)
3082 {
3083         log_println("JVM_GetClassMethod: IMPLEMENT ME!");
3084
3085         return NULL;
3086 }
3087
3088
3089 /* JVM_GetClassConstructor */
3090
3091 jobject JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which)
3092 {
3093         log_println("JVM_GetClassConstructor: IMPLEMENT ME!");
3094
3095         return NULL;
3096 }
3097
3098
3099 /* JVM_NewInstance */
3100
3101 jobject JVM_NewInstance(JNIEnv *env, jclass cls)
3102 {
3103         log_println("JVM_NewInstance: IMPLEMENT ME!");
3104
3105         return NULL;
3106 }
3107
3108
3109 /* JVM_GetField */
3110
3111 jobject JVM_GetField(JNIEnv *env, jobject field, jobject obj)
3112 {
3113         log_println("JVM_GetField: IMPLEMENT ME!");
3114
3115         return NULL;
3116 }
3117
3118
3119 /* JVM_GetPrimitiveField */
3120
3121 jvalue JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode)
3122 {
3123         jvalue jv;
3124
3125         log_println("JVM_GetPrimitiveField: IMPLEMENT ME!");
3126
3127         jv.l = NULL;
3128
3129         return jv;
3130 }
3131
3132
3133 /* JVM_SetField */
3134
3135 void JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val)
3136 {
3137         log_println("JVM_SetField: IMPLEMENT ME!");
3138 }
3139
3140
3141 /* JVM_SetPrimitiveField */
3142
3143 void JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode)
3144 {
3145         log_println("JVM_SetPrimitiveField: IMPLEMENT ME!");
3146 }
3147
3148
3149 /* JVM_InvokeMethod */
3150
3151 jobject JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)
3152 {
3153         TRACEJVMCALLS(("JVM_InvokeMethod(env=%p, method=%p, obj=%p, args0=%p)", env, method, obj, args0));
3154
3155         java_lang_reflect_Method jlrm(method);
3156         
3157         java_handle_t* result = jlrm.invoke((java_handle_t*) obj, (java_handle_objectarray_t*) args0);
3158
3159         return (jobject) result;
3160 }
3161
3162
3163 /* JVM_NewInstanceFromConstructor */
3164
3165 jobject JVM_NewInstanceFromConstructor(JNIEnv *env, jobject con, jobjectArray args0)
3166 {
3167         TRACEJVMCALLS(("JVM_NewInstanceFromConstructor(env=%p, c=%p, args0=%p)", env, con, args0));
3168
3169         java_lang_reflect_Constructor jlrc(con);
3170         java_handle_t* o = jlrc.new_instance((java_handle_objectarray_t*) args0);
3171
3172         return (jobject) o;
3173 }
3174
3175
3176 /* JVM_SupportsCX8 */
3177
3178 jboolean JVM_SupportsCX8()
3179 {
3180         TRACEJVMCALLS(("JVM_SupportsCX8()"));
3181
3182         /* IMPLEMENT ME */
3183
3184         return 0;
3185 }
3186
3187
3188 /* JVM_CX8Field */
3189
3190 jboolean JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal)
3191 {
3192         log_println("JVM_CX8Field: IMPLEMENT ME!");
3193
3194         return 0;
3195 }
3196
3197
3198 /* JVM_GetAllThreads */
3199
3200 jobjectArray JVM_GetAllThreads(JNIEnv *env, jclass dummy)
3201 {
3202         // Get a list of all active threads.
3203         list<threadobject*> active_threads;
3204         ThreadList::get_active_threads(active_threads);
3205
3206         // Allocate array to hold the java.lang.Thread objects.
3207         int32_t length = active_threads.size();
3208         java_handle_objectarray_t* oa = builtin_anewarray(length, class_java_lang_Thread);
3209
3210         if (oa == NULL)
3211                 return NULL;
3212
3213         // Iterate over all threads (which were active just a second ago).
3214         int32_t index = 0;
3215         for (List<threadobject*>::iterator it = active_threads.begin(); it != active_threads.end(); it++) {
3216                 threadobject* t = *it;
3217
3218                 java_handle_t* h = thread_get_object(t);
3219                 assert(h != NULL);
3220
3221                 array_objectarray_element_set(oa, index, h);
3222
3223                 index++;
3224         }
3225
3226         return oa;
3227 }
3228
3229
3230 /* JVM_DumpThreads */
3231
3232 jobjectArray JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)
3233 {
3234         int32_t i;
3235
3236         TRACEJVMCALLS(("JVM_DumpThreads((env=%p, threadClass=%p, threads=%p)", env, threadClass, threads));
3237
3238         if (threads == NULL) {
3239                 exceptions_throw_nullpointerexception();
3240                 return NULL;
3241         }
3242
3243         // Get length of the threads array.
3244         int32_t length = array_length_get((java_handle_t*) threads);
3245
3246         if (length <= 0) {
3247                 exceptions_throw_illegalargumentexception();
3248                 return NULL;
3249         }
3250
3251         // Allocate array to hold stacktraces.
3252         classinfo* arrayclass = class_array_of(class_java_lang_StackTraceElement, true);
3253         java_handle_objectarray_t* oas = builtin_anewarray(length, arrayclass);
3254
3255         if (oas == NULL) {
3256                 return NULL;
3257         }
3258
3259         // Iterate over all passed thread objects.
3260         for (i = 0; i < length; i++) {
3261                 java_handle_t* thread = array_objectarray_element_get(threads, i);
3262
3263                 // Get thread for the given thread object.
3264                 threadobject* t = thread_get_thread(thread);
3265
3266                 // The threadobject is null when a thread is created in Java.
3267                 if (t == NULL)
3268                         continue;
3269
3270                 // Get stacktrace for given thread.
3271                 stacktrace_t* st = stacktrace_get_of_thread(t);
3272
3273                 // Convert stacktrace into array of StackTraceElements.
3274                 java_handle_objectarray_t* oa = stacktrace_get_StackTraceElements(st);
3275
3276                 if (oa == NULL)
3277                         return NULL;
3278
3279                 array_objectarray_element_set(oas, i, (java_handle_t*) oa);
3280         }
3281
3282         return oas;
3283 }
3284
3285
3286 /* JVM_GetManagement */
3287
3288 void *JVM_GetManagement(jint version)
3289 {
3290         TRACEJVMCALLS(("JVM_GetManagement(version=%d)", version));
3291
3292         return Management::get_jmm_interface(version);
3293 }
3294
3295
3296 /* JVM_InitAgentProperties */
3297
3298 jobject JVM_InitAgentProperties(JNIEnv *env, jobject properties)
3299 {
3300         log_println("JVM_InitAgentProperties: IMPLEMENT ME!");
3301
3302         return NULL;
3303 }
3304
3305
3306 /* JVM_GetEnclosingMethodInfo */
3307
3308 jobjectArray JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)
3309 {
3310         classinfo                 *c;
3311         methodinfo                *m;
3312         java_handle_objectarray_t *oa;
3313
3314         TRACEJVMCALLS(("JVM_GetEnclosingMethodInfo(env=%p, ofClass=%p)", env, ofClass));
3315
3316         c = LLNI_classinfo_unwrap(ofClass);
3317
3318         if ((c == NULL) || class_is_primitive(c))
3319                 return NULL;
3320
3321         m = class_get_enclosingmethod_raw(c);
3322
3323         if (m == NULL)
3324                 return NULL;
3325
3326         oa = builtin_anewarray(3, class_java_lang_Object);
3327
3328         if (oa == NULL)
3329                 return NULL;
3330
3331         array_objectarray_element_set(oa, 0, (java_handle_t *) LLNI_classinfo_wrap(m->clazz));
3332         array_objectarray_element_set(oa, 1, javastring_new(m->name));
3333         array_objectarray_element_set(oa, 2, javastring_new(m->descriptor));
3334
3335         return (jobjectArray) oa;
3336 }
3337
3338
3339 /* JVM_GetThreadStateValues */
3340
3341 jintArray JVM_GetThreadStateValues(JNIEnv* env, jint javaThreadState)
3342 {
3343         java_handle_intarray_t *ia;
3344
3345         TRACEJVMCALLS(("JVM_GetThreadStateValues(env=%p, javaThreadState=%d)",
3346                                   env, javaThreadState));
3347
3348         /* If new thread states are added in future JDK and VM versions,
3349            this should check if the JDK version is compatible with thread
3350            states supported by the VM.  Return NULL if not compatible.
3351         
3352            This function must map the VM java_lang_Thread::ThreadStatus
3353            to the Java thread state that the JDK supports. */
3354
3355         switch (javaThreadState) {
3356     case THREAD_STATE_NEW:
3357                 ia = builtin_newarray_int(1);
3358
3359                 if (ia == NULL)
3360                         return NULL;
3361
3362                 array_intarray_element_set(ia, 0, THREAD_STATE_NEW);
3363                 break; 
3364
3365     case THREAD_STATE_RUNNABLE:
3366                 ia = builtin_newarray_int(1);
3367
3368                 if (ia == NULL)
3369                         return NULL;
3370
3371                 array_intarray_element_set(ia, 0, THREAD_STATE_RUNNABLE);
3372                 break; 
3373
3374     case THREAD_STATE_BLOCKED:
3375                 ia = builtin_newarray_int(1);
3376
3377                 if (ia == NULL)
3378                         return NULL;
3379
3380                 array_intarray_element_set(ia, 0, THREAD_STATE_BLOCKED);
3381                 break; 
3382
3383     case THREAD_STATE_WAITING:
3384                 ia = builtin_newarray_int(2);
3385
3386                 if (ia == NULL)
3387                         return NULL;
3388
3389                 array_intarray_element_set(ia, 0, THREAD_STATE_WAITING);
3390                 array_intarray_element_set(ia, 1, THREAD_STATE_PARKED);
3391                 break; 
3392
3393     case THREAD_STATE_TIMED_WAITING:
3394                 ia = builtin_newarray_int(2);
3395
3396                 if (ia == NULL)
3397                         return NULL;
3398
3399                 /* XXX Not sure about that one. */
3400 /*              array_intarray_element_set(ia, 0, SLEEPING); */
3401                 array_intarray_element_set(ia, 0, THREAD_STATE_TIMED_WAITING);
3402                 array_intarray_element_set(ia, 1, THREAD_STATE_TIMED_PARKED);
3403                 break; 
3404
3405     case THREAD_STATE_TERMINATED:
3406                 ia = builtin_newarray_int(1);
3407
3408                 if (ia == NULL)
3409                         return NULL;
3410
3411                 array_intarray_element_set(ia, 0, THREAD_STATE_TERMINATED);
3412                 break; 
3413
3414     default:
3415                 /* Unknown state - probably incompatible JDK version */
3416                 return NULL;
3417         }
3418
3419         return (jintArray) ia;
3420 }
3421
3422
3423 /* JVM_GetThreadStateNames */
3424
3425 jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArray values)
3426 {
3427         java_handle_intarray_t    *ia;
3428         java_handle_objectarray_t *oa;
3429         java_object_t             *s;
3430
3431         TRACEJVMCALLS(("JVM_GetThreadStateNames(env=%p, javaThreadState=%d, values=%p)",
3432                                   env, javaThreadState, values));
3433
3434         ia = (java_handle_intarray_t *) values;
3435
3436         /* If new thread states are added in future JDK and VM versions,
3437            this should check if the JDK version is compatible with thread
3438            states supported by the VM.  Return NULL if not compatible.
3439         
3440            This function must map the VM java_lang_Thread::ThreadStatus
3441            to the Java thread state that the JDK supports. */
3442
3443         if (values == NULL) {
3444                 exceptions_throw_nullpointerexception();
3445                 return NULL;
3446         }
3447
3448         switch (javaThreadState) {
3449     case THREAD_STATE_NEW:
3450                 assert(ia->header.size == 1 && ia->data[0] == THREAD_STATE_NEW);
3451
3452                 oa = builtin_anewarray(1, class_java_lang_String);
3453
3454                 if (oa == NULL)
3455                         return NULL;
3456
3457                 s = javastring_new(utf_new_char("NEW"));
3458
3459                 if (s == NULL)
3460                         return NULL;
3461
3462                 array_objectarray_element_set(oa, 0, s);
3463                 break; 
3464
3465     case THREAD_STATE_RUNNABLE:
3466                 oa = builtin_anewarray(1, class_java_lang_String);
3467
3468                 if (oa == NULL)
3469                         return NULL;
3470
3471                 s = javastring_new(utf_new_char("RUNNABLE"));
3472
3473                 if (s == NULL)
3474                         return NULL;
3475
3476                 array_objectarray_element_set(oa, 0, s);
3477                 break; 
3478
3479     case THREAD_STATE_BLOCKED:
3480                 oa = builtin_anewarray(1, class_java_lang_String);
3481
3482                 if (oa == NULL)
3483                         return NULL;
3484
3485                 s = javastring_new(utf_new_char("BLOCKED"));
3486
3487                 if (s == NULL)
3488                         return NULL;
3489
3490                 array_objectarray_element_set(oa, 0, s);
3491                 break; 
3492
3493     case THREAD_STATE_WAITING:
3494                 oa = builtin_anewarray(2, class_java_lang_String);
3495
3496                 if (oa == NULL)
3497                         return NULL;
3498
3499                 s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
3500
3501                 if (s == NULL)
3502                         return NULL;
3503
3504                 array_objectarray_element_set(oa, 0, s);
3505
3506                 s = javastring_new(utf_new_char("WAITING.PARKED"));
3507
3508                 if (s == NULL)
3509                         return NULL;
3510
3511                 array_objectarray_element_set(oa, 1, s);
3512                 break; 
3513
3514     case THREAD_STATE_TIMED_WAITING:
3515                 oa = builtin_anewarray(2, class_java_lang_String);
3516
3517                 if (oa == NULL)
3518                         return NULL;
3519
3520 /*              s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
3521                 s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
3522
3523                 if (s == NULL)
3524                         return NULL;
3525
3526                 array_objectarray_element_set(oa, 0, s);
3527
3528                 s = javastring_new(utf_new_char("TIMED_WAITING.PARKED"));
3529
3530                 if (s == NULL)
3531                         return NULL;
3532
3533                 array_objectarray_element_set(oa, 1, s);
3534                 break; 
3535
3536     case THREAD_STATE_TERMINATED:
3537                 oa = builtin_anewarray(1, class_java_lang_String);
3538
3539                 if (oa == NULL)
3540                         return NULL;
3541
3542                 s = javastring_new(utf_new_char("TERMINATED"));
3543
3544                 if (s == NULL)
3545                         return NULL;
3546
3547                 array_objectarray_element_set(oa, 0, s);
3548                 break; 
3549
3550         default:
3551                 /* Unknown state - probably incompatible JDK version */
3552                 return NULL;
3553         }
3554
3555         return (jobjectArray) oa;
3556 }
3557
3558
3559 /* JVM_GetVersionInfo */
3560
3561 void JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)
3562 {
3563         log_println("JVM_GetVersionInfo: IMPLEMENT ME!");
3564 }
3565
3566
3567 /* OS: JVM_RegisterSignal */
3568
3569 void *JVM_RegisterSignal(jint sig, void *handler)
3570 {
3571         functionptr newHandler;
3572
3573         TRACEJVMCALLS(("JVM_RegisterSignal(sig=%d, handler=%p)", sig, handler));
3574
3575         if (handler == (void *) 2)
3576                 newHandler = (functionptr) signal_thread_handler;
3577         else
3578                 newHandler = (functionptr) (uintptr_t) handler;
3579
3580         switch (sig) {
3581     case SIGILL:
3582     case SIGFPE:
3583     case SIGUSR1:
3584     case SIGSEGV:
3585                 /* These signals are already used by the VM. */
3586                 return (void *) -1;
3587
3588     case SIGQUIT:
3589                 /* This signal is used by the VM to dump thread stacks unless
3590                    ReduceSignalUsage is set, in which case the user is allowed
3591                    to set his own _native_ handler for this signal; thus, in
3592                    either case, we do not allow JVM_RegisterSignal to change
3593                    the handler. */
3594                 return (void *) -1;
3595
3596         case SIGHUP:
3597         case SIGINT:
3598         case SIGTERM:
3599                 break;
3600         }
3601
3602         signal_register_signal(sig, newHandler, SA_RESTART | SA_SIGINFO);
3603
3604         /* XXX Should return old handler. */
3605
3606         return (void *) 2;
3607 }
3608
3609
3610 /* OS: JVM_RaiseSignal */
3611
3612 jboolean JVM_RaiseSignal(jint sig)
3613 {
3614         log_println("JVM_RaiseSignal: IMPLEMENT ME! sig=%s", sig);
3615
3616         return false;
3617 }
3618
3619
3620 /* OS: JVM_FindSignal */
3621
3622 jint JVM_FindSignal(const char *name)
3623 {
3624         TRACEJVMCALLS(("JVM_FindSignal(name=%s)", name));
3625
3626 #if defined(__LINUX__)
3627         if (strcmp(name, "HUP") == 0)
3628                 return SIGHUP;
3629
3630         if (strcmp(name, "INT") == 0)
3631                 return SIGINT;
3632
3633         if (strcmp(name, "TERM") == 0)
3634                 return SIGTERM;
3635 #elif defined(__SOLARIS__)
3636         int signum;
3637
3638         if (os::str2sig(name, &signum) == -1)
3639                 return -1;
3640
3641         return signum;
3642 #else
3643 # error Not implemented for this OS.
3644 #endif
3645
3646         return -1;
3647 }
3648
3649 } // extern "C"
3650
3651
3652 /*
3653  * These are local overrides for various environment variables in Emacs.
3654  * Please do not remove this and leave it at the end of the file, where
3655  * Emacs will automagically detect them.
3656  * ---------------------------------------------------------------------
3657  * Local variables:
3658  * mode: c++
3659  * indent-tabs-mode: t
3660  * c-basic-offset: 4
3661  * tab-width: 4
3662  * End:
3663  * vim:noexpandtab:sw=4:ts=4:
3664  */