1 // jni.cc - JNI implementation, including the jump table.
3 /* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
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>
24 #include <java/lang/Class.h>
25 #include <java/lang/ClassLoader.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/ArrayIndexOutOfBoundsException.h>
28 #include <java/lang/StringIndexOutOfBoundsException.h>
29 #include <java/lang/AbstractMethodError.h>
30 #include <java/lang/InstantiationException.h>
31 #include <java/lang/NoSuchFieldError.h>
32 #include <java/lang/NoSuchMethodError.h>
33 #include <java/lang/reflect/Constructor.h>
34 #include <java/lang/reflect/Method.h>
35 #include <java/lang/reflect/Modifier.h>
36 #include <java/lang/OutOfMemoryError.h>
37 #include <java/util/Hashtable.h>
38 #include <java/lang/Integer.h>
39 #include <gnu/gcj/jni/NativeThread.h>
41 #include <gcj/method.h>
42 #include <gcj/field.h>
44 #include <java-interp.h>
46 #define ClassClass _CL_Q34java4lang5Class
47 extern java::lang::Class ClassClass
;
48 #define ObjectClass _CL_Q34java4lang6Object
49 extern java::lang::Class ObjectClass
;
51 #define ThrowableClass _CL_Q34java4lang9Throwable
52 extern java::lang::Class ThrowableClass
;
53 #define MethodClass _CL_Q44java4lang7reflect6Method
54 extern java::lang::Class MethodClass
;
55 #define ThreadGroupClass _CL_Q34java4lang11ThreadGroup
56 extern java::lang::Class ThreadGroupClass
;
57 #define NativeThreadClass _CL_Q43gnu3gcj3jni12NativeThread
58 extern java::lang::Class ThreadGroupClass
;
60 // This enum is used to select different template instantiations in
61 // the invocation code.
70 // Forward declarations.
71 extern struct JNINativeInterface _Jv_JNIFunctions
;
72 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions
;
74 // Number of slots in the default frame. The VM must allow at least
78 // Mark value indicating this is an overflow frame.
80 // Mark value indicating this is a user frame.
82 // Mark value indicating this is a system frame.
85 // This structure is used to keep track of local references.
86 struct _Jv_JNI_LocalFrame
88 // This is true if this frame object represents a pushed frame (eg
89 // from PushLocalFrame).
92 // Number of elements in frame.
95 // Next frame in chain.
96 _Jv_JNI_LocalFrame
*next
;
98 // The elements. These are allocated using the C "struct hack".
102 // This holds a reference count for all local and global references.
103 static java::util::Hashtable
*ref_table
;
106 static JavaVM
*the_vm
;
113 ref_table
= new java::util::Hashtable
;
116 // Tell the GC that a certain pointer is live.
118 mark_for_gc (jobject obj
)
120 JvSynchronize
sync (ref_table
);
122 using namespace java::lang
;
123 Integer
*refcount
= (Integer
*) ref_table
->get (obj
);
124 jint val
= (refcount
== NULL
) ? 0 : refcount
->intValue ();
125 // FIXME: what about out of memory error?
126 ref_table
->put (obj
, new Integer (val
+ 1));
131 unmark_for_gc (jobject obj
)
133 JvSynchronize
sync (ref_table
);
135 using namespace java::lang
;
136 Integer
*refcount
= (Integer
*) ref_table
->get (obj
);
138 jint val
= refcount
->intValue () - 1;
140 ref_table
->remove (obj
);
142 // FIXME: what about out of memory error?
143 ref_table
->put (obj
, new Integer (val
));
149 _Jv_JNI_NewGlobalRef (JNIEnv
*, jobject obj
)
156 _Jv_JNI_DeleteGlobalRef (JNIEnv
*, jobject obj
)
162 _Jv_JNI_DeleteLocalRef (JNIEnv
*env
, jobject obj
)
164 _Jv_JNI_LocalFrame
*frame
;
166 for (frame
= env
->locals
; frame
!= NULL
; frame
= frame
->next
)
168 for (int i
= 0; i
< FRAME_SIZE
; ++i
)
170 if (frame
->vec
[i
] == obj
)
172 frame
->vec
[i
] = NULL
;
178 // Don't go past a marked frame.
179 JvAssert (frame
->marker
== MARK_NONE
);
186 _Jv_JNI_EnsureLocalCapacity (JNIEnv
*env
, jint size
)
188 // It is easier to just always allocate a new frame of the requested
189 // size. This isn't the most efficient thing, but for now we don't
190 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
192 _Jv_JNI_LocalFrame
*frame
;
195 frame
= (_Jv_JNI_LocalFrame
*) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame
)
196 + size
* sizeof (jobject
));
204 frame
->marker
= MARK_NONE
;
206 memset (&frame
->vec
[0], 0, size
* sizeof (jobject
));
207 frame
->next
= env
->locals
;
214 _Jv_JNI_PushLocalFrame (JNIEnv
*env
, jint size
)
216 jint r
= _Jv_JNI_EnsureLocalCapacity (env
, size
);
220 // The new frame is on top.
221 env
->locals
->marker
= MARK_USER
;
227 _Jv_JNI_NewLocalRef (JNIEnv
*env
, jobject obj
)
229 // Try to find an open slot somewhere in the topmost frame.
230 _Jv_JNI_LocalFrame
*frame
= env
->locals
;
231 bool done
= false, set
= false;
232 while (frame
!= NULL
&& ! done
)
234 for (int i
= 0; i
< frame
->size
; ++i
)
235 if (frame
->vec
[i
] == NULL
)
246 // No slots, so we allocate a new frame. According to the spec
247 // we could just die here. FIXME: return value.
248 _Jv_JNI_EnsureLocalCapacity (env
, 16);
249 // We know the first element of the new frame will be ok.
250 env
->locals
->vec
[0] = obj
;
258 _Jv_JNI_PopLocalFrame (JNIEnv
*env
, jobject result
, int stop
)
260 _Jv_JNI_LocalFrame
*rf
= env
->locals
;
263 while (rf
!= NULL
&& ! done
)
265 for (int i
= 0; i
< rf
->size
; ++i
)
266 if (rf
->vec
[i
] != NULL
)
267 unmark_for_gc (rf
->vec
[i
]);
269 // If the frame we just freed is the marker frame, we are done.
270 done
= (rf
->marker
== stop
);
272 _Jv_JNI_LocalFrame
*n
= rf
->next
;
273 // When N==NULL, we've reached the stack-allocated frame, and we
274 // must not free it. However, we must be sure to clear all its
275 // elements, since we might conceivably reuse it.
278 memset (&rf
->vec
[0], 0, rf
->size
* sizeof (jobject
));
286 return result
== NULL
? NULL
: _Jv_JNI_NewLocalRef (env
, result
);
290 _Jv_JNI_PopLocalFrame (JNIEnv
*env
, jobject result
)
292 return _Jv_JNI_PopLocalFrame (env
, result
, MARK_USER
);
295 // Pop a `system' frame from the stack. This is `extern "C"' as it is
296 // used by the compiler.
298 _Jv_JNI_PopSystemFrame (JNIEnv
*env
)
300 _Jv_JNI_PopLocalFrame (env
, NULL
, MARK_SYSTEM
);
303 // This function is used from other template functions. It wraps the
304 // return value appropriately; we specialize it so that object returns
305 // are turned into local references.
308 wrap_value (JNIEnv
*, T value
)
315 wrap_value (JNIEnv
*env
, jobject value
)
317 return value
== NULL
? value
: _Jv_JNI_NewLocalRef (env
, value
);
323 _Jv_JNI_GetVersion (JNIEnv
*)
325 return JNI_VERSION_1_2
;
329 _Jv_JNI_DefineClass (JNIEnv
*env
, jobject loader
,
330 const jbyte
*buf
, jsize bufLen
)
334 jbyteArray bytes
= JvNewByteArray (bufLen
);
336 jbyte
*elts
= elements (bytes
);
337 memcpy (elts
, buf
, bufLen
* sizeof (jbyte
));
339 java::lang::ClassLoader
*l
340 = reinterpret_cast<java::lang::ClassLoader
*> (loader
);
342 jclass result
= l
->defineClass (bytes
, 0, bufLen
);
343 return (jclass
) wrap_value (env
, result
);
353 _Jv_JNI_FindClass (JNIEnv
*env
, const char *name
)
355 // FIXME: assume that NAME isn't too long.
356 int len
= strlen (name
);
358 for (int i
= 0; i
<= len
; ++i
)
359 s
[i
] = (name
[i
] == '/') ? '.' : name
[i
];
364 // This might throw an out of memory exception.
365 jstring n
= JvNewStringUTF (s
);
367 java::lang::ClassLoader
*loader
;
368 if (env
->klass
== NULL
)
370 // FIXME: should use getBaseClassLoader, but we don't have that
372 loader
= java::lang::ClassLoader::getSystemClassLoader ();
375 loader
= env
->klass
->getClassLoader ();
377 r
= loader
->loadClass (n
);
384 return (jclass
) wrap_value (env
, r
);
388 _Jv_JNI_GetSuperclass (JNIEnv
*env
, jclass clazz
)
390 return (jclass
) wrap_value (env
, clazz
->getSuperclass ());
394 _Jv_JNI_IsAssignableFrom(JNIEnv
*, jclass clazz1
, jclass clazz2
)
396 return clazz1
->isAssignableFrom (clazz2
);
400 _Jv_JNI_Throw (JNIEnv
*env
, jthrowable obj
)
402 // We check in case the user did some funky cast.
403 JvAssert (obj
!= NULL
&& (&ThrowableClass
)->isInstance (obj
));
409 _Jv_JNI_ThrowNew (JNIEnv
*env
, jclass clazz
, const char *message
)
411 using namespace java::lang::reflect
;
413 JvAssert ((&ThrowableClass
)->isAssignableFrom (clazz
));
418 JArray
<jclass
> *argtypes
419 = (JArray
<jclass
> *) JvNewObjectArray (1, &ClassClass
, NULL
);
421 jclass
*elts
= elements (argtypes
);
422 elts
[0] = &StringClass
;
424 Constructor
*cons
= clazz
->getConstructor (argtypes
);
426 jobjectArray values
= JvNewObjectArray (1, &StringClass
, NULL
);
427 jobject
*velts
= elements (values
);
428 velts
[0] = JvNewStringUTF (message
);
430 jobject obj
= cons
->newInstance (values
);
432 env
->ex
= reinterpret_cast<jthrowable
> (obj
);
444 _Jv_JNI_ExceptionOccurred (JNIEnv
*env
)
446 return (jthrowable
) wrap_value (env
, env
->ex
);
450 _Jv_JNI_ExceptionDescribe (JNIEnv
*env
)
453 env
->ex
->printStackTrace();
457 _Jv_JNI_ExceptionClear (JNIEnv
*env
)
463 _Jv_JNI_ExceptionCheck (JNIEnv
*env
)
465 return env
->ex
!= NULL
;
469 _Jv_JNI_FatalError (JNIEnv
*, const char *message
)
477 _Jv_JNI_IsSameObject (JNIEnv
*, jobject obj1
, jobject obj2
)
483 _Jv_JNI_AllocObject (JNIEnv
*env
, jclass clazz
)
486 using namespace java::lang::reflect
;
490 JvAssert (clazz
&& ! clazz
->isArray ());
491 if (clazz
->isInterface() || Modifier::isAbstract(clazz
->getModifiers()))
492 env
->ex
= new java::lang::InstantiationException ();
495 // FIXME: will this work for String?
496 obj
= JvAllocObject (clazz
);
504 return wrap_value (env
, obj
);
508 _Jv_JNI_GetObjectClass (JNIEnv
*env
, jobject obj
)
511 return (jclass
) wrap_value (env
, obj
->getClass());
515 _Jv_JNI_IsInstanceOf (JNIEnv
*, jobject obj
, jclass clazz
)
517 return clazz
->isInstance(obj
);
523 // This section concerns method invocation.
526 template<jboolean is_static
>
528 _Jv_JNI_GetAnyMethodID (JNIEnv
*env
, jclass clazz
,
529 const char *name
, const char *sig
)
533 _Jv_InitClass (clazz
);
535 _Jv_Utf8Const
*name_u
= _Jv_makeUtf8Const ((char *) name
, -1);
536 _Jv_Utf8Const
*sig_u
= _Jv_makeUtf8Const ((char *) sig
, -1);
538 JvAssert (! clazz
->isPrimitive());
540 using namespace java::lang::reflect
;
542 while (clazz
!= NULL
)
544 jint count
= JvNumMethods (clazz
);
545 jmethodID meth
= JvGetFirstMethod (clazz
);
547 for (jint i
= 0; i
< count
; ++i
)
549 if (((is_static
&& Modifier::isStatic (meth
->accflags
))
550 || (! is_static
&& ! Modifier::isStatic (meth
->accflags
)))
551 && _Jv_equalUtf8Consts (meth
->name
, name_u
)
552 && _Jv_equalUtf8Consts (meth
->signature
, sig_u
))
555 meth
= meth
->getNextMethod();
558 clazz
= clazz
->getSuperclass ();
561 env
->ex
= new java::lang::NoSuchMethodError ();
571 // This is a helper function which turns a va_list into an array of
572 // `jvalue's. It needs signature information in order to do its work.
573 // The array of values must already be allocated.
575 array_from_valist (jvalue
*values
, JArray
<jclass
> *arg_types
, va_list vargs
)
577 jclass
*arg_elts
= elements (arg_types
);
578 for (int i
= 0; i
< arg_types
->length
; ++i
)
580 if (arg_elts
[i
] == JvPrimClass (byte
))
581 values
[i
].b
= va_arg (vargs
, jbyte
);
582 else if (arg_elts
[i
] == JvPrimClass (short))
583 values
[i
].s
= va_arg (vargs
, jshort
);
584 else if (arg_elts
[i
] == JvPrimClass (int))
585 values
[i
].i
= va_arg (vargs
, jint
);
586 else if (arg_elts
[i
] == JvPrimClass (long))
587 values
[i
].j
= va_arg (vargs
, jlong
);
588 else if (arg_elts
[i
] == JvPrimClass (float))
589 values
[i
].f
= va_arg (vargs
, jfloat
);
590 else if (arg_elts
[i
] == JvPrimClass (double))
591 values
[i
].d
= va_arg (vargs
, jdouble
);
592 else if (arg_elts
[i
] == JvPrimClass (boolean
))
593 values
[i
].z
= va_arg (vargs
, jboolean
);
594 else if (arg_elts
[i
] == JvPrimClass (char))
595 values
[i
].c
= va_arg (vargs
, jchar
);
599 values
[i
].l
= va_arg (vargs
, jobject
);
604 // This can call any sort of method: virtual, "nonvirtual", static, or
606 template<typename T
, invocation_type style
>
608 _Jv_JNI_CallAnyMethodV (JNIEnv
*env
, jobject obj
, jclass klass
,
609 jmethodID id
, va_list vargs
)
612 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
614 jclass decl_class
= klass
? klass
: obj
->getClass ();
615 JvAssert (decl_class
!= NULL
);
618 JArray
<jclass
> *arg_types
;
622 _Jv_GetTypesFromSignature (id
, decl_class
,
623 &arg_types
, &return_type
);
625 jvalue args
[arg_types
->length
];
626 array_from_valist (args
, arg_types
, vargs
);
628 // For constructors we need to pass the Class we are instantiating.
629 if (style
== constructor
)
633 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
634 style
== constructor
,
635 arg_types
, args
, &result
);
640 // We cheat a little here. FIXME.
641 return wrap_value (env
, * (T
*) &result
);
648 return wrap_value (env
, (T
) 0);
651 template<typename T
, invocation_type style
>
653 _Jv_JNI_CallAnyMethod (JNIEnv
*env
, jobject obj
, jclass klass
,
654 jmethodID method
, ...)
659 va_start (args
, method
);
660 result
= _Jv_JNI_CallAnyMethodV
<T
, style
> (env
, obj
, klass
, method
, args
);
666 template<typename T
, invocation_type style
>
668 _Jv_JNI_CallAnyMethodA (JNIEnv
*env
, jobject obj
, jclass klass
,
669 jmethodID id
, jvalue
*args
)
672 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
674 jclass decl_class
= klass
? klass
: obj
->getClass ();
675 JvAssert (decl_class
!= NULL
);
678 JArray
<jclass
> *arg_types
;
681 _Jv_GetTypesFromSignature (id
, decl_class
,
682 &arg_types
, &return_type
);
684 // For constructors we need to pass the Class we are instantiating.
685 if (style
== constructor
)
689 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
690 style
== constructor
,
691 arg_types
, args
, &result
);
696 // We cheat a little here. FIXME.
697 return wrap_value (env
, * (T
*) &result
);
704 return wrap_value (env
, (T
) 0);
707 template<invocation_type style
>
709 _Jv_JNI_CallAnyVoidMethodV (JNIEnv
*env
, jobject obj
, jclass klass
,
710 jmethodID id
, va_list vargs
)
713 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
715 jclass decl_class
= klass
? klass
: obj
->getClass ();
716 JvAssert (decl_class
!= NULL
);
719 JArray
<jclass
> *arg_types
;
722 _Jv_GetTypesFromSignature (id
, decl_class
,
723 &arg_types
, &return_type
);
725 jvalue args
[arg_types
->length
];
726 array_from_valist (args
, arg_types
, vargs
);
728 // For constructors we need to pass the Class we are instantiating.
729 if (style
== constructor
)
732 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
733 style
== constructor
,
734 arg_types
, args
, NULL
);
745 template<invocation_type style
>
747 _Jv_JNI_CallAnyVoidMethod (JNIEnv
*env
, jobject obj
, jclass klass
,
748 jmethodID method
, ...)
752 va_start (args
, method
);
753 _Jv_JNI_CallAnyVoidMethodV
<style
> (env
, obj
, klass
, method
, args
);
757 template<invocation_type style
>
759 _Jv_JNI_CallAnyVoidMethodA (JNIEnv
*env
, jobject obj
, jclass klass
,
760 jmethodID id
, jvalue
*args
)
763 id
= _Jv_LookupDeclaredMethod (obj
->getClass (), id
->name
, id
->signature
);
765 jclass decl_class
= klass
? klass
: obj
->getClass ();
766 JvAssert (decl_class
!= NULL
);
769 JArray
<jclass
> *arg_types
;
772 _Jv_GetTypesFromSignature (id
, decl_class
,
773 &arg_types
, &return_type
);
775 jthrowable ex
= _Jv_CallAnyMethodA (obj
, return_type
, id
,
776 style
== constructor
,
777 arg_types
, args
, NULL
);
788 // Functions with this signature are used to implement functions in
789 // the CallMethod family.
792 _Jv_JNI_CallMethodV (JNIEnv
*env
, jobject obj
, jmethodID id
, va_list args
)
794 return _Jv_JNI_CallAnyMethodV
<T
, normal
> (env
, obj
, NULL
, id
, args
);
797 // Functions with this signature are used to implement functions in
798 // the CallMethod family.
801 _Jv_JNI_CallMethod (JNIEnv
*env
, jobject obj
, jmethodID id
, ...)
807 result
= _Jv_JNI_CallAnyMethodV
<T
, normal
> (env
, obj
, NULL
, id
, args
);
813 // Functions with this signature are used to implement functions in
814 // the CallMethod family.
817 _Jv_JNI_CallMethodA (JNIEnv
*env
, jobject obj
, jmethodID id
, jvalue
*args
)
819 return _Jv_JNI_CallAnyMethodA
<T
, normal
> (env
, obj
, NULL
, id
, args
);
823 _Jv_JNI_CallVoidMethodV (JNIEnv
*env
, jobject obj
, jmethodID id
, va_list args
)
825 _Jv_JNI_CallAnyVoidMethodV
<normal
> (env
, obj
, NULL
, id
, args
);
829 _Jv_JNI_CallVoidMethod (JNIEnv
*env
, jobject obj
, jmethodID id
, ...)
834 _Jv_JNI_CallAnyVoidMethodV
<normal
> (env
, obj
, NULL
, id
, args
);
839 _Jv_JNI_CallVoidMethodA (JNIEnv
*env
, jobject obj
, jmethodID id
, jvalue
*args
)
841 _Jv_JNI_CallAnyVoidMethodA
<normal
> (env
, obj
, NULL
, id
, args
);
844 // Functions with this signature are used to implement functions in
845 // the CallStaticMethod family.
848 _Jv_JNI_CallStaticMethodV (JNIEnv
*env
, jclass klass
,
849 jmethodID id
, va_list args
)
851 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
852 JvAssert ((&ClassClass
)->isInstance (klass
));
854 return _Jv_JNI_CallAnyMethodV
<T
, static_type
> (env
, NULL
, klass
, id
, args
);
857 // Functions with this signature are used to implement functions in
858 // the CallStaticMethod family.
861 _Jv_JNI_CallStaticMethod (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
866 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
867 JvAssert ((&ClassClass
)->isInstance (klass
));
870 result
= _Jv_JNI_CallAnyMethodV
<T
, static_type
> (env
, NULL
, klass
,
877 // Functions with this signature are used to implement functions in
878 // the CallStaticMethod family.
881 _Jv_JNI_CallStaticMethodA (JNIEnv
*env
, jclass klass
, jmethodID id
,
884 JvAssert (((id
->accflags
) & java::lang::reflect::Modifier::STATIC
));
885 JvAssert ((&ClassClass
)->isInstance (klass
));
887 return _Jv_JNI_CallAnyMethodA
<T
, static_type
> (env
, NULL
, klass
, id
, args
);
891 _Jv_JNI_CallStaticVoidMethodV (JNIEnv
*env
, jclass klass
, jmethodID id
,
894 _Jv_JNI_CallAnyVoidMethodV
<static_type
> (env
, NULL
, klass
, id
, args
);
898 _Jv_JNI_CallStaticVoidMethod (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
903 _Jv_JNI_CallAnyVoidMethodV
<static_type
> (env
, NULL
, klass
, id
, args
);
908 _Jv_JNI_CallStaticVoidMethodA (JNIEnv
*env
, jclass klass
, jmethodID id
,
911 _Jv_JNI_CallAnyVoidMethodA
<static_type
> (env
, NULL
, klass
, id
, args
);
915 _Jv_JNI_NewObjectV (JNIEnv
*env
, jclass klass
,
916 jmethodID id
, va_list args
)
918 JvAssert (klass
&& ! klass
->isArray ());
919 JvAssert (! strcmp (id
->name
->data
, "<init>")
920 && id
->signature
->length
> 2
921 && id
->signature
->data
[0] == '('
922 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
925 return _Jv_JNI_CallAnyMethodV
<jobject
, constructor
> (env
, NULL
, klass
,
930 _Jv_JNI_NewObject (JNIEnv
*env
, jclass klass
, jmethodID id
, ...)
932 JvAssert (klass
&& ! klass
->isArray ());
933 JvAssert (! strcmp (id
->name
->data
, "<init>")
934 && id
->signature
->length
> 2
935 && id
->signature
->data
[0] == '('
936 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
943 result
= _Jv_JNI_CallAnyMethodV
<jobject
, constructor
> (env
, NULL
, klass
,
951 _Jv_JNI_NewObjectA (JNIEnv
*env
, jclass klass
, jmethodID id
,
954 JvAssert (klass
&& ! klass
->isArray ());
955 JvAssert (! strcmp (id
->name
->data
, "<init>")
956 && id
->signature
->length
> 2
957 && id
->signature
->data
[0] == '('
958 && ! strcmp (&id
->signature
->data
[id
->signature
->length
- 2],
961 return _Jv_JNI_CallAnyMethodA
<jobject
, constructor
> (env
, NULL
, klass
,
969 _Jv_JNI_GetField (JNIEnv
*env
, jobject obj
, jfieldID field
)
972 T
*ptr
= (T
*) ((char *) obj
+ field
->getOffset ());
973 return wrap_value (env
, *ptr
);
978 _Jv_JNI_SetField (JNIEnv
*, jobject obj
, jfieldID field
, T value
)
981 T
*ptr
= (T
*) ((char *) obj
+ field
->getOffset ());
985 template<jboolean is_static
>
987 _Jv_JNI_GetAnyFieldID (JNIEnv
*env
, jclass clazz
,
988 const char *name
, const char *sig
)
992 _Jv_InitClass (clazz
);
994 _Jv_Utf8Const
*a_name
= _Jv_makeUtf8Const ((char *) name
, -1);
996 jclass field_class
= NULL
;
998 field_class
= _Jv_FindClassFromSignature ((char *) sig
, NULL
);
1001 _Jv_Utf8Const
*sig_u
= _Jv_makeUtf8Const ((char *) sig
, -1);
1002 field_class
= _Jv_FindClass (sig_u
, NULL
);
1005 // FIXME: what if field_class == NULL?
1007 while (clazz
!= NULL
)
1009 jint count
= (is_static
1010 ? JvNumStaticFields (clazz
)
1011 : JvNumInstanceFields (clazz
));
1012 jfieldID field
= (is_static
1013 ? JvGetFirstStaticField (clazz
)
1014 : JvGetFirstInstanceField (clazz
));
1015 for (jint i
= 0; i
< count
; ++i
)
1017 // The field is resolved as a side effect of class
1019 JvAssert (field
->isResolved ());
1021 _Jv_Utf8Const
*f_name
= field
->getNameUtf8Const(clazz
);
1023 if (_Jv_equalUtf8Consts (f_name
, a_name
)
1024 && field
->getClass() == field_class
)
1027 field
= field
->getNextField ();
1030 clazz
= clazz
->getSuperclass ();
1033 env
->ex
= new java::lang::NoSuchFieldError ();
1035 catch (jthrowable t
)
1042 template<typename T
>
1044 _Jv_JNI_GetStaticField (JNIEnv
*env
, jclass
, jfieldID field
)
1046 T
*ptr
= (T
*) field
->u
.addr
;
1047 return wrap_value (env
, *ptr
);
1050 template<typename T
>
1052 _Jv_JNI_SetStaticField (JNIEnv
*, jclass
, jfieldID field
, T value
)
1054 T
*ptr
= (T
*) field
->u
.addr
;
1059 _Jv_JNI_NewString (JNIEnv
*env
, const jchar
*unichars
, jsize len
)
1063 jstring r
= _Jv_NewString (unichars
, len
);
1064 return (jstring
) wrap_value (env
, r
);
1066 catch (jthrowable t
)
1074 _Jv_JNI_GetStringLength (JNIEnv
*, jstring string
)
1076 return string
->length();
1079 static const jchar
*
1080 _Jv_JNI_GetStringChars (JNIEnv
*, jstring string
, jboolean
*isCopy
)
1082 jchar
*result
= _Jv_GetStringChars (string
);
1083 mark_for_gc (string
);
1086 return (const jchar
*) result
;
1090 _Jv_JNI_ReleaseStringChars (JNIEnv
*, jstring string
, const jchar
*)
1092 unmark_for_gc (string
);
1096 _Jv_JNI_NewStringUTF (JNIEnv
*env
, const char *bytes
)
1100 jstring result
= JvNewStringUTF (bytes
);
1101 return (jstring
) wrap_value (env
, result
);
1103 catch (jthrowable t
)
1111 _Jv_JNI_GetStringUTFLength (JNIEnv
*, jstring string
)
1113 return JvGetStringUTFLength (string
);
1117 _Jv_JNI_GetStringUTFChars (JNIEnv
*env
, jstring string
, jboolean
*isCopy
)
1119 jsize len
= JvGetStringUTFLength (string
);
1122 char *r
= (char *) _Jv_Malloc (len
+ 1);
1123 JvGetStringUTFRegion (string
, 0, len
, r
);
1129 return (const char *) r
;
1131 catch (jthrowable t
)
1139 _Jv_JNI_ReleaseStringUTFChars (JNIEnv
*, jstring
, const char *utf
)
1141 _Jv_Free ((void *) utf
);
1145 _Jv_JNI_GetStringRegion (JNIEnv
*env
, jstring string
, jsize start
, jsize len
,
1148 jchar
*result
= _Jv_GetStringChars (string
);
1149 if (start
< 0 || start
> string
->length ()
1150 || len
< 0 || start
+ len
> string
->length ())
1154 env
->ex
= new java::lang::StringIndexOutOfBoundsException ();
1156 catch (jthrowable t
)
1162 memcpy (buf
, &result
[start
], len
* sizeof (jchar
));
1166 _Jv_JNI_GetStringUTFRegion (JNIEnv
*env
, jstring str
, jsize start
,
1167 jsize len
, char *buf
)
1169 if (start
< 0 || start
> str
->length ()
1170 || len
< 0 || start
+ len
> str
->length ())
1174 env
->ex
= new java::lang::StringIndexOutOfBoundsException ();
1176 catch (jthrowable t
)
1182 _Jv_GetStringUTFRegion (str
, start
, len
, buf
);
1185 static const jchar
*
1186 _Jv_JNI_GetStringCritical (JNIEnv
*, jstring str
, jboolean
*isCopy
)
1188 jchar
*result
= _Jv_GetStringChars (str
);
1195 _Jv_JNI_ReleaseStringCritical (JNIEnv
*, jstring
, const jchar
*)
1201 _Jv_JNI_GetArrayLength (JNIEnv
*, jarray array
)
1203 return array
->length
;
1207 _Jv_JNI_NewObjectArray (JNIEnv
*env
, jsize length
, jclass elementClass
,
1212 jarray result
= JvNewObjectArray (length
, elementClass
, init
);
1213 return (jarray
) wrap_value (env
, result
);
1215 catch (jthrowable t
)
1223 _Jv_JNI_GetObjectArrayElement (JNIEnv
*env
, jobjectArray array
, jsize index
)
1225 jobject
*elts
= elements (array
);
1226 return wrap_value (env
, elts
[index
]);
1230 _Jv_JNI_SetObjectArrayElement (JNIEnv
*env
, jobjectArray array
, jsize index
,
1235 _Jv_CheckArrayStore (array
, value
);
1236 jobject
*elts
= elements (array
);
1237 elts
[index
] = value
;
1239 catch (jthrowable t
)
1245 template<typename T
, jclass K
>
1247 _Jv_JNI_NewPrimitiveArray (JNIEnv
*env
, jsize length
)
1251 return (JArray
<T
> *) wrap_value (env
, _Jv_NewPrimArray (K
, length
));
1253 catch (jthrowable t
)
1260 template<typename T
>
1262 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv
*, JArray
<T
> *array
,
1265 T
*elts
= elements (array
);
1268 // We elect never to copy.
1271 mark_for_gc (array
);
1275 template<typename T
>
1277 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv
*, JArray
<T
> *array
,
1278 T
*, jint
/* mode */)
1280 // Note that we ignore MODE. We can do this because we never copy
1281 // the array elements. My reading of the JNI documentation is that
1282 // this is an option for the implementor.
1283 unmark_for_gc (array
);
1286 template<typename T
>
1288 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv
*env
, JArray
<T
> *array
,
1289 jsize start
, jsize len
,
1292 if (start
< 0 || len
>= array
->length
|| start
+ len
>= array
->length
)
1297 env
->ex
= new java::lang::ArrayIndexOutOfBoundsException ();
1299 catch (jthrowable t
)
1301 // Could have thown out of memory error.
1307 T
*elts
= elements (array
) + start
;
1308 memcpy (buf
, elts
, len
* sizeof (T
));
1312 template<typename T
>
1314 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv
*env
, JArray
<T
> *array
,
1315 jsize start
, jsize len
, T
*buf
)
1317 if (start
< 0 || len
>= array
->length
|| start
+ len
>= array
->length
)
1322 env
->ex
= new java::lang::ArrayIndexOutOfBoundsException ();
1324 catch (jthrowable t
)
1331 T
*elts
= elements (array
) + start
;
1332 memcpy (elts
, buf
, len
* sizeof (T
));
1337 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv
*, jarray array
,
1340 // FIXME: does this work?
1341 jclass klass
= array
->getClass()->getComponentType();
1342 JvAssert (klass
->isPrimitive ());
1343 char *r
= _Jv_GetArrayElementFromElementType (array
, klass
);
1350 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv
*, jarray
, void *, jint
)
1356 _Jv_JNI_MonitorEnter (JNIEnv
*env
, jobject obj
)
1360 return _Jv_MonitorEnter (obj
);
1362 catch (jthrowable t
)
1370 _Jv_JNI_MonitorExit (JNIEnv
*env
, jobject obj
)
1374 return _Jv_MonitorExit (obj
);
1376 catch (jthrowable t
)
1385 _Jv_JNI_ToReflectedField (JNIEnv
*env
, jclass cls
, jfieldID fieldID
,
1390 java::lang::reflect::Field
*field
= new java::lang::reflect::Field();
1391 field
->declaringClass
= cls
;
1392 field
->offset
= (char*) fieldID
- (char *) cls
->fields
;
1393 field
->name
= _Jv_NewStringUtf8Const (fieldID
->getNameUtf8Const (cls
));
1394 return wrap_value (env
, field
);
1396 catch (jthrowable t
)
1405 _Jv_JNI_FromReflectedField (JNIEnv
*, jobject f
)
1407 using namespace java::lang::reflect
;
1409 Field
*field
= reinterpret_cast<Field
*> (f
);
1410 return _Jv_FromReflectedField (field
);
1414 _Jv_JNI_ToReflectedMethod (JNIEnv
*env
, jclass klass
, jmethodID id
,
1417 using namespace java::lang::reflect
;
1420 static _Jv_Utf8Const
*init_name
= _Jv_makeUtf8Const ("<init>", 6);
1422 jobject result
= NULL
;
1426 if (_Jv_equalUtf8Consts (id
->name
, init_name
))
1429 Constructor
*cons
= new Constructor ();
1430 cons
->offset
= (char *) id
- (char *) &klass
->methods
;
1431 cons
->declaringClass
= klass
;
1436 Method
*meth
= new Method ();
1437 meth
->offset
= (char *) id
- (char *) &klass
->methods
;
1438 meth
->declaringClass
= klass
;
1442 catch (jthrowable t
)
1447 return wrap_value (env
, result
);
1451 _Jv_JNI_FromReflectedMethod (JNIEnv
*, jobject method
)
1453 using namespace java::lang::reflect
;
1454 if ((&MethodClass
)->isInstance (method
))
1455 return _Jv_FromReflectedMethod (reinterpret_cast<Method
*> (method
));
1457 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor
*> (method
));
1461 _Jv_JNI_RegisterNatives (JNIEnv
*env
, jclass k
,
1462 const JNINativeMethod
*methods
,
1466 // For now, this only matters for interpreted methods. FIXME.
1467 if (! _Jv_IsInterpretedClass (k
))
1469 // FIXME: throw exception.
1472 _Jv_InterpClass
*klass
= reinterpret_cast<_Jv_InterpClass
*> (k
);
1474 // Look at each descriptor given us, and find the corresponding
1475 // method in the class.
1476 for (int j
= 0; j
< nMethods
; ++j
)
1480 _Jv_MethodBase
**imeths
= _Jv_GetFirstMethod (klass
);
1481 for (int i
= 0; i
< JvNumMethods (klass
); ++i
)
1483 _Jv_MethodBase
*meth
= imeths
[i
];
1484 _Jv_Method
*self
= meth
->get_method ();
1486 if (! strcmp (self
->name
->data
, methods
[j
].name
)
1487 && ! strcmp (self
->signature
->data
, methods
[j
].signature
))
1489 if (! (self
->accflags
1490 & java::lang::reflect::Modifier::NATIVE
))
1493 // Found a match that is native.
1494 _Jv_JNIMethod
*jmeth
= reinterpret_cast<_Jv_JNIMethod
*> (meth
);
1495 jmeth
->set_function (methods
[i
].fnPtr
);
1503 jstring m
= JvNewStringUTF (methods
[j
].name
);
1506 env
->ex
=new java::lang::NoSuchMethodError (m
);
1508 catch (jthrowable t
)
1517 #else /* INTERPRETER */
1519 #endif /* INTERPRETER */
1523 _Jv_JNI_UnregisterNatives (JNIEnv
*, jclass
)
1532 // Add a character to the buffer, encoding properly.
1534 add_char (char *buf
, jchar c
, int *here
)
1538 buf
[(*here
)++] = '_';
1539 buf
[(*here
)++] = '1';
1543 buf
[(*here
)++] = '_';
1544 buf
[(*here
)++] = '2';
1548 buf
[(*here
)++] = '_';
1549 buf
[(*here
)++] = '3';
1552 buf
[(*here
)++] = '_';
1553 else if ((c
>= '0' && c
<= '9')
1554 || (c
>= 'a' && c
<= 'z')
1555 || (c
>= 'A' && c
<= 'Z'))
1556 buf
[(*here
)++] = (char) c
;
1559 // "Unicode" character.
1560 buf
[(*here
)++] = '_';
1561 buf
[(*here
)++] = '0';
1562 for (int i
= 0; i
< 4; ++i
)
1565 buf
[(*here
) + 4 - i
] = (val
> 10) ? ('a' + val
- 10) : ('0' + val
);
1572 // Compute a mangled name for a native function. This computes the
1573 // long name, and also returns an index which indicates where a NUL
1574 // can be placed to create the short name. This function assumes that
1575 // the buffer is large enough for its results.
1577 mangled_name (jclass klass
, _Jv_Utf8Const
*func_name
,
1578 _Jv_Utf8Const
*signature
, char *buf
, int *long_start
)
1580 strcpy (buf
, "Java_");
1583 // Add fully qualified class name.
1584 jchar
*chars
= _Jv_GetStringChars (klass
->getName ());
1585 jint len
= klass
->getName ()->length ();
1586 for (int i
= 0; i
< len
; ++i
)
1587 add_char (buf
, chars
[i
], &here
);
1589 // Don't use add_char because we need a literal `_'.
1592 const unsigned char *fn
= (const unsigned char *) func_name
->data
;
1593 const unsigned char *limit
= fn
+ func_name
->length
;
1594 for (int i
= 0; ; ++i
)
1596 int ch
= UTF8_GET (fn
, limit
);
1599 add_char (buf
, ch
, &here
);
1602 // This is where the long signature begins.
1607 const unsigned char *sig
= (const unsigned char *) signature
->data
;
1608 limit
= sig
+ signature
->length
;
1609 JvAssert (signature
[0] == '(');
1613 int ch
= UTF8_GET (sig
, limit
);
1614 if (ch
== ')' || ch
< 0)
1616 add_char (buf
, ch
, &here
);
1622 // Return the current thread's JNIEnv; if one does not exist, create
1623 // it. Also create a new system frame for use. This is `extern "C"'
1624 // because the compiler calls it.
1626 _Jv_GetJNIEnvNewFrame (jclass klass
)
1628 JNIEnv
*env
= _Jv_GetCurrentJNIEnv ();
1631 env
= (JNIEnv
*) _Jv_MallocUnchecked (sizeof (JNIEnv
));
1632 env
->p
= &_Jv_JNIFunctions
;
1637 _Jv_SetCurrentJNIEnv (env
);
1640 _Jv_JNI_LocalFrame
*frame
1641 = (_Jv_JNI_LocalFrame
*) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame
)
1643 * sizeof (jobject
)));
1645 frame
->marker
= MARK_SYSTEM
;
1646 frame
->size
= FRAME_SIZE
;
1647 frame
->next
= env
->locals
;
1648 env
->locals
= frame
;
1650 for (int i
= 0; i
< frame
->size
; ++i
)
1651 frame
->vec
[i
] = NULL
;
1656 // Return the function which implements a particular JNI method. If
1657 // we can't find the function, we throw the appropriate exception.
1658 // This is `extern "C"' because the compiler uses it.
1660 _Jv_LookupJNIMethod (jclass klass
, _Jv_Utf8Const
*name
,
1661 _Jv_Utf8Const
*signature
)
1663 char buf
[10 + 6 * (name
->length
+ signature
->length
)];
1667 mangled_name (klass
, name
, signature
, buf
, &long_start
);
1668 char c
= buf
[long_start
];
1669 buf
[long_start
] = '\0';
1670 function
= _Jv_FindSymbolInExecutable (buf
);
1671 if (function
== NULL
)
1673 buf
[long_start
] = c
;
1674 function
= _Jv_FindSymbolInExecutable (buf
);
1675 if (function
== NULL
)
1677 jstring str
= JvNewStringUTF (name
->data
);
1678 JvThrow (new java::lang::AbstractMethodError (str
));
1685 // This function is the stub which is used to turn an ordinary (CNI)
1686 // method call into a JNI call.
1688 _Jv_JNIMethod::call (ffi_cif
*, void *ret
, ffi_raw
*args
, void *__this
)
1690 _Jv_JNIMethod
* _this
= (_Jv_JNIMethod
*) __this
;
1692 JNIEnv
*env
= _Jv_GetJNIEnvNewFrame (_this
->defining_class
);
1694 // FIXME: we should mark every reference parameter as a local. For
1695 // now we assume a conservative GC, and we assume that the
1696 // references are on the stack somewhere.
1698 // We cache the value that we find, of course, but if we don't find
1699 // a value we don't cache that fact -- we might subsequently load a
1700 // library which finds the function in question.
1701 if (_this
->function
== NULL
)
1702 _this
->function
= _Jv_LookupJNIMethod (_this
->defining_class
,
1704 _this
->self
->signature
);
1706 JvAssert (_this
->args_raw_size
% sizeof (ffi_raw
) == 0);
1707 ffi_raw real_args
[2 + _this
->args_raw_size
/ sizeof (ffi_raw
)];
1710 // First argument is always the environment pointer.
1711 real_args
[offset
++].ptr
= env
;
1713 // For a static method, we pass in the Class. For non-static
1714 // methods, the `this' argument is already handled.
1715 if ((_this
->self
->accflags
& java::lang::reflect::Modifier::STATIC
))
1716 real_args
[offset
++].ptr
= _this
->defining_class
;
1718 // Copy over passed-in arguments.
1719 memcpy (&real_args
[offset
], args
, _this
->args_raw_size
);
1721 // The actual call to the JNI function.
1722 ffi_raw_call (&_this
->jni_cif
, (void (*) (...)) _this
->function
,
1725 _Jv_JNI_PopSystemFrame (env
);
1731 #endif /* INTERPRETER */
1739 // An internal helper function.
1741 _Jv_JNI_AttachCurrentThread (JavaVM
*, jstring name
, void **penv
, void *args
)
1743 JavaVMAttachArgs
*attach
= reinterpret_cast<JavaVMAttachArgs
*> (args
);
1744 java::lang::ThreadGroup
*group
= NULL
;
1748 // FIXME: do we really want to support 1.1?
1749 if (attach
->version
!= JNI_VERSION_1_2
1750 && attach
->version
!= JNI_VERSION_1_1
)
1751 return JNI_EVERSION
;
1753 JvAssert ((&ThreadGroupClass
)->isInstance (attach
->group
));
1754 group
= reinterpret_cast<java::lang::ThreadGroup
*> (attach
->group
);
1757 // Attaching an already-attached thread is a no-op.
1758 if (_Jv_GetCurrentJNIEnv () != NULL
)
1761 JNIEnv
*env
= (JNIEnv
*) _Jv_MallocUnchecked (sizeof (JNIEnv
));
1764 env
->p
= &_Jv_JNIFunctions
;
1768 = (_Jv_JNI_LocalFrame
*) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame
)
1770 * sizeof (jobject
)));
1771 if (env
->locals
== NULL
)
1776 *penv
= reinterpret_cast<void *> (env
);
1778 // This thread might already be a Java thread -- this function might
1779 // have been called simply to set the new JNIEnv.
1780 if (_Jv_ThreadCurrent () == NULL
)
1784 (void) new gnu::gcj::jni::NativeThread (group
, name
);
1786 catch (jthrowable t
)
1791 _Jv_SetCurrentJNIEnv (env
);
1796 // This is the one actually used by JNI.
1798 _Jv_JNI_AttachCurrentThread (JavaVM
*vm
, void **penv
, void *args
)
1800 return _Jv_JNI_AttachCurrentThread (vm
, NULL
, penv
, args
);
1804 _Jv_JNI_DestroyJavaVM (JavaVM
*vm
)
1806 JvAssert (the_vm
&& vm
== the_vm
);
1809 if (_Jv_ThreadCurrent () != NULL
)
1815 main_name
= JvNewStringLatin1 ("main");
1817 catch (jthrowable t
)
1822 jint r
= _Jv_JNI_AttachCurrentThread (vm
,
1824 reinterpret_cast<void **> (&env
),
1830 env
= _Jv_GetCurrentJNIEnv ();
1834 // Docs say that this always returns an error code.
1839 _Jv_JNI_DetachCurrentThread (JavaVM
*)
1841 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
1843 return JNI_EDETACHED
;
1845 // FIXME: we only allow threads attached via AttachCurrentThread to
1846 // be detached. I have no idea how we could implement detaching
1847 // other threads, given the requirement that we must release all the
1848 // monitors. That just seems evil.
1849 JvAssert ((&NativeThreadClass
)->isInstance (t
));
1851 // FIXME: release the monitors. We'll take this to mean all
1852 // monitors acquired via the JNI interface. This means we have to
1853 // keep track of them.
1855 gnu::gcj::jni::NativeThread
*nt
1856 = reinterpret_cast<gnu::gcj::jni::NativeThread
*> (t
);
1863 _Jv_JNI_GetEnv (JavaVM
*, void **penv
, jint version
)
1865 if (_Jv_ThreadCurrent () == NULL
)
1868 return JNI_EDETACHED
;
1871 // FIXME: do we really want to support 1.1?
1872 if (version
!= JNI_VERSION_1_2
&& version
!= JNI_VERSION_1_1
)
1875 return JNI_EVERSION
;
1878 *penv
= (void *) _Jv_GetCurrentJNIEnv ();
1883 JNI_GetDefaultJavaVMInitArgs (void *args
)
1885 jint version
= * (jint
*) args
;
1886 // Here we only support 1.2.
1887 if (version
!= JNI_VERSION_1_2
)
1888 return JNI_EVERSION
;
1890 JavaVMInitArgs
*ia
= reinterpret_cast<JavaVMInitArgs
*> (args
);
1891 ia
->version
= JNI_VERSION_1_2
;
1894 ia
->ignoreUnrecognized
= true;
1900 JNI_CreateJavaVM (JavaVM
**vm
, void **penv
, void *args
)
1902 JvAssert (! the_vm
);
1903 // FIXME: synchronize
1904 JavaVM
*nvm
= (JavaVM
*) _Jv_MallocUnchecked (sizeof (JavaVM
));
1907 nvm
->functions
= &_Jv_JNI_InvokeFunctions
;
1909 // Parse the arguments.
1912 jint version
= * (jint
*) args
;
1913 // We only support 1.2.
1914 if (version
!= JNI_VERSION_1_2
)
1915 return JNI_EVERSION
;
1916 JavaVMInitArgs
*ia
= reinterpret_cast<JavaVMInitArgs
*> (args
);
1917 for (int i
= 0; i
< ia
->nOptions
; ++i
)
1919 if (! strcmp (ia
->options
[i
].optionString
, "vfprintf")
1920 || ! strcmp (ia
->options
[i
].optionString
, "exit")
1921 || ! strcmp (ia
->options
[i
].optionString
, "abort"))
1923 // We are required to recognize these, but for now we
1924 // don't handle them in any way. FIXME.
1927 else if (! strncmp (ia
->options
[i
].optionString
,
1928 "-verbose", sizeof ("-verbose") - 1))
1930 // We don't do anything with this option either. We
1931 // might want to make sure the argument is valid, but we
1932 // don't really care all that much for now.
1935 else if (! strncmp (ia
->options
[i
].optionString
, "-D", 2))
1940 else if (ia
->ignoreUnrecognized
)
1942 if (ia
->options
[i
].optionString
[0] == '_'
1943 || ! strncmp (ia
->options
[i
].optionString
, "-X", 2))
1951 jint r
=_Jv_JNI_AttachCurrentThread (nvm
, penv
, NULL
);
1961 JNI_GetCreatedJavaVMs (JavaVM
**vm_buffer
, jsize
/* buf_len */, jsize
*n_vms
)
1963 JvAssert (buf_len
> 0);
1964 // We only support a single VM.
1967 vm_buffer
[0] = the_vm
;
1978 // FIXME: synchronize
1981 JavaVM
*nvm
= (JavaVM
*) _Jv_MallocUnchecked (sizeof (JavaVM
));
1983 nvm
->functions
= &_Jv_JNI_InvokeFunctions
;
1987 // If this is a Java thread, we want to make sure it has an
1988 // associated JNIEnv.
1989 if (_Jv_ThreadCurrent () != NULL
)
1992 _Jv_JNI_AttachCurrentThread (the_vm
, &ignore
, NULL
);
1999 _Jv_JNI_GetJavaVM (JNIEnv
*, JavaVM
**vm
)
2001 *vm
= _Jv_GetJavaVM ();
2002 return *vm
== NULL
? JNI_ERR
: JNI_OK
;
2007 #define NOT_IMPL NULL
2008 #define RESERVED NULL
2010 struct JNINativeInterface _Jv_JNIFunctions
=
2017 _Jv_JNI_DefineClass
,
2019 _Jv_JNI_FromReflectedMethod
,
2020 _Jv_JNI_FromReflectedField
,
2021 _Jv_JNI_ToReflectedMethod
,
2022 _Jv_JNI_GetSuperclass
,
2023 _Jv_JNI_IsAssignableFrom
,
2024 _Jv_JNI_ToReflectedField
,
2027 _Jv_JNI_ExceptionOccurred
,
2028 _Jv_JNI_ExceptionDescribe
,
2029 _Jv_JNI_ExceptionClear
,
2032 _Jv_JNI_PushLocalFrame
,
2033 _Jv_JNI_PopLocalFrame
,
2034 _Jv_JNI_NewGlobalRef
,
2035 _Jv_JNI_DeleteGlobalRef
,
2036 _Jv_JNI_DeleteLocalRef
,
2038 _Jv_JNI_IsSameObject
,
2040 _Jv_JNI_NewLocalRef
,
2041 _Jv_JNI_EnsureLocalCapacity
,
2043 _Jv_JNI_AllocObject
,
2047 _Jv_JNI_GetObjectClass
,
2048 _Jv_JNI_IsInstanceOf
,
2049 _Jv_JNI_GetAnyMethodID
<false>,
2051 _Jv_JNI_CallMethod
<jobject
>,
2052 _Jv_JNI_CallMethodV
<jobject
>,
2053 _Jv_JNI_CallMethodA
<jobject
>,
2054 _Jv_JNI_CallMethod
<jboolean
>,
2055 _Jv_JNI_CallMethodV
<jboolean
>,
2056 _Jv_JNI_CallMethodA
<jboolean
>,
2057 _Jv_JNI_CallMethod
<jbyte
>,
2058 _Jv_JNI_CallMethodV
<jbyte
>,
2059 _Jv_JNI_CallMethodA
<jbyte
>,
2060 _Jv_JNI_CallMethod
<jchar
>,
2061 _Jv_JNI_CallMethodV
<jchar
>,
2062 _Jv_JNI_CallMethodA
<jchar
>,
2063 _Jv_JNI_CallMethod
<jshort
>,
2064 _Jv_JNI_CallMethodV
<jshort
>,
2065 _Jv_JNI_CallMethodA
<jshort
>,
2066 _Jv_JNI_CallMethod
<jint
>,
2067 _Jv_JNI_CallMethodV
<jint
>,
2068 _Jv_JNI_CallMethodA
<jint
>,
2069 _Jv_JNI_CallMethod
<jlong
>,
2070 _Jv_JNI_CallMethodV
<jlong
>,
2071 _Jv_JNI_CallMethodA
<jlong
>,
2072 _Jv_JNI_CallMethod
<jfloat
>,
2073 _Jv_JNI_CallMethodV
<jfloat
>,
2074 _Jv_JNI_CallMethodA
<jfloat
>,
2075 _Jv_JNI_CallMethod
<jdouble
>,
2076 _Jv_JNI_CallMethodV
<jdouble
>,
2077 _Jv_JNI_CallMethodA
<jdouble
>,
2078 _Jv_JNI_CallVoidMethod
,
2079 _Jv_JNI_CallVoidMethodV
,
2080 _Jv_JNI_CallVoidMethodA
,
2082 // Nonvirtual method invocation functions follow.
2083 _Jv_JNI_CallAnyMethod
<jobject
, nonvirtual
>,
2084 _Jv_JNI_CallAnyMethodV
<jobject
, nonvirtual
>,
2085 _Jv_JNI_CallAnyMethodA
<jobject
, nonvirtual
>,
2086 _Jv_JNI_CallAnyMethod
<jboolean
, nonvirtual
>,
2087 _Jv_JNI_CallAnyMethodV
<jboolean
, nonvirtual
>,
2088 _Jv_JNI_CallAnyMethodA
<jboolean
, nonvirtual
>,
2089 _Jv_JNI_CallAnyMethod
<jbyte
, nonvirtual
>,
2090 _Jv_JNI_CallAnyMethodV
<jbyte
, nonvirtual
>,
2091 _Jv_JNI_CallAnyMethodA
<jbyte
, nonvirtual
>,
2092 _Jv_JNI_CallAnyMethod
<jchar
, nonvirtual
>,
2093 _Jv_JNI_CallAnyMethodV
<jchar
, nonvirtual
>,
2094 _Jv_JNI_CallAnyMethodA
<jchar
, nonvirtual
>,
2095 _Jv_JNI_CallAnyMethod
<jshort
, nonvirtual
>,
2096 _Jv_JNI_CallAnyMethodV
<jshort
, nonvirtual
>,
2097 _Jv_JNI_CallAnyMethodA
<jshort
, nonvirtual
>,
2098 _Jv_JNI_CallAnyMethod
<jint
, nonvirtual
>,
2099 _Jv_JNI_CallAnyMethodV
<jint
, nonvirtual
>,
2100 _Jv_JNI_CallAnyMethodA
<jint
, nonvirtual
>,
2101 _Jv_JNI_CallAnyMethod
<jlong
, nonvirtual
>,
2102 _Jv_JNI_CallAnyMethodV
<jlong
, nonvirtual
>,
2103 _Jv_JNI_CallAnyMethodA
<jlong
, nonvirtual
>,
2104 _Jv_JNI_CallAnyMethod
<jfloat
, nonvirtual
>,
2105 _Jv_JNI_CallAnyMethodV
<jfloat
, nonvirtual
>,
2106 _Jv_JNI_CallAnyMethodA
<jfloat
, nonvirtual
>,
2107 _Jv_JNI_CallAnyMethod
<jdouble
, nonvirtual
>,
2108 _Jv_JNI_CallAnyMethodV
<jdouble
, nonvirtual
>,
2109 _Jv_JNI_CallAnyMethodA
<jdouble
, nonvirtual
>,
2110 _Jv_JNI_CallAnyVoidMethod
<nonvirtual
>,
2111 _Jv_JNI_CallAnyVoidMethodV
<nonvirtual
>,
2112 _Jv_JNI_CallAnyVoidMethodA
<nonvirtual
>,
2114 _Jv_JNI_GetAnyFieldID
<false>,
2115 _Jv_JNI_GetField
<jobject
>,
2116 _Jv_JNI_GetField
<jboolean
>,
2117 _Jv_JNI_GetField
<jbyte
>,
2118 _Jv_JNI_GetField
<jchar
>,
2119 _Jv_JNI_GetField
<jshort
>,
2120 _Jv_JNI_GetField
<jint
>,
2121 _Jv_JNI_GetField
<jlong
>,
2122 _Jv_JNI_GetField
<jfloat
>,
2123 _Jv_JNI_GetField
<jdouble
>,
2133 _Jv_JNI_GetAnyMethodID
<true>,
2135 _Jv_JNI_CallStaticMethod
<jobject
>,
2136 _Jv_JNI_CallStaticMethodV
<jobject
>,
2137 _Jv_JNI_CallStaticMethodA
<jobject
>,
2138 _Jv_JNI_CallStaticMethod
<jboolean
>,
2139 _Jv_JNI_CallStaticMethodV
<jboolean
>,
2140 _Jv_JNI_CallStaticMethodA
<jboolean
>,
2141 _Jv_JNI_CallStaticMethod
<jbyte
>,
2142 _Jv_JNI_CallStaticMethodV
<jbyte
>,
2143 _Jv_JNI_CallStaticMethodA
<jbyte
>,
2144 _Jv_JNI_CallStaticMethod
<jchar
>,
2145 _Jv_JNI_CallStaticMethodV
<jchar
>,
2146 _Jv_JNI_CallStaticMethodA
<jchar
>,
2147 _Jv_JNI_CallStaticMethod
<jshort
>,
2148 _Jv_JNI_CallStaticMethodV
<jshort
>,
2149 _Jv_JNI_CallStaticMethodA
<jshort
>,
2150 _Jv_JNI_CallStaticMethod
<jint
>,
2151 _Jv_JNI_CallStaticMethodV
<jint
>,
2152 _Jv_JNI_CallStaticMethodA
<jint
>,
2153 _Jv_JNI_CallStaticMethod
<jlong
>,
2154 _Jv_JNI_CallStaticMethodV
<jlong
>,
2155 _Jv_JNI_CallStaticMethodA
<jlong
>,
2156 _Jv_JNI_CallStaticMethod
<jfloat
>,
2157 _Jv_JNI_CallStaticMethodV
<jfloat
>,
2158 _Jv_JNI_CallStaticMethodA
<jfloat
>,
2159 _Jv_JNI_CallStaticMethod
<jdouble
>,
2160 _Jv_JNI_CallStaticMethodV
<jdouble
>,
2161 _Jv_JNI_CallStaticMethodA
<jdouble
>,
2162 _Jv_JNI_CallStaticVoidMethod
,
2163 _Jv_JNI_CallStaticVoidMethodV
,
2164 _Jv_JNI_CallStaticVoidMethodA
,
2166 _Jv_JNI_GetAnyFieldID
<true>,
2167 _Jv_JNI_GetStaticField
<jobject
>,
2168 _Jv_JNI_GetStaticField
<jboolean
>,
2169 _Jv_JNI_GetStaticField
<jbyte
>,
2170 _Jv_JNI_GetStaticField
<jchar
>,
2171 _Jv_JNI_GetStaticField
<jshort
>,
2172 _Jv_JNI_GetStaticField
<jint
>,
2173 _Jv_JNI_GetStaticField
<jlong
>,
2174 _Jv_JNI_GetStaticField
<jfloat
>,
2175 _Jv_JNI_GetStaticField
<jdouble
>,
2176 _Jv_JNI_SetStaticField
,
2177 _Jv_JNI_SetStaticField
,
2178 _Jv_JNI_SetStaticField
,
2179 _Jv_JNI_SetStaticField
,
2180 _Jv_JNI_SetStaticField
,
2181 _Jv_JNI_SetStaticField
,
2182 _Jv_JNI_SetStaticField
,
2183 _Jv_JNI_SetStaticField
,
2184 _Jv_JNI_SetStaticField
,
2186 _Jv_JNI_GetStringLength
,
2187 _Jv_JNI_GetStringChars
,
2188 _Jv_JNI_ReleaseStringChars
,
2189 _Jv_JNI_NewStringUTF
,
2190 _Jv_JNI_GetStringUTFLength
,
2191 _Jv_JNI_GetStringUTFChars
,
2192 _Jv_JNI_ReleaseStringUTFChars
,
2193 _Jv_JNI_GetArrayLength
,
2194 _Jv_JNI_NewObjectArray
,
2195 _Jv_JNI_GetObjectArrayElement
,
2196 _Jv_JNI_SetObjectArrayElement
,
2197 _Jv_JNI_NewPrimitiveArray
<jboolean
, JvPrimClass (boolean
)>,
2198 _Jv_JNI_NewPrimitiveArray
<jbyte
, JvPrimClass (byte
)>,
2199 _Jv_JNI_NewPrimitiveArray
<jchar
, JvPrimClass (char)>,
2200 _Jv_JNI_NewPrimitiveArray
<jshort
, JvPrimClass (short)>,
2201 _Jv_JNI_NewPrimitiveArray
<jint
, JvPrimClass (int)>,
2202 _Jv_JNI_NewPrimitiveArray
<jlong
, JvPrimClass (long)>,
2203 _Jv_JNI_NewPrimitiveArray
<jfloat
, JvPrimClass (float)>,
2204 _Jv_JNI_NewPrimitiveArray
<jdouble
, JvPrimClass (double)>,
2205 _Jv_JNI_GetPrimitiveArrayElements
,
2206 _Jv_JNI_GetPrimitiveArrayElements
,
2207 _Jv_JNI_GetPrimitiveArrayElements
,
2208 _Jv_JNI_GetPrimitiveArrayElements
,
2209 _Jv_JNI_GetPrimitiveArrayElements
,
2210 _Jv_JNI_GetPrimitiveArrayElements
,
2211 _Jv_JNI_GetPrimitiveArrayElements
,
2212 _Jv_JNI_GetPrimitiveArrayElements
,
2213 _Jv_JNI_ReleasePrimitiveArrayElements
,
2214 _Jv_JNI_ReleasePrimitiveArrayElements
,
2215 _Jv_JNI_ReleasePrimitiveArrayElements
,
2216 _Jv_JNI_ReleasePrimitiveArrayElements
,
2217 _Jv_JNI_ReleasePrimitiveArrayElements
,
2218 _Jv_JNI_ReleasePrimitiveArrayElements
,
2219 _Jv_JNI_ReleasePrimitiveArrayElements
,
2220 _Jv_JNI_ReleasePrimitiveArrayElements
,
2221 _Jv_JNI_GetPrimitiveArrayRegion
,
2222 _Jv_JNI_GetPrimitiveArrayRegion
,
2223 _Jv_JNI_GetPrimitiveArrayRegion
,
2224 _Jv_JNI_GetPrimitiveArrayRegion
,
2225 _Jv_JNI_GetPrimitiveArrayRegion
,
2226 _Jv_JNI_GetPrimitiveArrayRegion
,
2227 _Jv_JNI_GetPrimitiveArrayRegion
,
2228 _Jv_JNI_GetPrimitiveArrayRegion
,
2229 _Jv_JNI_SetPrimitiveArrayRegion
,
2230 _Jv_JNI_SetPrimitiveArrayRegion
,
2231 _Jv_JNI_SetPrimitiveArrayRegion
,
2232 _Jv_JNI_SetPrimitiveArrayRegion
,
2233 _Jv_JNI_SetPrimitiveArrayRegion
,
2234 _Jv_JNI_SetPrimitiveArrayRegion
,
2235 _Jv_JNI_SetPrimitiveArrayRegion
,
2236 _Jv_JNI_SetPrimitiveArrayRegion
,
2237 _Jv_JNI_RegisterNatives
,
2238 _Jv_JNI_UnregisterNatives
,
2239 _Jv_JNI_MonitorEnter
,
2240 _Jv_JNI_MonitorExit
,
2243 _Jv_JNI_GetStringRegion
,
2244 _Jv_JNI_GetStringUTFRegion
,
2245 _Jv_JNI_GetPrimitiveArrayCritical
,
2246 _Jv_JNI_ReleasePrimitiveArrayCritical
,
2247 _Jv_JNI_GetStringCritical
,
2248 _Jv_JNI_ReleaseStringCritical
,
2250 NOT_IMPL
/* newweakglobalref */,
2251 NOT_IMPL
/* deleteweakglobalref */,
2253 _Jv_JNI_ExceptionCheck
2256 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions
=
2262 _Jv_JNI_DestroyJavaVM
,
2263 _Jv_JNI_AttachCurrentThread
,
2264 _Jv_JNI_DetachCurrentThread
,