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