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