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