* c-decl.c (c_expand_body): Update call tree_rest_of_compilation.
[official-gcc.git] / libjava / jni.cc
blobe9e557a3645ec7b2de53edad4808696858a6a83f
1 // jni.cc - JNI implementation, including the jump table.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 #include <config.h>
14 #include <stdio.h>
15 #include <stddef.h>
16 #include <string.h>
18 #include <gcj/cni.h>
19 #include <jvm.h>
20 #include <java-assert.h>
21 #include <jni.h>
22 #ifdef ENABLE_JVMPI
23 #include <jvmpi.h>
24 #endif
26 #include <java/lang/Class.h>
27 #include <java/lang/ClassLoader.h>
28 #include <java/lang/Throwable.h>
29 #include <java/lang/ArrayIndexOutOfBoundsException.h>
30 #include <java/lang/StringIndexOutOfBoundsException.h>
31 #include <java/lang/StringBuffer.h>
32 #include <java/lang/UnsatisfiedLinkError.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/lang/Integer.h>
41 #include <java/lang/ThreadGroup.h>
42 #include <java/lang/Thread.h>
43 #include <java/lang/IllegalAccessError.h>
44 #include <java/nio/DirectByteBufferImpl.h>
45 #include <java/util/IdentityHashMap.h>
46 #include <gnu/gcj/RawData.h>
48 #include <gcj/method.h>
49 #include <gcj/field.h>
51 #include <java-interp.h>
52 #include <java-threads.h>
54 using namespace gcj;
56 // This enum is used to select different template instantiations in
57 // the invocation code.
58 enum invocation_type
60 normal,
61 nonvirtual,
62 static_type,
63 constructor
66 // Forward declarations.
67 extern struct JNINativeInterface _Jv_JNIFunctions;
68 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
70 // Number of slots in the default frame. The VM must allow at least
71 // 16.
72 #define FRAME_SIZE 32
74 // Mark value indicating this is an overflow frame.
75 #define MARK_NONE 0
76 // Mark value indicating this is a user frame.
77 #define MARK_USER 1
78 // Mark value indicating this is a system frame.
79 #define MARK_SYSTEM 2
81 // This structure is used to keep track of local references.
82 struct _Jv_JNI_LocalFrame
84 // This is true if this frame object represents a pushed frame (eg
85 // from PushLocalFrame).
86 int marker : 2;
88 // Number of elements in frame.
89 int size : 30;
91 // Next frame in chain.
92 _Jv_JNI_LocalFrame *next;
94 // The elements. These are allocated using the C "struct hack".
95 jobject vec[0];
98 // This holds a reference count for all local references.
99 static java::util::IdentityHashMap *local_ref_table;
100 // This holds a reference count for all global references.
101 static java::util::IdentityHashMap *global_ref_table;
103 // The only VM.
104 static JavaVM *the_vm;
106 #ifdef ENABLE_JVMPI
107 // The only JVMPI interface description.
108 static JVMPI_Interface _Jv_JVMPI_Interface;
110 static jint
111 jvmpiEnableEvent (jint event_type, void *)
113 switch (event_type)
115 case JVMPI_EVENT_OBJECT_ALLOC:
116 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
117 break;
119 case JVMPI_EVENT_THREAD_START:
120 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
121 break;
123 case JVMPI_EVENT_THREAD_END:
124 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
125 break;
127 default:
128 return JVMPI_NOT_AVAILABLE;
131 return JVMPI_SUCCESS;
134 static jint
135 jvmpiDisableEvent (jint event_type, void *)
137 switch (event_type)
139 case JVMPI_EVENT_OBJECT_ALLOC:
140 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
141 break;
143 default:
144 return JVMPI_NOT_AVAILABLE;
147 return JVMPI_SUCCESS;
149 #endif
153 void
154 _Jv_JNI_Init (void)
156 local_ref_table = new java::util::IdentityHashMap;
157 global_ref_table = new java::util::IdentityHashMap;
159 #ifdef ENABLE_JVMPI
160 _Jv_JVMPI_Interface.version = 1;
161 _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
162 _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
163 _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
164 _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
165 _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
166 #endif
169 // Tell the GC that a certain pointer is live.
170 static void
171 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
173 JvSynchronize sync (ref_table);
175 using namespace java::lang;
176 Integer *refcount = (Integer *) ref_table->get (obj);
177 jint val = (refcount == NULL) ? 0 : refcount->intValue ();
178 // FIXME: what about out of memory error?
179 ref_table->put (obj, new Integer (val + 1));
182 // Unmark a pointer.
183 static void
184 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
186 JvSynchronize sync (ref_table);
188 using namespace java::lang;
189 Integer *refcount = (Integer *) ref_table->get (obj);
190 JvAssert (refcount);
191 jint val = refcount->intValue () - 1;
192 JvAssert (val >= 0);
193 if (val == 0)
194 ref_table->remove (obj);
195 else
196 // FIXME: what about out of memory error?
197 ref_table->put (obj, new Integer (val));
200 // "Unwrap" some random non-reference type. This exists to simplify
201 // other template functions.
202 template<typename T>
203 static T
204 unwrap (T val)
206 return val;
209 // Unwrap a weak reference, if required.
210 template<typename T>
211 static T *
212 unwrap (T *obj)
214 using namespace gnu::gcj::runtime;
215 // We can compare the class directly because JNIWeakRef is `final'.
216 // Doing it this way is much faster.
217 if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
218 return obj;
219 JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
220 return reinterpret_cast<T *> (wr->get ());
225 static jobject JNICALL
226 _Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
228 // This seems weird but I think it is correct.
229 obj = unwrap (obj);
230 mark_for_gc (obj, global_ref_table);
231 return obj;
234 static void JNICALL
235 _Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
237 // This seems weird but I think it is correct.
238 obj = unwrap (obj);
239 unmark_for_gc (obj, global_ref_table);
242 static void JNICALL
243 _Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
245 _Jv_JNI_LocalFrame *frame;
247 // This seems weird but I think it is correct.
248 obj = unwrap (obj);
250 for (frame = env->locals; frame != NULL; frame = frame->next)
252 for (int i = 0; i < frame->size; ++i)
254 if (frame->vec[i] == obj)
256 frame->vec[i] = NULL;
257 unmark_for_gc (obj, local_ref_table);
258 return;
262 // Don't go past a marked frame.
263 JvAssert (frame->marker == MARK_NONE);
266 JvAssert (0);
269 static jint JNICALL
270 _Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
272 // It is easier to just always allocate a new frame of the requested
273 // size. This isn't the most efficient thing, but for now we don't
274 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
276 _Jv_JNI_LocalFrame *frame;
279 frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
280 + size * sizeof (jobject));
282 catch (jthrowable t)
284 env->ex = t;
285 return JNI_ERR;
288 frame->marker = MARK_NONE;
289 frame->size = size;
290 memset (&frame->vec[0], 0, size * sizeof (jobject));
291 frame->next = env->locals;
292 env->locals = frame;
294 return 0;
297 static jint JNICALL
298 _Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
300 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
301 if (r < 0)
302 return r;
304 // The new frame is on top.
305 env->locals->marker = MARK_USER;
307 return 0;
310 static jobject JNICALL
311 _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
313 // This seems weird but I think it is correct.
314 obj = unwrap (obj);
316 // Try to find an open slot somewhere in the topmost frame.
317 _Jv_JNI_LocalFrame *frame = env->locals;
318 bool done = false, set = false;
319 for (; frame != NULL && ! done; frame = frame->next)
321 for (int i = 0; i < frame->size; ++i)
323 if (frame->vec[i] == NULL)
325 set = true;
326 done = true;
327 frame->vec[i] = obj;
328 break;
332 // If we found a slot, or if the frame we just searched is the
333 // mark frame, then we are done.
334 if (done || frame == NULL || frame->marker != MARK_NONE)
335 break;
338 if (! set)
340 // No slots, so we allocate a new frame. According to the spec
341 // we could just die here. FIXME: return value.
342 _Jv_JNI_EnsureLocalCapacity (env, 16);
343 // We know the first element of the new frame will be ok.
344 env->locals->vec[0] = obj;
347 mark_for_gc (obj, local_ref_table);
348 return obj;
351 static jobject JNICALL
352 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
354 _Jv_JNI_LocalFrame *rf = env->locals;
356 bool done = false;
357 while (rf != NULL && ! done)
359 for (int i = 0; i < rf->size; ++i)
360 if (rf->vec[i] != NULL)
361 unmark_for_gc (rf->vec[i], local_ref_table);
363 // If the frame we just freed is the marker frame, we are done.
364 done = (rf->marker == stop);
366 _Jv_JNI_LocalFrame *n = rf->next;
367 // When N==NULL, we've reached the stack-allocated frame, and we
368 // must not free it. However, we must be sure to clear all its
369 // elements, since we might conceivably reuse it.
370 if (n == NULL)
372 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
373 break;
376 _Jv_Free (rf);
377 rf = n;
380 // Update the local frame information.
381 env->locals = rf;
383 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
386 static jobject JNICALL
387 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
389 return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
392 // Make sure an array's type is compatible with the type of the
393 // destination.
394 template<typename T>
395 static bool
396 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
398 jclass klass = array->getClass()->getComponentType();
399 if (__builtin_expect (klass != K, false))
401 env->ex = new java::lang::IllegalAccessError ();
402 return false;
404 else
405 return true;
408 // Pop a `system' frame from the stack. This is `extern "C"' as it is
409 // used by the compiler.
410 extern "C" void
411 _Jv_JNI_PopSystemFrame (JNIEnv *env)
413 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
415 if (env->ex)
417 jthrowable t = env->ex;
418 env->ex = NULL;
419 throw t;
423 template<typename T> T extract_from_jvalue(jvalue const & t);
424 template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
425 template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
426 template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
427 template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
428 template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
429 template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
430 template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
431 template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
432 template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
435 // This function is used from other template functions. It wraps the
436 // return value appropriately; we specialize it so that object returns
437 // are turned into local references.
438 template<typename T>
439 static T
440 wrap_value (JNIEnv *, T value)
442 return value;
445 // This specialization is used for jobject, jclass, jstring, jarray,
446 // etc.
447 template<typename R, typename T>
448 static T *
449 wrap_value (JNIEnv *env, T *value)
451 return (value == NULL
452 ? value
453 : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
458 static jint JNICALL
459 _Jv_JNI_GetVersion (JNIEnv *)
461 return JNI_VERSION_1_4;
464 static jclass JNICALL
465 _Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
466 const jbyte *buf, jsize bufLen)
470 loader = unwrap (loader);
472 jstring sname = JvNewStringUTF (name);
473 jbyteArray bytes = JvNewByteArray (bufLen);
475 jbyte *elts = elements (bytes);
476 memcpy (elts, buf, bufLen * sizeof (jbyte));
478 java::lang::ClassLoader *l
479 = reinterpret_cast<java::lang::ClassLoader *> (loader);
481 jclass result = l->defineClass (sname, bytes, 0, bufLen);
482 return (jclass) wrap_value (env, result);
484 catch (jthrowable t)
486 env->ex = t;
487 return NULL;
491 static jclass JNICALL
492 _Jv_JNI_FindClass (JNIEnv *env, const char *name)
494 // FIXME: assume that NAME isn't too long.
495 int len = strlen (name);
496 char s[len + 1];
497 for (int i = 0; i <= len; ++i)
498 s[i] = (name[i] == '/') ? '.' : name[i];
500 jclass r = NULL;
503 // This might throw an out of memory exception.
504 jstring n = JvNewStringUTF (s);
506 java::lang::ClassLoader *loader = NULL;
507 if (env->klass != NULL)
508 loader = env->klass->getClassLoaderInternal ();
510 if (loader == NULL)
512 // FIXME: should use getBaseClassLoader, but we don't have that
513 // yet.
514 loader = java::lang::ClassLoader::getSystemClassLoader ();
517 r = loader->loadClass (n);
519 catch (jthrowable t)
521 env->ex = t;
524 return (jclass) wrap_value (env, r);
527 static jclass JNICALL
528 _Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
530 return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
533 static jboolean JNICALL
534 _Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
536 return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
539 static jint JNICALL
540 _Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
542 // We check in case the user did some funky cast.
543 obj = unwrap (obj);
544 JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
545 env->ex = obj;
546 return 0;
549 static jint JNICALL
550 _Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
552 using namespace java::lang::reflect;
554 clazz = unwrap (clazz);
555 JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
557 int r = JNI_OK;
560 JArray<jclass> *argtypes
561 = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
562 NULL);
564 jclass *elts = elements (argtypes);
565 elts[0] = &StringClass;
567 Constructor *cons = clazz->getConstructor (argtypes);
569 jobjectArray values = JvNewObjectArray (1, &StringClass, NULL);
570 jobject *velts = elements (values);
571 velts[0] = JvNewStringUTF (message);
573 jobject obj = cons->newInstance (values);
575 env->ex = reinterpret_cast<jthrowable> (obj);
577 catch (jthrowable t)
579 env->ex = t;
580 r = JNI_ERR;
583 return r;
586 static jthrowable JNICALL
587 _Jv_JNI_ExceptionOccurred (JNIEnv *env)
589 return (jthrowable) wrap_value (env, env->ex);
592 static void JNICALL
593 _Jv_JNI_ExceptionDescribe (JNIEnv *env)
595 if (env->ex != NULL)
596 env->ex->printStackTrace();
599 static void JNICALL
600 _Jv_JNI_ExceptionClear (JNIEnv *env)
602 env->ex = NULL;
605 static jboolean JNICALL
606 _Jv_JNI_ExceptionCheck (JNIEnv *env)
608 return env->ex != NULL;
611 static void JNICALL
612 _Jv_JNI_FatalError (JNIEnv *, const char *message)
614 JvFail (message);
619 static jboolean JNICALL
620 _Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
622 return unwrap (obj1) == unwrap (obj2);
625 static jobject JNICALL
626 _Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
628 jobject obj = NULL;
629 using namespace java::lang::reflect;
633 clazz = unwrap (clazz);
634 JvAssert (clazz && ! clazz->isArray ());
635 if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
636 env->ex = new java::lang::InstantiationException ();
637 else
638 obj = _Jv_AllocObject (clazz);
640 catch (jthrowable t)
642 env->ex = t;
645 return wrap_value (env, obj);
648 static jclass JNICALL
649 _Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
651 obj = unwrap (obj);
652 JvAssert (obj);
653 return (jclass) wrap_value (env, obj->getClass());
656 static jboolean JNICALL
657 _Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
659 return unwrap (clazz)->isInstance(unwrap (obj));
665 // This section concerns method invocation.
668 template<jboolean is_static>
669 static jmethodID JNICALL
670 _Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
671 const char *name, const char *sig)
675 clazz = unwrap (clazz);
676 _Jv_InitClass (clazz);
678 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
680 // FIXME: assume that SIG isn't too long.
681 int len = strlen (sig);
682 char s[len + 1];
683 for (int i = 0; i <= len; ++i)
684 s[i] = (sig[i] == '/') ? '.' : sig[i];
685 _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
687 JvAssert (! clazz->isPrimitive());
689 using namespace java::lang::reflect;
691 while (clazz != NULL)
693 jint count = JvNumMethods (clazz);
694 jmethodID meth = JvGetFirstMethod (clazz);
696 for (jint i = 0; i < count; ++i)
698 if (((is_static && Modifier::isStatic (meth->accflags))
699 || (! is_static && ! Modifier::isStatic (meth->accflags)))
700 && _Jv_equalUtf8Consts (meth->name, name_u)
701 && _Jv_equalUtf8Consts (meth->signature, sig_u))
702 return meth;
704 meth = meth->getNextMethod();
707 clazz = clazz->getSuperclass ();
710 java::lang::StringBuffer *name_sig =
711 new java::lang::StringBuffer (JvNewStringUTF (name));
712 name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
713 env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
715 catch (jthrowable t)
717 env->ex = t;
720 return NULL;
723 // This is a helper function which turns a va_list into an array of
724 // `jvalue's. It needs signature information in order to do its work.
725 // The array of values must already be allocated.
726 static void
727 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
729 jclass *arg_elts = elements (arg_types);
730 for (int i = 0; i < arg_types->length; ++i)
732 // Here we assume that sizeof(int) >= sizeof(jint), because we
733 // use `int' when decoding the varargs. Likewise for
734 // float, and double. Also we assume that sizeof(jlong) >=
735 // sizeof(int), i.e. that jlong values are not further
736 // promoted.
737 JvAssert (sizeof (int) >= sizeof (jint));
738 JvAssert (sizeof (jlong) >= sizeof (int));
739 JvAssert (sizeof (double) >= sizeof (jfloat));
740 JvAssert (sizeof (double) >= sizeof (jdouble));
741 if (arg_elts[i] == JvPrimClass (byte))
742 values[i].b = (jbyte) va_arg (vargs, int);
743 else if (arg_elts[i] == JvPrimClass (short))
744 values[i].s = (jshort) va_arg (vargs, int);
745 else if (arg_elts[i] == JvPrimClass (int))
746 values[i].i = (jint) va_arg (vargs, int);
747 else if (arg_elts[i] == JvPrimClass (long))
748 values[i].j = (jlong) va_arg (vargs, jlong);
749 else if (arg_elts[i] == JvPrimClass (float))
750 values[i].f = (jfloat) va_arg (vargs, double);
751 else if (arg_elts[i] == JvPrimClass (double))
752 values[i].d = (jdouble) va_arg (vargs, double);
753 else if (arg_elts[i] == JvPrimClass (boolean))
754 values[i].z = (jboolean) va_arg (vargs, int);
755 else if (arg_elts[i] == JvPrimClass (char))
756 values[i].c = (jchar) va_arg (vargs, int);
757 else
759 // An object.
760 values[i].l = unwrap (va_arg (vargs, jobject));
765 // This can call any sort of method: virtual, "nonvirtual", static, or
766 // constructor.
767 template<typename T, invocation_type style>
768 static T JNICALL
769 _Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
770 jmethodID id, va_list vargs)
772 obj = unwrap (obj);
773 klass = unwrap (klass);
775 jclass decl_class = klass ? klass : obj->getClass ();
776 JvAssert (decl_class != NULL);
778 jclass return_type;
779 JArray<jclass> *arg_types;
783 _Jv_GetTypesFromSignature (id, decl_class,
784 &arg_types, &return_type);
786 jvalue args[arg_types->length];
787 array_from_valist (args, arg_types, vargs);
789 // For constructors we need to pass the Class we are instantiating.
790 if (style == constructor)
791 return_type = klass;
793 jvalue result;
794 _Jv_CallAnyMethodA (obj, return_type, id,
795 style == constructor,
796 style == normal,
797 arg_types, args, &result);
799 return wrap_value (env, extract_from_jvalue<T>(result));
801 catch (jthrowable t)
803 env->ex = t;
806 return wrap_value (env, (T) 0);
809 template<typename T, invocation_type style>
810 static T JNICALL
811 _Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
812 jmethodID method, ...)
814 va_list args;
815 T result;
817 va_start (args, method);
818 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
819 va_end (args);
821 return result;
824 template<typename T, invocation_type style>
825 static T JNICALL
826 _Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
827 jmethodID id, jvalue *args)
829 obj = unwrap (obj);
830 klass = unwrap (klass);
832 jclass decl_class = klass ? klass : obj->getClass ();
833 JvAssert (decl_class != NULL);
835 jclass return_type;
836 JArray<jclass> *arg_types;
839 _Jv_GetTypesFromSignature (id, decl_class,
840 &arg_types, &return_type);
842 // For constructors we need to pass the Class we are instantiating.
843 if (style == constructor)
844 return_type = klass;
846 // Unwrap arguments as required. Eww.
847 jclass *type_elts = elements (arg_types);
848 jvalue arg_copy[arg_types->length];
849 for (int i = 0; i < arg_types->length; ++i)
851 if (type_elts[i]->isPrimitive ())
852 arg_copy[i] = args[i];
853 else
854 arg_copy[i].l = unwrap (args[i].l);
857 jvalue result;
858 _Jv_CallAnyMethodA (obj, return_type, id,
859 style == constructor,
860 style == normal,
861 arg_types, arg_copy, &result);
863 return wrap_value (env, extract_from_jvalue<T>(result));
865 catch (jthrowable t)
867 env->ex = t;
870 return wrap_value (env, (T) 0);
873 template<invocation_type style>
874 static void JNICALL
875 _Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
876 jmethodID id, va_list vargs)
878 obj = unwrap (obj);
879 klass = unwrap (klass);
881 jclass decl_class = klass ? klass : obj->getClass ();
882 JvAssert (decl_class != NULL);
884 jclass return_type;
885 JArray<jclass> *arg_types;
888 _Jv_GetTypesFromSignature (id, decl_class,
889 &arg_types, &return_type);
891 jvalue args[arg_types->length];
892 array_from_valist (args, arg_types, vargs);
894 // For constructors we need to pass the Class we are instantiating.
895 if (style == constructor)
896 return_type = klass;
898 _Jv_CallAnyMethodA (obj, return_type, id,
899 style == constructor,
900 style == normal,
901 arg_types, args, NULL);
903 catch (jthrowable t)
905 env->ex = t;
909 template<invocation_type style>
910 static void JNICALL
911 _Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
912 jmethodID method, ...)
914 va_list args;
916 va_start (args, method);
917 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
918 va_end (args);
921 template<invocation_type style>
922 static void JNICALL
923 _Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
924 jmethodID id, jvalue *args)
926 jclass decl_class = klass ? klass : obj->getClass ();
927 JvAssert (decl_class != NULL);
929 jclass return_type;
930 JArray<jclass> *arg_types;
933 _Jv_GetTypesFromSignature (id, decl_class,
934 &arg_types, &return_type);
936 // Unwrap arguments as required. Eww.
937 jclass *type_elts = elements (arg_types);
938 jvalue arg_copy[arg_types->length];
939 for (int i = 0; i < arg_types->length; ++i)
941 if (type_elts[i]->isPrimitive ())
942 arg_copy[i] = args[i];
943 else
944 arg_copy[i].l = unwrap (args[i].l);
947 _Jv_CallAnyMethodA (obj, return_type, id,
948 style == constructor,
949 style == normal,
950 arg_types, args, NULL);
952 catch (jthrowable t)
954 env->ex = t;
958 // Functions with this signature are used to implement functions in
959 // the CallMethod family.
960 template<typename T>
961 static T JNICALL
962 _Jv_JNI_CallMethodV (JNIEnv *env, jobject obj,
963 jmethodID id, va_list args)
965 return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
968 // Functions with this signature are used to implement functions in
969 // the CallMethod family.
970 template<typename T>
971 static T JNICALL
972 _Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
974 va_list args;
975 T result;
977 va_start (args, id);
978 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
979 va_end (args);
981 return result;
984 // Functions with this signature are used to implement functions in
985 // the CallMethod family.
986 template<typename T>
987 static T JNICALL
988 _Jv_JNI_CallMethodA (JNIEnv *env, jobject obj,
989 jmethodID id, jvalue *args)
991 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
994 static void JNICALL
995 _Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj,
996 jmethodID id, va_list args)
998 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1001 static void JNICALL
1002 _Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1004 va_list args;
1006 va_start (args, id);
1007 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1008 va_end (args);
1011 static void JNICALL
1012 _Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj,
1013 jmethodID id, jvalue *args)
1015 _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1018 // Functions with this signature are used to implement functions in
1019 // the CallStaticMethod family.
1020 template<typename T>
1021 static T JNICALL
1022 _Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
1023 jmethodID id, va_list args)
1025 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1026 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1028 return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1031 // Functions with this signature are used to implement functions in
1032 // the CallStaticMethod family.
1033 template<typename T>
1034 static T JNICALL
1035 _Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass,
1036 jmethodID id, ...)
1038 va_list args;
1039 T result;
1041 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1042 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1044 va_start (args, id);
1045 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1046 id, args);
1047 va_end (args);
1049 return result;
1052 // Functions with this signature are used to implement functions in
1053 // the CallStaticMethod family.
1054 template<typename T>
1055 static T JNICALL
1056 _Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
1057 jvalue *args)
1059 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1060 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1062 return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1065 static void JNICALL
1066 _Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass,
1067 jmethodID id, va_list args)
1069 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1072 static void JNICALL
1073 _Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass,
1074 jmethodID id, ...)
1076 va_list args;
1078 va_start (args, id);
1079 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1080 va_end (args);
1083 static void JNICALL
1084 _Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass,
1085 jmethodID id, jvalue *args)
1087 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1090 static jobject JNICALL
1091 _Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
1092 jmethodID id, va_list args)
1094 JvAssert (klass && ! klass->isArray ());
1095 JvAssert (! strcmp (id->name->data, "<init>")
1096 && id->signature->length > 2
1097 && id->signature->data[0] == '('
1098 && ! strcmp (&id->signature->data[id->signature->length - 2],
1099 ")V"));
1101 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1102 id, args);
1105 static jobject JNICALL
1106 _Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
1108 JvAssert (klass && ! klass->isArray ());
1109 JvAssert (! strcmp (id->name->data, "<init>")
1110 && id->signature->length > 2
1111 && id->signature->data[0] == '('
1112 && ! strcmp (&id->signature->data[id->signature->length - 2],
1113 ")V"));
1115 va_list args;
1116 jobject result;
1118 va_start (args, id);
1119 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1120 id, args);
1121 va_end (args);
1123 return result;
1126 static jobject JNICALL
1127 _Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
1128 jvalue *args)
1130 JvAssert (klass && ! klass->isArray ());
1131 JvAssert (! strcmp (id->name->data, "<init>")
1132 && id->signature->length > 2
1133 && id->signature->data[0] == '('
1134 && ! strcmp (&id->signature->data[id->signature->length - 2],
1135 ")V"));
1137 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1138 id, args);
1143 template<typename T>
1144 static T JNICALL
1145 _Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
1147 obj = unwrap (obj);
1148 JvAssert (obj);
1149 T *ptr = (T *) ((char *) obj + field->getOffset ());
1150 return wrap_value (env, *ptr);
1153 template<typename T>
1154 static void JNICALL
1155 _Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
1157 obj = unwrap (obj);
1158 value = unwrap (value);
1160 JvAssert (obj);
1161 T *ptr = (T *) ((char *) obj + field->getOffset ());
1162 *ptr = value;
1165 template<jboolean is_static>
1166 static jfieldID JNICALL
1167 _Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
1168 const char *name, const char *sig)
1172 clazz = unwrap (clazz);
1174 _Jv_InitClass (clazz);
1176 _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1178 // FIXME: assume that SIG isn't too long.
1179 int len = strlen (sig);
1180 char s[len + 1];
1181 for (int i = 0; i <= len; ++i)
1182 s[i] = (sig[i] == '/') ? '.' : sig[i];
1183 jclass field_class = _Jv_FindClassFromSignature ((char *) s, NULL);
1185 // FIXME: what if field_class == NULL?
1187 java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1188 while (clazz != NULL)
1190 // We acquire the class lock so that fields aren't resolved
1191 // while we are running.
1192 JvSynchronize sync (clazz);
1194 jint count = (is_static
1195 ? JvNumStaticFields (clazz)
1196 : JvNumInstanceFields (clazz));
1197 jfieldID field = (is_static
1198 ? JvGetFirstStaticField (clazz)
1199 : JvGetFirstInstanceField (clazz));
1200 for (jint i = 0; i < count; ++i)
1202 _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1204 // The field might be resolved or it might not be. It
1205 // is much simpler to always resolve it.
1206 _Jv_ResolveField (field, loader);
1207 if (_Jv_equalUtf8Consts (f_name, a_name)
1208 && field->getClass() == field_class)
1209 return field;
1211 field = field->getNextField ();
1214 clazz = clazz->getSuperclass ();
1217 env->ex = new java::lang::NoSuchFieldError ();
1219 catch (jthrowable t)
1221 env->ex = t;
1223 return NULL;
1226 template<typename T>
1227 static T JNICALL
1228 _Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
1230 T *ptr = (T *) field->u.addr;
1231 return wrap_value (env, *ptr);
1234 template<typename T>
1235 static void JNICALL
1236 _Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
1238 value = unwrap (value);
1239 T *ptr = (T *) field->u.addr;
1240 *ptr = value;
1243 static jstring JNICALL
1244 _Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
1248 jstring r = _Jv_NewString (unichars, len);
1249 return (jstring) wrap_value (env, r);
1251 catch (jthrowable t)
1253 env->ex = t;
1254 return NULL;
1258 static jsize JNICALL
1259 _Jv_JNI_GetStringLength (JNIEnv *, jstring string)
1261 return unwrap (string)->length();
1264 static const jchar * JNICALL
1265 _Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
1267 string = unwrap (string);
1268 jchar *result = _Jv_GetStringChars (string);
1269 mark_for_gc (string, global_ref_table);
1270 if (isCopy)
1271 *isCopy = false;
1272 return (const jchar *) result;
1275 static void JNICALL
1276 _Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
1278 unmark_for_gc (unwrap (string), global_ref_table);
1281 static jstring JNICALL
1282 _Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
1286 jstring result = JvNewStringUTF (bytes);
1287 return (jstring) wrap_value (env, result);
1289 catch (jthrowable t)
1291 env->ex = t;
1292 return NULL;
1296 static jsize JNICALL
1297 _Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
1299 return JvGetStringUTFLength (unwrap (string));
1302 static const char * JNICALL
1303 _Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string,
1304 jboolean *isCopy)
1308 string = unwrap (string);
1309 if (string == NULL)
1310 return NULL;
1311 jsize len = JvGetStringUTFLength (string);
1312 char *r = (char *) _Jv_Malloc (len + 1);
1313 JvGetStringUTFRegion (string, 0, string->length(), r);
1314 r[len] = '\0';
1316 if (isCopy)
1317 *isCopy = true;
1319 return (const char *) r;
1321 catch (jthrowable t)
1323 env->ex = t;
1324 return NULL;
1328 static void JNICALL
1329 _Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
1331 _Jv_Free ((void *) utf);
1334 static void JNICALL
1335 _Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start,
1336 jsize len, jchar *buf)
1338 string = unwrap (string);
1339 jchar *result = _Jv_GetStringChars (string);
1340 if (start < 0 || start > string->length ()
1341 || len < 0 || start + len > string->length ())
1345 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1347 catch (jthrowable t)
1349 env->ex = t;
1352 else
1353 memcpy (buf, &result[start], len * sizeof (jchar));
1356 static void JNICALL
1357 _Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
1358 jsize len, char *buf)
1360 str = unwrap (str);
1362 if (start < 0 || start > str->length ()
1363 || len < 0 || start + len > str->length ())
1367 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1369 catch (jthrowable t)
1371 env->ex = t;
1374 else
1375 _Jv_GetStringUTFRegion (str, start, len, buf);
1378 static const jchar * JNICALL
1379 _Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
1381 jchar *result = _Jv_GetStringChars (unwrap (str));
1382 if (isCopy)
1383 *isCopy = false;
1384 return result;
1387 static void JNICALL
1388 _Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
1390 // Nothing.
1393 static jsize JNICALL
1394 _Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
1396 return unwrap (array)->length;
1399 static jarray JNICALL
1400 _Jv_JNI_NewObjectArray (JNIEnv *env, jsize length,
1401 jclass elementClass, jobject init)
1405 elementClass = unwrap (elementClass);
1406 init = unwrap (init);
1408 _Jv_CheckCast (elementClass, init);
1409 jarray result = JvNewObjectArray (length, elementClass, init);
1410 return (jarray) wrap_value (env, result);
1412 catch (jthrowable t)
1414 env->ex = t;
1415 return NULL;
1419 static jobject JNICALL
1420 _Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array,
1421 jsize index)
1423 if ((unsigned) index >= (unsigned) array->length)
1424 _Jv_ThrowBadArrayIndex (index);
1425 jobject *elts = elements (unwrap (array));
1426 return wrap_value (env, elts[index]);
1429 static void JNICALL
1430 _Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array,
1431 jsize index, jobject value)
1435 array = unwrap (array);
1436 value = unwrap (value);
1438 _Jv_CheckArrayStore (array, value);
1439 if ((unsigned) index >= (unsigned) array->length)
1440 _Jv_ThrowBadArrayIndex (index);
1441 jobject *elts = elements (array);
1442 elts[index] = value;
1444 catch (jthrowable t)
1446 env->ex = t;
1450 template<typename T, jclass K>
1451 static JArray<T> * JNICALL
1452 _Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
1456 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1458 catch (jthrowable t)
1460 env->ex = t;
1461 return NULL;
1465 template<typename T, jclass K>
1466 static T * JNICALL
1467 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1468 jboolean *isCopy)
1470 array = unwrap (array);
1471 if (! _Jv_JNI_check_types (env, array, K))
1472 return NULL;
1473 T *elts = elements (array);
1474 if (isCopy)
1476 // We elect never to copy.
1477 *isCopy = false;
1479 mark_for_gc (array, global_ref_table);
1480 return elts;
1483 template<typename T, jclass K>
1484 static void JNICALL
1485 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1486 T *, jint /* mode */)
1488 array = unwrap (array);
1489 _Jv_JNI_check_types (env, array, K);
1490 // Note that we ignore MODE. We can do this because we never copy
1491 // the array elements. My reading of the JNI documentation is that
1492 // this is an option for the implementor.
1493 unmark_for_gc (array, global_ref_table);
1496 template<typename T, jclass K>
1497 static void JNICALL
1498 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1499 jsize start, jsize len,
1500 T *buf)
1502 array = unwrap (array);
1503 if (! _Jv_JNI_check_types (env, array, K))
1504 return;
1506 // The cast to unsigned lets us save a comparison.
1507 if (start < 0 || len < 0
1508 || (unsigned long) (start + len) > (unsigned long) array->length)
1512 // FIXME: index.
1513 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1515 catch (jthrowable t)
1517 // Could have thown out of memory error.
1518 env->ex = t;
1521 else
1523 T *elts = elements (array) + start;
1524 memcpy (buf, elts, len * sizeof (T));
1528 template<typename T, jclass K>
1529 static void JNICALL
1530 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1531 jsize start, jsize len, T *buf)
1533 array = unwrap (array);
1534 if (! _Jv_JNI_check_types (env, array, K))
1535 return;
1537 // The cast to unsigned lets us save a comparison.
1538 if (start < 0 || len < 0
1539 || (unsigned long) (start + len) > (unsigned long) array->length)
1543 // FIXME: index.
1544 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1546 catch (jthrowable t)
1548 env->ex = t;
1551 else
1553 T *elts = elements (array) + start;
1554 memcpy (elts, buf, len * sizeof (T));
1558 static void * JNICALL
1559 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
1560 jboolean *isCopy)
1562 array = unwrap (array);
1563 // FIXME: does this work?
1564 jclass klass = array->getClass()->getComponentType();
1565 JvAssert (klass->isPrimitive ());
1566 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1567 if (isCopy)
1568 *isCopy = false;
1569 return r;
1572 static void JNICALL
1573 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
1575 // Nothing.
1578 static jint JNICALL
1579 _Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
1583 _Jv_MonitorEnter (unwrap (obj));
1584 return 0;
1586 catch (jthrowable t)
1588 env->ex = t;
1590 return JNI_ERR;
1593 static jint JNICALL
1594 _Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
1598 _Jv_MonitorExit (unwrap (obj));
1599 return 0;
1601 catch (jthrowable t)
1603 env->ex = t;
1605 return JNI_ERR;
1608 // JDK 1.2
1609 jobject JNICALL
1610 _Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
1611 jboolean)
1615 cls = unwrap (cls);
1616 java::lang::reflect::Field *field = new java::lang::reflect::Field();
1617 field->declaringClass = cls;
1618 field->offset = (char*) fieldID - (char *) cls->fields;
1619 field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1620 return wrap_value (env, field);
1622 catch (jthrowable t)
1624 env->ex = t;
1626 return NULL;
1629 // JDK 1.2
1630 static jfieldID JNICALL
1631 _Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
1633 using namespace java::lang::reflect;
1635 f = unwrap (f);
1636 Field *field = reinterpret_cast<Field *> (f);
1637 return _Jv_FromReflectedField (field);
1640 jobject JNICALL
1641 _Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
1642 jboolean)
1644 using namespace java::lang::reflect;
1646 jobject result = NULL;
1647 klass = unwrap (klass);
1651 if (_Jv_equalUtf8Consts (id->name, init_name))
1653 // A constructor.
1654 Constructor *cons = new Constructor ();
1655 cons->offset = (char *) id - (char *) &klass->methods;
1656 cons->declaringClass = klass;
1657 result = cons;
1659 else
1661 Method *meth = new Method ();
1662 meth->offset = (char *) id - (char *) &klass->methods;
1663 meth->declaringClass = klass;
1664 result = meth;
1667 catch (jthrowable t)
1669 env->ex = t;
1672 return wrap_value (env, result);
1675 static jmethodID JNICALL
1676 _Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
1678 using namespace java::lang::reflect;
1679 method = unwrap (method);
1680 if (Method::class$.isInstance (method))
1681 return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1682 return
1683 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1686 // JDK 1.2.
1687 jweak JNICALL
1688 _Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
1690 using namespace gnu::gcj::runtime;
1691 JNIWeakRef *ref = NULL;
1695 // This seems weird but I think it is correct.
1696 obj = unwrap (obj);
1697 ref = new JNIWeakRef (obj);
1698 mark_for_gc (ref, global_ref_table);
1700 catch (jthrowable t)
1702 env->ex = t;
1705 return reinterpret_cast<jweak> (ref);
1708 void JNICALL
1709 _Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
1711 using namespace gnu::gcj::runtime;
1712 JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1713 unmark_for_gc (ref, global_ref_table);
1714 ref->clear ();
1719 // Direct byte buffers.
1721 static jobject JNICALL
1722 _Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
1724 using namespace gnu::gcj;
1725 using namespace java::nio;
1726 return new DirectByteBufferImpl (reinterpret_cast<RawData *> (address),
1727 length);
1730 static void * JNICALL
1731 _Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
1733 using namespace java::nio;
1734 DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
1735 return reinterpret_cast<void *> (bb->address);
1738 static jlong JNICALL
1739 _Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
1741 using namespace java::nio;
1742 DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
1743 return bb->capacity();
1748 // Hash table of native methods.
1749 static JNINativeMethod *nathash;
1750 // Number of slots used.
1751 static int nathash_count = 0;
1752 // Number of slots available. Must be power of 2.
1753 static int nathash_size = 0;
1755 #define DELETED_ENTRY ((char *) (~0))
1757 // Compute a hash value for a native method descriptor.
1758 static int
1759 hash (const JNINativeMethod *method)
1761 char *ptr;
1762 int hash = 0;
1764 ptr = method->name;
1765 while (*ptr)
1766 hash = (31 * hash) + *ptr++;
1768 ptr = method->signature;
1769 while (*ptr)
1770 hash = (31 * hash) + *ptr++;
1772 return hash;
1775 // Find the slot where a native method goes.
1776 static JNINativeMethod *
1777 nathash_find_slot (const JNINativeMethod *method)
1779 jint h = hash (method);
1780 int step = (h ^ (h >> 16)) | 1;
1781 int w = h & (nathash_size - 1);
1782 int del = -1;
1784 for (;;)
1786 JNINativeMethod *slotp = &nathash[w];
1787 if (slotp->name == NULL)
1789 if (del >= 0)
1790 return &nathash[del];
1791 else
1792 return slotp;
1794 else if (slotp->name == DELETED_ENTRY)
1795 del = w;
1796 else if (! strcmp (slotp->name, method->name)
1797 && ! strcmp (slotp->signature, method->signature))
1798 return slotp;
1799 w = (w + step) & (nathash_size - 1);
1803 // Find a method. Return NULL if it isn't in the hash table.
1804 static void *
1805 nathash_find (JNINativeMethod *method)
1807 if (nathash == NULL)
1808 return NULL;
1809 JNINativeMethod *slot = nathash_find_slot (method);
1810 if (slot->name == NULL || slot->name == DELETED_ENTRY)
1811 return NULL;
1812 return slot->fnPtr;
1815 static void
1816 natrehash ()
1818 if (nathash == NULL)
1820 nathash_size = 1024;
1821 nathash =
1822 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1823 * sizeof (JNINativeMethod));
1824 memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1826 else
1828 int savesize = nathash_size;
1829 JNINativeMethod *savehash = nathash;
1830 nathash_size *= 2;
1831 nathash =
1832 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1833 * sizeof (JNINativeMethod));
1834 memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1836 for (int i = 0; i < savesize; ++i)
1838 if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1840 JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
1841 *slot = savehash[i];
1847 static void
1848 nathash_add (const JNINativeMethod *method)
1850 if (3 * nathash_count >= 2 * nathash_size)
1851 natrehash ();
1852 JNINativeMethod *slot = nathash_find_slot (method);
1853 // If the slot has a real entry in it, then there is no work to do.
1854 if (slot->name != NULL && slot->name != DELETED_ENTRY)
1855 return;
1856 // FIXME
1857 slot->name = strdup (method->name);
1858 slot->signature = strdup (method->signature);
1859 slot->fnPtr = method->fnPtr;
1862 static jint JNICALL
1863 _Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
1864 const JNINativeMethod *methods,
1865 jint nMethods)
1867 // Synchronize while we do the work. This must match
1868 // synchronization in some other functions that manipulate or use
1869 // the nathash table.
1870 JvSynchronize sync (global_ref_table);
1872 // Look at each descriptor given us, and find the corresponding
1873 // method in the class.
1874 for (int j = 0; j < nMethods; ++j)
1876 bool found = false;
1878 _Jv_Method *imeths = JvGetFirstMethod (klass);
1879 for (int i = 0; i < JvNumMethods (klass); ++i)
1881 _Jv_Method *self = &imeths[i];
1883 if (! strcmp (self->name->chars (), methods[j].name)
1884 && ! strcmp (self->signature->chars (), methods[j].signature))
1886 if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
1887 break;
1889 // Found a match that is native.
1890 found = true;
1891 nathash_add (&methods[j]);
1893 break;
1897 if (! found)
1899 jstring m = JvNewStringUTF (methods[j].name);
1902 env->ex = new java::lang::NoSuchMethodError (m);
1904 catch (jthrowable t)
1906 env->ex = t;
1908 return JNI_ERR;
1912 return JNI_OK;
1915 static jint JNICALL
1916 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1918 // FIXME -- we could implement this.
1919 return JNI_ERR;
1924 // Add a character to the buffer, encoding properly.
1925 static void
1926 add_char (char *buf, jchar c, int *here)
1928 if (c == '_')
1930 buf[(*here)++] = '_';
1931 buf[(*here)++] = '1';
1933 else if (c == ';')
1935 buf[(*here)++] = '_';
1936 buf[(*here)++] = '2';
1938 else if (c == '[')
1940 buf[(*here)++] = '_';
1941 buf[(*here)++] = '3';
1944 // Also check for `.' here because we might be passed an internal
1945 // qualified class name like `foo.bar'.
1946 else if (c == '/' || c == '.')
1947 buf[(*here)++] = '_';
1948 else if ((c >= '0' && c <= '9')
1949 || (c >= 'a' && c <= 'z')
1950 || (c >= 'A' && c <= 'Z'))
1951 buf[(*here)++] = (char) c;
1952 else
1954 // "Unicode" character.
1955 buf[(*here)++] = '_';
1956 buf[(*here)++] = '0';
1957 for (int i = 0; i < 4; ++i)
1959 int val = c & 0x0f;
1960 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1961 c >>= 4;
1963 *here += 4;
1967 // Compute a mangled name for a native function. This computes the
1968 // long name, and also returns an index which indicates where a NUL
1969 // can be placed to create the short name. This function assumes that
1970 // the buffer is large enough for its results.
1971 static void
1972 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1973 _Jv_Utf8Const *signature, char *buf, int *long_start)
1975 strcpy (buf, "Java_");
1976 int here = 5;
1978 // Add fully qualified class name.
1979 jchar *chars = _Jv_GetStringChars (klass->getName ());
1980 jint len = klass->getName ()->length ();
1981 for (int i = 0; i < len; ++i)
1982 add_char (buf, chars[i], &here);
1984 // Don't use add_char because we need a literal `_'.
1985 buf[here++] = '_';
1987 const unsigned char *fn = (const unsigned char *) func_name->chars ();
1988 const unsigned char *limit = fn + func_name->len ();
1989 for (int i = 0; ; ++i)
1991 int ch = UTF8_GET (fn, limit);
1992 if (ch < 0)
1993 break;
1994 add_char (buf, ch, &here);
1997 // This is where the long signature begins.
1998 *long_start = here;
1999 buf[here++] = '_';
2000 buf[here++] = '_';
2002 const unsigned char *sig = (const unsigned char *) signature->chars ();
2003 limit = sig + signature->len ();
2004 JvAssert (sig[0] == '(');
2005 ++sig;
2006 while (1)
2008 int ch = UTF8_GET (sig, limit);
2009 if (ch == ')' || ch < 0)
2010 break;
2011 add_char (buf, ch, &here);
2014 buf[here] = '\0';
2017 // Return the current thread's JNIEnv; if one does not exist, create
2018 // it. Also create a new system frame for use. This is `extern "C"'
2019 // because the compiler calls it.
2020 extern "C" JNIEnv *
2021 _Jv_GetJNIEnvNewFrame (jclass klass)
2023 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2024 if (env == NULL)
2026 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2027 env->p = &_Jv_JNIFunctions;
2028 env->klass = klass;
2029 env->locals = NULL;
2030 // We set env->ex below.
2032 _Jv_SetCurrentJNIEnv (env);
2035 _Jv_JNI_LocalFrame *frame
2036 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2037 + (FRAME_SIZE
2038 * sizeof (jobject)));
2040 frame->marker = MARK_SYSTEM;
2041 frame->size = FRAME_SIZE;
2042 frame->next = env->locals;
2044 for (int i = 0; i < frame->size; ++i)
2045 frame->vec[i] = NULL;
2047 env->locals = frame;
2048 env->ex = NULL;
2050 return env;
2053 // Return the function which implements a particular JNI method. If
2054 // we can't find the function, we throw the appropriate exception.
2055 // This is `extern "C"' because the compiler uses it.
2056 extern "C" void *
2057 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2058 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
2060 int name_length = name->len();
2061 int sig_length = signature->len();
2062 char buf[10 + 6 * (name_length + sig_length) + 12];
2063 int long_start;
2064 void *function;
2066 // Synchronize on something convenient. Right now we use the hash.
2067 JvSynchronize sync (global_ref_table);
2069 // First see if we have an override in the hash table.
2070 strncpy (buf, name->chars (), name_length);
2071 buf[name_length] = '\0';
2072 strncpy (buf + name_length + 1, signature->chars (), sig_length);
2073 buf[name_length + sig_length + 1] = '\0';
2074 JNINativeMethod meth;
2075 meth.name = buf;
2076 meth.signature = buf + name_length + 1;
2077 function = nathash_find (&meth);
2078 if (function != NULL)
2079 return function;
2081 // If there was no override, then look in the symbol table.
2082 buf[0] = '_';
2083 mangled_name (klass, name, signature, buf + 1, &long_start);
2084 char c = buf[long_start + 1];
2085 buf[long_start + 1] = '\0';
2087 function = _Jv_FindSymbolInExecutable (buf + 1);
2088 #ifdef WIN32
2089 // On Win32, we use the "stdcall" calling convention (see JNICALL
2090 // in jni.h).
2092 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2093 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2094 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2095 // take care of all these variations here.
2097 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2098 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2100 if (function == NULL)
2102 // We have tried searching for the 'fooBar' form (BCC) - now
2103 // try the others.
2105 // First, save the part of the long name that will be damaged
2106 // by appending '@nn'.
2107 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2109 sprintf (asz_buf, "@%d", args_size);
2110 strcat (buf, asz_buf);
2112 // Search for the '_fooBar@nn' form (MSVC).
2113 function = _Jv_FindSymbolInExecutable (buf);
2115 if (function == NULL)
2117 // Search for the 'fooBar@nn' form (MinGW GCC).
2118 function = _Jv_FindSymbolInExecutable (buf + 1);
2121 #endif /* WIN32 */
2123 if (function == NULL)
2125 buf[long_start + 1] = c;
2126 #ifdef WIN32
2127 // Restore the part of the long name that was damaged by
2128 // appending the '@nn'.
2129 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2130 #endif /* WIN32 */
2131 function = _Jv_FindSymbolInExecutable (buf + 1);
2132 if (function == NULL)
2134 #ifdef WIN32
2135 strcat (buf, asz_buf);
2136 function = _Jv_FindSymbolInExecutable (buf);
2137 if (function == NULL)
2138 function = _Jv_FindSymbolInExecutable (buf + 1);
2140 if (function == NULL)
2141 #endif /* WIN32 */
2143 jstring str = JvNewStringUTF (name->chars ());
2144 throw new java::lang::UnsatisfiedLinkError (str);
2149 return function;
2152 #ifdef INTERPRETER
2154 // This function is the stub which is used to turn an ordinary (CNI)
2155 // method call into a JNI call.
2156 void
2157 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2159 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2161 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2163 // FIXME: we should mark every reference parameter as a local. For
2164 // now we assume a conservative GC, and we assume that the
2165 // references are on the stack somewhere.
2167 // We cache the value that we find, of course, but if we don't find
2168 // a value we don't cache that fact -- we might subsequently load a
2169 // library which finds the function in question.
2171 // Synchronize on a convenient object to ensure sanity in case two
2172 // threads reach this point for the same function at the same
2173 // time.
2174 JvSynchronize sync (global_ref_table);
2175 if (_this->function == NULL)
2177 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2179 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2180 args_size += sizeof (_this->defining_class);
2182 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2183 _this->self->name,
2184 _this->self->signature,
2185 args_size);
2189 JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2190 ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2191 int offset = 0;
2193 // First argument is always the environment pointer.
2194 real_args[offset++].ptr = env;
2196 // For a static method, we pass in the Class. For non-static
2197 // methods, the `this' argument is already handled.
2198 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2199 real_args[offset++].ptr = _this->defining_class;
2201 // In libgcj, the callee synchronizes.
2202 jobject sync = NULL;
2203 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2205 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2206 sync = _this->defining_class;
2207 else
2208 sync = (jobject) args[0].ptr;
2209 _Jv_MonitorEnter (sync);
2212 // Copy over passed-in arguments.
2213 memcpy (&real_args[offset], args, _this->args_raw_size);
2215 // The actual call to the JNI function.
2216 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2217 ret, real_args);
2219 if (sync != NULL)
2220 _Jv_MonitorExit (sync);
2222 _Jv_JNI_PopSystemFrame (env);
2225 #endif /* INTERPRETER */
2230 // Invocation API.
2233 // An internal helper function.
2234 static jint
2235 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2236 void *args, jboolean is_daemon)
2238 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2239 java::lang::ThreadGroup *group = NULL;
2241 if (attach)
2243 // FIXME: do we really want to support 1.1?
2244 if (attach->version != JNI_VERSION_1_4
2245 && attach->version != JNI_VERSION_1_2
2246 && attach->version != JNI_VERSION_1_1)
2247 return JNI_EVERSION;
2249 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2250 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2253 // Attaching an already-attached thread is a no-op.
2254 if (_Jv_GetCurrentJNIEnv () != NULL)
2255 return 0;
2257 JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2258 if (env == NULL)
2259 return JNI_ERR;
2260 env->p = &_Jv_JNIFunctions;
2261 env->ex = NULL;
2262 env->klass = NULL;
2263 env->locals
2264 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2265 + (FRAME_SIZE
2266 * sizeof (jobject)));
2267 if (env->locals == NULL)
2269 _Jv_Free (env);
2270 return JNI_ERR;
2273 env->locals->marker = MARK_SYSTEM;
2274 env->locals->size = FRAME_SIZE;
2275 env->locals->next = NULL;
2277 for (int i = 0; i < env->locals->size; ++i)
2278 env->locals->vec[i] = NULL;
2280 *penv = reinterpret_cast<void *> (env);
2282 // This thread might already be a Java thread -- this function might
2283 // have been called simply to set the new JNIEnv.
2284 if (_Jv_ThreadCurrent () == NULL)
2288 if (is_daemon)
2289 _Jv_AttachCurrentThreadAsDaemon (name, group);
2290 else
2291 _Jv_AttachCurrentThread (name, group);
2293 catch (jthrowable t)
2295 return JNI_ERR;
2298 _Jv_SetCurrentJNIEnv (env);
2300 return 0;
2303 // This is the one actually used by JNI.
2304 static jint JNICALL
2305 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
2307 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2310 static jint JNICALL
2311 _Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
2312 void *args)
2314 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2317 static jint JNICALL
2318 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
2320 JvAssert (the_vm && vm == the_vm);
2322 JNIEnv *env;
2323 if (_Jv_ThreadCurrent () != NULL)
2325 jstring main_name;
2326 // This sucks.
2329 main_name = JvNewStringLatin1 ("main");
2331 catch (jthrowable t)
2333 return JNI_ERR;
2336 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
2337 reinterpret_cast<void **> (&env),
2338 NULL, false);
2339 if (r < 0)
2340 return r;
2342 else
2343 env = _Jv_GetCurrentJNIEnv ();
2345 _Jv_ThreadWait ();
2347 // Docs say that this always returns an error code.
2348 return JNI_ERR;
2351 jint JNICALL
2352 _Jv_JNI_DetachCurrentThread (JavaVM *)
2354 jint code = _Jv_DetachCurrentThread ();
2355 return code ? JNI_EDETACHED : 0;
2358 static jint JNICALL
2359 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
2361 if (_Jv_ThreadCurrent () == NULL)
2363 *penv = NULL;
2364 return JNI_EDETACHED;
2367 #ifdef ENABLE_JVMPI
2368 // Handle JVMPI requests.
2369 if (version == JVMPI_VERSION_1)
2371 *penv = (void *) &_Jv_JVMPI_Interface;
2372 return 0;
2374 #endif
2376 // FIXME: do we really want to support 1.1?
2377 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2378 && version != JNI_VERSION_1_1)
2380 *penv = NULL;
2381 return JNI_EVERSION;
2384 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2385 return 0;
2388 jint JNICALL
2389 JNI_GetDefaultJavaVMInitArgs (void *args)
2391 jint version = * (jint *) args;
2392 // Here we only support 1.2 and 1.4.
2393 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2394 return JNI_EVERSION;
2396 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2397 ia->version = JNI_VERSION_1_4;
2398 ia->nOptions = 0;
2399 ia->options = NULL;
2400 ia->ignoreUnrecognized = true;
2402 return 0;
2405 jint JNICALL
2406 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
2408 JvAssert (! the_vm);
2410 _Jv_CreateJavaVM (NULL);
2412 // FIXME: synchronize
2413 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2414 if (nvm == NULL)
2415 return JNI_ERR;
2416 nvm->functions = &_Jv_JNI_InvokeFunctions;
2418 // Parse the arguments.
2419 if (args != NULL)
2421 jint version = * (jint *) args;
2422 // We only support 1.2 and 1.4.
2423 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2424 return JNI_EVERSION;
2425 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2426 for (int i = 0; i < ia->nOptions; ++i)
2428 if (! strcmp (ia->options[i].optionString, "vfprintf")
2429 || ! strcmp (ia->options[i].optionString, "exit")
2430 || ! strcmp (ia->options[i].optionString, "abort"))
2432 // We are required to recognize these, but for now we
2433 // don't handle them in any way. FIXME.
2434 continue;
2436 else if (! strncmp (ia->options[i].optionString,
2437 "-verbose", sizeof ("-verbose") - 1))
2439 // We don't do anything with this option either. We
2440 // might want to make sure the argument is valid, but we
2441 // don't really care all that much for now.
2442 continue;
2444 else if (! strncmp (ia->options[i].optionString, "-D", 2))
2446 // FIXME.
2447 continue;
2449 else if (ia->ignoreUnrecognized)
2451 if (ia->options[i].optionString[0] == '_'
2452 || ! strncmp (ia->options[i].optionString, "-X", 2))
2453 continue;
2456 return JNI_ERR;
2460 jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2461 if (r < 0)
2462 return r;
2464 the_vm = nvm;
2465 *vm = the_vm;
2467 return 0;
2470 jint JNICALL
2471 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2473 if (buf_len <= 0)
2474 return JNI_ERR;
2476 // We only support a single VM.
2477 if (the_vm != NULL)
2479 vm_buffer[0] = the_vm;
2480 *n_vms = 1;
2482 else
2483 *n_vms = 0;
2484 return 0;
2487 JavaVM *
2488 _Jv_GetJavaVM ()
2490 // FIXME: synchronize
2491 if (! the_vm)
2493 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2494 if (nvm != NULL)
2495 nvm->functions = &_Jv_JNI_InvokeFunctions;
2496 the_vm = nvm;
2499 // If this is a Java thread, we want to make sure it has an
2500 // associated JNIEnv.
2501 if (_Jv_ThreadCurrent () != NULL)
2503 void *ignore;
2504 _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2507 return the_vm;
2510 static jint JNICALL
2511 _Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
2513 *vm = _Jv_GetJavaVM ();
2514 return *vm == NULL ? JNI_ERR : JNI_OK;
2519 #define RESERVED NULL
2521 struct JNINativeInterface _Jv_JNIFunctions =
2523 RESERVED,
2524 RESERVED,
2525 RESERVED,
2526 RESERVED,
2527 _Jv_JNI_GetVersion, // GetVersion
2528 _Jv_JNI_DefineClass, // DefineClass
2529 _Jv_JNI_FindClass, // FindClass
2530 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2531 _Jv_JNI_FromReflectedField, // FromReflectedField
2532 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2533 _Jv_JNI_GetSuperclass, // GetSuperclass
2534 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2535 _Jv_JNI_ToReflectedField, // ToReflectedField
2536 _Jv_JNI_Throw, // Throw
2537 _Jv_JNI_ThrowNew, // ThrowNew
2538 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2539 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2540 _Jv_JNI_ExceptionClear, // ExceptionClear
2541 _Jv_JNI_FatalError, // FatalError
2543 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2544 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2545 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2546 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2547 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2549 _Jv_JNI_IsSameObject, // IsSameObject
2551 _Jv_JNI_NewLocalRef, // NewLocalRef
2552 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2554 _Jv_JNI_AllocObject, // AllocObject
2555 _Jv_JNI_NewObject, // NewObject
2556 _Jv_JNI_NewObjectV, // NewObjectV
2557 _Jv_JNI_NewObjectA, // NewObjectA
2558 _Jv_JNI_GetObjectClass, // GetObjectClass
2559 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2560 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2562 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2563 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2564 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2565 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2566 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2567 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2568 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2569 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2570 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2571 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2572 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2573 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2574 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2575 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2576 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2577 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2578 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2579 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2580 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2581 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2582 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2583 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2584 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2585 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2586 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2587 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2588 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2589 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2590 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2591 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
2593 // Nonvirtual method invocation functions follow.
2594 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2595 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2596 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2597 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2598 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2599 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2600 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2601 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2602 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2603 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2604 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2605 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2606 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2607 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2608 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2609 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2610 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2611 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2612 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2613 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2614 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2615 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2616 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2617 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2618 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2619 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2620 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2621 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2622 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2623 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2625 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2626 _Jv_JNI_GetField<jobject>, // GetObjectField
2627 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2628 _Jv_JNI_GetField<jbyte>, // GetByteField
2629 _Jv_JNI_GetField<jchar>, // GetCharField
2630 _Jv_JNI_GetField<jshort>, // GetShortField
2631 _Jv_JNI_GetField<jint>, // GetIntField
2632 _Jv_JNI_GetField<jlong>, // GetLongField
2633 _Jv_JNI_GetField<jfloat>, // GetFloatField
2634 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2635 _Jv_JNI_SetField, // SetObjectField
2636 _Jv_JNI_SetField, // SetBooleanField
2637 _Jv_JNI_SetField, // SetByteField
2638 _Jv_JNI_SetField, // SetCharField
2639 _Jv_JNI_SetField, // SetShortField
2640 _Jv_JNI_SetField, // SetIntField
2641 _Jv_JNI_SetField, // SetLongField
2642 _Jv_JNI_SetField, // SetFloatField
2643 _Jv_JNI_SetField, // SetDoubleField
2644 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2646 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2647 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2648 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2649 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2650 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2651 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2652 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2653 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2654 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2655 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2656 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2657 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2658 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2659 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2660 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2661 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2662 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2663 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2664 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2665 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2666 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2667 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2668 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2669 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2670 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2671 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2672 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2673 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2674 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2675 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2677 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2678 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2679 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2680 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2681 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2682 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2683 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2684 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2685 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2686 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2687 _Jv_JNI_SetStaticField, // SetStaticObjectField
2688 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2689 _Jv_JNI_SetStaticField, // SetStaticByteField
2690 _Jv_JNI_SetStaticField, // SetStaticCharField
2691 _Jv_JNI_SetStaticField, // SetStaticShortField
2692 _Jv_JNI_SetStaticField, // SetStaticIntField
2693 _Jv_JNI_SetStaticField, // SetStaticLongField
2694 _Jv_JNI_SetStaticField, // SetStaticFloatField
2695 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2696 _Jv_JNI_NewString, // NewString
2697 _Jv_JNI_GetStringLength, // GetStringLength
2698 _Jv_JNI_GetStringChars, // GetStringChars
2699 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2700 _Jv_JNI_NewStringUTF, // NewStringUTF
2701 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
2702 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
2703 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2704 _Jv_JNI_GetArrayLength, // GetArrayLength
2705 _Jv_JNI_NewObjectArray, // NewObjectArray
2706 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2707 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
2708 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2709 // NewBooleanArray
2710 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2711 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2712 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2713 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2714 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2715 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2716 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2717 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2718 // GetBooleanArrayElements
2719 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2720 // GetByteArrayElements
2721 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2722 // GetCharArrayElements
2723 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2724 // GetShortArrayElements
2725 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2726 // GetIntArrayElements
2727 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2728 // GetLongArrayElements
2729 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2730 // GetFloatArrayElements
2731 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2732 // GetDoubleArrayElements
2733 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2734 // ReleaseBooleanArrayElements
2735 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2736 // ReleaseByteArrayElements
2737 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2738 // ReleaseCharArrayElements
2739 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2740 // ReleaseShortArrayElements
2741 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2742 // ReleaseIntArrayElements
2743 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2744 // ReleaseLongArrayElements
2745 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2746 // ReleaseFloatArrayElements
2747 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2748 // ReleaseDoubleArrayElements
2749 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2750 // GetBooleanArrayRegion
2751 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2752 // GetByteArrayRegion
2753 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2754 // GetCharArrayRegion
2755 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2756 // GetShortArrayRegion
2757 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2758 // GetIntArrayRegion
2759 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2760 // GetLongArrayRegion
2761 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2762 // GetFloatArrayRegion
2763 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2764 // GetDoubleArrayRegion
2765 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2766 // SetBooleanArrayRegion
2767 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2768 // SetByteArrayRegion
2769 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2770 // SetCharArrayRegion
2771 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2772 // SetShortArrayRegion
2773 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2774 // SetIntArrayRegion
2775 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2776 // SetLongArrayRegion
2777 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2778 // SetFloatArrayRegion
2779 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2780 // SetDoubleArrayRegion
2781 _Jv_JNI_RegisterNatives, // RegisterNatives
2782 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2783 _Jv_JNI_MonitorEnter, // MonitorEnter
2784 _Jv_JNI_MonitorExit, // MonitorExit
2785 _Jv_JNI_GetJavaVM, // GetJavaVM
2787 _Jv_JNI_GetStringRegion, // GetStringRegion
2788 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2789 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2790 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2791 _Jv_JNI_GetStringCritical, // GetStringCritical
2792 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
2794 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2795 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
2797 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2799 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2800 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
2801 _Jv_JNI_GetDirectBufferCapacity // GetDirectBufferCapacity
2804 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2806 RESERVED,
2807 RESERVED,
2808 RESERVED,
2810 _Jv_JNI_DestroyJavaVM,
2811 _Jv_JNI_AttachCurrentThread,
2812 _Jv_JNI_DetachCurrentThread,
2813 _Jv_JNI_GetEnv,
2814 _Jv_JNI_AttachCurrentThreadAsDaemon