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