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