1 // jni.cc - JNI implementation, including the jump table.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
16 // Define this before including jni.h.
17 #define __GCJ_JNI_IMPL__
21 #include <java-assert.h>
27 #include <java/lang/Class.h>
28 #include <java/lang/ClassLoader.h>
29 #include <java/lang/Throwable.h>
30 #include <java/lang/ArrayIndexOutOfBoundsException.h>
31 #include <java/lang/StringIndexOutOfBoundsException.h>
32 #include <java/lang/AbstractMethodError.h>
33 #include <java/lang/InstantiationException.h>
34 #include <java/lang/NoSuchFieldError.h>
35 #include <java/lang/NoSuchMethodError.h>
36 #include <java/lang/reflect/Constructor.h>
37 #include <java/lang/reflect/Method.h>
38 #include <java/lang/reflect/Modifier.h>
39 #include <java/lang/OutOfMemoryError.h>
40 #include <java/util/Hashtable.h>
41 #include <java/lang/Integer.h>
42 #include <gnu/gcj/jni/NativeThread.h>
44 #include <gcj/method.h>
45 #include <gcj/field.h>
47 #include <java-interp.h>
49 #define ClassClass _CL_Q34java4lang5Class
50 extern java::lang::Class ClassClass
;
51 #define ObjectClass _CL_Q34java4lang6Object
52 extern java::lang::Class ObjectClass
;
54 #define ThrowableClass _CL_Q34java4lang9Throwable
55 extern java::lang::Class ThrowableClass
;
56 #define MethodClass _CL_Q44java4lang7reflect6Method
57 extern java::lang::Class MethodClass
;
58 #define ThreadGroupClass _CL_Q34java4lang11ThreadGroup
59 extern java::lang::Class ThreadGroupClass
;
60 #define NativeThreadClass _CL_Q43gnu3gcj3jni12NativeThread
61 extern java::lang::Class ThreadGroupClass
;
63 // This enum is used to select different template instantiations in
64 // the invocation code.
73 // Forward declarations.
74 extern struct JNINativeInterface _Jv_JNIFunctions
;
75 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions
;
77 // Number of slots in the default frame. The VM must allow at least
81 // Mark value indicating this is an overflow frame.
83 // Mark value indicating this is a user frame.
85 // Mark value indicating this is a system frame.
88 // This structure is used to keep track of local references.
89 struct _Jv_JNI_LocalFrame
91 // This is true if this frame object represents a pushed frame (eg
92 // from PushLocalFrame).
95 // Number of elements in frame.
98 // Next frame in chain.
99 _Jv_JNI_LocalFrame
*next
;
101 // The elements. These are allocated using the C "struct hack".
105 // This holds a reference count for all local and global references.
106 static java::util::Hashtable
*ref_table
;
109 static JavaVM
*the_vm
;
112 // The only JVMPI interface description.
113 static JVMPI_Interface _Jv_JVMPI_Interface
;
116 jvmpiEnableEvent (jint event_type
, void *)
120 case JVMPI_EVENT_OBJECT_ALLOC
:
121 _Jv_JVMPI_Notify_OBJECT_ALLOC
= _Jv_JVMPI_Interface
.NotifyEvent
;
124 case JVMPI_EVENT_THREAD_START
:
125 _Jv_JVMPI_Notify_THREAD_START
= _Jv_JVMPI_Interface
.NotifyEvent
;
128 case JVMPI_EVENT_THREAD_END
:
129 _Jv_JVMPI_Notify_THREAD_END
= _Jv_JVMPI_Interface
.NotifyEvent
;
133 return JVMPI_NOT_AVAILABLE
;
136 return JVMPI_SUCCESS
;
140 jvmpiDisableEvent (jint event_type
, void *)
144 case JVMPI_EVENT_OBJECT_ALLOC
:
145 _Jv_JVMPI_Notify_OBJECT_ALLOC
= NULL
;
149 return JVMPI_NOT_AVAILABLE
;
152 return JVMPI_SUCCESS
;
161 ref_table
= new java::util::Hashtable
;
164 _Jv_JVMPI_Interface
.version
= 1;
165 _Jv_JVMPI_Interface
.EnableEvent
= &jvmpiEnableEvent
;
166 _Jv_JVMPI_Interface
.DisableEvent
= &jvmpiDisableEvent
;
167 _Jv_JVMPI_Interface
.EnableGC
= &_Jv_EnableGC
;
168 _Jv_JVMPI_Interface
.DisableGC
= &_Jv_DisableGC
;
169 _Jv_JVMPI_Interface
.RunGC
= &_Jv_RunGC
;
173 // Tell the GC that a certain pointer is live.
175 mark_for_gc (jobject obj
)
177 JvSynchronize
sync (ref_table
);
179 using namespace java::lang
;
180 Integer
*refcount
= (Integer
*) ref_table
->get (obj
);
181 jint val
= (refcount
== NULL
) ? 0 : refcount
->intValue ();
182 // FIXME: what about out of memory error?
183 ref_table
->put (obj
, new Integer (val
+ 1));
188 unmark_for_gc (jobject obj
)
190 JvSynchronize
sync (ref_table
);
192 using namespace java::lang
;
193 Integer
*refcount
= (Integer
*) ref_table
->get (obj
);
195 jint val
= refcount
->intValue () - 1;
197 ref_table
->remove (obj
);
199 // FIXME: what about out of memory error?
200 ref_table
->put (obj
, new Integer (val
));
206 _Jv_JNI_NewGlobalRef (JNIEnv
*, jobject obj
)
213 _Jv_JNI_DeleteGlobalRef (JNIEnv
*, jobject obj
)
219 _Jv_JNI_DeleteLocalRef (JNIEnv
*env
, jobject obj
)
221 _Jv_JNI_LocalFrame
*frame
;
223 for (frame
= env
->locals
; frame
!= NULL
; frame
= frame
->next
)
225 for (int i
= 0; i
< FRAME_SIZE
; ++i
)
227 if (frame
->vec
[i
] == obj
)
229 frame
->vec
[i
] = NULL
;
235 // Don't go past a marked frame.
236 JvAssert (frame
->marker
== MARK_NONE
);
243 _Jv_JNI_EnsureLocalCapacity (JNIEnv
*env
, jint size
)
245 // It is easier to just always allocate a new frame of the requested
246 // size. This isn't the most efficient thing, but for now we don't
247 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
249 _Jv_JNI_LocalFrame
*frame
;
252 frame
= (_Jv_JNI_LocalFrame
*) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame
)
253 + size
* sizeof (jobject
));
261 frame
->marker
= MARK_NONE
;
263 memset (&frame
->vec
[0], 0, size
* sizeof (jobject
));
264 frame
->next
= env
->locals
;
271 _Jv_JNI_PushLocalFrame (JNIEnv
*env
, jint size
)
273 jint r
= _Jv_JNI_EnsureLocalCapacity (env
, size
);
277 // The new frame is on top.
278 env
->locals
->marker
= MARK_USER
;
284 _Jv_JNI_NewLocalRef (JNIEnv
*env
, jobject obj
)
286 // Try to find an open slot somewhere in the topmost frame.
287 _Jv_JNI_LocalFrame
*frame
= env
->locals
;
288 bool done
= false, set
= false;
289 while (frame
!= NULL
&& ! done
)
291 for (int i
= 0; i
< frame
->size
; ++i
)
292 if (frame
->vec
[i
] == NULL
)
303 // No slots, so we allocate a new frame. According to the spec
304 // we could just die here. FIXME: return value.
305 _Jv_JNI_EnsureLocalCapacity (env
, 16);
306 // We know the first element of the new frame will be ok.
307 env
->locals
->vec
[0] = obj
;
315 _Jv_JNI_PopLocalFrame (JNIEnv
*env
, jobject result
, int stop
)
317 _Jv_JNI_LocalFrame
*rf
= env
->locals
;
320 while (rf
!= NULL
&& ! done
)
322 for (int i
= 0; i
< rf
->size
; ++i
)
323 if (rf
->vec
[i
] != NULL
)
324 unmark_for_gc (rf
->vec
[i
]);
326 // If the frame we just freed is the marker frame, we are done.
327 done
= (rf
->marker
== stop
);
329 _Jv_JNI_LocalFrame
*n
= rf
->next
;
330 // When N==NULL, we've reached the stack-allocated frame, and we
331 // must not free it. However, we must be sure to clear all its
332 // elements, since we might conceivably reuse it.
335 memset (&rf
->vec
[0], 0, rf
->size
* sizeof (jobject
));
343 return result
== NULL
? NULL
: _Jv_JNI_NewLocalRef (env
, result
);
347 _Jv_JNI_PopLocalFrame (JNIEnv
*env
, jobject result
)
349 return _Jv_JNI_PopLocalFrame (env
, result
, MARK_USER
);
352 // Pop a `system' frame from the stack. This is `extern "C"' as it is
353 // used by the compiler.
355 _Jv_JNI_PopSystemFrame (JNIEnv
*env
)
357 _Jv_JNI_PopLocalFrame (env
, NULL
, MARK_SYSTEM
);
361 jthrowable t
= env
->ex
;
367 // This function is used from other template functions. It wraps the
368 // return value appropriately; we specialize it so that object returns
369 // are turned into local references.
372 wrap_value (JNIEnv
*, T value
)
379 wrap_value (JNIEnv
*env
, jobject value
)
381 return value
== NULL
? value
: _Jv_JNI_NewLocalRef (env
, value
);
387 _Jv_JNI_GetVersion (JNIEnv
*)
389 return JNI_VERSION_1_2
;
393 _Jv_JNI_DefineClass (JNIEnv
*env
, jobject loader
,
394 const jbyte
*buf
, jsize bufLen
)
398 jbyteArray bytes
= JvNewByteArray (bufLen
);
400 jbyte
*elts
= elements (bytes
);
401 memcpy (elts
, buf
, bufLen
* sizeof (jbyte
));
403 java::lang::ClassLoader
*l
404 = reinterpret_cast<java::lang::ClassLoader
*> (loader
);
406 jclass result
= l
->defineClass (bytes
, 0, bufLen
);
407 return (jclass
) wrap_value (env
, result
);
417 _Jv_JNI_FindClass (JNIEnv
*env
, const char *name
)
419 // FIXME: assume that NAME isn't too long.
420 int len
= strlen (name
);
422 for (int i
= 0; i
<= len
; ++i
)
423 s
[i
] = (name
[i
] == '/') ? '.' : name
[i
];
428 // This might throw an out of memory exception.
429 jstring n
= JvNewStringUTF (s
);
431 java::lang::ClassLoader
*loader
= NULL
;
432 if (env
->klass
!= NULL
)
433 loader
= env
->klass
->getClassLoader ();
437 // FIXME: should use getBaseClassLoader, but we don't have that
439 loader
= java::lang::ClassLoader::getSystemClassLoader ();
442 r
= loader
->loadClass (n
);
449 return (jclass
) wrap_value (env
, r
);
453 _Jv_JNI_GetSuperclass (JNIEnv
*env
, jclass clazz
)
455 return (jclass
) wrap_value (env
, clazz
->getSuperclass ());
459 _Jv_JNI_IsAssignableFrom(JNIEnv
*, jclass clazz1
, jclass clazz2
)
461 return clazz1
->isAssignableFrom (clazz2
);
465 _Jv_JNI_Throw (JNIEnv
*env
, jthrowable obj
)
467 // We check in case the user did some funky cast.
468 JvAssert (obj
!= NULL
&& (&ThrowableClass
)->isInstance (obj
));
474 _Jv_JNI_ThrowNew (JNIEnv
*env
, jclass clazz
, const char *message
)
476 using namespace java::lang::reflect
;
478 JvAssert ((&ThrowableClass
)->isAssignableFrom (clazz
));
483 JArray
<jclass
> *argtypes
484 = (JArray
<jclass
> *) JvNewObjectArray (1, &ClassClass
, NULL
);
486 jclass
*elts
= elements (argtypes
);
487 elts
[0] = &StringClass
;
489 Constructor
*cons
= clazz
->getConstructor (argtypes
);
491 jobjectArray values
= JvNewObjectArray (1, &StringClass
, NULL
);
492 jobject
*velts
= elements (values
);
493 velts
[0] = JvNewStringUTF (message
);
495 jobject obj
= cons
->newInstance (values
);
497 env
->ex
= reinterpret_cast<jthrowable
> (obj
);
509 _Jv_JNI_ExceptionOccurred (JNIEnv
*env
)
511 return (jthrowable
) wrap_value (env
, env
->ex
);
515 _Jv_JNI_ExceptionDescribe (JNIEnv
*env
)
518 env
->ex
->printStackTrace();
522 _Jv_JNI_ExceptionClear (JNIEnv
*env
)
528 _Jv_JNI_ExceptionCheck (JNIEnv
*env
)
530 return env
->ex
!= NULL
;
534 _Jv_JNI_FatalError (JNIEnv
*, const char *message
)
542 _Jv_JNI_IsSameObject (JNIEnv
*, jobject obj1
, jobject obj2
)
548 _Jv_JNI_AllocObject (JNIEnv
*env
, jclass clazz
)
551 using namespace java::lang::reflect
;
555 JvAssert (clazz
&& ! clazz
->isArray ());
556 if (clazz
->isInterface() || Modifier::isAbstract(clazz
->getModifiers()))
557 env
->ex
= new java::lang::InstantiationException ();
560 // FIXME: will this work for String?
561 obj
= JvAllocObject (clazz
);
569 return wrap_value (env
, obj
);
573 _Jv_JNI_GetObjectClass (JNIEnv
*env
, jobject obj
)
576 return (jclass
) wrap_value (env
, obj
->getClass());
580 _Jv_JNI_IsInstanceOf (JNIEnv
*, jobject obj
, jclass clazz
)
582 return clazz
->isInstance(obj
);
588 // This section concerns method invocation.
591 template<jboolean is_static
>
593 _Jv_JNI_GetAnyMethodID (JNIEnv
*env
, jclass clazz
,
594 const char *name
, const char *sig
)
598 _Jv_InitClass (clazz
);
600 _Jv_Utf8Const
*name_u
= _Jv_makeUtf8Const ((char *) name
, -1);
601 _Jv_Utf8Const
*sig_u
= _Jv_makeUtf8Const ((char *) sig
, -1);
603 JvAssert (! clazz
->isPrimitive());
605 using namespace java::lang::reflect
;
607 while (clazz
!= NULL
)
609 jint count
= JvNumMethods (clazz
);
610 jmethodID meth
= JvGetFirstMethod (clazz
);
612 for (jint i
= 0; i
< count
; ++i
)
614 if (((is_static
&& Modifier::isStatic (meth
->accflags
))
615 || (! is_static
&& ! Modifier::isStatic (meth
->accflags
)))
616 && _Jv_equalUtf8Consts (meth
->name
, name_u
)
617 && _Jv_equalUtf8Consts (meth
->signature
, sig_u
))
620 meth
= meth
->getNextMethod();
623 clazz
= clazz
->getSuperclass ();
626 env
->ex
= new java::lang::NoSuchMethodError ();
636 // This is a helper function which turns a va_list into an array of
637 // `jvalue's. It needs signature information in order to do its work.
638 // The array of values must already be allocated.
640 array_from_valist (jvalue
*values
, JArray
<jclass
> *arg_types
, va_list vargs
)
642 jclass
*arg_elts
= elements (arg_types
);
643 for (int i
= 0; i
< arg_types
->length
; ++i
)
645 if (arg_elts
[i
] == JvPrimClass (byte
))
646 values
[i
].b
= va_arg (vargs
, jbyte
);
647 else if (arg_elts
[i
] == JvPrimClass (short))
648 values
[i
].s
= va_arg (vargs
, jshort
);
649 else if (arg_elts
[i
] == JvPrimClass (int))
650 values
[i
].i
= va_arg (vargs
, jint
);
651 else if (arg_elts
[i
] == JvPrimClass (long))
652 values
[i
].j
= va_arg (vargs
, jlong
);
653 else if (arg_elts
[i
] == JvPrimClass (float))
654 values
[i
].f
= va_arg (vargs
, jfloat
);
655 else if (arg_elts
[i
] == JvPrimClass (double))
656 values
[i
].d
= va_arg (vargs
, jdouble
);
657 else if (arg_elts
[i
] == JvPrimClass (boolean
))
658 values
[i
].z
= va_arg (vargs
, jboolean
);
659 else if (arg_elts
[i
] == JvPrimClass (char))
660 values
[i
].c
= va_arg (vargs
, jchar
);
664 values
[i
].l
= va_arg (vargs
, jobject
);
669 // This can call any sort of method: virtual, "nonvirtual", static, or
671 template<typename T
, invocation_type style
>
673 _Jv_JNI_CallAnyMethodV (JNIEnv
*env
, jobject obj
, jclass klass
,
674 jmethodID id
, va_list vargs
)
677 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
679 jclass decl_class
= klass
? klass
: obj
->getClass ();
680 JvAssert (decl_class
!= NULL
);
683 JArray
<jclass
> *arg_types
;
687 _Jv_GetTypesFromSignature (id
, decl_class
,
688 &arg_types
, &return_type
);
690 jvalue args
[arg_types
->length
];
691 array_from_valist (args
, arg_types
, vargs
);
693 // For constructors we need to pass the Class we are instantiating.
694 if (style
== constructor
)
698 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
699 style
== constructor
,
700 arg_types
, args
, &result
);
705 // We cheat a little here. FIXME.
706 return wrap_value (env
, * (T
*) &result
);
713 return wrap_value (env
, (T
) 0);
716 template<typename T
, invocation_type style
>
718 _Jv_JNI_CallAnyMethod (JNIEnv
*env
, jobject obj
, jclass klass
,
719 jmethodID method
, ...)
724 va_start (args
, method
);
725 result
= _Jv_JNI_CallAnyMethodV
<T
, style
> (env
, obj
, klass
, method
, args
);
731 template<typename T
, invocation_type style
>
733 _Jv_JNI_CallAnyMethodA (JNIEnv
*env
, jobject obj
, jclass klass
,
734 jmethodID id
, jvalue
*args
)
737 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
739 jclass decl_class
= klass
? klass
: obj
->getClass ();
740 JvAssert (decl_class
!= NULL
);
743 JArray
<jclass
> *arg_types
;
746 _Jv_GetTypesFromSignature (id
, decl_class
,
747 &arg_types
, &return_type
);
749 // For constructors we need to pass the Class we are instantiating.
750 if (style
== constructor
)
754 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
755 style
== constructor
,
756 arg_types
, args
, &result
);
761 // We cheat a little here. FIXME.
762 return wrap_value (env
, * (T
*) &result
);
769 return wrap_value (env
, (T
) 0);
772 template<invocation_type style
>
774 _Jv_JNI_CallAnyVoidMethodV (JNIEnv
*env
, jobject obj
, jclass klass
,
775 jmethodID id
, va_list vargs
)
778 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
780 jclass decl_class
= klass
? klass
: obj
->getClass ();
781 JvAssert (decl_class
!= NULL
);
784 JArray
<jclass
> *arg_types
;
787 _Jv_GetTypesFromSignature (id
, decl_class
,
788 &arg_types
, &return_type
);
790 jvalue args
[arg_types
->length
];
791 array_from_valist (args
, arg_types
, vargs
);
793 // For constructors we need to pass the Class we are instantiating.
794 if (style
== constructor
)
797 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
798 style
== constructor
,
799 arg_types
, args
, NULL
);
810 template<invocation_type style
>
812 _Jv_JNI_CallAnyVoidMethod (JNIEnv
*env
, jobject obj
, jclass klass
,
813 jmethodID method
, ...)
817 va_start (args
, method
);
818 _Jv_JNI_CallAnyVoidMethodV
<style
> (env
, obj
, klass
, method
, args
);
822 template<invocation_type style
>
824 _Jv_JNI_CallAnyVoidMethodA (JNIEnv
*env
, jobject obj
, jclass klass
,
825 jmethodID id
, jvalue
*args
)
828 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
830 jclass decl_class
= klass
? klass
: obj
->getClass ();
831 JvAssert (decl_class
!= NULL
);
834 JArray
<jclass
> *arg_types
;
837 _Jv_GetTypesFromSignature (id
, decl_class
,
838 &arg_types
, &return_type
);
840 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
841 style
== constructor
,
842 arg_types
, args
, NULL
);
853 // Functions with this signature are used to implement functions in
854 // the CallMethod family.
857 _Jv_JNI_CallMethodV (JNIEnv
*env
, jobject obj
, jmethodID id
, va_list args
)
859 return _Jv_JNI_CallAnyMethodV
<T
, normal
> (env
, obj
, NULL
, id
, args
);
862 // Functions with this signature are used to implement functions in
863 // the CallMethod family.
866 _Jv_JNI_CallMethod (JNIEnv
*env
, jobject obj
, jmethodID id
, ...)
872 result
= _Jv_JNI_CallAnyMethodV
<T
, normal
> (env
, obj
, NULL
, id
, args
);
878 // Functions with this signature are used to implement functions in
879 // the CallMethod family.
882 _Jv_JNI_CallMethodA (JNIEnv
*env
, jobject obj
, jmethodID id
, jvalue
*args
)
884 return _Jv_JNI_CallAnyMethodA
<T
, normal
> (env
, obj
, NULL
, id
, args
);
888 _Jv_JNI_CallVoidMethodV (JNIEnv
*env
, jobject obj
, jmethodID id
, va_list args
)
890 _Jv_JNI_CallAnyVoidMethodV
<normal
> (env
, obj
, NULL
, id
, args
);
894 _Jv_JNI_CallVoidMethod (JNIEnv
*env
, jobject obj
, jmethodID id
, ...)
899 _Jv_JNI_CallAnyVoidMethodV
<normal
> (env
, obj
, NULL
, id
, args
);
904 _Jv_JNI_CallVoidMethodA (JNIEnv
*env
, jobject obj
, jmethodID id
, jvalue
*args
)
906 _Jv_JNI_CallAnyVoidMethodA
<normal
> (env
, obj
, NULL
, id
, args
);
909 // Functions with this signature are used to implement functions in
910 // the CallStaticMethod family.
913 _Jv_JNI_CallStaticMethodV (JNIEnv
*env
, jclass klass
,
914 jmethodID id
, va_list args
)
916 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
917 JvAssert ((&ClassClass
)->isInstance (klass
));
919 return _Jv_JNI_CallAnyMethodV
<T
, static_type
> (env
, NULL
, klass
, id
, args
);
922 // Functions with this signature are used to implement functions in
923 // the CallStaticMethod family.
926 _Jv_JNI_CallStaticMethod (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
931 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
932 JvAssert ((&ClassClass
)->isInstance (klass
));
935 result
= _Jv_JNI_CallAnyMethodV
<T
, static_type
> (env
, NULL
, klass
,
942 // Functions with this signature are used to implement functions in
943 // the CallStaticMethod family.
946 _Jv_JNI_CallStaticMethodA (JNIEnv
*env
, jclass klass
, jmethodID id
,
949 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
950 JvAssert ((&ClassClass
)->isInstance (klass
));
952 return _Jv_JNI_CallAnyMethodA
<T
, static_type
> (env
, NULL
, klass
, id
, args
);
956 _Jv_JNI_CallStaticVoidMethodV (JNIEnv
*env
, jclass klass
, jmethodID id
,
959 _Jv_JNI_CallAnyVoidMethodV
<static_type
> (env
, NULL
, klass
, id
, args
);
963 _Jv_JNI_CallStaticVoidMethod (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
968 _Jv_JNI_CallAnyVoidMethodV
<static_type
> (env
, NULL
, klass
, id
, args
);
973 _Jv_JNI_CallStaticVoidMethodA (JNIEnv
*env
, jclass klass
, jmethodID id
,
976 _Jv_JNI_CallAnyVoidMethodA
<static_type
> (env
, NULL
, klass
, id
, args
);
980 _Jv_JNI_NewObjectV (JNIEnv
*env
, jclass klass
,
981 jmethodID id
, va_list args
)
983 JvAssert (klass
&& ! klass
->isArray ());
984 JvAssert (! strcmp (id
->name
->data
, "<init>")
985 && id
->signature
->length
> 2
986 && id
->signature
->data
[0] == '('
987 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
990 return _Jv_JNI_CallAnyMethodV
<jobject
, constructor
> (env
, NULL
, klass
,
995 _Jv_JNI_NewObject (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
997 JvAssert (klass
&& ! klass
->isArray ());
998 JvAssert (! strcmp (id
->name
->data
, "<init>")
999 && id
->signature
->length
> 2
1000 && id
->signature
->data
[0] == '('
1001 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
1007 va_start (args
, id
);
1008 result
= _Jv_JNI_CallAnyMethodV
<jobject
, constructor
> (env
, NULL
, klass
,
1016 _Jv_JNI_NewObjectA (JNIEnv
*env
, jclass klass
, jmethodID id
,
1019 JvAssert (klass
&& ! klass
->isArray ());
1020 JvAssert (! strcmp (id
->name
->data
, "<init>")
1021 && id
->signature
->length
> 2
1022 && id
->signature
->data
[0] == '('
1023 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
1026 return _Jv_JNI_CallAnyMethodA
<jobject
, constructor
> (env
, NULL
, klass
,
1032 template<typename T
>
1034 _Jv_JNI_GetField (JNIEnv
*env
, jobject obj
, jfieldID field
)
1037 T
*ptr
= (T
*) ((char *) obj
+ field
->getOffset ());
1038 return wrap_value (env
, *ptr
);
1041 template<typename T
>
1043 _Jv_JNI_SetField (JNIEnv
*, jobject obj
, jfieldID field
, T value
)
1046 T
*ptr
= (T
*) ((char *) obj
+ field
->getOffset ());
1050 template<jboolean is_static
>
1052 _Jv_JNI_GetAnyFieldID (JNIEnv
*env
, jclass clazz
,
1053 const char *name
, const char *sig
)
1057 _Jv_InitClass (clazz
);
1059 _Jv_Utf8Const
*a_name
= _Jv_makeUtf8Const ((char *) name
, -1);
1061 jclass field_class
= NULL
;
1063 field_class
= _Jv_FindClassFromSignature ((char *) sig
, NULL
);
1066 _Jv_Utf8Const
*sig_u
= _Jv_makeUtf8Const ((char *) sig
, -1);
1067 field_class
= _Jv_FindClass (sig_u
, NULL
);
1070 // FIXME: what if field_class == NULL?
1072 while (clazz
!= NULL
)
1074 jint count
= (is_static
1075 ? JvNumStaticFields (clazz
)
1076 : JvNumInstanceFields (clazz
));
1077 jfieldID field
= (is_static
1078 ? JvGetFirstStaticField (clazz
)
1079 : JvGetFirstInstanceField (clazz
));
1080 for (jint i
= 0; i
< count
; ++i
)
1082 // The field is resolved as a side effect of class
1084 JvAssert (field
->isResolved ());
1086 _Jv_Utf8Const
*f_name
= field
->getNameUtf8Const(clazz
);
1088 if (_Jv_equalUtf8Consts (f_name
, a_name
)
1089 && field
->getClass() == field_class
)
1092 field
= field
->getNextField ();
1095 clazz
= clazz
->getSuperclass ();
1098 env
->ex
= new java::lang::NoSuchFieldError ();
1100 catch (jthrowable t
)
1107 template<typename T
>
1109 _Jv_JNI_GetStaticField (JNIEnv
*env
, jclass
, jfieldID field
)
1111 T
*ptr
= (T
*) field
->u
.addr
;
1112 return wrap_value (env
, *ptr
);
1115 template<typename T
>
1117 _Jv_JNI_SetStaticField (JNIEnv
*, jclass
, jfieldID field
, T value
)
1119 T
*ptr
= (T
*) field
->u
.addr
;
1124 _Jv_JNI_NewString (JNIEnv
*env
, const jchar
*unichars
, jsize len
)
1128 jstring r
= _Jv_NewString (unichars
, len
);
1129 return (jstring
) wrap_value (env
, r
);
1131 catch (jthrowable t
)
1139 _Jv_JNI_GetStringLength (JNIEnv
*, jstring string
)
1141 return string
->length();
1144 static const jchar
*
1145 _Jv_JNI_GetStringChars (JNIEnv
*, jstring string
, jboolean
*isCopy
)
1147 jchar
*result
= _Jv_GetStringChars (string
);
1148 mark_for_gc (string
);
1151 return (const jchar
*) result
;
1155 _Jv_JNI_ReleaseStringChars (JNIEnv
*, jstring string
, const jchar
*)
1157 unmark_for_gc (string
);
1161 _Jv_JNI_NewStringUTF (JNIEnv
*env
, const char *bytes
)
1165 jstring result
= JvNewStringUTF (bytes
);
1166 return (jstring
) wrap_value (env
, result
);
1168 catch (jthrowable t
)
1176 _Jv_JNI_GetStringUTFLength (JNIEnv
*, jstring string
)
1178 return JvGetStringUTFLength (string
);
1182 _Jv_JNI_GetStringUTFChars (JNIEnv
*env
, jstring string
, jboolean
*isCopy
)
1184 jsize len
= JvGetStringUTFLength (string
);
1187 char *r
= (char *) _Jv_Malloc (len
+ 1);
1188 JvGetStringUTFRegion (string
, 0, len
, r
);
1194 return (const char *) r
;
1196 catch (jthrowable t
)
1204 _Jv_JNI_ReleaseStringUTFChars (JNIEnv
*, jstring
, const char *utf
)
1206 _Jv_Free ((void *) utf
);
1210 _Jv_JNI_GetStringRegion (JNIEnv
*env
, jstring string
, jsize start
, jsize len
,
1213 jchar
*result
= _Jv_GetStringChars (string
);
1214 if (start
< 0 || start
> string
->length ()
1215 || len
< 0 || start
+ len
> string
->length ())
1219 env
->ex
= new java::lang::StringIndexOutOfBoundsException ();
1221 catch (jthrowable t
)
1227 memcpy (buf
, &result
[start
], len
* sizeof (jchar
));
1231 _Jv_JNI_GetStringUTFRegion (JNIEnv
*env
, jstring str
, jsize start
,
1232 jsize len
, char *buf
)
1234 if (start
< 0 || start
> str
->length ()
1235 || len
< 0 || start
+ len
> str
->length ())
1239 env
->ex
= new java::lang::StringIndexOutOfBoundsException ();
1241 catch (jthrowable t
)
1247 _Jv_GetStringUTFRegion (str
, start
, len
, buf
);
1250 static const jchar
*
1251 _Jv_JNI_GetStringCritical (JNIEnv
*, jstring str
, jboolean
*isCopy
)
1253 jchar
*result
= _Jv_GetStringChars (str
);
1260 _Jv_JNI_ReleaseStringCritical (JNIEnv
*, jstring
, const jchar
*)
1266 _Jv_JNI_GetArrayLength (JNIEnv
*, jarray array
)
1268 return array
->length
;
1272 _Jv_JNI_NewObjectArray (JNIEnv
*env
, jsize length
, jclass elementClass
,
1277 jarray result
= JvNewObjectArray (length
, elementClass
, init
);
1278 return (jarray
) wrap_value (env
, result
);
1280 catch (jthrowable t
)
1288 _Jv_JNI_GetObjectArrayElement (JNIEnv
*env
, jobjectArray array
, jsize index
)
1290 jobject
*elts
= elements (array
);
1291 return wrap_value (env
, elts
[index
]);
1295 _Jv_JNI_SetObjectArrayElement (JNIEnv
*env
, jobjectArray array
, jsize index
,
1300 _Jv_CheckArrayStore (array
, value
);
1301 jobject
*elts
= elements (array
);
1302 elts
[index
] = value
;
1304 catch (jthrowable t
)
1310 template<typename T
, jclass K
>
1312 _Jv_JNI_NewPrimitiveArray (JNIEnv
*env
, jsize length
)
1316 return (JArray
<T
> *) wrap_value (env
, _Jv_NewPrimArray (K
, length
));
1318 catch (jthrowable t
)
1325 template<typename T
>
1327 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv
*, JArray
<T
> *array
,
1330 T
*elts
= elements (array
);
1333 // We elect never to copy.
1336 mark_for_gc (array
);
1340 template<typename T
>
1342 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv
*, JArray
<T
> *array
,
1343 T
*, jint
/* mode */)
1345 // Note that we ignore MODE. We can do this because we never copy
1346 // the array elements. My reading of the JNI documentation is that
1347 // this is an option for the implementor.
1348 unmark_for_gc (array
);
1351 template<typename T
>
1353 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv
*env
, JArray
<T
> *array
,
1354 jsize start
, jsize len
,
1357 if (start
< 0 || len
>= array
->length
|| start
+ len
>= array
->length
)
1362 env
->ex
= new java::lang::ArrayIndexOutOfBoundsException ();
1364 catch (jthrowable t
)
1366 // Could have thown out of memory error.
1372 T
*elts
= elements (array
) + start
;
1373 memcpy (buf
, elts
, len
* sizeof (T
));
1377 template<typename T
>
1379 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv
*env
, JArray
<T
> *array
,
1380 jsize start
, jsize len
, T
*buf
)
1382 if (start
< 0 || len
>= array
->length
|| start
+ len
>= array
->length
)
1387 env
->ex
= new java::lang::ArrayIndexOutOfBoundsException ();
1389 catch (jthrowable t
)
1396 T
*elts
= elements (array
) + start
;
1397 memcpy (elts
, buf
, len
* sizeof (T
));
1402 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv
*, jarray array
,
1405 // FIXME: does this work?
1406 jclass klass
= array
->getClass()->getComponentType();
1407 JvAssert (klass
->isPrimitive ());
1408 char *r
= _Jv_GetArrayElementFromElementType (array
, klass
);
1415 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv
*, jarray
, void *, jint
)
1421 _Jv_JNI_MonitorEnter (JNIEnv
*env
, jobject obj
)
1425 return _Jv_MonitorEnter (obj
);
1427 catch (jthrowable t
)
1435 _Jv_JNI_MonitorExit (JNIEnv
*env
, jobject obj
)
1439 return _Jv_MonitorExit (obj
);
1441 catch (jthrowable t
)
1450 _Jv_JNI_ToReflectedField (JNIEnv
*env
, jclass cls
, jfieldID fieldID
,
1455 java::lang::reflect::Field
*field
= new java::lang::reflect::Field();
1456 field
->declaringClass
= cls
;
1457 field
->offset
= (char*) fieldID
- (char *) cls
->fields
;
1458 field
->name
= _Jv_NewStringUtf8Const (fieldID
->getNameUtf8Const (cls
));
1459 return wrap_value (env
, field
);
1461 catch (jthrowable t
)
1470 _Jv_JNI_FromReflectedField (JNIEnv
*, jobject f
)
1472 using namespace java::lang::reflect
;
1474 Field
*field
= reinterpret_cast<Field
*> (f
);
1475 return _Jv_FromReflectedField (field
);
1479 _Jv_JNI_ToReflectedMethod (JNIEnv
*env
, jclass klass
, jmethodID id
,
1482 using namespace java::lang::reflect
;
1485 static _Jv_Utf8Const
*init_name
= _Jv_makeUtf8Const ("<init>", 6);
1487 jobject result
= NULL
;
1491 if (_Jv_equalUtf8Consts (id
->name
, init_name
))
1494 Constructor
*cons
= new Constructor ();
1495 cons
->offset
= (char *) id
- (char *) &klass
->methods
;
1496 cons
->declaringClass
= klass
;
1501 Method
*meth
= new Method ();
1502 meth
->offset
= (char *) id
- (char *) &klass
->methods
;
1503 meth
->declaringClass
= klass
;
1507 catch (jthrowable t
)
1512 return wrap_value (env
, result
);
1516 _Jv_JNI_FromReflectedMethod (JNIEnv
*, jobject method
)
1518 using namespace java::lang::reflect
;
1519 if ((&MethodClass
)->isInstance (method
))
1520 return _Jv_FromReflectedMethod (reinterpret_cast<Method
*> (method
));
1522 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor
*> (method
));
1526 _Jv_JNI_RegisterNatives (JNIEnv
*env
, jclass k
,
1527 const JNINativeMethod
*methods
,
1531 // For now, this only matters for interpreted methods. FIXME.
1532 if (! _Jv_IsInterpretedClass (k
))
1534 // FIXME: throw exception.
1537 _Jv_InterpClass
*klass
= reinterpret_cast<_Jv_InterpClass
*> (k
);
1539 // Look at each descriptor given us, and find the corresponding
1540 // method in the class.
1541 for (int j
= 0; j
< nMethods
; ++j
)
1545 _Jv_MethodBase
**imeths
= _Jv_GetFirstMethod (klass
);
1546 for (int i
= 0; i
< JvNumMethods (klass
); ++i
)
1548 _Jv_MethodBase
*meth
= imeths
[i
];
1549 _Jv_Method
*self
= meth
->get_method ();
1551 if (! strcmp (self
->name
->data
, methods
[j
].name
)
1552 && ! strcmp (self
->signature
->data
, methods
[j
].signature
))
1554 if (! (self
->accflags
1555 & java::lang::reflect::Modifier::NATIVE
))
1558 // Found a match that is native.
1559 _Jv_JNIMethod
*jmeth
= reinterpret_cast<_Jv_JNIMethod
*> (meth
);
1560 jmeth
->set_function (methods
[i
].fnPtr
);
1568 jstring m
= JvNewStringUTF (methods
[j
].name
);
1571 env
->ex
=new java::lang::NoSuchMethodError (m
);
1573 catch (jthrowable t
)
1582 #else /* INTERPRETER */
1584 #endif /* INTERPRETER */
1588 _Jv_JNI_UnregisterNatives (JNIEnv
*, jclass
)
1597 // Add a character to the buffer, encoding properly.
1599 add_char (char *buf
, jchar c
, int *here
)
1603 buf
[(*here
)++] = '_';
1604 buf
[(*here
)++] = '1';
1608 buf
[(*here
)++] = '_';
1609 buf
[(*here
)++] = '2';
1613 buf
[(*here
)++] = '_';
1614 buf
[(*here
)++] = '3';
1617 buf
[(*here
)++] = '_';
1618 else if ((c
>= '0' && c
<= '9')
1619 || (c
>= 'a' && c
<= 'z')
1620 || (c
>= 'A' && c
<= 'Z'))
1621 buf
[(*here
)++] = (char) c
;
1624 // "Unicode" character.
1625 buf
[(*here
)++] = '_';
1626 buf
[(*here
)++] = '0';
1627 for (int i
= 0; i
< 4; ++i
)
1630 buf
[(*here
) + 4 - i
] = (val
> 10) ? ('a' + val
- 10) : ('0' + val
);
1637 // Compute a mangled name for a native function. This computes the
1638 // long name, and also returns an index which indicates where a NUL
1639 // can be placed to create the short name. This function assumes that
1640 // the buffer is large enough for its results.
1642 mangled_name (jclass klass
, _Jv_Utf8Const
*func_name
,
1643 _Jv_Utf8Const
*signature
, char *buf
, int *long_start
)
1645 strcpy (buf
, "Java_");
1648 // Add fully qualified class name.
1649 jchar
*chars
= _Jv_GetStringChars (klass
->getName ());
1650 jint len
= klass
->getName ()->length ();
1651 for (int i
= 0; i
< len
; ++i
)
1652 add_char (buf
, chars
[i
], &here
);
1654 // Don't use add_char because we need a literal `_'.
1657 const unsigned char *fn
= (const unsigned char *) func_name
->data
;
1658 const unsigned char *limit
= fn
+ func_name
->length
;
1659 for (int i
= 0; ; ++i
)
1661 int ch
= UTF8_GET (fn
, limit
);
1664 add_char (buf
, ch
, &here
);
1667 // This is where the long signature begins.
1672 const unsigned char *sig
= (const unsigned char *) signature
->data
;
1673 limit
= sig
+ signature
->length
;
1674 JvAssert (sig
[0] == '(');
1678 int ch
= UTF8_GET (sig
, limit
);
1679 if (ch
== ')' || ch
< 0)
1681 add_char (buf
, ch
, &here
);
1687 // Return the current thread's JNIEnv; if one does not exist, create
1688 // it. Also create a new system frame for use. This is `extern "C"'
1689 // because the compiler calls it.
1691 _Jv_GetJNIEnvNewFrame (jclass klass
)
1693 JNIEnv
*env
= _Jv_GetCurrentJNIEnv ();
1696 env
= (JNIEnv
*) _Jv_MallocUnchecked (sizeof (JNIEnv
));
1697 env
->p
= &_Jv_JNIFunctions
;
1702 _Jv_SetCurrentJNIEnv (env
);
1705 _Jv_JNI_LocalFrame
*frame
1706 = (_Jv_JNI_LocalFrame
*) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame
)
1708 * sizeof (jobject
)));
1710 frame
->marker
= MARK_SYSTEM
;
1711 frame
->size
= FRAME_SIZE
;
1712 frame
->next
= env
->locals
;
1713 env
->locals
= frame
;
1715 for (int i
= 0; i
< frame
->size
; ++i
)
1716 frame
->vec
[i
] = NULL
;
1721 // Return the function which implements a particular JNI method. If
1722 // we can't find the function, we throw the appropriate exception.
1723 // This is `extern "C"' because the compiler uses it.
1725 _Jv_LookupJNIMethod (jclass klass
, _Jv_Utf8Const
*name
,
1726 _Jv_Utf8Const
*signature
)
1728 char buf
[10 + 6 * (name
->length
+ signature
->length
)];
1732 mangled_name (klass
, name
, signature
, buf
, &long_start
);
1733 char c
= buf
[long_start
];
1734 buf
[long_start
] = '\0';
1735 function
= _Jv_FindSymbolInExecutable (buf
);
1736 if (function
== NULL
)
1738 buf
[long_start
] = c
;
1739 function
= _Jv_FindSymbolInExecutable (buf
);
1740 if (function
== NULL
)
1742 jstring str
= JvNewStringUTF (name
->data
);
1743 JvThrow (new java::lang::AbstractMethodError (str
));
1750 // This function is the stub which is used to turn an ordinary (CNI)
1751 // method call into a JNI call.
1753 _Jv_JNIMethod::call (ffi_cif
*, void *ret
, ffi_raw
*args
, void *__this
)
1755 _Jv_JNIMethod
* _this
= (_Jv_JNIMethod
*) __this
;
1757 JNIEnv
*env
= _Jv_GetJNIEnvNewFrame (_this
->defining_class
);
1759 // FIXME: we should mark every reference parameter as a local. For
1760 // now we assume a conservative GC, and we assume that the
1761 // references are on the stack somewhere.
1763 // We cache the value that we find, of course, but if we don't find
1764 // a value we don't cache that fact -- we might subsequently load a
1765 // library which finds the function in question.
1766 if (_this
->function
== NULL
)
1767 _this
->function
= _Jv_LookupJNIMethod (_this
->defining_class
,
1769 _this
->self
->signature
);
1771 JvAssert (_this
->args_raw_size
% sizeof (ffi_raw
) == 0);
1772 ffi_raw real_args
[2 + _this
->args_raw_size
/ sizeof (ffi_raw
)];
1775 // First argument is always the environment pointer.
1776 real_args
[offset
++].ptr
= env
;
1778 // For a static method, we pass in the Class. For non-static
1779 // methods, the `this' argument is already handled.
1780 if ((_this
->self
->accflags
& java::lang::reflect::Modifier::STATIC
))
1781 real_args
[offset
++].ptr
= _this
->defining_class
;
1783 // Copy over passed-in arguments.
1784 memcpy (&real_args
[offset
], args
, _this
->args_raw_size
);
1786 // The actual call to the JNI function.
1787 ffi_raw_call (&_this
->jni_cif
, (void (*) (...)) _this
->function
,
1790 _Jv_JNI_PopSystemFrame (env
);
1793 #endif /* INTERPRETER */
1801 // An internal helper function.
1803 _Jv_JNI_AttachCurrentThread (JavaVM
*, jstring name
, void **penv
, void *args
)
1805 JavaVMAttachArgs
*attach
= reinterpret_cast<JavaVMAttachArgs
*> (args
);
1806 java::lang::ThreadGroup
*group
= NULL
;
1810 // FIXME: do we really want to support 1.1?
1811 if (attach
->version
!= JNI_VERSION_1_2
1812 && attach
->version
!= JNI_VERSION_1_1
)
1813 return JNI_EVERSION
;
1815 JvAssert ((&ThreadGroupClass
)->isInstance (attach
->group
));
1816 group
= reinterpret_cast<java::lang::ThreadGroup
*> (attach
->group
);
1819 // Attaching an already-attached thread is a no-op.
1820 if (_Jv_GetCurrentJNIEnv () != NULL
)
1823 JNIEnv
*env
= (JNIEnv
*) _Jv_MallocUnchecked (sizeof (JNIEnv
));
1826 env
->p
= &_Jv_JNIFunctions
;
1830 = (_Jv_JNI_LocalFrame
*) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame
)
1832 * sizeof (jobject
)));
1833 if (env
->locals
== NULL
)
1838 *penv
= reinterpret_cast<void *> (env
);
1840 // This thread might already be a Java thread -- this function might
1841 // have been called simply to set the new JNIEnv.
1842 if (_Jv_ThreadCurrent () == NULL
)
1846 (void) new gnu::gcj::jni::NativeThread (group
, name
);
1848 catch (jthrowable t
)
1853 _Jv_SetCurrentJNIEnv (env
);
1858 // This is the one actually used by JNI.
1860 _Jv_JNI_AttachCurrentThread (JavaVM
*vm
, void **penv
, void *args
)
1862 return _Jv_JNI_AttachCurrentThread (vm
, NULL
, penv
, args
);
1866 _Jv_JNI_DestroyJavaVM (JavaVM
*vm
)
1868 JvAssert (the_vm
&& vm
== the_vm
);
1871 if (_Jv_ThreadCurrent () != NULL
)
1877 main_name
= JvNewStringLatin1 ("main");
1879 catch (jthrowable t
)
1884 jint r
= _Jv_JNI_AttachCurrentThread (vm
,
1886 reinterpret_cast<void **> (&env
),
1892 env
= _Jv_GetCurrentJNIEnv ();
1896 // Docs say that this always returns an error code.
1901 _Jv_JNI_DetachCurrentThread (JavaVM
*)
1903 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
1905 return JNI_EDETACHED
;
1907 // FIXME: we only allow threads attached via AttachCurrentThread to
1908 // be detached. I have no idea how we could implement detaching
1909 // other threads, given the requirement that we must release all the
1910 // monitors. That just seems evil.
1911 JvAssert ((&NativeThreadClass
)->isInstance (t
));
1913 // FIXME: release the monitors. We'll take this to mean all
1914 // monitors acquired via the JNI interface. This means we have to
1915 // keep track of them.
1917 gnu::gcj::jni::NativeThread
*nt
1918 = reinterpret_cast<gnu::gcj::jni::NativeThread
*> (t
);
1925 _Jv_JNI_GetEnv (JavaVM
*, void **penv
, jint version
)
1927 if (_Jv_ThreadCurrent () == NULL
)
1930 return JNI_EDETACHED
;
1934 // Handle JVMPI requests.
1935 if (version
== JVMPI_VERSION_1
)
1937 *penv
= (void *) &_Jv_JVMPI_Interface
;
1942 // FIXME: do we really want to support 1.1?
1943 if (version
!= JNI_VERSION_1_2
&& version
!= JNI_VERSION_1_1
)
1946 return JNI_EVERSION
;
1949 *penv
= (void *) _Jv_GetCurrentJNIEnv ();
1954 JNI_GetDefaultJavaVMInitArgs (void *args
)
1956 jint version
= * (jint
*) args
;
1957 // Here we only support 1.2.
1958 if (version
!= JNI_VERSION_1_2
)
1959 return JNI_EVERSION
;
1961 JavaVMInitArgs
*ia
= reinterpret_cast<JavaVMInitArgs
*> (args
);
1962 ia
->version
= JNI_VERSION_1_2
;
1965 ia
->ignoreUnrecognized
= true;
1971 JNI_CreateJavaVM (JavaVM
**vm
, void **penv
, void *args
)
1973 JvAssert (! the_vm
);
1974 // FIXME: synchronize
1975 JavaVM
*nvm
= (JavaVM
*) _Jv_MallocUnchecked (sizeof (JavaVM
));
1978 nvm
->functions
= &_Jv_JNI_InvokeFunctions
;
1980 // Parse the arguments.
1983 jint version
= * (jint
*) args
;
1984 // We only support 1.2.
1985 if (version
!= JNI_VERSION_1_2
)
1986 return JNI_EVERSION
;
1987 JavaVMInitArgs
*ia
= reinterpret_cast<JavaVMInitArgs
*> (args
);
1988 for (int i
= 0; i
< ia
->nOptions
; ++i
)
1990 if (! strcmp (ia
->options
[i
].optionString
, "vfprintf")
1991 || ! strcmp (ia
->options
[i
].optionString
, "exit")
1992 || ! strcmp (ia
->options
[i
].optionString
, "abort"))
1994 // We are required to recognize these, but for now we
1995 // don't handle them in any way. FIXME.
1998 else if (! strncmp (ia
->options
[i
].optionString
,
1999 "-verbose", sizeof ("-verbose") - 1))
2001 // We don't do anything with this option either. We
2002 // might want to make sure the argument is valid, but we
2003 // don't really care all that much for now.
2006 else if (! strncmp (ia
->options
[i
].optionString
, "-D", 2))
2011 else if (ia
->ignoreUnrecognized
)
2013 if (ia
->options
[i
].optionString
[0] == '_'
2014 || ! strncmp (ia
->options
[i
].optionString
, "-X", 2))
2022 jint r
=_Jv_JNI_AttachCurrentThread (nvm
, penv
, NULL
);
2032 JNI_GetCreatedJavaVMs (JavaVM
**vm_buffer
, jsize buf_len
, jsize
*n_vms
)
2037 // We only support a single VM.
2040 vm_buffer
[0] = the_vm
;
2051 // FIXME: synchronize
2054 JavaVM
*nvm
= (JavaVM
*) _Jv_MallocUnchecked (sizeof (JavaVM
));
2056 nvm
->functions
= &_Jv_JNI_InvokeFunctions
;
2060 // If this is a Java thread, we want to make sure it has an
2061 // associated JNIEnv.
2062 if (_Jv_ThreadCurrent () != NULL
)
2065 _Jv_JNI_AttachCurrentThread (the_vm
, &ignore
, NULL
);
2072 _Jv_JNI_GetJavaVM (JNIEnv
*, JavaVM
**vm
)
2074 *vm
= _Jv_GetJavaVM ();
2075 return *vm
== NULL
? JNI_ERR
: JNI_OK
;
2080 #define NOT_IMPL NULL
2081 #define RESERVED NULL
2083 struct JNINativeInterface _Jv_JNIFunctions
=
2090 _Jv_JNI_DefineClass
,
2092 _Jv_JNI_FromReflectedMethod
,
2093 _Jv_JNI_FromReflectedField
,
2094 _Jv_JNI_ToReflectedMethod
,
2095 _Jv_JNI_GetSuperclass
,
2096 _Jv_JNI_IsAssignableFrom
,
2097 _Jv_JNI_ToReflectedField
,
2100 _Jv_JNI_ExceptionOccurred
,
2101 _Jv_JNI_ExceptionDescribe
,
2102 _Jv_JNI_ExceptionClear
,
2105 _Jv_JNI_PushLocalFrame
,
2106 _Jv_JNI_PopLocalFrame
,
2107 _Jv_JNI_NewGlobalRef
,
2108 _Jv_JNI_DeleteGlobalRef
,
2109 _Jv_JNI_DeleteLocalRef
,
2111 _Jv_JNI_IsSameObject
,
2113 _Jv_JNI_NewLocalRef
,
2114 _Jv_JNI_EnsureLocalCapacity
,
2116 _Jv_JNI_AllocObject
,
2120 _Jv_JNI_GetObjectClass
,
2121 _Jv_JNI_IsInstanceOf
,
2122 _Jv_JNI_GetAnyMethodID
<false>,
2124 _Jv_JNI_CallMethod
<jobject
>,
2125 _Jv_JNI_CallMethodV
<jobject
>,
2126 _Jv_JNI_CallMethodA
<jobject
>,
2127 _Jv_JNI_CallMethod
<jboolean
>,
2128 _Jv_JNI_CallMethodV
<jboolean
>,
2129 _Jv_JNI_CallMethodA
<jboolean
>,
2130 _Jv_JNI_CallMethod
<jbyte
>,
2131 _Jv_JNI_CallMethodV
<jbyte
>,
2132 _Jv_JNI_CallMethodA
<jbyte
>,
2133 _Jv_JNI_CallMethod
<jchar
>,
2134 _Jv_JNI_CallMethodV
<jchar
>,
2135 _Jv_JNI_CallMethodA
<jchar
>,
2136 _Jv_JNI_CallMethod
<jshort
>,
2137 _Jv_JNI_CallMethodV
<jshort
>,
2138 _Jv_JNI_CallMethodA
<jshort
>,
2139 _Jv_JNI_CallMethod
<jint
>,
2140 _Jv_JNI_CallMethodV
<jint
>,
2141 _Jv_JNI_CallMethodA
<jint
>,
2142 _Jv_JNI_CallMethod
<jlong
>,
2143 _Jv_JNI_CallMethodV
<jlong
>,
2144 _Jv_JNI_CallMethodA
<jlong
>,
2145 _Jv_JNI_CallMethod
<jfloat
>,
2146 _Jv_JNI_CallMethodV
<jfloat
>,
2147 _Jv_JNI_CallMethodA
<jfloat
>,
2148 _Jv_JNI_CallMethod
<jdouble
>,
2149 _Jv_JNI_CallMethodV
<jdouble
>,
2150 _Jv_JNI_CallMethodA
<jdouble
>,
2151 _Jv_JNI_CallVoidMethod
,
2152 _Jv_JNI_CallVoidMethodV
,
2153 _Jv_JNI_CallVoidMethodA
,
2155 // Nonvirtual method invocation functions follow.
2156 _Jv_JNI_CallAnyMethod
<jobject
, nonvirtual
>,
2157 _Jv_JNI_CallAnyMethodV
<jobject
, nonvirtual
>,
2158 _Jv_JNI_CallAnyMethodA
<jobject
, nonvirtual
>,
2159 _Jv_JNI_CallAnyMethod
<jboolean
, nonvirtual
>,
2160 _Jv_JNI_CallAnyMethodV
<jboolean
, nonvirtual
>,
2161 _Jv_JNI_CallAnyMethodA
<jboolean
, nonvirtual
>,
2162 _Jv_JNI_CallAnyMethod
<jbyte
, nonvirtual
>,
2163 _Jv_JNI_CallAnyMethodV
<jbyte
, nonvirtual
>,
2164 _Jv_JNI_CallAnyMethodA
<jbyte
, nonvirtual
>,
2165 _Jv_JNI_CallAnyMethod
<jchar
, nonvirtual
>,
2166 _Jv_JNI_CallAnyMethodV
<jchar
, nonvirtual
>,
2167 _Jv_JNI_CallAnyMethodA
<jchar
, nonvirtual
>,
2168 _Jv_JNI_CallAnyMethod
<jshort
, nonvirtual
>,
2169 _Jv_JNI_CallAnyMethodV
<jshort
, nonvirtual
>,
2170 _Jv_JNI_CallAnyMethodA
<jshort
, nonvirtual
>,
2171 _Jv_JNI_CallAnyMethod
<jint
, nonvirtual
>,
2172 _Jv_JNI_CallAnyMethodV
<jint
, nonvirtual
>,
2173 _Jv_JNI_CallAnyMethodA
<jint
, nonvirtual
>,
2174 _Jv_JNI_CallAnyMethod
<jlong
, nonvirtual
>,
2175 _Jv_JNI_CallAnyMethodV
<jlong
, nonvirtual
>,
2176 _Jv_JNI_CallAnyMethodA
<jlong
, nonvirtual
>,
2177 _Jv_JNI_CallAnyMethod
<jfloat
, nonvirtual
>,
2178 _Jv_JNI_CallAnyMethodV
<jfloat
, nonvirtual
>,
2179 _Jv_JNI_CallAnyMethodA
<jfloat
, nonvirtual
>,
2180 _Jv_JNI_CallAnyMethod
<jdouble
, nonvirtual
>,
2181 _Jv_JNI_CallAnyMethodV
<jdouble
, nonvirtual
>,
2182 _Jv_JNI_CallAnyMethodA
<jdouble
, nonvirtual
>,
2183 _Jv_JNI_CallAnyVoidMethod
<nonvirtual
>,
2184 _Jv_JNI_CallAnyVoidMethodV
<nonvirtual
>,
2185 _Jv_JNI_CallAnyVoidMethodA
<nonvirtual
>,
2187 _Jv_JNI_GetAnyFieldID
<false>,
2188 _Jv_JNI_GetField
<jobject
>,
2189 _Jv_JNI_GetField
<jboolean
>,
2190 _Jv_JNI_GetField
<jbyte
>,
2191 _Jv_JNI_GetField
<jchar
>,
2192 _Jv_JNI_GetField
<jshort
>,
2193 _Jv_JNI_GetField
<jint
>,
2194 _Jv_JNI_GetField
<jlong
>,
2195 _Jv_JNI_GetField
<jfloat
>,
2196 _Jv_JNI_GetField
<jdouble
>,
2206 _Jv_JNI_GetAnyMethodID
<true>,
2208 _Jv_JNI_CallStaticMethod
<jobject
>,
2209 _Jv_JNI_CallStaticMethodV
<jobject
>,
2210 _Jv_JNI_CallStaticMethodA
<jobject
>,
2211 _Jv_JNI_CallStaticMethod
<jboolean
>,
2212 _Jv_JNI_CallStaticMethodV
<jboolean
>,
2213 _Jv_JNI_CallStaticMethodA
<jboolean
>,
2214 _Jv_JNI_CallStaticMethod
<jbyte
>,
2215 _Jv_JNI_CallStaticMethodV
<jbyte
>,
2216 _Jv_JNI_CallStaticMethodA
<jbyte
>,
2217 _Jv_JNI_CallStaticMethod
<jchar
>,
2218 _Jv_JNI_CallStaticMethodV
<jchar
>,
2219 _Jv_JNI_CallStaticMethodA
<jchar
>,
2220 _Jv_JNI_CallStaticMethod
<jshort
>,
2221 _Jv_JNI_CallStaticMethodV
<jshort
>,
2222 _Jv_JNI_CallStaticMethodA
<jshort
>,
2223 _Jv_JNI_CallStaticMethod
<jint
>,
2224 _Jv_JNI_CallStaticMethodV
<jint
>,
2225 _Jv_JNI_CallStaticMethodA
<jint
>,
2226 _Jv_JNI_CallStaticMethod
<jlong
>,
2227 _Jv_JNI_CallStaticMethodV
<jlong
>,
2228 _Jv_JNI_CallStaticMethodA
<jlong
>,
2229 _Jv_JNI_CallStaticMethod
<jfloat
>,
2230 _Jv_JNI_CallStaticMethodV
<jfloat
>,
2231 _Jv_JNI_CallStaticMethodA
<jfloat
>,
2232 _Jv_JNI_CallStaticMethod
<jdouble
>,
2233 _Jv_JNI_CallStaticMethodV
<jdouble
>,
2234 _Jv_JNI_CallStaticMethodA
<jdouble
>,
2235 _Jv_JNI_CallStaticVoidMethod
,
2236 _Jv_JNI_CallStaticVoidMethodV
,
2237 _Jv_JNI_CallStaticVoidMethodA
,
2239 _Jv_JNI_GetAnyFieldID
<true>,
2240 _Jv_JNI_GetStaticField
<jobject
>,
2241 _Jv_JNI_GetStaticField
<jboolean
>,
2242 _Jv_JNI_GetStaticField
<jbyte
>,
2243 _Jv_JNI_GetStaticField
<jchar
>,
2244 _Jv_JNI_GetStaticField
<jshort
>,
2245 _Jv_JNI_GetStaticField
<jint
>,
2246 _Jv_JNI_GetStaticField
<jlong
>,
2247 _Jv_JNI_GetStaticField
<jfloat
>,
2248 _Jv_JNI_GetStaticField
<jdouble
>,
2249 _Jv_JNI_SetStaticField
,
2250 _Jv_JNI_SetStaticField
,
2251 _Jv_JNI_SetStaticField
,
2252 _Jv_JNI_SetStaticField
,
2253 _Jv_JNI_SetStaticField
,
2254 _Jv_JNI_SetStaticField
,
2255 _Jv_JNI_SetStaticField
,
2256 _Jv_JNI_SetStaticField
,
2257 _Jv_JNI_SetStaticField
,
2259 _Jv_JNI_GetStringLength
,
2260 _Jv_JNI_GetStringChars
,
2261 _Jv_JNI_ReleaseStringChars
,
2262 _Jv_JNI_NewStringUTF
,
2263 _Jv_JNI_GetStringUTFLength
,
2264 _Jv_JNI_GetStringUTFChars
,
2265 _Jv_JNI_ReleaseStringUTFChars
,
2266 _Jv_JNI_GetArrayLength
,
2267 _Jv_JNI_NewObjectArray
,
2268 _Jv_JNI_GetObjectArrayElement
,
2269 _Jv_JNI_SetObjectArrayElement
,
2270 _Jv_JNI_NewPrimitiveArray
<jboolean
, JvPrimClass (boolean
)>,
2271 _Jv_JNI_NewPrimitiveArray
<jbyte
, JvPrimClass (byte
)>,
2272 _Jv_JNI_NewPrimitiveArray
<jchar
, JvPrimClass (char)>,
2273 _Jv_JNI_NewPrimitiveArray
<jshort
, JvPrimClass (short)>,
2274 _Jv_JNI_NewPrimitiveArray
<jint
, JvPrimClass (int)>,
2275 _Jv_JNI_NewPrimitiveArray
<jlong
, JvPrimClass (long)>,
2276 _Jv_JNI_NewPrimitiveArray
<jfloat
, JvPrimClass (float)>,
2277 _Jv_JNI_NewPrimitiveArray
<jdouble
, JvPrimClass (double)>,
2278 _Jv_JNI_GetPrimitiveArrayElements
,
2279 _Jv_JNI_GetPrimitiveArrayElements
,
2280 _Jv_JNI_GetPrimitiveArrayElements
,
2281 _Jv_JNI_GetPrimitiveArrayElements
,
2282 _Jv_JNI_GetPrimitiveArrayElements
,
2283 _Jv_JNI_GetPrimitiveArrayElements
,
2284 _Jv_JNI_GetPrimitiveArrayElements
,
2285 _Jv_JNI_GetPrimitiveArrayElements
,
2286 _Jv_JNI_ReleasePrimitiveArrayElements
,
2287 _Jv_JNI_ReleasePrimitiveArrayElements
,
2288 _Jv_JNI_ReleasePrimitiveArrayElements
,
2289 _Jv_JNI_ReleasePrimitiveArrayElements
,
2290 _Jv_JNI_ReleasePrimitiveArrayElements
,
2291 _Jv_JNI_ReleasePrimitiveArrayElements
,
2292 _Jv_JNI_ReleasePrimitiveArrayElements
,
2293 _Jv_JNI_ReleasePrimitiveArrayElements
,
2294 _Jv_JNI_GetPrimitiveArrayRegion
,
2295 _Jv_JNI_GetPrimitiveArrayRegion
,
2296 _Jv_JNI_GetPrimitiveArrayRegion
,
2297 _Jv_JNI_GetPrimitiveArrayRegion
,
2298 _Jv_JNI_GetPrimitiveArrayRegion
,
2299 _Jv_JNI_GetPrimitiveArrayRegion
,
2300 _Jv_JNI_GetPrimitiveArrayRegion
,
2301 _Jv_JNI_GetPrimitiveArrayRegion
,
2302 _Jv_JNI_SetPrimitiveArrayRegion
,
2303 _Jv_JNI_SetPrimitiveArrayRegion
,
2304 _Jv_JNI_SetPrimitiveArrayRegion
,
2305 _Jv_JNI_SetPrimitiveArrayRegion
,
2306 _Jv_JNI_SetPrimitiveArrayRegion
,
2307 _Jv_JNI_SetPrimitiveArrayRegion
,
2308 _Jv_JNI_SetPrimitiveArrayRegion
,
2309 _Jv_JNI_SetPrimitiveArrayRegion
,
2310 _Jv_JNI_RegisterNatives
,
2311 _Jv_JNI_UnregisterNatives
,
2312 _Jv_JNI_MonitorEnter
,
2313 _Jv_JNI_MonitorExit
,
2316 _Jv_JNI_GetStringRegion
,
2317 _Jv_JNI_GetStringUTFRegion
,
2318 _Jv_JNI_GetPrimitiveArrayCritical
,
2319 _Jv_JNI_ReleasePrimitiveArrayCritical
,
2320 _Jv_JNI_GetStringCritical
,
2321 _Jv_JNI_ReleaseStringCritical
,
2323 NOT_IMPL
/* newweakglobalref */,
2324 NOT_IMPL
/* deleteweakglobalref */,
2326 _Jv_JNI_ExceptionCheck
2329 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions
=
2335 _Jv_JNI_DestroyJavaVM
,
2336 _Jv_JNI_AttachCurrentThread
,
2337 _Jv_JNI_DetachCurrentThread
,