* trans-const.c: Don't include unused math.h.
[official-gcc.git] / libjava / jni.cc
blob2f4c3e4fc84c966938fe109fe6b40d8ac01f1fa0
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/Buffer.h>
45 #include <java/nio/DirectByteBufferImpl.h>
46 #include <java/nio/DirectByteBufferImpl$ReadWrite.h>
47 #include <java/util/IdentityHashMap.h>
48 #include <gnu/gcj/RawData.h>
50 #include <gcj/method.h>
51 #include <gcj/field.h>
53 #include <java-interp.h>
54 #include <java-threads.h>
56 using namespace gcj;
58 // This enum is used to select different template instantiations in
59 // the invocation code.
60 enum invocation_type
62 normal,
63 nonvirtual,
64 static_type,
65 constructor
68 // Forward declarations.
69 extern struct JNINativeInterface _Jv_JNIFunctions;
70 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
72 // Number of slots in the default frame. The VM must allow at least
73 // 16.
74 #define FRAME_SIZE 32
76 // Mark value indicating this is an overflow frame.
77 #define MARK_NONE 0
78 // Mark value indicating this is a user frame.
79 #define MARK_USER 1
80 // Mark value indicating this is a system frame.
81 #define MARK_SYSTEM 2
83 // This structure is used to keep track of local references.
84 struct _Jv_JNI_LocalFrame
86 // This is true if this frame object represents a pushed frame (eg
87 // from PushLocalFrame).
88 int marker : 2;
90 // Number of elements in frame.
91 int size : 30;
93 // Next frame in chain.
94 _Jv_JNI_LocalFrame *next;
96 // The elements. These are allocated using the C "struct hack".
97 jobject vec[0];
100 // This holds a reference count for all local references.
101 static java::util::IdentityHashMap *local_ref_table;
102 // This holds a reference count for all global references.
103 static java::util::IdentityHashMap *global_ref_table;
105 // The only VM.
106 static JavaVM *the_vm;
108 #ifdef ENABLE_JVMPI
109 // The only JVMPI interface description.
110 static JVMPI_Interface _Jv_JVMPI_Interface;
112 static jint
113 jvmpiEnableEvent (jint event_type, void *)
115 switch (event_type)
117 case JVMPI_EVENT_OBJECT_ALLOC:
118 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
119 break;
121 case JVMPI_EVENT_THREAD_START:
122 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
123 break;
125 case JVMPI_EVENT_THREAD_END:
126 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
127 break;
129 default:
130 return JVMPI_NOT_AVAILABLE;
133 return JVMPI_SUCCESS;
136 static jint
137 jvmpiDisableEvent (jint event_type, void *)
139 switch (event_type)
141 case JVMPI_EVENT_OBJECT_ALLOC:
142 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
143 break;
145 default:
146 return JVMPI_NOT_AVAILABLE;
149 return JVMPI_SUCCESS;
151 #endif
155 void
156 _Jv_JNI_Init (void)
158 local_ref_table = new java::util::IdentityHashMap;
159 global_ref_table = new java::util::IdentityHashMap;
161 #ifdef ENABLE_JVMPI
162 _Jv_JVMPI_Interface.version = 1;
163 _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
164 _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
165 _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
166 _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
167 _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
168 #endif
171 // Tell the GC that a certain pointer is live.
172 static void
173 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
175 JvSynchronize sync (ref_table);
177 using namespace java::lang;
178 Integer *refcount = (Integer *) ref_table->get (obj);
179 jint val = (refcount == NULL) ? 0 : refcount->intValue ();
180 // FIXME: what about out of memory error?
181 ref_table->put (obj, new Integer (val + 1));
184 // Unmark a pointer.
185 static void
186 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
188 JvSynchronize sync (ref_table);
190 using namespace java::lang;
191 Integer *refcount = (Integer *) ref_table->get (obj);
192 JvAssert (refcount);
193 jint val = refcount->intValue () - 1;
194 JvAssert (val >= 0);
195 if (val == 0)
196 ref_table->remove (obj);
197 else
198 // FIXME: what about out of memory error?
199 ref_table->put (obj, new Integer (val));
202 // "Unwrap" some random non-reference type. This exists to simplify
203 // other template functions.
204 template<typename T>
205 static T
206 unwrap (T val)
208 return val;
211 // Unwrap a weak reference, if required.
212 template<typename T>
213 static T *
214 unwrap (T *obj)
216 using namespace gnu::gcj::runtime;
217 // We can compare the class directly because JNIWeakRef is `final'.
218 // Doing it this way is much faster.
219 if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
220 return obj;
221 JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
222 return reinterpret_cast<T *> (wr->get ());
227 static jobject JNICALL
228 _Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
230 // This seems weird but I think it is correct.
231 obj = unwrap (obj);
232 mark_for_gc (obj, global_ref_table);
233 return obj;
236 static void JNICALL
237 _Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
239 // This seems weird but I think it is correct.
240 obj = unwrap (obj);
241 unmark_for_gc (obj, global_ref_table);
244 static void JNICALL
245 _Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
247 _Jv_JNI_LocalFrame *frame;
249 // This seems weird but I think it is correct.
250 obj = unwrap (obj);
252 for (frame = env->locals; frame != NULL; frame = frame->next)
254 for (int i = 0; i < frame->size; ++i)
256 if (frame->vec[i] == obj)
258 frame->vec[i] = NULL;
259 unmark_for_gc (obj, local_ref_table);
260 return;
264 // Don't go past a marked frame.
265 JvAssert (frame->marker == MARK_NONE);
268 JvAssert (0);
271 static jint JNICALL
272 _Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
274 // It is easier to just always allocate a new frame of the requested
275 // size. This isn't the most efficient thing, but for now we don't
276 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
278 _Jv_JNI_LocalFrame *frame;
281 frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
282 + size * sizeof (jobject));
284 catch (jthrowable t)
286 env->ex = t;
287 return JNI_ERR;
290 frame->marker = MARK_NONE;
291 frame->size = size;
292 memset (&frame->vec[0], 0, size * sizeof (jobject));
293 frame->next = env->locals;
294 env->locals = frame;
296 return 0;
299 static jint JNICALL
300 _Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
302 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
303 if (r < 0)
304 return r;
306 // The new frame is on top.
307 env->locals->marker = MARK_USER;
309 return 0;
312 static jobject JNICALL
313 _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
315 // This seems weird but I think it is correct.
316 obj = unwrap (obj);
318 // Try to find an open slot somewhere in the topmost frame.
319 _Jv_JNI_LocalFrame *frame = env->locals;
320 bool done = false, set = false;
321 for (; frame != NULL && ! done; frame = frame->next)
323 for (int i = 0; i < frame->size; ++i)
325 if (frame->vec[i] == NULL)
327 set = true;
328 done = true;
329 frame->vec[i] = obj;
330 break;
334 // If we found a slot, or if the frame we just searched is the
335 // mark frame, then we are done.
336 if (done || frame == NULL || frame->marker != MARK_NONE)
337 break;
340 if (! set)
342 // No slots, so we allocate a new frame. According to the spec
343 // we could just die here. FIXME: return value.
344 _Jv_JNI_EnsureLocalCapacity (env, 16);
345 // We know the first element of the new frame will be ok.
346 env->locals->vec[0] = obj;
349 mark_for_gc (obj, local_ref_table);
350 return obj;
353 static jobject JNICALL
354 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
356 _Jv_JNI_LocalFrame *rf = env->locals;
358 bool done = false;
359 while (rf != NULL && ! done)
361 for (int i = 0; i < rf->size; ++i)
362 if (rf->vec[i] != NULL)
363 unmark_for_gc (rf->vec[i], local_ref_table);
365 // If the frame we just freed is the marker frame, we are done.
366 done = (rf->marker == stop);
368 _Jv_JNI_LocalFrame *n = rf->next;
369 // When N==NULL, we've reached the stack-allocated frame, and we
370 // must not free it. However, we must be sure to clear all its
371 // elements, since we might conceivably reuse it.
372 if (n == NULL)
374 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
375 break;
378 _Jv_Free (rf);
379 rf = n;
382 // Update the local frame information.
383 env->locals = rf;
385 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
388 static jobject JNICALL
389 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
391 return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
394 // Make sure an array's type is compatible with the type of the
395 // destination.
396 template<typename T>
397 static bool
398 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
400 jclass klass = array->getClass()->getComponentType();
401 if (__builtin_expect (klass != K, false))
403 env->ex = new java::lang::IllegalAccessError ();
404 return false;
406 else
407 return true;
410 // Pop a `system' frame from the stack. This is `extern "C"' as it is
411 // used by the compiler.
412 extern "C" void
413 _Jv_JNI_PopSystemFrame (JNIEnv *env)
415 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
417 if (env->ex)
419 jthrowable t = env->ex;
420 env->ex = NULL;
421 throw t;
425 template<typename T> T extract_from_jvalue(jvalue const & t);
426 template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
427 template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
428 template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
429 template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
430 template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
431 template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
432 template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
433 template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
434 template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
437 // This function is used from other template functions. It wraps the
438 // return value appropriately; we specialize it so that object returns
439 // are turned into local references.
440 template<typename T>
441 static T
442 wrap_value (JNIEnv *, T value)
444 return value;
447 // This specialization is used for jobject, jclass, jstring, jarray,
448 // etc.
449 template<typename R, typename T>
450 static T *
451 wrap_value (JNIEnv *env, T *value)
453 return (value == NULL
454 ? value
455 : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
460 static jint JNICALL
461 _Jv_JNI_GetVersion (JNIEnv *)
463 return JNI_VERSION_1_4;
466 static jclass JNICALL
467 _Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
468 const jbyte *buf, jsize bufLen)
472 loader = unwrap (loader);
474 jstring sname = JvNewStringUTF (name);
475 jbyteArray bytes = JvNewByteArray (bufLen);
477 jbyte *elts = elements (bytes);
478 memcpy (elts, buf, bufLen * sizeof (jbyte));
480 java::lang::ClassLoader *l
481 = reinterpret_cast<java::lang::ClassLoader *> (loader);
483 jclass result = l->defineClass (sname, bytes, 0, bufLen);
484 return (jclass) wrap_value (env, result);
486 catch (jthrowable t)
488 env->ex = t;
489 return NULL;
493 static jclass JNICALL
494 _Jv_JNI_FindClass (JNIEnv *env, const char *name)
496 // FIXME: assume that NAME isn't too long.
497 int len = strlen (name);
498 char s[len + 1];
499 for (int i = 0; i <= len; ++i)
500 s[i] = (name[i] == '/') ? '.' : name[i];
502 jclass r = NULL;
505 // This might throw an out of memory exception.
506 jstring n = JvNewStringUTF (s);
508 java::lang::ClassLoader *loader = NULL;
509 if (env->klass != NULL)
510 loader = env->klass->getClassLoaderInternal ();
512 if (loader == NULL)
514 // FIXME: should use getBaseClassLoader, but we don't have that
515 // yet.
516 loader = java::lang::ClassLoader::getSystemClassLoader ();
519 r = loader->loadClass (n);
521 catch (jthrowable t)
523 env->ex = t;
526 return (jclass) wrap_value (env, r);
529 static jclass JNICALL
530 _Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
532 return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
535 static jboolean JNICALL
536 _Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
538 return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
541 static jint JNICALL
542 _Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
544 // We check in case the user did some funky cast.
545 obj = unwrap (obj);
546 JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
547 env->ex = obj;
548 return 0;
551 static jint JNICALL
552 _Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
554 using namespace java::lang::reflect;
556 clazz = unwrap (clazz);
557 JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
559 int r = JNI_OK;
562 JArray<jclass> *argtypes
563 = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
564 NULL);
566 jclass *elts = elements (argtypes);
567 elts[0] = &java::lang::String::class$;
569 Constructor *cons = clazz->getConstructor (argtypes);
571 jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
572 NULL);
573 jobject *velts = elements (values);
574 velts[0] = JvNewStringUTF (message);
576 jobject obj = cons->newInstance (values);
578 env->ex = reinterpret_cast<jthrowable> (obj);
580 catch (jthrowable t)
582 env->ex = t;
583 r = JNI_ERR;
586 return r;
589 static jthrowable JNICALL
590 _Jv_JNI_ExceptionOccurred (JNIEnv *env)
592 return (jthrowable) wrap_value (env, env->ex);
595 static void JNICALL
596 _Jv_JNI_ExceptionDescribe (JNIEnv *env)
598 if (env->ex != NULL)
599 env->ex->printStackTrace();
602 static void JNICALL
603 _Jv_JNI_ExceptionClear (JNIEnv *env)
605 env->ex = NULL;
608 static jboolean JNICALL
609 _Jv_JNI_ExceptionCheck (JNIEnv *env)
611 return env->ex != NULL;
614 static void JNICALL
615 _Jv_JNI_FatalError (JNIEnv *, const char *message)
617 JvFail (message);
622 static jboolean JNICALL
623 _Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
625 return unwrap (obj1) == unwrap (obj2);
628 static jobject JNICALL
629 _Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
631 jobject obj = NULL;
632 using namespace java::lang::reflect;
636 clazz = unwrap (clazz);
637 JvAssert (clazz && ! clazz->isArray ());
638 if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
639 env->ex = new java::lang::InstantiationException ();
640 else
641 obj = _Jv_AllocObject (clazz);
643 catch (jthrowable t)
645 env->ex = t;
648 return wrap_value (env, obj);
651 static jclass JNICALL
652 _Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
654 obj = unwrap (obj);
655 JvAssert (obj);
656 return (jclass) wrap_value (env, obj->getClass());
659 static jboolean JNICALL
660 _Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
662 return unwrap (clazz)->isInstance(unwrap (obj));
668 // This section concerns method invocation.
671 template<jboolean is_static>
672 static jmethodID JNICALL
673 _Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
674 const char *name, const char *sig)
678 clazz = unwrap (clazz);
679 _Jv_InitClass (clazz);
681 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
683 // FIXME: assume that SIG isn't too long.
684 int len = strlen (sig);
685 char s[len + 1];
686 for (int i = 0; i <= len; ++i)
687 s[i] = (sig[i] == '/') ? '.' : sig[i];
688 _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
690 JvAssert (! clazz->isPrimitive());
692 using namespace java::lang::reflect;
694 while (clazz != NULL)
696 jint count = JvNumMethods (clazz);
697 jmethodID meth = JvGetFirstMethod (clazz);
699 for (jint i = 0; i < count; ++i)
701 if (((is_static && Modifier::isStatic (meth->accflags))
702 || (! is_static && ! Modifier::isStatic (meth->accflags)))
703 && _Jv_equalUtf8Consts (meth->name, name_u)
704 && _Jv_equalUtf8Consts (meth->signature, sig_u))
705 return meth;
707 meth = meth->getNextMethod();
710 clazz = clazz->getSuperclass ();
713 java::lang::StringBuffer *name_sig =
714 new java::lang::StringBuffer (JvNewStringUTF (name));
715 name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
716 env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
718 catch (jthrowable t)
720 env->ex = t;
723 return NULL;
726 // This is a helper function which turns a va_list into an array of
727 // `jvalue's. It needs signature information in order to do its work.
728 // The array of values must already be allocated.
729 static void
730 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
732 jclass *arg_elts = elements (arg_types);
733 for (int i = 0; i < arg_types->length; ++i)
735 // Here we assume that sizeof(int) >= sizeof(jint), because we
736 // use `int' when decoding the varargs. Likewise for
737 // float, and double. Also we assume that sizeof(jlong) >=
738 // sizeof(int), i.e. that jlong values are not further
739 // promoted.
740 JvAssert (sizeof (int) >= sizeof (jint));
741 JvAssert (sizeof (jlong) >= sizeof (int));
742 JvAssert (sizeof (double) >= sizeof (jfloat));
743 JvAssert (sizeof (double) >= sizeof (jdouble));
744 if (arg_elts[i] == JvPrimClass (byte))
745 values[i].b = (jbyte) va_arg (vargs, int);
746 else if (arg_elts[i] == JvPrimClass (short))
747 values[i].s = (jshort) va_arg (vargs, int);
748 else if (arg_elts[i] == JvPrimClass (int))
749 values[i].i = (jint) va_arg (vargs, int);
750 else if (arg_elts[i] == JvPrimClass (long))
751 values[i].j = (jlong) va_arg (vargs, jlong);
752 else if (arg_elts[i] == JvPrimClass (float))
753 values[i].f = (jfloat) va_arg (vargs, double);
754 else if (arg_elts[i] == JvPrimClass (double))
755 values[i].d = (jdouble) va_arg (vargs, double);
756 else if (arg_elts[i] == JvPrimClass (boolean))
757 values[i].z = (jboolean) va_arg (vargs, int);
758 else if (arg_elts[i] == JvPrimClass (char))
759 values[i].c = (jchar) va_arg (vargs, int);
760 else
762 // An object.
763 values[i].l = unwrap (va_arg (vargs, jobject));
768 // This can call any sort of method: virtual, "nonvirtual", static, or
769 // constructor.
770 template<typename T, invocation_type style>
771 static T JNICALL
772 _Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
773 jmethodID id, va_list vargs)
775 obj = unwrap (obj);
776 klass = unwrap (klass);
778 jclass decl_class = klass ? klass : obj->getClass ();
779 JvAssert (decl_class != NULL);
781 jclass return_type;
782 JArray<jclass> *arg_types;
786 _Jv_GetTypesFromSignature (id, decl_class,
787 &arg_types, &return_type);
789 jvalue args[arg_types->length];
790 array_from_valist (args, arg_types, vargs);
792 // For constructors we need to pass the Class we are instantiating.
793 if (style == constructor)
794 return_type = klass;
796 jvalue result;
797 _Jv_CallAnyMethodA (obj, return_type, id,
798 style == constructor,
799 style == normal,
800 arg_types, args, &result);
802 return wrap_value (env, extract_from_jvalue<T>(result));
804 catch (jthrowable t)
806 env->ex = t;
809 return wrap_value (env, (T) 0);
812 template<typename T, invocation_type style>
813 static T JNICALL
814 _Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
815 jmethodID method, ...)
817 va_list args;
818 T result;
820 va_start (args, method);
821 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
822 va_end (args);
824 return result;
827 template<typename T, invocation_type style>
828 static T JNICALL
829 _Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
830 jmethodID id, jvalue *args)
832 obj = unwrap (obj);
833 klass = unwrap (klass);
835 jclass decl_class = klass ? klass : obj->getClass ();
836 JvAssert (decl_class != NULL);
838 jclass return_type;
839 JArray<jclass> *arg_types;
842 _Jv_GetTypesFromSignature (id, decl_class,
843 &arg_types, &return_type);
845 // For constructors we need to pass the Class we are instantiating.
846 if (style == constructor)
847 return_type = klass;
849 // Unwrap arguments as required. Eww.
850 jclass *type_elts = elements (arg_types);
851 jvalue arg_copy[arg_types->length];
852 for (int i = 0; i < arg_types->length; ++i)
854 if (type_elts[i]->isPrimitive ())
855 arg_copy[i] = args[i];
856 else
857 arg_copy[i].l = unwrap (args[i].l);
860 jvalue result;
861 _Jv_CallAnyMethodA (obj, return_type, id,
862 style == constructor,
863 style == normal,
864 arg_types, arg_copy, &result);
866 return wrap_value (env, extract_from_jvalue<T>(result));
868 catch (jthrowable t)
870 env->ex = t;
873 return wrap_value (env, (T) 0);
876 template<invocation_type style>
877 static void JNICALL
878 _Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
879 jmethodID id, va_list vargs)
881 obj = unwrap (obj);
882 klass = unwrap (klass);
884 jclass decl_class = klass ? klass : obj->getClass ();
885 JvAssert (decl_class != NULL);
887 jclass return_type;
888 JArray<jclass> *arg_types;
891 _Jv_GetTypesFromSignature (id, decl_class,
892 &arg_types, &return_type);
894 jvalue args[arg_types->length];
895 array_from_valist (args, arg_types, vargs);
897 // For constructors we need to pass the Class we are instantiating.
898 if (style == constructor)
899 return_type = klass;
901 _Jv_CallAnyMethodA (obj, return_type, id,
902 style == constructor,
903 style == normal,
904 arg_types, args, NULL);
906 catch (jthrowable t)
908 env->ex = t;
912 template<invocation_type style>
913 static void JNICALL
914 _Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
915 jmethodID method, ...)
917 va_list args;
919 va_start (args, method);
920 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
921 va_end (args);
924 template<invocation_type style>
925 static void JNICALL
926 _Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
927 jmethodID id, jvalue *args)
929 jclass decl_class = klass ? klass : obj->getClass ();
930 JvAssert (decl_class != NULL);
932 jclass return_type;
933 JArray<jclass> *arg_types;
936 _Jv_GetTypesFromSignature (id, decl_class,
937 &arg_types, &return_type);
939 // Unwrap arguments as required. Eww.
940 jclass *type_elts = elements (arg_types);
941 jvalue arg_copy[arg_types->length];
942 for (int i = 0; i < arg_types->length; ++i)
944 if (type_elts[i]->isPrimitive ())
945 arg_copy[i] = args[i];
946 else
947 arg_copy[i].l = unwrap (args[i].l);
950 _Jv_CallAnyMethodA (obj, return_type, id,
951 style == constructor,
952 style == normal,
953 arg_types, args, NULL);
955 catch (jthrowable t)
957 env->ex = t;
961 // Functions with this signature are used to implement functions in
962 // the CallMethod family.
963 template<typename T>
964 static T JNICALL
965 _Jv_JNI_CallMethodV (JNIEnv *env, jobject obj,
966 jmethodID id, va_list args)
968 return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
971 // Functions with this signature are used to implement functions in
972 // the CallMethod family.
973 template<typename T>
974 static T JNICALL
975 _Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
977 va_list args;
978 T result;
980 va_start (args, id);
981 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
982 va_end (args);
984 return result;
987 // Functions with this signature are used to implement functions in
988 // the CallMethod family.
989 template<typename T>
990 static T JNICALL
991 _Jv_JNI_CallMethodA (JNIEnv *env, jobject obj,
992 jmethodID id, jvalue *args)
994 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
997 static void JNICALL
998 _Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj,
999 jmethodID id, va_list args)
1001 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1004 static void JNICALL
1005 _Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1007 va_list args;
1009 va_start (args, id);
1010 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1011 va_end (args);
1014 static void JNICALL
1015 _Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj,
1016 jmethodID id, jvalue *args)
1018 _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1021 // Functions with this signature are used to implement functions in
1022 // the CallStaticMethod family.
1023 template<typename T>
1024 static T JNICALL
1025 _Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
1026 jmethodID id, va_list args)
1028 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1029 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1031 return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1034 // Functions with this signature are used to implement functions in
1035 // the CallStaticMethod family.
1036 template<typename T>
1037 static T JNICALL
1038 _Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass,
1039 jmethodID id, ...)
1041 va_list args;
1042 T result;
1044 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1045 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1047 va_start (args, id);
1048 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1049 id, args);
1050 va_end (args);
1052 return result;
1055 // Functions with this signature are used to implement functions in
1056 // the CallStaticMethod family.
1057 template<typename T>
1058 static T JNICALL
1059 _Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
1060 jvalue *args)
1062 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1063 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1065 return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1068 static void JNICALL
1069 _Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass,
1070 jmethodID id, va_list args)
1072 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1075 static void JNICALL
1076 _Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass,
1077 jmethodID id, ...)
1079 va_list args;
1081 va_start (args, id);
1082 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1083 va_end (args);
1086 static void JNICALL
1087 _Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass,
1088 jmethodID id, jvalue *args)
1090 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1093 static jobject JNICALL
1094 _Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
1095 jmethodID id, va_list args)
1097 JvAssert (klass && ! klass->isArray ());
1098 JvAssert (! strcmp (id->name->data, "<init>")
1099 && id->signature->length > 2
1100 && id->signature->data[0] == '('
1101 && ! strcmp (&id->signature->data[id->signature->length - 2],
1102 ")V"));
1104 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1105 id, args);
1108 static jobject JNICALL
1109 _Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
1111 JvAssert (klass && ! klass->isArray ());
1112 JvAssert (! strcmp (id->name->data, "<init>")
1113 && id->signature->length > 2
1114 && id->signature->data[0] == '('
1115 && ! strcmp (&id->signature->data[id->signature->length - 2],
1116 ")V"));
1118 va_list args;
1119 jobject result;
1121 va_start (args, id);
1122 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1123 id, args);
1124 va_end (args);
1126 return result;
1129 static jobject JNICALL
1130 _Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
1131 jvalue *args)
1133 JvAssert (klass && ! klass->isArray ());
1134 JvAssert (! strcmp (id->name->data, "<init>")
1135 && id->signature->length > 2
1136 && id->signature->data[0] == '('
1137 && ! strcmp (&id->signature->data[id->signature->length - 2],
1138 ")V"));
1140 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1141 id, args);
1146 template<typename T>
1147 static T JNICALL
1148 _Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
1150 obj = unwrap (obj);
1151 JvAssert (obj);
1152 T *ptr = (T *) ((char *) obj + field->getOffset ());
1153 return wrap_value (env, *ptr);
1156 template<typename T>
1157 static void JNICALL
1158 _Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
1160 obj = unwrap (obj);
1161 value = unwrap (value);
1163 JvAssert (obj);
1164 T *ptr = (T *) ((char *) obj + field->getOffset ());
1165 *ptr = value;
1168 template<jboolean is_static>
1169 static jfieldID JNICALL
1170 _Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
1171 const char *name, const char *sig)
1175 clazz = unwrap (clazz);
1177 _Jv_InitClass (clazz);
1179 _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1181 // FIXME: assume that SIG isn't too long.
1182 int len = strlen (sig);
1183 char s[len + 1];
1184 for (int i = 0; i <= len; ++i)
1185 s[i] = (sig[i] == '/') ? '.' : sig[i];
1186 jclass field_class = _Jv_FindClassFromSignature ((char *) s, NULL);
1188 // FIXME: what if field_class == NULL?
1190 java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1191 while (clazz != NULL)
1193 // We acquire the class lock so that fields aren't resolved
1194 // while we are running.
1195 JvSynchronize sync (clazz);
1197 jint count = (is_static
1198 ? JvNumStaticFields (clazz)
1199 : JvNumInstanceFields (clazz));
1200 jfieldID field = (is_static
1201 ? JvGetFirstStaticField (clazz)
1202 : JvGetFirstInstanceField (clazz));
1203 for (jint i = 0; i < count; ++i)
1205 _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1207 // The field might be resolved or it might not be. It
1208 // is much simpler to always resolve it.
1209 _Jv_Linker::resolve_field (field, loader);
1210 if (_Jv_equalUtf8Consts (f_name, a_name)
1211 && field->getClass() == field_class)
1212 return field;
1214 field = field->getNextField ();
1217 clazz = clazz->getSuperclass ();
1220 env->ex = new java::lang::NoSuchFieldError ();
1222 catch (jthrowable t)
1224 env->ex = t;
1226 return NULL;
1229 template<typename T>
1230 static T JNICALL
1231 _Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
1233 T *ptr = (T *) field->u.addr;
1234 return wrap_value (env, *ptr);
1237 template<typename T>
1238 static void JNICALL
1239 _Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
1241 value = unwrap (value);
1242 T *ptr = (T *) field->u.addr;
1243 *ptr = value;
1246 static jstring JNICALL
1247 _Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
1251 jstring r = _Jv_NewString (unichars, len);
1252 return (jstring) wrap_value (env, r);
1254 catch (jthrowable t)
1256 env->ex = t;
1257 return NULL;
1261 static jsize JNICALL
1262 _Jv_JNI_GetStringLength (JNIEnv *, jstring string)
1264 return unwrap (string)->length();
1267 static const jchar * JNICALL
1268 _Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
1270 string = unwrap (string);
1271 jchar *result = _Jv_GetStringChars (string);
1272 mark_for_gc (string, global_ref_table);
1273 if (isCopy)
1274 *isCopy = false;
1275 return (const jchar *) result;
1278 static void JNICALL
1279 _Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
1281 unmark_for_gc (unwrap (string), global_ref_table);
1284 static jstring JNICALL
1285 _Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
1289 jstring result = JvNewStringUTF (bytes);
1290 return (jstring) wrap_value (env, result);
1292 catch (jthrowable t)
1294 env->ex = t;
1295 return NULL;
1299 static jsize JNICALL
1300 _Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
1302 return JvGetStringUTFLength (unwrap (string));
1305 static const char * JNICALL
1306 _Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string,
1307 jboolean *isCopy)
1311 string = unwrap (string);
1312 if (string == NULL)
1313 return NULL;
1314 jsize len = JvGetStringUTFLength (string);
1315 char *r = (char *) _Jv_Malloc (len + 1);
1316 JvGetStringUTFRegion (string, 0, string->length(), r);
1317 r[len] = '\0';
1319 if (isCopy)
1320 *isCopy = true;
1322 return (const char *) r;
1324 catch (jthrowable t)
1326 env->ex = t;
1327 return NULL;
1331 static void JNICALL
1332 _Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
1334 _Jv_Free ((void *) utf);
1337 static void JNICALL
1338 _Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start,
1339 jsize len, jchar *buf)
1341 string = unwrap (string);
1342 jchar *result = _Jv_GetStringChars (string);
1343 if (start < 0 || start > string->length ()
1344 || len < 0 || start + len > string->length ())
1348 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1350 catch (jthrowable t)
1352 env->ex = t;
1355 else
1356 memcpy (buf, &result[start], len * sizeof (jchar));
1359 static void JNICALL
1360 _Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
1361 jsize len, char *buf)
1363 str = unwrap (str);
1365 if (start < 0 || start > str->length ()
1366 || len < 0 || start + len > str->length ())
1370 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1372 catch (jthrowable t)
1374 env->ex = t;
1377 else
1378 _Jv_GetStringUTFRegion (str, start, len, buf);
1381 static const jchar * JNICALL
1382 _Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
1384 jchar *result = _Jv_GetStringChars (unwrap (str));
1385 if (isCopy)
1386 *isCopy = false;
1387 return result;
1390 static void JNICALL
1391 _Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
1393 // Nothing.
1396 static jsize JNICALL
1397 _Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
1399 return unwrap (array)->length;
1402 static jobjectArray JNICALL
1403 _Jv_JNI_NewObjectArray (JNIEnv *env, jsize length,
1404 jclass elementClass, jobject init)
1408 elementClass = unwrap (elementClass);
1409 init = unwrap (init);
1411 _Jv_CheckCast (elementClass, init);
1412 jarray result = JvNewObjectArray (length, elementClass, init);
1413 return (jobjectArray) wrap_value (env, result);
1415 catch (jthrowable t)
1417 env->ex = t;
1418 return NULL;
1422 static jobject JNICALL
1423 _Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array,
1424 jsize index)
1426 if ((unsigned) index >= (unsigned) array->length)
1427 _Jv_ThrowBadArrayIndex (index);
1428 jobject *elts = elements (unwrap (array));
1429 return wrap_value (env, elts[index]);
1432 static void JNICALL
1433 _Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array,
1434 jsize index, jobject value)
1438 array = unwrap (array);
1439 value = unwrap (value);
1441 _Jv_CheckArrayStore (array, value);
1442 if ((unsigned) index >= (unsigned) array->length)
1443 _Jv_ThrowBadArrayIndex (index);
1444 jobject *elts = elements (array);
1445 elts[index] = value;
1447 catch (jthrowable t)
1449 env->ex = t;
1453 template<typename T, jclass K>
1454 static JArray<T> * JNICALL
1455 _Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
1459 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1461 catch (jthrowable t)
1463 env->ex = t;
1464 return NULL;
1468 template<typename T, jclass K>
1469 static T * JNICALL
1470 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1471 jboolean *isCopy)
1473 array = unwrap (array);
1474 if (! _Jv_JNI_check_types (env, array, K))
1475 return NULL;
1476 T *elts = elements (array);
1477 if (isCopy)
1479 // We elect never to copy.
1480 *isCopy = false;
1482 mark_for_gc (array, global_ref_table);
1483 return elts;
1486 template<typename T, jclass K>
1487 static void JNICALL
1488 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1489 T *, jint /* mode */)
1491 array = unwrap (array);
1492 _Jv_JNI_check_types (env, array, K);
1493 // Note that we ignore MODE. We can do this because we never copy
1494 // the array elements. My reading of the JNI documentation is that
1495 // this is an option for the implementor.
1496 unmark_for_gc (array, global_ref_table);
1499 template<typename T, jclass K>
1500 static void JNICALL
1501 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1502 jsize start, jsize len,
1503 T *buf)
1505 array = unwrap (array);
1506 if (! _Jv_JNI_check_types (env, array, K))
1507 return;
1509 // The cast to unsigned lets us save a comparison.
1510 if (start < 0 || len < 0
1511 || (unsigned long) (start + len) > (unsigned long) array->length)
1515 // FIXME: index.
1516 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1518 catch (jthrowable t)
1520 // Could have thown out of memory error.
1521 env->ex = t;
1524 else
1526 T *elts = elements (array) + start;
1527 memcpy (buf, elts, len * sizeof (T));
1531 template<typename T, jclass K>
1532 static void JNICALL
1533 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1534 jsize start, jsize len, T *buf)
1536 array = unwrap (array);
1537 if (! _Jv_JNI_check_types (env, array, K))
1538 return;
1540 // The cast to unsigned lets us save a comparison.
1541 if (start < 0 || len < 0
1542 || (unsigned long) (start + len) > (unsigned long) array->length)
1546 // FIXME: index.
1547 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1549 catch (jthrowable t)
1551 env->ex = t;
1554 else
1556 T *elts = elements (array) + start;
1557 memcpy (elts, buf, len * sizeof (T));
1561 static void * JNICALL
1562 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
1563 jboolean *isCopy)
1565 array = unwrap (array);
1566 // FIXME: does this work?
1567 jclass klass = array->getClass()->getComponentType();
1568 JvAssert (klass->isPrimitive ());
1569 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1570 if (isCopy)
1571 *isCopy = false;
1572 return r;
1575 static void JNICALL
1576 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
1578 // Nothing.
1581 static jint JNICALL
1582 _Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
1586 _Jv_MonitorEnter (unwrap (obj));
1587 return 0;
1589 catch (jthrowable t)
1591 env->ex = t;
1593 return JNI_ERR;
1596 static jint JNICALL
1597 _Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
1601 _Jv_MonitorExit (unwrap (obj));
1602 return 0;
1604 catch (jthrowable t)
1606 env->ex = t;
1608 return JNI_ERR;
1611 // JDK 1.2
1612 jobject JNICALL
1613 _Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
1614 jboolean)
1618 cls = unwrap (cls);
1619 java::lang::reflect::Field *field = new java::lang::reflect::Field();
1620 field->declaringClass = cls;
1621 field->offset = (char*) fieldID - (char *) cls->fields;
1622 field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1623 return wrap_value (env, field);
1625 catch (jthrowable t)
1627 env->ex = t;
1629 return NULL;
1632 // JDK 1.2
1633 static jfieldID JNICALL
1634 _Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
1636 using namespace java::lang::reflect;
1638 f = unwrap (f);
1639 Field *field = reinterpret_cast<Field *> (f);
1640 return _Jv_FromReflectedField (field);
1643 jobject JNICALL
1644 _Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
1645 jboolean)
1647 using namespace java::lang::reflect;
1649 jobject result = NULL;
1650 klass = unwrap (klass);
1654 if (_Jv_equalUtf8Consts (id->name, init_name))
1656 // A constructor.
1657 Constructor *cons = new Constructor ();
1658 cons->offset = (char *) id - (char *) &klass->methods;
1659 cons->declaringClass = klass;
1660 result = cons;
1662 else
1664 Method *meth = new Method ();
1665 meth->offset = (char *) id - (char *) &klass->methods;
1666 meth->declaringClass = klass;
1667 result = meth;
1670 catch (jthrowable t)
1672 env->ex = t;
1675 return wrap_value (env, result);
1678 static jmethodID JNICALL
1679 _Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
1681 using namespace java::lang::reflect;
1682 method = unwrap (method);
1683 if (Method::class$.isInstance (method))
1684 return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1685 return
1686 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1689 // JDK 1.2.
1690 jweak JNICALL
1691 _Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
1693 using namespace gnu::gcj::runtime;
1694 JNIWeakRef *ref = NULL;
1698 // This seems weird but I think it is correct.
1699 obj = unwrap (obj);
1700 ref = new JNIWeakRef (obj);
1701 mark_for_gc (ref, global_ref_table);
1703 catch (jthrowable t)
1705 env->ex = t;
1708 return reinterpret_cast<jweak> (ref);
1711 void JNICALL
1712 _Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
1714 using namespace gnu::gcj::runtime;
1715 JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1716 unmark_for_gc (ref, global_ref_table);
1717 ref->clear ();
1722 // Direct byte buffers.
1724 static jobject JNICALL
1725 _Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
1727 using namespace gnu::gcj;
1728 using namespace java::nio;
1729 return new DirectByteBufferImpl$ReadWrite
1730 (reinterpret_cast<RawData *> (address), length);
1733 static void * JNICALL
1734 _Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
1736 using namespace java::nio;
1737 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1738 return NULL;
1739 Buffer *tmp = static_cast<Buffer *> (buffer);
1740 return reinterpret_cast<void *> (tmp->address);
1743 static jlong JNICALL
1744 _Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
1746 using namespace java::nio;
1747 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1748 return -1;
1749 Buffer *tmp = static_cast<Buffer *> (buffer);
1750 if (tmp->address == NULL)
1751 return -1;
1752 return tmp->capacity();
1757 // Hash table of native methods.
1758 static JNINativeMethod *nathash;
1759 // Number of slots used.
1760 static int nathash_count = 0;
1761 // Number of slots available. Must be power of 2.
1762 static int nathash_size = 0;
1764 #define DELETED_ENTRY ((char *) (~0))
1766 // Compute a hash value for a native method descriptor.
1767 static int
1768 hash (const JNINativeMethod *method)
1770 char *ptr;
1771 int hash = 0;
1773 ptr = method->name;
1774 while (*ptr)
1775 hash = (31 * hash) + *ptr++;
1777 ptr = method->signature;
1778 while (*ptr)
1779 hash = (31 * hash) + *ptr++;
1781 return hash;
1784 // Find the slot where a native method goes.
1785 static JNINativeMethod *
1786 nathash_find_slot (const JNINativeMethod *method)
1788 jint h = hash (method);
1789 int step = (h ^ (h >> 16)) | 1;
1790 int w = h & (nathash_size - 1);
1791 int del = -1;
1793 for (;;)
1795 JNINativeMethod *slotp = &nathash[w];
1796 if (slotp->name == NULL)
1798 if (del >= 0)
1799 return &nathash[del];
1800 else
1801 return slotp;
1803 else if (slotp->name == DELETED_ENTRY)
1804 del = w;
1805 else if (! strcmp (slotp->name, method->name)
1806 && ! strcmp (slotp->signature, method->signature))
1807 return slotp;
1808 w = (w + step) & (nathash_size - 1);
1812 // Find a method. Return NULL if it isn't in the hash table.
1813 static void *
1814 nathash_find (JNINativeMethod *method)
1816 if (nathash == NULL)
1817 return NULL;
1818 JNINativeMethod *slot = nathash_find_slot (method);
1819 if (slot->name == NULL || slot->name == DELETED_ENTRY)
1820 return NULL;
1821 return slot->fnPtr;
1824 static void
1825 natrehash ()
1827 if (nathash == NULL)
1829 nathash_size = 1024;
1830 nathash =
1831 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1832 * sizeof (JNINativeMethod));
1833 memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1835 else
1837 int savesize = nathash_size;
1838 JNINativeMethod *savehash = nathash;
1839 nathash_size *= 2;
1840 nathash =
1841 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1842 * sizeof (JNINativeMethod));
1843 memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1845 for (int i = 0; i < savesize; ++i)
1847 if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1849 JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
1850 *slot = savehash[i];
1856 static void
1857 nathash_add (const JNINativeMethod *method)
1859 if (3 * nathash_count >= 2 * nathash_size)
1860 natrehash ();
1861 JNINativeMethod *slot = nathash_find_slot (method);
1862 // If the slot has a real entry in it, then there is no work to do.
1863 if (slot->name != NULL && slot->name != DELETED_ENTRY)
1864 return;
1865 // FIXME
1866 slot->name = strdup (method->name);
1867 slot->signature = strdup (method->signature);
1868 slot->fnPtr = method->fnPtr;
1871 static jint JNICALL
1872 _Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
1873 const JNINativeMethod *methods,
1874 jint nMethods)
1876 // Synchronize while we do the work. This must match
1877 // synchronization in some other functions that manipulate or use
1878 // the nathash table.
1879 JvSynchronize sync (global_ref_table);
1881 // Look at each descriptor given us, and find the corresponding
1882 // method in the class.
1883 for (int j = 0; j < nMethods; ++j)
1885 bool found = false;
1887 _Jv_Method *imeths = JvGetFirstMethod (klass);
1888 for (int i = 0; i < JvNumMethods (klass); ++i)
1890 _Jv_Method *self = &imeths[i];
1892 if (! strcmp (self->name->chars (), methods[j].name)
1893 && ! strcmp (self->signature->chars (), methods[j].signature))
1895 if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
1896 break;
1898 // Found a match that is native.
1899 found = true;
1900 nathash_add (&methods[j]);
1902 break;
1906 if (! found)
1908 jstring m = JvNewStringUTF (methods[j].name);
1911 env->ex = new java::lang::NoSuchMethodError (m);
1913 catch (jthrowable t)
1915 env->ex = t;
1917 return JNI_ERR;
1921 return JNI_OK;
1924 static jint JNICALL
1925 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1927 // FIXME -- we could implement this.
1928 return JNI_ERR;
1933 // Add a character to the buffer, encoding properly.
1934 static void
1935 add_char (char *buf, jchar c, int *here)
1937 if (c == '_')
1939 buf[(*here)++] = '_';
1940 buf[(*here)++] = '1';
1942 else if (c == ';')
1944 buf[(*here)++] = '_';
1945 buf[(*here)++] = '2';
1947 else if (c == '[')
1949 buf[(*here)++] = '_';
1950 buf[(*here)++] = '3';
1953 // Also check for `.' here because we might be passed an internal
1954 // qualified class name like `foo.bar'.
1955 else if (c == '/' || c == '.')
1956 buf[(*here)++] = '_';
1957 else if ((c >= '0' && c <= '9')
1958 || (c >= 'a' && c <= 'z')
1959 || (c >= 'A' && c <= 'Z'))
1960 buf[(*here)++] = (char) c;
1961 else
1963 // "Unicode" character.
1964 buf[(*here)++] = '_';
1965 buf[(*here)++] = '0';
1966 for (int i = 0; i < 4; ++i)
1968 int val = c & 0x0f;
1969 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1970 c >>= 4;
1972 *here += 4;
1976 // Compute a mangled name for a native function. This computes the
1977 // long name, and also returns an index which indicates where a NUL
1978 // can be placed to create the short name. This function assumes that
1979 // the buffer is large enough for its results.
1980 static void
1981 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1982 _Jv_Utf8Const *signature, char *buf, int *long_start)
1984 strcpy (buf, "Java_");
1985 int here = 5;
1987 // Add fully qualified class name.
1988 jchar *chars = _Jv_GetStringChars (klass->getName ());
1989 jint len = klass->getName ()->length ();
1990 for (int i = 0; i < len; ++i)
1991 add_char (buf, chars[i], &here);
1993 // Don't use add_char because we need a literal `_'.
1994 buf[here++] = '_';
1996 const unsigned char *fn = (const unsigned char *) func_name->chars ();
1997 const unsigned char *limit = fn + func_name->len ();
1998 for (int i = 0; ; ++i)
2000 int ch = UTF8_GET (fn, limit);
2001 if (ch < 0)
2002 break;
2003 add_char (buf, ch, &here);
2006 // This is where the long signature begins.
2007 *long_start = here;
2008 buf[here++] = '_';
2009 buf[here++] = '_';
2011 const unsigned char *sig = (const unsigned char *) signature->chars ();
2012 limit = sig + signature->len ();
2013 JvAssert (sig[0] == '(');
2014 ++sig;
2015 while (1)
2017 int ch = UTF8_GET (sig, limit);
2018 if (ch == ')' || ch < 0)
2019 break;
2020 add_char (buf, ch, &here);
2023 buf[here] = '\0';
2026 // Return the current thread's JNIEnv; if one does not exist, create
2027 // it. Also create a new system frame for use. This is `extern "C"'
2028 // because the compiler calls it.
2029 extern "C" JNIEnv *
2030 _Jv_GetJNIEnvNewFrame (jclass klass)
2032 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2033 if (env == NULL)
2035 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2036 env->p = &_Jv_JNIFunctions;
2037 env->klass = klass;
2038 env->locals = NULL;
2039 // We set env->ex below.
2041 _Jv_SetCurrentJNIEnv (env);
2044 _Jv_JNI_LocalFrame *frame
2045 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2046 + (FRAME_SIZE
2047 * sizeof (jobject)));
2049 frame->marker = MARK_SYSTEM;
2050 frame->size = FRAME_SIZE;
2051 frame->next = env->locals;
2053 for (int i = 0; i < frame->size; ++i)
2054 frame->vec[i] = NULL;
2056 env->locals = frame;
2057 env->ex = NULL;
2059 return env;
2062 // Return the function which implements a particular JNI method. If
2063 // we can't find the function, we throw the appropriate exception.
2064 // This is `extern "C"' because the compiler uses it.
2065 extern "C" void *
2066 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2067 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
2069 int name_length = name->len();
2070 int sig_length = signature->len();
2071 char buf[10 + 6 * (name_length + sig_length) + 12];
2072 int long_start;
2073 void *function;
2075 // Synchronize on something convenient. Right now we use the hash.
2076 JvSynchronize sync (global_ref_table);
2078 // First see if we have an override in the hash table.
2079 strncpy (buf, name->chars (), name_length);
2080 buf[name_length] = '\0';
2081 strncpy (buf + name_length + 1, signature->chars (), sig_length);
2082 buf[name_length + sig_length + 1] = '\0';
2083 JNINativeMethod meth;
2084 meth.name = buf;
2085 meth.signature = buf + name_length + 1;
2086 function = nathash_find (&meth);
2087 if (function != NULL)
2088 return function;
2090 // If there was no override, then look in the symbol table.
2091 buf[0] = '_';
2092 mangled_name (klass, name, signature, buf + 1, &long_start);
2093 char c = buf[long_start + 1];
2094 buf[long_start + 1] = '\0';
2096 function = _Jv_FindSymbolInExecutable (buf + 1);
2097 #ifdef WIN32
2098 // On Win32, we use the "stdcall" calling convention (see JNICALL
2099 // in jni.h).
2101 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2102 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2103 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2104 // take care of all these variations here.
2106 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2107 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2109 if (function == NULL)
2111 // We have tried searching for the 'fooBar' form (BCC) - now
2112 // try the others.
2114 // First, save the part of the long name that will be damaged
2115 // by appending '@nn'.
2116 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2118 sprintf (asz_buf, "@%d", args_size);
2119 strcat (buf, asz_buf);
2121 // Search for the '_fooBar@nn' form (MSVC).
2122 function = _Jv_FindSymbolInExecutable (buf);
2124 if (function == NULL)
2126 // Search for the 'fooBar@nn' form (MinGW GCC).
2127 function = _Jv_FindSymbolInExecutable (buf + 1);
2130 #endif /* WIN32 */
2132 if (function == NULL)
2134 buf[long_start + 1] = c;
2135 #ifdef WIN32
2136 // Restore the part of the long name that was damaged by
2137 // appending the '@nn'.
2138 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2139 #endif /* WIN32 */
2140 function = _Jv_FindSymbolInExecutable (buf + 1);
2141 if (function == NULL)
2143 #ifdef WIN32
2144 strcat (buf, asz_buf);
2145 function = _Jv_FindSymbolInExecutable (buf);
2146 if (function == NULL)
2147 function = _Jv_FindSymbolInExecutable (buf + 1);
2149 if (function == NULL)
2150 #endif /* WIN32 */
2152 jstring str = JvNewStringUTF (name->chars ());
2153 throw new java::lang::UnsatisfiedLinkError (str);
2158 return function;
2161 #ifdef INTERPRETER
2163 // This function is the stub which is used to turn an ordinary (CNI)
2164 // method call into a JNI call.
2165 void
2166 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2168 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2170 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2172 // FIXME: we should mark every reference parameter as a local. For
2173 // now we assume a conservative GC, and we assume that the
2174 // references are on the stack somewhere.
2176 // We cache the value that we find, of course, but if we don't find
2177 // a value we don't cache that fact -- we might subsequently load a
2178 // library which finds the function in question.
2180 // Synchronize on a convenient object to ensure sanity in case two
2181 // threads reach this point for the same function at the same
2182 // time.
2183 JvSynchronize sync (global_ref_table);
2184 if (_this->function == NULL)
2186 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2188 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2189 args_size += sizeof (_this->defining_class);
2191 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2192 _this->self->name,
2193 _this->self->signature,
2194 args_size);
2198 JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2199 ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2200 int offset = 0;
2202 // First argument is always the environment pointer.
2203 real_args[offset++].ptr = env;
2205 // For a static method, we pass in the Class. For non-static
2206 // methods, the `this' argument is already handled.
2207 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2208 real_args[offset++].ptr = _this->defining_class;
2210 // In libgcj, the callee synchronizes.
2211 jobject sync = NULL;
2212 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2214 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2215 sync = _this->defining_class;
2216 else
2217 sync = (jobject) args[0].ptr;
2218 _Jv_MonitorEnter (sync);
2221 // Copy over passed-in arguments.
2222 memcpy (&real_args[offset], args, _this->args_raw_size);
2224 // The actual call to the JNI function.
2225 #if FFI_NATIVE_RAW_API
2226 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2227 ret, real_args);
2228 #else
2229 ffi_java_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2230 ret, real_args);
2231 #endif
2233 if (sync != NULL)
2234 _Jv_MonitorExit (sync);
2236 _Jv_JNI_PopSystemFrame (env);
2239 #endif /* INTERPRETER */
2244 // Invocation API.
2247 // An internal helper function.
2248 static jint
2249 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2250 void *args, jboolean is_daemon)
2252 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2253 java::lang::ThreadGroup *group = NULL;
2255 if (attach)
2257 // FIXME: do we really want to support 1.1?
2258 if (attach->version != JNI_VERSION_1_4
2259 && attach->version != JNI_VERSION_1_2
2260 && attach->version != JNI_VERSION_1_1)
2261 return JNI_EVERSION;
2263 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2264 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2267 // Attaching an already-attached thread is a no-op.
2268 if (_Jv_GetCurrentJNIEnv () != NULL)
2269 return 0;
2271 JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2272 if (env == NULL)
2273 return JNI_ERR;
2274 env->p = &_Jv_JNIFunctions;
2275 env->ex = NULL;
2276 env->klass = NULL;
2277 env->locals
2278 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2279 + (FRAME_SIZE
2280 * sizeof (jobject)));
2281 if (env->locals == NULL)
2283 _Jv_Free (env);
2284 return JNI_ERR;
2287 env->locals->marker = MARK_SYSTEM;
2288 env->locals->size = FRAME_SIZE;
2289 env->locals->next = NULL;
2291 for (int i = 0; i < env->locals->size; ++i)
2292 env->locals->vec[i] = NULL;
2294 *penv = reinterpret_cast<void *> (env);
2296 // This thread might already be a Java thread -- this function might
2297 // have been called simply to set the new JNIEnv.
2298 if (_Jv_ThreadCurrent () == NULL)
2302 if (is_daemon)
2303 _Jv_AttachCurrentThreadAsDaemon (name, group);
2304 else
2305 _Jv_AttachCurrentThread (name, group);
2307 catch (jthrowable t)
2309 return JNI_ERR;
2312 _Jv_SetCurrentJNIEnv (env);
2314 return 0;
2317 // This is the one actually used by JNI.
2318 static jint JNICALL
2319 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
2321 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2324 static jint JNICALL
2325 _Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
2326 void *args)
2328 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2331 static jint JNICALL
2332 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
2334 JvAssert (the_vm && vm == the_vm);
2336 JNIEnv *env;
2337 if (_Jv_ThreadCurrent () != NULL)
2339 jstring main_name;
2340 // This sucks.
2343 main_name = JvNewStringLatin1 ("main");
2345 catch (jthrowable t)
2347 return JNI_ERR;
2350 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
2351 reinterpret_cast<void **> (&env),
2352 NULL, false);
2353 if (r < 0)
2354 return r;
2356 else
2357 env = _Jv_GetCurrentJNIEnv ();
2359 _Jv_ThreadWait ();
2361 // Docs say that this always returns an error code.
2362 return JNI_ERR;
2365 jint JNICALL
2366 _Jv_JNI_DetachCurrentThread (JavaVM *)
2368 jint code = _Jv_DetachCurrentThread ();
2369 return code ? JNI_EDETACHED : 0;
2372 static jint JNICALL
2373 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
2375 if (_Jv_ThreadCurrent () == NULL)
2377 *penv = NULL;
2378 return JNI_EDETACHED;
2381 #ifdef ENABLE_JVMPI
2382 // Handle JVMPI requests.
2383 if (version == JVMPI_VERSION_1)
2385 *penv = (void *) &_Jv_JVMPI_Interface;
2386 return 0;
2388 #endif
2390 // FIXME: do we really want to support 1.1?
2391 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2392 && version != JNI_VERSION_1_1)
2394 *penv = NULL;
2395 return JNI_EVERSION;
2398 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2399 return 0;
2402 jint JNICALL
2403 JNI_GetDefaultJavaVMInitArgs (void *args)
2405 jint version = * (jint *) args;
2406 // Here we only support 1.2 and 1.4.
2407 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2408 return JNI_EVERSION;
2410 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2411 ia->version = JNI_VERSION_1_4;
2412 ia->nOptions = 0;
2413 ia->options = NULL;
2414 ia->ignoreUnrecognized = true;
2416 return 0;
2419 jint JNICALL
2420 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
2422 JvAssert (! the_vm);
2424 _Jv_CreateJavaVM (NULL);
2426 // FIXME: synchronize
2427 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2428 if (nvm == NULL)
2429 return JNI_ERR;
2430 nvm->functions = &_Jv_JNI_InvokeFunctions;
2432 // Parse the arguments.
2433 if (args != NULL)
2435 jint version = * (jint *) args;
2436 // We only support 1.2 and 1.4.
2437 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2438 return JNI_EVERSION;
2439 JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2440 for (int i = 0; i < ia->nOptions; ++i)
2442 if (! strcmp (ia->options[i].optionString, "vfprintf")
2443 || ! strcmp (ia->options[i].optionString, "exit")
2444 || ! strcmp (ia->options[i].optionString, "abort"))
2446 // We are required to recognize these, but for now we
2447 // don't handle them in any way. FIXME.
2448 continue;
2450 else if (! strncmp (ia->options[i].optionString,
2451 "-verbose", sizeof ("-verbose") - 1))
2453 // We don't do anything with this option either. We
2454 // might want to make sure the argument is valid, but we
2455 // don't really care all that much for now.
2456 continue;
2458 else if (! strncmp (ia->options[i].optionString, "-D", 2))
2460 // FIXME.
2461 continue;
2463 else if (ia->ignoreUnrecognized)
2465 if (ia->options[i].optionString[0] == '_'
2466 || ! strncmp (ia->options[i].optionString, "-X", 2))
2467 continue;
2470 return JNI_ERR;
2474 jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2475 if (r < 0)
2476 return r;
2478 the_vm = nvm;
2479 *vm = the_vm;
2481 return 0;
2484 jint JNICALL
2485 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2487 if (buf_len <= 0)
2488 return JNI_ERR;
2490 // We only support a single VM.
2491 if (the_vm != NULL)
2493 vm_buffer[0] = the_vm;
2494 *n_vms = 1;
2496 else
2497 *n_vms = 0;
2498 return 0;
2501 JavaVM *
2502 _Jv_GetJavaVM ()
2504 // FIXME: synchronize
2505 if (! the_vm)
2507 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2508 if (nvm != NULL)
2509 nvm->functions = &_Jv_JNI_InvokeFunctions;
2510 the_vm = nvm;
2513 // If this is a Java thread, we want to make sure it has an
2514 // associated JNIEnv.
2515 if (_Jv_ThreadCurrent () != NULL)
2517 void *ignore;
2518 _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2521 return the_vm;
2524 static jint JNICALL
2525 _Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
2527 *vm = _Jv_GetJavaVM ();
2528 return *vm == NULL ? JNI_ERR : JNI_OK;
2533 #define RESERVED NULL
2535 struct JNINativeInterface _Jv_JNIFunctions =
2537 RESERVED,
2538 RESERVED,
2539 RESERVED,
2540 RESERVED,
2541 _Jv_JNI_GetVersion, // GetVersion
2542 _Jv_JNI_DefineClass, // DefineClass
2543 _Jv_JNI_FindClass, // FindClass
2544 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2545 _Jv_JNI_FromReflectedField, // FromReflectedField
2546 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2547 _Jv_JNI_GetSuperclass, // GetSuperclass
2548 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2549 _Jv_JNI_ToReflectedField, // ToReflectedField
2550 _Jv_JNI_Throw, // Throw
2551 _Jv_JNI_ThrowNew, // ThrowNew
2552 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2553 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2554 _Jv_JNI_ExceptionClear, // ExceptionClear
2555 _Jv_JNI_FatalError, // FatalError
2557 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2558 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2559 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2560 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2561 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2563 _Jv_JNI_IsSameObject, // IsSameObject
2565 _Jv_JNI_NewLocalRef, // NewLocalRef
2566 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2568 _Jv_JNI_AllocObject, // AllocObject
2569 _Jv_JNI_NewObject, // NewObject
2570 _Jv_JNI_NewObjectV, // NewObjectV
2571 _Jv_JNI_NewObjectA, // NewObjectA
2572 _Jv_JNI_GetObjectClass, // GetObjectClass
2573 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2574 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2576 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2577 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2578 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2579 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2580 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2581 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2582 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2583 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2584 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2585 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2586 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2587 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2588 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2589 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2590 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2591 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2592 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2593 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2594 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2595 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2596 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2597 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2598 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2599 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2600 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2601 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2602 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2603 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2604 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2605 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
2607 // Nonvirtual method invocation functions follow.
2608 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2609 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2610 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2611 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2612 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2613 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2614 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2615 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2616 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2617 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2618 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2619 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2620 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2621 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2622 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2623 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2624 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2625 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2626 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2627 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2628 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2629 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2630 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2631 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2632 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2633 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2634 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2635 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2636 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2637 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2639 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2640 _Jv_JNI_GetField<jobject>, // GetObjectField
2641 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2642 _Jv_JNI_GetField<jbyte>, // GetByteField
2643 _Jv_JNI_GetField<jchar>, // GetCharField
2644 _Jv_JNI_GetField<jshort>, // GetShortField
2645 _Jv_JNI_GetField<jint>, // GetIntField
2646 _Jv_JNI_GetField<jlong>, // GetLongField
2647 _Jv_JNI_GetField<jfloat>, // GetFloatField
2648 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2649 _Jv_JNI_SetField, // SetObjectField
2650 _Jv_JNI_SetField, // SetBooleanField
2651 _Jv_JNI_SetField, // SetByteField
2652 _Jv_JNI_SetField, // SetCharField
2653 _Jv_JNI_SetField, // SetShortField
2654 _Jv_JNI_SetField, // SetIntField
2655 _Jv_JNI_SetField, // SetLongField
2656 _Jv_JNI_SetField, // SetFloatField
2657 _Jv_JNI_SetField, // SetDoubleField
2658 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2660 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2661 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2662 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2663 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2664 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2665 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2666 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2667 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2668 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2669 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2670 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2671 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2672 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2673 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2674 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2675 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2676 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2677 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2678 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2679 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2680 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2681 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2682 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2683 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2684 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2685 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2686 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2687 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2688 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2689 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2691 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2692 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2693 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2694 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2695 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2696 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2697 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2698 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2699 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2700 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2701 _Jv_JNI_SetStaticField, // SetStaticObjectField
2702 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2703 _Jv_JNI_SetStaticField, // SetStaticByteField
2704 _Jv_JNI_SetStaticField, // SetStaticCharField
2705 _Jv_JNI_SetStaticField, // SetStaticShortField
2706 _Jv_JNI_SetStaticField, // SetStaticIntField
2707 _Jv_JNI_SetStaticField, // SetStaticLongField
2708 _Jv_JNI_SetStaticField, // SetStaticFloatField
2709 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2710 _Jv_JNI_NewString, // NewString
2711 _Jv_JNI_GetStringLength, // GetStringLength
2712 _Jv_JNI_GetStringChars, // GetStringChars
2713 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2714 _Jv_JNI_NewStringUTF, // NewStringUTF
2715 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
2716 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
2717 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2718 _Jv_JNI_GetArrayLength, // GetArrayLength
2719 _Jv_JNI_NewObjectArray, // NewObjectArray
2720 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2721 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
2722 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2723 // NewBooleanArray
2724 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2725 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2726 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2727 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2728 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2729 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2730 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2731 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2732 // GetBooleanArrayElements
2733 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2734 // GetByteArrayElements
2735 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2736 // GetCharArrayElements
2737 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2738 // GetShortArrayElements
2739 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2740 // GetIntArrayElements
2741 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2742 // GetLongArrayElements
2743 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2744 // GetFloatArrayElements
2745 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2746 // GetDoubleArrayElements
2747 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2748 // ReleaseBooleanArrayElements
2749 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2750 // ReleaseByteArrayElements
2751 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2752 // ReleaseCharArrayElements
2753 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2754 // ReleaseShortArrayElements
2755 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2756 // ReleaseIntArrayElements
2757 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2758 // ReleaseLongArrayElements
2759 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2760 // ReleaseFloatArrayElements
2761 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2762 // ReleaseDoubleArrayElements
2763 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2764 // GetBooleanArrayRegion
2765 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2766 // GetByteArrayRegion
2767 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2768 // GetCharArrayRegion
2769 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2770 // GetShortArrayRegion
2771 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2772 // GetIntArrayRegion
2773 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2774 // GetLongArrayRegion
2775 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2776 // GetFloatArrayRegion
2777 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2778 // GetDoubleArrayRegion
2779 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2780 // SetBooleanArrayRegion
2781 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2782 // SetByteArrayRegion
2783 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2784 // SetCharArrayRegion
2785 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2786 // SetShortArrayRegion
2787 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2788 // SetIntArrayRegion
2789 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2790 // SetLongArrayRegion
2791 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2792 // SetFloatArrayRegion
2793 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2794 // SetDoubleArrayRegion
2795 _Jv_JNI_RegisterNatives, // RegisterNatives
2796 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2797 _Jv_JNI_MonitorEnter, // MonitorEnter
2798 _Jv_JNI_MonitorExit, // MonitorExit
2799 _Jv_JNI_GetJavaVM, // GetJavaVM
2801 _Jv_JNI_GetStringRegion, // GetStringRegion
2802 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2803 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2804 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2805 _Jv_JNI_GetStringCritical, // GetStringCritical
2806 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
2808 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2809 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
2811 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2813 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2814 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
2815 _Jv_JNI_GetDirectBufferCapacity // GetDirectBufferCapacity
2818 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2820 RESERVED,
2821 RESERVED,
2822 RESERVED,
2824 _Jv_JNI_DestroyJavaVM,
2825 _Jv_JNI_AttachCurrentThread,
2826 _Jv_JNI_DetachCurrentThread,
2827 _Jv_JNI_GetEnv,
2828 _Jv_JNI_AttachCurrentThreadAsDaemon