Add D30V options
[official-gcc.git] / libjava / jni.cc
blob020f7e856966acda16c36421ef6558d3ca08bb17
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
9 details. */
11 #include <config.h>
13 #include <stddef.h>
14 #include <string.h>
16 // Define this before including jni.h.
17 #define __GCJ_JNI_IMPL__
19 #include <gcj/cni.h>
20 #include <jvm.h>
21 #include <java-assert.h>
22 #include <jni.h>
23 #ifdef ENABLE_JVMPI
24 #include <jvmpi.h>
25 #endif
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.
65 enum invocation_type
67 normal,
68 nonvirtual,
69 static_type,
70 constructor
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
78 // 16.
79 #define FRAME_SIZE 32
81 // Mark value indicating this is an overflow frame.
82 #define MARK_NONE 0
83 // Mark value indicating this is a user frame.
84 #define MARK_USER 1
85 // Mark value indicating this is a system frame.
86 #define MARK_SYSTEM 2
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).
93 int marker : 2;
95 // Number of elements in frame.
96 int size : 30;
98 // Next frame in chain.
99 _Jv_JNI_LocalFrame *next;
101 // The elements. These are allocated using the C "struct hack".
102 jobject vec[0];
105 // This holds a reference count for all local and global references.
106 static java::util::Hashtable *ref_table;
108 // The only VM.
109 static JavaVM *the_vm;
111 #ifdef ENABLE_JVMPI
112 // The only JVMPI interface description.
113 static JVMPI_Interface _Jv_JVMPI_Interface;
115 static jint
116 jvmpiEnableEvent (jint event_type, void *)
118 switch (event_type)
120 case JVMPI_EVENT_OBJECT_ALLOC:
121 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
122 break;
124 case JVMPI_EVENT_THREAD_START:
125 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
126 break;
128 case JVMPI_EVENT_THREAD_END:
129 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
130 break;
132 default:
133 return JVMPI_NOT_AVAILABLE;
136 return JVMPI_SUCCESS;
139 static jint
140 jvmpiDisableEvent (jint event_type, void *)
142 switch (event_type)
144 case JVMPI_EVENT_OBJECT_ALLOC:
145 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
146 break;
148 default:
149 return JVMPI_NOT_AVAILABLE;
152 return JVMPI_SUCCESS;
154 #endif
158 void
159 _Jv_JNI_Init (void)
161 ref_table = new java::util::Hashtable;
163 #ifdef ENABLE_JVMPI
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;
170 #endif
173 // Tell the GC that a certain pointer is live.
174 static void
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));
186 // Unmark a pointer.
187 static void
188 unmark_for_gc (jobject obj)
190 JvSynchronize sync (ref_table);
192 using namespace java::lang;
193 Integer *refcount = (Integer *) ref_table->get (obj);
194 JvAssert (refcount);
195 jint val = refcount->intValue () - 1;
196 if (val == 0)
197 ref_table->remove (obj);
198 else
199 // FIXME: what about out of memory error?
200 ref_table->put (obj, new Integer (val));
205 static jobject
206 _Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
208 mark_for_gc (obj);
209 return obj;
212 static void
213 _Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
215 unmark_for_gc (obj);
218 static void
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;
230 unmark_for_gc (obj);
231 return;
235 // Don't go past a marked frame.
236 JvAssert (frame->marker == MARK_NONE);
239 JvAssert (0);
242 static jint
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));
255 catch (jthrowable t)
257 env->ex = t;
258 return JNI_ERR;
261 frame->marker = MARK_NONE;
262 frame->size = size;
263 memset (&frame->vec[0], 0, size * sizeof (jobject));
264 frame->next = env->locals;
265 env->locals = frame;
267 return 0;
270 static jint
271 _Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
273 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
274 if (r < 0)
275 return r;
277 // The new frame is on top.
278 env->locals->marker = MARK_USER;
280 return 0;
283 static jobject
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)
294 set = true;
295 done = true;
296 frame->vec[i] = obj;
297 break;
301 if (! set)
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;
310 mark_for_gc (obj);
311 return obj;
314 static jobject
315 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
317 _Jv_JNI_LocalFrame *rf = env->locals;
319 bool done = false;
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.
333 if (n == NULL)
335 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
336 break;
339 _Jv_Free (rf);
340 rf = n;
343 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
346 static jobject
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.
354 extern "C" void
355 _Jv_JNI_PopSystemFrame (JNIEnv *env)
357 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
359 if (env->ex)
361 jthrowable t = env->ex;
362 env->ex = NULL;
363 throw t;
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.
370 template<typename T>
371 static T
372 wrap_value (JNIEnv *, T value)
374 return value;
377 template<>
378 static jobject
379 wrap_value (JNIEnv *env, jobject value)
381 return value == NULL ? value : _Jv_JNI_NewLocalRef (env, value);
386 static jint
387 _Jv_JNI_GetVersion (JNIEnv *)
389 return JNI_VERSION_1_2;
392 static jclass
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);
409 catch (jthrowable t)
411 env->ex = t;
412 return NULL;
416 static jclass
417 _Jv_JNI_FindClass (JNIEnv *env, const char *name)
419 // FIXME: assume that NAME isn't too long.
420 int len = strlen (name);
421 char s[len + 1];
422 for (int i = 0; i <= len; ++i)
423 s[i] = (name[i] == '/') ? '.' : name[i];
425 jclass r = NULL;
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 ();
435 if (loader == NULL)
437 // FIXME: should use getBaseClassLoader, but we don't have that
438 // yet.
439 loader = java::lang::ClassLoader::getSystemClassLoader ();
442 r = loader->loadClass (n);
444 catch (jthrowable t)
446 env->ex = t;
449 return (jclass) wrap_value (env, r);
452 static jclass
453 _Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
455 return (jclass) wrap_value (env, clazz->getSuperclass ());
458 static jboolean
459 _Jv_JNI_IsAssignableFrom(JNIEnv *, jclass clazz1, jclass clazz2)
461 return clazz1->isAssignableFrom (clazz2);
464 static jint
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));
469 env->ex = obj;
470 return 0;
473 static jint
474 _Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
476 using namespace java::lang::reflect;
478 JvAssert ((&ThrowableClass)->isAssignableFrom (clazz));
480 int r = JNI_OK;
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);
499 catch (jthrowable t)
501 env->ex = t;
502 r = JNI_ERR;
505 return r;
508 static jthrowable
509 _Jv_JNI_ExceptionOccurred (JNIEnv *env)
511 return (jthrowable) wrap_value (env, env->ex);
514 static void
515 _Jv_JNI_ExceptionDescribe (JNIEnv *env)
517 if (env->ex != NULL)
518 env->ex->printStackTrace();
521 static void
522 _Jv_JNI_ExceptionClear (JNIEnv *env)
524 env->ex = NULL;
527 static jboolean
528 _Jv_JNI_ExceptionCheck (JNIEnv *env)
530 return env->ex != NULL;
533 static void
534 _Jv_JNI_FatalError (JNIEnv *, const char *message)
536 JvFail (message);
541 static jboolean
542 _Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
544 return obj1 == obj2;
547 static jobject
548 _Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
550 jobject obj = NULL;
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 ();
558 else
560 // FIXME: will this work for String?
561 obj = JvAllocObject (clazz);
564 catch (jthrowable t)
566 env->ex = t;
569 return wrap_value (env, obj);
572 static jclass
573 _Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
575 JvAssert (obj);
576 return (jclass) wrap_value (env, obj->getClass());
579 static jboolean
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>
592 static jmethodID
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))
618 return meth;
620 meth = meth->getNextMethod();
623 clazz = clazz->getSuperclass ();
626 env->ex = new java::lang::NoSuchMethodError ();
628 catch (jthrowable t)
630 env->ex = t;
633 return NULL;
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.
639 static void
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);
661 else
663 // An object.
664 values[i].l = va_arg (vargs, jobject);
669 // This can call any sort of method: virtual, "nonvirtual", static, or
670 // constructor.
671 template<typename T, invocation_type style>
672 static T
673 _Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
674 jmethodID id, va_list vargs)
676 if (style == normal)
677 id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
679 jclass decl_class = klass ? klass : obj->getClass ();
680 JvAssert (decl_class != NULL);
682 jclass return_type;
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)
695 return_type = klass;
697 jvalue result;
698 jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
699 style == constructor,
700 arg_types, args, &result);
702 if (ex != NULL)
703 env->ex = ex;
705 // We cheat a little here. FIXME.
706 return wrap_value (env, * (T *) &result);
708 catch (jthrowable t)
710 env->ex = t;
713 return wrap_value (env, (T) 0);
716 template<typename T, invocation_type style>
717 static T
718 _Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
719 jmethodID method, ...)
721 va_list args;
722 T result;
724 va_start (args, method);
725 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
726 va_end (args);
728 return result;
731 template<typename T, invocation_type style>
732 static T
733 _Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
734 jmethodID id, jvalue *args)
736 if (style == normal)
737 id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
739 jclass decl_class = klass ? klass : obj->getClass ();
740 JvAssert (decl_class != NULL);
742 jclass return_type;
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)
751 return_type = klass;
753 jvalue result;
754 jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
755 style == constructor,
756 arg_types, args, &result);
758 if (ex != NULL)
759 env->ex = ex;
761 // We cheat a little here. FIXME.
762 return wrap_value (env, * (T *) &result);
764 catch (jthrowable t)
766 env->ex = t;
769 return wrap_value (env, (T) 0);
772 template<invocation_type style>
773 static void
774 _Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
775 jmethodID id, va_list vargs)
777 if (style == normal)
778 id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
780 jclass decl_class = klass ? klass : obj->getClass ();
781 JvAssert (decl_class != NULL);
783 jclass return_type;
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)
795 return_type = klass;
797 jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
798 style == constructor,
799 arg_types, args, NULL);
801 if (ex != NULL)
802 env->ex = ex;
804 catch (jthrowable t)
806 env->ex = t;
810 template<invocation_type style>
811 static void
812 _Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
813 jmethodID method, ...)
815 va_list args;
817 va_start (args, method);
818 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
819 va_end (args);
822 template<invocation_type style>
823 static void
824 _Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
825 jmethodID id, jvalue *args)
827 if (style == normal)
828 id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
830 jclass decl_class = klass ? klass : obj->getClass ();
831 JvAssert (decl_class != NULL);
833 jclass return_type;
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);
844 if (ex != NULL)
845 env->ex = ex;
847 catch (jthrowable t)
849 env->ex = t;
853 // Functions with this signature are used to implement functions in
854 // the CallMethod family.
855 template<typename T>
856 static T
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.
864 template<typename T>
865 static T
866 _Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
868 va_list args;
869 T result;
871 va_start (args, id);
872 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
873 va_end (args);
875 return result;
878 // Functions with this signature are used to implement functions in
879 // the CallMethod family.
880 template<typename T>
881 static T
882 _Jv_JNI_CallMethodA (JNIEnv *env, jobject obj, jmethodID id, jvalue *args)
884 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
887 static void
888 _Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj, jmethodID id, va_list args)
890 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
893 static void
894 _Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
896 va_list args;
898 va_start (args, id);
899 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
900 va_end (args);
903 static void
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.
911 template<typename T>
912 static T
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.
924 template<typename T>
925 static T
926 _Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass, jmethodID id, ...)
928 va_list args;
929 T result;
931 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
932 JvAssert ((&ClassClass)->isInstance (klass));
934 va_start (args, id);
935 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
936 id, args);
937 va_end (args);
939 return result;
942 // Functions with this signature are used to implement functions in
943 // the CallStaticMethod family.
944 template<typename T>
945 static T
946 _Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
947 jvalue *args)
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);
955 static void
956 _Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass, jmethodID id,
957 va_list args)
959 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
962 static void
963 _Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass, jmethodID id, ...)
965 va_list args;
967 va_start (args, id);
968 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
969 va_end (args);
972 static void
973 _Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass, jmethodID id,
974 jvalue *args)
976 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
979 static jobject
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],
988 ")V"));
990 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
991 id, args);
994 static jobject
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],
1002 ")V"));
1004 va_list args;
1005 jobject result;
1007 va_start (args, id);
1008 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1009 id, args);
1010 va_end (args);
1012 return result;
1015 static jobject
1016 _Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
1017 jvalue *args)
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],
1024 ")V"));
1026 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1027 id, args);
1032 template<typename T>
1033 static T
1034 _Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
1036 JvAssert (obj);
1037 T *ptr = (T *) ((char *) obj + field->getOffset ());
1038 return wrap_value (env, *ptr);
1041 template<typename T>
1042 static void
1043 _Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
1045 JvAssert (obj);
1046 T *ptr = (T *) ((char *) obj + field->getOffset ());
1047 *ptr = value;
1050 template<jboolean is_static>
1051 static jfieldID
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;
1062 if (sig[0] == '[')
1063 field_class = _Jv_FindClassFromSignature ((char *) sig, NULL);
1064 else
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
1083 // initialization.
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)
1090 return field;
1092 field = field->getNextField ();
1095 clazz = clazz->getSuperclass ();
1098 env->ex = new java::lang::NoSuchFieldError ();
1100 catch (jthrowable t)
1102 env->ex = t;
1104 return NULL;
1107 template<typename T>
1108 static 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>
1116 static void
1117 _Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
1119 T *ptr = (T *) field->u.addr;
1120 *ptr = value;
1123 static jstring
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)
1133 env->ex = t;
1134 return NULL;
1138 static jsize
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);
1149 if (isCopy)
1150 *isCopy = false;
1151 return (const jchar *) result;
1154 static void
1155 _Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
1157 unmark_for_gc (string);
1160 static jstring
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)
1170 env->ex = t;
1171 return NULL;
1175 static jsize
1176 _Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
1178 return JvGetStringUTFLength (string);
1181 static const char *
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);
1189 r[len] = '\0';
1191 if (isCopy)
1192 *isCopy = true;
1194 return (const char *) r;
1196 catch (jthrowable t)
1198 env->ex = t;
1199 return NULL;
1203 static void
1204 _Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
1206 _Jv_Free ((void *) utf);
1209 static void
1210 _Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start, jsize len,
1211 jchar *buf)
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)
1223 env->ex = t;
1226 else
1227 memcpy (buf, &result[start], len * sizeof (jchar));
1230 static void
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)
1243 env->ex = t;
1246 else
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);
1254 if (isCopy)
1255 *isCopy = false;
1256 return result;
1259 static void
1260 _Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
1262 // Nothing.
1265 static jsize
1266 _Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
1268 return array->length;
1271 static jarray
1272 _Jv_JNI_NewObjectArray (JNIEnv *env, jsize length, jclass elementClass,
1273 jobject init)
1277 jarray result = JvNewObjectArray (length, elementClass, init);
1278 return (jarray) wrap_value (env, result);
1280 catch (jthrowable t)
1282 env->ex = t;
1283 return NULL;
1287 static jobject
1288 _Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array, jsize index)
1290 jobject *elts = elements (array);
1291 return wrap_value (env, elts[index]);
1294 static void
1295 _Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array, jsize index,
1296 jobject value)
1300 _Jv_CheckArrayStore (array, value);
1301 jobject *elts = elements (array);
1302 elts[index] = value;
1304 catch (jthrowable t)
1306 env->ex = t;
1310 template<typename T, jclass K>
1311 static JArray<T> *
1312 _Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
1316 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1318 catch (jthrowable t)
1320 env->ex = t;
1321 return NULL;
1325 template<typename T>
1326 static T *
1327 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv *, JArray<T> *array,
1328 jboolean *isCopy)
1330 T *elts = elements (array);
1331 if (isCopy)
1333 // We elect never to copy.
1334 *isCopy = false;
1336 mark_for_gc (array);
1337 return elts;
1340 template<typename T>
1341 static void
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>
1352 static void
1353 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1354 jsize start, jsize len,
1355 T *buf)
1357 if (start < 0 || len >= array->length || start + len >= array->length)
1361 // FIXME: index.
1362 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1364 catch (jthrowable t)
1366 // Could have thown out of memory error.
1367 env->ex = t;
1370 else
1372 T *elts = elements (array) + start;
1373 memcpy (buf, elts, len * sizeof (T));
1377 template<typename T>
1378 static void
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)
1386 // FIXME: index.
1387 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1389 catch (jthrowable t)
1391 env->ex = t;
1394 else
1396 T *elts = elements (array) + start;
1397 memcpy (elts, buf, len * sizeof (T));
1401 static void *
1402 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
1403 jboolean *isCopy)
1405 // FIXME: does this work?
1406 jclass klass = array->getClass()->getComponentType();
1407 JvAssert (klass->isPrimitive ());
1408 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1409 if (isCopy)
1410 *isCopy = false;
1411 return r;
1414 static void
1415 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
1417 // Nothing.
1420 static jint
1421 _Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
1425 return _Jv_MonitorEnter (obj);
1427 catch (jthrowable t)
1429 env->ex = t;
1431 return JNI_ERR;
1434 static jint
1435 _Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
1439 return _Jv_MonitorExit (obj);
1441 catch (jthrowable t)
1443 env->ex = t;
1445 return JNI_ERR;
1448 // JDK 1.2
1449 jobject
1450 _Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
1451 jboolean)
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)
1463 env->ex = t;
1465 return NULL;
1468 // JDK 1.2
1469 static jfieldID
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);
1478 jobject
1479 _Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
1480 jboolean)
1482 using namespace java::lang::reflect;
1484 // FIXME.
1485 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
1487 jobject result = NULL;
1491 if (_Jv_equalUtf8Consts (id->name, init_name))
1493 // A constructor.
1494 Constructor *cons = new Constructor ();
1495 cons->offset = (char *) id - (char *) &klass->methods;
1496 cons->declaringClass = klass;
1497 result = cons;
1499 else
1501 Method *meth = new Method ();
1502 meth->offset = (char *) id - (char *) &klass->methods;
1503 meth->declaringClass = klass;
1504 result = meth;
1507 catch (jthrowable t)
1509 env->ex = t;
1512 return wrap_value (env, result);
1515 static jmethodID
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));
1521 return
1522 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1525 static jint
1526 _Jv_JNI_RegisterNatives (JNIEnv *env, jclass k,
1527 const JNINativeMethod *methods,
1528 jint nMethods)
1530 #ifdef INTERPRETER
1531 // For now, this only matters for interpreted methods. FIXME.
1532 if (! _Jv_IsInterpretedClass (k))
1534 // FIXME: throw exception.
1535 return JNI_ERR;
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)
1543 bool found = false;
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))
1556 break;
1558 // Found a match that is native.
1559 _Jv_JNIMethod *jmeth = reinterpret_cast<_Jv_JNIMethod *> (meth);
1560 jmeth->set_function (methods[i].fnPtr);
1561 found = true;
1562 break;
1566 if (! found)
1568 jstring m = JvNewStringUTF (methods[j].name);
1571 env->ex =new java::lang::NoSuchMethodError (m);
1573 catch (jthrowable t)
1575 env->ex = t;
1577 return JNI_ERR;
1581 return JNI_OK;
1582 #else /* INTERPRETER */
1583 return JNI_ERR;
1584 #endif /* INTERPRETER */
1587 static jint
1588 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1590 return JNI_ERR;
1595 #ifdef INTERPRETER
1597 // Add a character to the buffer, encoding properly.
1598 static void
1599 add_char (char *buf, jchar c, int *here)
1601 if (c == '_')
1603 buf[(*here)++] = '_';
1604 buf[(*here)++] = '1';
1606 else if (c == ';')
1608 buf[(*here)++] = '_';
1609 buf[(*here)++] = '2';
1611 else if (c == '[')
1613 buf[(*here)++] = '_';
1614 buf[(*here)++] = '3';
1616 else if (c == '/')
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;
1622 else
1624 // "Unicode" character.
1625 buf[(*here)++] = '_';
1626 buf[(*here)++] = '0';
1627 for (int i = 0; i < 4; ++i)
1629 int val = c & 0x0f;
1630 buf[(*here) + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1631 c >>= 4;
1633 *here += 4;
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.
1641 static void
1642 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1643 _Jv_Utf8Const *signature, char *buf, int *long_start)
1645 strcpy (buf, "Java_");
1646 int here = 5;
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 `_'.
1655 buf[here++] = '_';
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);
1662 if (ch < 0)
1663 break;
1664 add_char (buf, ch, &here);
1667 // This is where the long signature begins.
1668 *long_start = here;
1669 buf[here++] = '_';
1670 buf[here++] = '_';
1672 const unsigned char *sig = (const unsigned char *) signature->data;
1673 limit = sig + signature->length;
1674 JvAssert (sig[0] == '(');
1675 ++sig;
1676 while (1)
1678 int ch = UTF8_GET (sig, limit);
1679 if (ch == ')' || ch < 0)
1680 break;
1681 add_char (buf, ch, &here);
1684 buf[here] = '\0';
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.
1690 extern "C" JNIEnv *
1691 _Jv_GetJNIEnvNewFrame (jclass klass)
1693 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
1694 if (env == NULL)
1696 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
1697 env->p = &_Jv_JNIFunctions;
1698 env->ex = NULL;
1699 env->klass = klass;
1700 env->locals = NULL;
1702 _Jv_SetCurrentJNIEnv (env);
1705 _Jv_JNI_LocalFrame *frame
1706 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
1707 + (FRAME_SIZE
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;
1718 return env;
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.
1724 extern "C" void *
1725 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
1726 _Jv_Utf8Const *signature)
1728 char buf[10 + 6 * (name->length + signature->length)];
1729 int long_start;
1730 void *function;
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));
1747 return function;
1750 // This function is the stub which is used to turn an ordinary (CNI)
1751 // method call into a JNI call.
1752 void
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,
1768 _this->self->name,
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)];
1773 int offset = 0;
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,
1788 ret, real_args);
1790 _Jv_JNI_PopSystemFrame (env);
1793 #endif /* INTERPRETER */
1798 // Invocation API.
1801 // An internal helper function.
1802 static jint
1803 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv, void *args)
1805 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
1806 java::lang::ThreadGroup *group = NULL;
1808 if (attach)
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)
1821 return 0;
1823 JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
1824 if (env == NULL)
1825 return JNI_ERR;
1826 env->p = &_Jv_JNIFunctions;
1827 env->ex = NULL;
1828 env->klass = NULL;
1829 env->locals
1830 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
1831 + (FRAME_SIZE
1832 * sizeof (jobject)));
1833 if (env->locals == NULL)
1835 _Jv_Free (env);
1836 return JNI_ERR;
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)
1850 return JNI_ERR;
1853 _Jv_SetCurrentJNIEnv (env);
1855 return 0;
1858 // This is the one actually used by JNI.
1859 static jint
1860 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
1862 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args);
1865 static jint
1866 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
1868 JvAssert (the_vm && vm == the_vm);
1870 JNIEnv *env;
1871 if (_Jv_ThreadCurrent () != NULL)
1873 jstring main_name;
1874 // This sucks.
1877 main_name = JvNewStringLatin1 ("main");
1879 catch (jthrowable t)
1881 return JNI_ERR;
1884 jint r = _Jv_JNI_AttachCurrentThread (vm,
1885 main_name,
1886 reinterpret_cast<void **> (&env),
1887 NULL);
1888 if (r < 0)
1889 return r;
1891 else
1892 env = _Jv_GetCurrentJNIEnv ();
1894 _Jv_ThreadWait ();
1896 // Docs say that this always returns an error code.
1897 return JNI_ERR;
1900 static jint
1901 _Jv_JNI_DetachCurrentThread (JavaVM *)
1903 java::lang::Thread *t = _Jv_ThreadCurrent ();
1904 if (t == NULL)
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);
1919 nt->finish ();
1921 return 0;
1924 static jint
1925 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
1927 if (_Jv_ThreadCurrent () == NULL)
1929 *penv = NULL;
1930 return JNI_EDETACHED;
1933 #ifdef ENABLE_JVMPI
1934 // Handle JVMPI requests.
1935 if (version == JVMPI_VERSION_1)
1937 *penv = (void *) &_Jv_JVMPI_Interface;
1938 return 0;
1940 #endif
1942 // FIXME: do we really want to support 1.1?
1943 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_1)
1945 *penv = NULL;
1946 return JNI_EVERSION;
1949 *penv = (void *) _Jv_GetCurrentJNIEnv ();
1950 return 0;
1953 jint
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;
1963 ia->nOptions = 0;
1964 ia->options = NULL;
1965 ia->ignoreUnrecognized = true;
1967 return 0;
1970 jint
1971 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
1973 JvAssert (! the_vm);
1974 // FIXME: synchronize
1975 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
1976 if (nvm == NULL)
1977 return JNI_ERR;
1978 nvm->functions = &_Jv_JNI_InvokeFunctions;
1980 // Parse the arguments.
1981 if (args != NULL)
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.
1996 continue;
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.
2004 continue;
2006 else if (! strncmp (ia->options[i].optionString, "-D", 2))
2008 // FIXME.
2009 continue;
2011 else if (ia->ignoreUnrecognized)
2013 if (ia->options[i].optionString[0] == '_'
2014 || ! strncmp (ia->options[i].optionString, "-X", 2))
2015 continue;
2018 return JNI_ERR;
2022 jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2023 if (r < 0)
2024 return r;
2026 the_vm = nvm;
2027 *vm = the_vm;
2028 return 0;
2031 jint
2032 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2034 if (buf_len <= 0)
2035 return JNI_ERR;
2037 // We only support a single VM.
2038 if (the_vm != NULL)
2040 vm_buffer[0] = the_vm;
2041 *n_vms = 1;
2043 else
2044 *n_vms = 0;
2045 return 0;
2048 JavaVM *
2049 _Jv_GetJavaVM ()
2051 // FIXME: synchronize
2052 if (! the_vm)
2054 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2055 if (nvm != NULL)
2056 nvm->functions = &_Jv_JNI_InvokeFunctions;
2057 the_vm = nvm;
2060 // If this is a Java thread, we want to make sure it has an
2061 // associated JNIEnv.
2062 if (_Jv_ThreadCurrent () != NULL)
2064 void *ignore;
2065 _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2068 return the_vm;
2071 static jint
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 =
2085 RESERVED,
2086 RESERVED,
2087 RESERVED,
2088 RESERVED,
2089 _Jv_JNI_GetVersion,
2090 _Jv_JNI_DefineClass,
2091 _Jv_JNI_FindClass,
2092 _Jv_JNI_FromReflectedMethod,
2093 _Jv_JNI_FromReflectedField,
2094 _Jv_JNI_ToReflectedMethod,
2095 _Jv_JNI_GetSuperclass,
2096 _Jv_JNI_IsAssignableFrom,
2097 _Jv_JNI_ToReflectedField,
2098 _Jv_JNI_Throw,
2099 _Jv_JNI_ThrowNew,
2100 _Jv_JNI_ExceptionOccurred,
2101 _Jv_JNI_ExceptionDescribe,
2102 _Jv_JNI_ExceptionClear,
2103 _Jv_JNI_FatalError,
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,
2117 _Jv_JNI_NewObject,
2118 _Jv_JNI_NewObjectV,
2119 _Jv_JNI_NewObjectA,
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>,
2197 _Jv_JNI_SetField,
2198 _Jv_JNI_SetField,
2199 _Jv_JNI_SetField,
2200 _Jv_JNI_SetField,
2201 _Jv_JNI_SetField,
2202 _Jv_JNI_SetField,
2203 _Jv_JNI_SetField,
2204 _Jv_JNI_SetField,
2205 _Jv_JNI_SetField,
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,
2258 _Jv_JNI_NewString,
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,
2314 _Jv_JNI_GetJavaVM,
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 =
2331 RESERVED,
2332 RESERVED,
2333 RESERVED,
2335 _Jv_JNI_DestroyJavaVM,
2336 _Jv_JNI_AttachCurrentThread,
2337 _Jv_JNI_DetachCurrentThread,
2338 _Jv_JNI_GetEnv