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