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