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