(arm_is_longcall_p): Update comment describing this funciton's behaviour.
[official-gcc.git] / libjava / jni.cc
blobf5a66d0948d1f92de4d2b3816e6932a0ca5f35fc
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->data, methods[j].name)
1884 && ! strcmp (self->signature->data, methods[j].signature))
1886 if (! (self->accflags
1887 & java::lang::reflect::Modifier::NATIVE))
1888 break;
1890 // Found a match that is native.
1891 found = true;
1892 nathash_add (&methods[j]);
1894 break;
1898 if (! found)
1900 jstring m = JvNewStringUTF (methods[j].name);
1903 env->ex =new java::lang::NoSuchMethodError (m);
1905 catch (jthrowable t)
1907 env->ex = t;
1909 return JNI_ERR;
1913 return JNI_OK;
1916 static jint JNICALL
1917 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1919 // FIXME -- we could implement this.
1920 return JNI_ERR;
1925 // Add a character to the buffer, encoding properly.
1926 static void
1927 add_char (char *buf, jchar c, int *here)
1929 if (c == '_')
1931 buf[(*here)++] = '_';
1932 buf[(*here)++] = '1';
1934 else if (c == ';')
1936 buf[(*here)++] = '_';
1937 buf[(*here)++] = '2';
1939 else if (c == '[')
1941 buf[(*here)++] = '_';
1942 buf[(*here)++] = '3';
1945 // Also check for `.' here because we might be passed an internal
1946 // qualified class name like `foo.bar'.
1947 else if (c == '/' || c == '.')
1948 buf[(*here)++] = '_';
1949 else if ((c >= '0' && c <= '9')
1950 || (c >= 'a' && c <= 'z')
1951 || (c >= 'A' && c <= 'Z'))
1952 buf[(*here)++] = (char) c;
1953 else
1955 // "Unicode" character.
1956 buf[(*here)++] = '_';
1957 buf[(*here)++] = '0';
1958 for (int i = 0; i < 4; ++i)
1960 int val = c & 0x0f;
1961 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1962 c >>= 4;
1964 *here += 4;
1968 // Compute a mangled name for a native function. This computes the
1969 // long name, and also returns an index which indicates where a NUL
1970 // can be placed to create the short name. This function assumes that
1971 // the buffer is large enough for its results.
1972 static void
1973 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1974 _Jv_Utf8Const *signature, char *buf, int *long_start)
1976 strcpy (buf, "Java_");
1977 int here = 5;
1979 // Add fully qualified class name.
1980 jchar *chars = _Jv_GetStringChars (klass->getName ());
1981 jint len = klass->getName ()->length ();
1982 for (int i = 0; i < len; ++i)
1983 add_char (buf, chars[i], &here);
1985 // Don't use add_char because we need a literal `_'.
1986 buf[here++] = '_';
1988 const unsigned char *fn = (const unsigned char *) func_name->data;
1989 const unsigned char *limit = fn + func_name->length;
1990 for (int i = 0; ; ++i)
1992 int ch = UTF8_GET (fn, limit);
1993 if (ch < 0)
1994 break;
1995 add_char (buf, ch, &here);
1998 // This is where the long signature begins.
1999 *long_start = here;
2000 buf[here++] = '_';
2001 buf[here++] = '_';
2003 const unsigned char *sig = (const unsigned char *) signature->data;
2004 limit = sig + signature->length;
2005 JvAssert (sig[0] == '(');
2006 ++sig;
2007 while (1)
2009 int ch = UTF8_GET (sig, limit);
2010 if (ch == ')' || ch < 0)
2011 break;
2012 add_char (buf, ch, &here);
2015 buf[here] = '\0';
2018 // Return the current thread's JNIEnv; if one does not exist, create
2019 // it. Also create a new system frame for use. This is `extern "C"'
2020 // because the compiler calls it.
2021 extern "C" JNIEnv *
2022 _Jv_GetJNIEnvNewFrame (jclass klass)
2024 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2025 if (env == NULL)
2027 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2028 env->p = &_Jv_JNIFunctions;
2029 env->klass = klass;
2030 env->locals = NULL;
2031 // We set env->ex below.
2033 _Jv_SetCurrentJNIEnv (env);
2036 _Jv_JNI_LocalFrame *frame
2037 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2038 + (FRAME_SIZE
2039 * sizeof (jobject)));
2041 frame->marker = MARK_SYSTEM;
2042 frame->size = FRAME_SIZE;
2043 frame->next = env->locals;
2045 for (int i = 0; i < frame->size; ++i)
2046 frame->vec[i] = NULL;
2048 env->locals = frame;
2049 env->ex = NULL;
2051 return env;
2054 // Return the function which implements a particular JNI method. If
2055 // we can't find the function, we throw the appropriate exception.
2056 // This is `extern "C"' because the compiler uses it.
2057 extern "C" void *
2058 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2059 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
2061 char buf[10 + 6 * (name->length + signature->length) + 12];
2062 int long_start;
2063 void *function;
2065 // Synchronize on something convenient. Right now we use the hash.
2066 JvSynchronize sync (global_ref_table);
2068 // First see if we have an override in the hash table.
2069 strncpy (buf, name->data, name->length);
2070 buf[name->length] = '\0';
2071 strncpy (buf + name->length + 1, signature->data, signature->length);
2072 buf[name->length + signature->length + 1] = '\0';
2073 JNINativeMethod meth;
2074 meth.name = buf;
2075 meth.signature = buf + name->length + 1;
2076 function = nathash_find (&meth);
2077 if (function != NULL)
2078 return function;
2080 // If there was no override, then look in the symbol table.
2081 buf[0] = '_';
2082 mangled_name (klass, name, signature, buf + 1, &long_start);
2083 char c = buf[long_start + 1];
2084 buf[long_start + 1] = '\0';
2086 function = _Jv_FindSymbolInExecutable (buf + 1);
2087 #ifdef WIN32
2088 // On Win32, we use the "stdcall" calling convention (see JNICALL
2089 // in jni.h).
2091 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2092 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2093 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2094 // take care of all these variations here.
2096 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2097 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2099 if (function == NULL)
2101 // We have tried searching for the 'fooBar' form (BCC) - now
2102 // try the others.
2104 // First, save the part of the long name that will be damaged
2105 // by appending '@nn'.
2106 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2108 sprintf (asz_buf, "@%d", args_size);
2109 strcat (buf, asz_buf);
2111 // Search for the '_fooBar@nn' form (MSVC).
2112 function = _Jv_FindSymbolInExecutable (buf);
2114 if (function == NULL)
2116 // Search for the 'fooBar@nn' form (MinGW GCC).
2117 function = _Jv_FindSymbolInExecutable (buf + 1);
2120 #endif /* WIN32 */
2122 if (function == NULL)
2124 buf[long_start + 1] = c;
2125 #ifdef WIN32
2126 // Restore the part of the long name that was damaged by
2127 // appending the '@nn'.
2128 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2129 #endif /* WIN32 */
2130 function = _Jv_FindSymbolInExecutable (buf + 1);
2131 if (function == NULL)
2133 #ifdef WIN32
2134 strcat (buf, asz_buf);
2135 function = _Jv_FindSymbolInExecutable (buf);
2136 if (function == NULL)
2137 function = _Jv_FindSymbolInExecutable (buf + 1);
2139 if (function == NULL)
2140 #endif /* WIN32 */
2142 jstring str = JvNewStringUTF (name->data);
2143 throw new java::lang::UnsatisfiedLinkError (str);
2148 return function;
2151 #ifdef INTERPRETER
2153 // This function is the stub which is used to turn an ordinary (CNI)
2154 // method call into a JNI call.
2155 void
2156 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2158 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2160 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2162 // FIXME: we should mark every reference parameter as a local. For
2163 // now we assume a conservative GC, and we assume that the
2164 // references are on the stack somewhere.
2166 // We cache the value that we find, of course, but if we don't find
2167 // a value we don't cache that fact -- we might subsequently load a
2168 // library which finds the function in question.
2170 // Synchronize on a convenient object to ensure sanity in case two
2171 // threads reach this point for the same function at the same
2172 // time.
2173 JvSynchronize sync (global_ref_table);
2174 if (_this->function == NULL)
2176 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2178 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2179 args_size += sizeof (_this->defining_class);
2181 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2182 _this->self->name,
2183 _this->self->signature,
2184 args_size);
2188 JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2189 ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2190 int offset = 0;
2192 // First argument is always the environment pointer.
2193 real_args[offset++].ptr = env;
2195 // For a static method, we pass in the Class. For non-static
2196 // methods, the `this' argument is already handled.
2197 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2198 real_args[offset++].ptr = _this->defining_class;
2200 // In libgcj, the callee synchronizes.
2201 jobject sync = NULL;
2202 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2204 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2205 sync = _this->defining_class;
2206 else
2207 sync = (jobject) args[0].ptr;
2208 _Jv_MonitorEnter (sync);
2211 // Copy over passed-in arguments.
2212 memcpy (&real_args[offset], args, _this->args_raw_size);
2214 // The actual call to the JNI function.
2215 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2216 ret, real_args);
2218 if (sync != NULL)
2219 _Jv_MonitorExit (sync);
2221 _Jv_JNI_PopSystemFrame (env);
2224 #endif /* INTERPRETER */
2229 // Invocation API.
2232 // An internal helper function.
2233 static jint
2234 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2235 void *args, jboolean is_daemon)
2237 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2238 java::lang::ThreadGroup *group = NULL;
2240 if (attach)
2242 // FIXME: do we really want to support 1.1?
2243 if (attach->version != JNI_VERSION_1_4
2244 && attach->version != JNI_VERSION_1_2
2245 && attach->version != JNI_VERSION_1_1)
2246 return JNI_EVERSION;
2248 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2249 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2252 // Attaching an already-attached thread is a no-op.
2253 if (_Jv_GetCurrentJNIEnv () != NULL)
2254 return 0;
2256 JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2257 if (env == NULL)
2258 return JNI_ERR;
2259 env->p = &_Jv_JNIFunctions;
2260 env->ex = NULL;
2261 env->klass = NULL;
2262 env->locals
2263 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2264 + (FRAME_SIZE
2265 * sizeof (jobject)));
2266 if (env->locals == NULL)
2268 _Jv_Free (env);
2269 return JNI_ERR;
2272 env->locals->marker = MARK_SYSTEM;
2273 env->locals->size = FRAME_SIZE;
2274 env->locals->next = NULL;
2276 for (int i = 0; i < env->locals->size; ++i)
2277 env->locals->vec[i] = NULL;
2279 *penv = reinterpret_cast<void *> (env);
2281 // This thread might already be a Java thread -- this function might
2282 // have been called simply to set the new JNIEnv.
2283 if (_Jv_ThreadCurrent () == NULL)
2287 if (is_daemon)
2288 _Jv_AttachCurrentThreadAsDaemon (name, group);
2289 else
2290 _Jv_AttachCurrentThread (name, group);
2292 catch (jthrowable t)
2294 return JNI_ERR;
2297 _Jv_SetCurrentJNIEnv (env);
2299 return 0;
2302 // This is the one actually used by JNI.
2303 static jint JNICALL
2304 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
2306 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2309 static jint JNICALL
2310 _Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
2311 void *args)
2313 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2316 static jint JNICALL
2317 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
2319 JvAssert (the_vm && vm == the_vm);
2321 JNIEnv *env;
2322 if (_Jv_ThreadCurrent () != NULL)
2324 jstring main_name;
2325 // This sucks.
2328 main_name = JvNewStringLatin1 ("main");
2330 catch (jthrowable t)
2332 return JNI_ERR;
2335 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
2336 reinterpret_cast<void **> (&env),
2337 NULL, false);
2338 if (r < 0)
2339 return r;
2341 else
2342 env = _Jv_GetCurrentJNIEnv ();
2344 _Jv_ThreadWait ();
2346 // Docs say that this always returns an error code.
2347 return JNI_ERR;
2350 jint JNICALL
2351 _Jv_JNI_DetachCurrentThread (JavaVM *)
2353 jint code = _Jv_DetachCurrentThread ();
2354 return code ? JNI_EDETACHED : 0;
2357 static jint JNICALL
2358 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
2360 if (_Jv_ThreadCurrent () == NULL)
2362 *penv = NULL;
2363 return JNI_EDETACHED;
2366 #ifdef ENABLE_JVMPI
2367 // Handle JVMPI requests.
2368 if (version == JVMPI_VERSION_1)
2370 *penv = (void *) &_Jv_JVMPI_Interface;
2371 return 0;
2373 #endif
2375 // FIXME: do we really want to support 1.1?
2376 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2377 && version != JNI_VERSION_1_1)
2379 *penv = NULL;
2380 return JNI_EVERSION;
2383 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2384 return 0;
2387 jint JNICALL
2388 JNI_GetDefaultJavaVMInitArgs (void *args)
2390 jint version = * (jint *) args;
2391 // Here we only support 1.2 and 1.4.
2392 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2393 return JNI_EVERSION;
2395 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2396 ia->version = JNI_VERSION_1_4;
2397 ia->nOptions = 0;
2398 ia->options = NULL;
2399 ia->ignoreUnrecognized = true;
2401 return 0;
2404 jint JNICALL
2405 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
2407 JvAssert (! the_vm);
2409 _Jv_CreateJavaVM (NULL);
2411 // FIXME: synchronize
2412 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2413 if (nvm == NULL)
2414 return JNI_ERR;
2415 nvm->functions = &_Jv_JNI_InvokeFunctions;
2417 // Parse the arguments.
2418 if (args != NULL)
2420 jint version = * (jint *) args;
2421 // We only support 1.2 and 1.4.
2422 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2423 return JNI_EVERSION;
2424 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2425 for (int i = 0; i < ia->nOptions; ++i)
2427 if (! strcmp (ia->options[i].optionString, "vfprintf")
2428 || ! strcmp (ia->options[i].optionString, "exit")
2429 || ! strcmp (ia->options[i].optionString, "abort"))
2431 // We are required to recognize these, but for now we
2432 // don't handle them in any way. FIXME.
2433 continue;
2435 else if (! strncmp (ia->options[i].optionString,
2436 "-verbose", sizeof ("-verbose") - 1))
2438 // We don't do anything with this option either. We
2439 // might want to make sure the argument is valid, but we
2440 // don't really care all that much for now.
2441 continue;
2443 else if (! strncmp (ia->options[i].optionString, "-D", 2))
2445 // FIXME.
2446 continue;
2448 else if (ia->ignoreUnrecognized)
2450 if (ia->options[i].optionString[0] == '_'
2451 || ! strncmp (ia->options[i].optionString, "-X", 2))
2452 continue;
2455 return JNI_ERR;
2459 jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2460 if (r < 0)
2461 return r;
2463 the_vm = nvm;
2464 *vm = the_vm;
2466 return 0;
2469 jint JNICALL
2470 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2472 if (buf_len <= 0)
2473 return JNI_ERR;
2475 // We only support a single VM.
2476 if (the_vm != NULL)
2478 vm_buffer[0] = the_vm;
2479 *n_vms = 1;
2481 else
2482 *n_vms = 0;
2483 return 0;
2486 JavaVM *
2487 _Jv_GetJavaVM ()
2489 // FIXME: synchronize
2490 if (! the_vm)
2492 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2493 if (nvm != NULL)
2494 nvm->functions = &_Jv_JNI_InvokeFunctions;
2495 the_vm = nvm;
2498 // If this is a Java thread, we want to make sure it has an
2499 // associated JNIEnv.
2500 if (_Jv_ThreadCurrent () != NULL)
2502 void *ignore;
2503 _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2506 return the_vm;
2509 static jint JNICALL
2510 _Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
2512 *vm = _Jv_GetJavaVM ();
2513 return *vm == NULL ? JNI_ERR : JNI_OK;
2518 #define RESERVED NULL
2520 struct JNINativeInterface _Jv_JNIFunctions =
2522 RESERVED,
2523 RESERVED,
2524 RESERVED,
2525 RESERVED,
2526 _Jv_JNI_GetVersion, // GetVersion
2527 _Jv_JNI_DefineClass, // DefineClass
2528 _Jv_JNI_FindClass, // FindClass
2529 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2530 _Jv_JNI_FromReflectedField, // FromReflectedField
2531 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2532 _Jv_JNI_GetSuperclass, // GetSuperclass
2533 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2534 _Jv_JNI_ToReflectedField, // ToReflectedField
2535 _Jv_JNI_Throw, // Throw
2536 _Jv_JNI_ThrowNew, // ThrowNew
2537 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2538 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2539 _Jv_JNI_ExceptionClear, // ExceptionClear
2540 _Jv_JNI_FatalError, // FatalError
2542 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2543 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2544 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2545 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2546 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2548 _Jv_JNI_IsSameObject, // IsSameObject
2550 _Jv_JNI_NewLocalRef, // NewLocalRef
2551 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2553 _Jv_JNI_AllocObject, // AllocObject
2554 _Jv_JNI_NewObject, // NewObject
2555 _Jv_JNI_NewObjectV, // NewObjectV
2556 _Jv_JNI_NewObjectA, // NewObjectA
2557 _Jv_JNI_GetObjectClass, // GetObjectClass
2558 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2559 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2561 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2562 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2563 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2564 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2565 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2566 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2567 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2568 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2569 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2570 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2571 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2572 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2573 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2574 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2575 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2576 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2577 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2578 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2579 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2580 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2581 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2582 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2583 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2584 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2585 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2586 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2587 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2588 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2589 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2590 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
2592 // Nonvirtual method invocation functions follow.
2593 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2594 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2595 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2596 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2597 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2598 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2599 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2600 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2601 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2602 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2603 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2604 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2605 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2606 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2607 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2608 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2609 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2610 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2611 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2612 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2613 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2614 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2615 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2616 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2617 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2618 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2619 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2620 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2621 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2622 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2624 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2625 _Jv_JNI_GetField<jobject>, // GetObjectField
2626 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2627 _Jv_JNI_GetField<jbyte>, // GetByteField
2628 _Jv_JNI_GetField<jchar>, // GetCharField
2629 _Jv_JNI_GetField<jshort>, // GetShortField
2630 _Jv_JNI_GetField<jint>, // GetIntField
2631 _Jv_JNI_GetField<jlong>, // GetLongField
2632 _Jv_JNI_GetField<jfloat>, // GetFloatField
2633 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2634 _Jv_JNI_SetField, // SetObjectField
2635 _Jv_JNI_SetField, // SetBooleanField
2636 _Jv_JNI_SetField, // SetByteField
2637 _Jv_JNI_SetField, // SetCharField
2638 _Jv_JNI_SetField, // SetShortField
2639 _Jv_JNI_SetField, // SetIntField
2640 _Jv_JNI_SetField, // SetLongField
2641 _Jv_JNI_SetField, // SetFloatField
2642 _Jv_JNI_SetField, // SetDoubleField
2643 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2645 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2646 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2647 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2648 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2649 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2650 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2651 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2652 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2653 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2654 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2655 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2656 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2657 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2658 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2659 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2660 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2661 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2662 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2663 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2664 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2665 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2666 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2667 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2668 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2669 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2670 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2671 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2672 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2673 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2674 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2676 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2677 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2678 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2679 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2680 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2681 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2682 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2683 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2684 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2685 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2686 _Jv_JNI_SetStaticField, // SetStaticObjectField
2687 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2688 _Jv_JNI_SetStaticField, // SetStaticByteField
2689 _Jv_JNI_SetStaticField, // SetStaticCharField
2690 _Jv_JNI_SetStaticField, // SetStaticShortField
2691 _Jv_JNI_SetStaticField, // SetStaticIntField
2692 _Jv_JNI_SetStaticField, // SetStaticLongField
2693 _Jv_JNI_SetStaticField, // SetStaticFloatField
2694 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2695 _Jv_JNI_NewString, // NewString
2696 _Jv_JNI_GetStringLength, // GetStringLength
2697 _Jv_JNI_GetStringChars, // GetStringChars
2698 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2699 _Jv_JNI_NewStringUTF, // NewStringUTF
2700 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
2701 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
2702 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2703 _Jv_JNI_GetArrayLength, // GetArrayLength
2704 _Jv_JNI_NewObjectArray, // NewObjectArray
2705 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2706 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
2707 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2708 // NewBooleanArray
2709 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2710 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2711 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2712 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2713 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2714 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2715 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2716 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2717 // GetBooleanArrayElements
2718 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2719 // GetByteArrayElements
2720 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2721 // GetCharArrayElements
2722 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2723 // GetShortArrayElements
2724 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2725 // GetIntArrayElements
2726 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2727 // GetLongArrayElements
2728 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2729 // GetFloatArrayElements
2730 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2731 // GetDoubleArrayElements
2732 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2733 // ReleaseBooleanArrayElements
2734 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2735 // ReleaseByteArrayElements
2736 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2737 // ReleaseCharArrayElements
2738 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2739 // ReleaseShortArrayElements
2740 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2741 // ReleaseIntArrayElements
2742 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2743 // ReleaseLongArrayElements
2744 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2745 // ReleaseFloatArrayElements
2746 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2747 // ReleaseDoubleArrayElements
2748 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2749 // GetBooleanArrayRegion
2750 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2751 // GetByteArrayRegion
2752 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2753 // GetCharArrayRegion
2754 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2755 // GetShortArrayRegion
2756 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2757 // GetIntArrayRegion
2758 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2759 // GetLongArrayRegion
2760 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2761 // GetFloatArrayRegion
2762 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2763 // GetDoubleArrayRegion
2764 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2765 // SetBooleanArrayRegion
2766 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2767 // SetByteArrayRegion
2768 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2769 // SetCharArrayRegion
2770 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2771 // SetShortArrayRegion
2772 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2773 // SetIntArrayRegion
2774 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2775 // SetLongArrayRegion
2776 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2777 // SetFloatArrayRegion
2778 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2779 // SetDoubleArrayRegion
2780 _Jv_JNI_RegisterNatives, // RegisterNatives
2781 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2782 _Jv_JNI_MonitorEnter, // MonitorEnter
2783 _Jv_JNI_MonitorExit, // MonitorExit
2784 _Jv_JNI_GetJavaVM, // GetJavaVM
2786 _Jv_JNI_GetStringRegion, // GetStringRegion
2787 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2788 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2789 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2790 _Jv_JNI_GetStringCritical, // GetStringCritical
2791 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
2793 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2794 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
2796 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2798 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2799 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
2800 _Jv_JNI_GetDirectBufferCapacity // GetDirectBufferCapacity
2803 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2805 RESERVED,
2806 RESERVED,
2807 RESERVED,
2809 _Jv_JNI_DestroyJavaVM,
2810 _Jv_JNI_AttachCurrentThread,
2811 _Jv_JNI_DetachCurrentThread,
2812 _Jv_JNI_GetEnv,
2813 _Jv_JNI_AttachCurrentThreadAsDaemon