Move entry to proper file.
[official-gcc.git] / libjava / jni.cc
blob19539c7714a83c7f67ab82bcdac7ad606f355606
1 // jni.cc - JNI implementation, including the jump table.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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>
49 #include <java/lang/ClassNotFoundException.h>
51 #include <gcj/method.h>
52 #include <gcj/field.h>
54 #include <java-interp.h>
55 #include <java-threads.h>
57 using namespace gcj;
59 // This enum is used to select different template instantiations in
60 // the invocation code.
61 enum invocation_type
63 normal,
64 nonvirtual,
65 static_type,
66 constructor
69 // Forward declarations.
70 extern struct JNINativeInterface _Jv_JNIFunctions;
71 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
73 // Number of slots in the default frame. The VM must allow at least
74 // 16.
75 #define FRAME_SIZE 16
77 // Mark value indicating this is an overflow frame.
78 #define MARK_NONE 0
79 // Mark value indicating this is a user frame.
80 #define MARK_USER 1
81 // Mark value indicating this is a system frame.
82 #define MARK_SYSTEM 2
84 // This structure is used to keep track of local references.
85 struct _Jv_JNI_LocalFrame
87 // This is true if this frame object represents a pushed frame (eg
88 // from PushLocalFrame).
89 int marker;
91 // Flag to indicate some locals were allocated.
92 int allocated_p;
94 // Number of elements in frame.
95 int size;
97 // Next frame in chain.
98 _Jv_JNI_LocalFrame *next;
100 // The elements. These are allocated using the C "struct hack".
101 jobject vec[0];
104 // This holds a reference count for all local references.
105 static java::util::IdentityHashMap *local_ref_table;
106 // This holds a reference count for all global references.
107 static java::util::IdentityHashMap *global_ref_table;
109 // The only VM.
110 JavaVM *_Jv_the_vm;
112 #ifdef ENABLE_JVMPI
113 // The only JVMPI interface description.
114 static JVMPI_Interface _Jv_JVMPI_Interface;
116 static jint
117 jvmpiEnableEvent (jint event_type, void *)
119 switch (event_type)
121 case JVMPI_EVENT_OBJECT_ALLOC:
122 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
123 break;
125 case JVMPI_EVENT_THREAD_START:
126 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
127 break;
129 case JVMPI_EVENT_THREAD_END:
130 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
131 break;
133 default:
134 return JVMPI_NOT_AVAILABLE;
137 return JVMPI_SUCCESS;
140 static jint
141 jvmpiDisableEvent (jint event_type, void *)
143 switch (event_type)
145 case JVMPI_EVENT_OBJECT_ALLOC:
146 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
147 break;
149 default:
150 return JVMPI_NOT_AVAILABLE;
153 return JVMPI_SUCCESS;
155 #endif
159 void
160 _Jv_JNI_Init (void)
162 local_ref_table = new java::util::IdentityHashMap;
163 global_ref_table = new java::util::IdentityHashMap;
165 #ifdef ENABLE_JVMPI
166 _Jv_JVMPI_Interface.version = 1;
167 _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
168 _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
169 _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
170 _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
171 _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
172 #endif
175 // Tell the GC that a certain pointer is live.
176 static void
177 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
179 JvSynchronize sync (ref_table);
181 using namespace java::lang;
182 Integer *refcount = (Integer *) ref_table->get (obj);
183 jint val = (refcount == NULL) ? 0 : refcount->intValue ();
184 // FIXME: what about out of memory error?
185 ref_table->put (obj, new Integer (val + 1));
188 // Unmark a pointer.
189 static void
190 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
192 JvSynchronize sync (ref_table);
194 using namespace java::lang;
195 Integer *refcount = (Integer *) ref_table->get (obj);
196 JvAssert (refcount);
197 jint val = refcount->intValue () - 1;
198 JvAssert (val >= 0);
199 if (val == 0)
200 ref_table->remove (obj);
201 else
202 // FIXME: what about out of memory error?
203 ref_table->put (obj, new Integer (val));
206 // "Unwrap" some random non-reference type. This exists to simplify
207 // other template functions.
208 template<typename T>
209 static T
210 unwrap (T val)
212 return val;
215 // Unwrap a weak reference, if required.
216 template<typename T>
217 static T *
218 unwrap (T *obj)
220 using namespace gnu::gcj::runtime;
221 // We can compare the class directly because JNIWeakRef is `final'.
222 // Doing it this way is much faster.
223 if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
224 return obj;
225 JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
226 return reinterpret_cast<T *> (wr->get ());
229 jobject
230 _Jv_UnwrapJNIweakReference (jobject obj)
232 return unwrap (obj);
237 static jobject JNICALL
238 _Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
240 // This seems weird but I think it is correct.
241 obj = unwrap (obj);
242 mark_for_gc (obj, global_ref_table);
243 return obj;
246 static void JNICALL
247 _Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
249 // This seems weird but I think it is correct.
250 obj = unwrap (obj);
251 unmark_for_gc (obj, global_ref_table);
254 static void JNICALL
255 _Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
257 _Jv_JNI_LocalFrame *frame;
259 // This seems weird but I think it is correct.
260 obj = unwrap (obj);
262 for (frame = env->locals; frame != NULL; frame = frame->next)
264 for (int i = 0; i < frame->size; ++i)
266 if (frame->vec[i] == obj)
268 frame->vec[i] = NULL;
269 unmark_for_gc (obj, local_ref_table);
270 return;
274 // Don't go past a marked frame.
275 JvAssert (frame->marker == MARK_NONE);
278 JvAssert (0);
281 static jint JNICALL
282 _Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
284 // It is easier to just always allocate a new frame of the requested
285 // size. This isn't the most efficient thing, but for now we don't
286 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
288 _Jv_JNI_LocalFrame *frame;
291 frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
292 + size * sizeof (jobject));
294 catch (jthrowable t)
296 env->ex = t;
297 return JNI_ERR;
300 frame->marker = MARK_NONE;
301 frame->size = size;
302 frame->allocated_p = 0;
303 memset (&frame->vec[0], 0, size * sizeof (jobject));
304 frame->next = env->locals;
305 env->locals = frame;
307 return 0;
310 static jint JNICALL
311 _Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
313 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
314 if (r < 0)
315 return r;
317 // The new frame is on top.
318 env->locals->marker = MARK_USER;
320 return 0;
323 static jobject JNICALL
324 _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
326 // This seems weird but I think it is correct.
327 obj = unwrap (obj);
329 // Try to find an open slot somewhere in the topmost frame.
330 _Jv_JNI_LocalFrame *frame = env->locals;
331 bool done = false, set = false;
332 for (; frame != NULL && ! done; frame = frame->next)
334 for (int i = 0; i < frame->size; ++i)
336 if (frame->vec[i] == NULL)
338 set = true;
339 done = true;
340 frame->vec[i] = obj;
341 frame->allocated_p = 1;
342 break;
346 // If we found a slot, or if the frame we just searched is the
347 // mark frame, then we are done.
348 if (done || frame == NULL || frame->marker != MARK_NONE)
349 break;
352 if (! set)
354 // No slots, so we allocate a new frame. According to the spec
355 // we could just die here. FIXME: return value.
356 _Jv_JNI_EnsureLocalCapacity (env, 16);
357 // We know the first element of the new frame will be ok.
358 env->locals->vec[0] = obj;
359 env->locals->allocated_p = 1;
362 mark_for_gc (obj, local_ref_table);
363 return obj;
366 static jobject JNICALL
367 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
369 _Jv_JNI_LocalFrame *rf = env->locals;
371 bool done = false;
372 while (rf != NULL && ! done)
374 for (int i = 0; i < rf->size; ++i)
375 if (rf->vec[i] != NULL)
376 unmark_for_gc (rf->vec[i], local_ref_table);
378 // If the frame we just freed is the marker frame, we are done.
379 done = (rf->marker == stop);
381 _Jv_JNI_LocalFrame *n = rf->next;
382 // When N==NULL, we've reached the reusable bottom_locals, and we must
383 // not free it. However, we must be sure to clear all its elements.
384 if (n == NULL)
386 if (rf->allocated_p)
387 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
388 rf->allocated_p = 0;
389 rf = NULL;
390 break;
393 _Jv_Free (rf);
394 rf = n;
397 // Update the local frame information.
398 env->locals = rf;
400 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
403 static jobject JNICALL
404 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
406 return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
409 // Make sure an array's type is compatible with the type of the
410 // destination.
411 template<typename T>
412 static bool
413 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
415 jclass klass = array->getClass()->getComponentType();
416 if (__builtin_expect (klass != K, false))
418 env->ex = new java::lang::IllegalAccessError ();
419 return false;
421 else
422 return true;
425 // Pop a `system' frame from the stack. This is `extern "C"' as it is
426 // used by the compiler.
427 extern "C" void
428 _Jv_JNI_PopSystemFrame (JNIEnv *env)
430 // Only enter slow path when we're not at the bottom, or there have been
431 // allocations. Usually this is false and we can just null out the locals
432 // field.
434 if (__builtin_expect ((env->locals->next
435 || env->locals->allocated_p), false))
436 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
437 else
438 env->locals = NULL;
440 if (__builtin_expect (env->ex != NULL, false))
442 jthrowable t = env->ex;
443 env->ex = NULL;
444 throw t;
448 template<typename T> T extract_from_jvalue(jvalue const & t);
449 template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
450 template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
451 template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
452 template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
453 template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
454 template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
455 template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
456 template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
457 template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
460 // This function is used from other template functions. It wraps the
461 // return value appropriately; we specialize it so that object returns
462 // are turned into local references.
463 template<typename T>
464 static T
465 wrap_value (JNIEnv *, T value)
467 return value;
470 // This specialization is used for jobject, jclass, jstring, jarray,
471 // etc.
472 template<typename R, typename T>
473 static T *
474 wrap_value (JNIEnv *env, T *value)
476 return (value == NULL
477 ? value
478 : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
483 static jint JNICALL
484 _Jv_JNI_GetVersion (JNIEnv *)
486 return JNI_VERSION_1_4;
489 static jclass JNICALL
490 _Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
491 const jbyte *buf, jsize bufLen)
495 loader = unwrap (loader);
497 jstring sname = JvNewStringUTF (name);
498 jbyteArray bytes = JvNewByteArray (bufLen);
500 jbyte *elts = elements (bytes);
501 memcpy (elts, buf, bufLen * sizeof (jbyte));
503 java::lang::ClassLoader *l
504 = reinterpret_cast<java::lang::ClassLoader *> (loader);
506 jclass result = l->defineClass (sname, bytes, 0, bufLen);
507 return (jclass) wrap_value (env, result);
509 catch (jthrowable t)
511 env->ex = t;
512 return NULL;
516 static jclass JNICALL
517 _Jv_JNI_FindClass (JNIEnv *env, const char *name)
519 // FIXME: assume that NAME isn't too long.
520 int len = strlen (name);
521 char s[len + 1];
522 for (int i = 0; i <= len; ++i)
523 s[i] = (name[i] == '/') ? '.' : name[i];
525 jclass r = NULL;
528 // This might throw an out of memory exception.
529 jstring n = JvNewStringUTF (s);
531 java::lang::ClassLoader *loader = NULL;
532 if (env->klass != NULL)
533 loader = env->klass->getClassLoaderInternal ();
535 if (loader == NULL)
537 // FIXME: should use getBaseClassLoader, but we don't have that
538 // yet.
539 loader = java::lang::ClassLoader::getSystemClassLoader ();
542 r = loader->loadClass (n);
544 catch (jthrowable t)
546 env->ex = t;
549 return (jclass) wrap_value (env, r);
552 static jclass JNICALL
553 _Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
555 return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
558 static jboolean JNICALL
559 _Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
561 return unwrap (clazz2)->isAssignableFrom (unwrap (clazz1));
564 static jint JNICALL
565 _Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
567 // We check in case the user did some funky cast.
568 obj = unwrap (obj);
569 JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
570 env->ex = obj;
571 return 0;
574 static jint JNICALL
575 _Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
577 using namespace java::lang::reflect;
579 clazz = unwrap (clazz);
580 JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
582 int r = JNI_OK;
585 JArray<jclass> *argtypes
586 = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
587 NULL);
589 jclass *elts = elements (argtypes);
590 elts[0] = &java::lang::String::class$;
592 Constructor *cons = clazz->getConstructor (argtypes);
594 jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
595 NULL);
596 jobject *velts = elements (values);
597 velts[0] = JvNewStringUTF (message);
599 jobject obj = cons->newInstance (values);
601 env->ex = reinterpret_cast<jthrowable> (obj);
603 catch (jthrowable t)
605 env->ex = t;
606 r = JNI_ERR;
609 return r;
612 static jthrowable JNICALL
613 _Jv_JNI_ExceptionOccurred (JNIEnv *env)
615 return (jthrowable) wrap_value (env, env->ex);
618 static void JNICALL
619 _Jv_JNI_ExceptionDescribe (JNIEnv *env)
621 if (env->ex != NULL)
622 env->ex->printStackTrace();
625 static void JNICALL
626 _Jv_JNI_ExceptionClear (JNIEnv *env)
628 env->ex = NULL;
631 static jboolean JNICALL
632 _Jv_JNI_ExceptionCheck (JNIEnv *env)
634 return env->ex != NULL;
637 static void JNICALL
638 _Jv_JNI_FatalError (JNIEnv *, const char *message)
640 JvFail (message);
645 static jboolean JNICALL
646 _Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
648 return unwrap (obj1) == unwrap (obj2);
651 static jobject JNICALL
652 _Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
654 jobject obj = NULL;
655 using namespace java::lang::reflect;
659 clazz = unwrap (clazz);
660 JvAssert (clazz && ! clazz->isArray ());
661 if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
662 env->ex = new java::lang::InstantiationException ();
663 else
664 obj = _Jv_AllocObject (clazz);
666 catch (jthrowable t)
668 env->ex = t;
671 return wrap_value (env, obj);
674 static jclass JNICALL
675 _Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
677 obj = unwrap (obj);
678 JvAssert (obj);
679 return (jclass) wrap_value (env, obj->getClass());
682 static jboolean JNICALL
683 _Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
685 return unwrap (clazz)->isInstance(unwrap (obj));
691 // This section concerns method invocation.
694 template<jboolean is_static>
695 static jmethodID JNICALL
696 _Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
697 const char *name, const char *sig)
701 clazz = unwrap (clazz);
702 _Jv_InitClass (clazz);
704 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
706 // FIXME: assume that SIG isn't too long.
707 int len = strlen (sig);
708 char s[len + 1];
709 for (int i = 0; i <= len; ++i)
710 s[i] = (sig[i] == '/') ? '.' : sig[i];
711 _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
713 JvAssert (! clazz->isPrimitive());
715 using namespace java::lang::reflect;
717 while (clazz != NULL)
719 jint count = JvNumMethods (clazz);
720 jmethodID meth = JvGetFirstMethod (clazz);
722 for (jint i = 0; i < count; ++i)
724 if (((is_static && Modifier::isStatic (meth->accflags))
725 || (! is_static && ! Modifier::isStatic (meth->accflags)))
726 && _Jv_equalUtf8Consts (meth->name, name_u)
727 && _Jv_equalUtf8Consts (meth->signature, sig_u))
728 return meth;
730 meth = meth->getNextMethod();
733 clazz = clazz->getSuperclass ();
736 java::lang::StringBuffer *name_sig =
737 new java::lang::StringBuffer (JvNewStringUTF (name));
738 name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
739 env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
741 catch (jthrowable t)
743 env->ex = t;
746 return NULL;
749 // This is a helper function which turns a va_list into an array of
750 // `jvalue's. It needs signature information in order to do its work.
751 // The array of values must already be allocated.
752 static void
753 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
755 jclass *arg_elts = elements (arg_types);
756 for (int i = 0; i < arg_types->length; ++i)
758 // Here we assume that sizeof(int) >= sizeof(jint), because we
759 // use `int' when decoding the varargs. Likewise for
760 // float, and double. Also we assume that sizeof(jlong) >=
761 // sizeof(int), i.e. that jlong values are not further
762 // promoted.
763 JvAssert (sizeof (int) >= sizeof (jint));
764 JvAssert (sizeof (jlong) >= sizeof (int));
765 JvAssert (sizeof (double) >= sizeof (jfloat));
766 JvAssert (sizeof (double) >= sizeof (jdouble));
767 if (arg_elts[i] == JvPrimClass (byte))
768 values[i].b = (jbyte) va_arg (vargs, int);
769 else if (arg_elts[i] == JvPrimClass (short))
770 values[i].s = (jshort) va_arg (vargs, int);
771 else if (arg_elts[i] == JvPrimClass (int))
772 values[i].i = (jint) va_arg (vargs, int);
773 else if (arg_elts[i] == JvPrimClass (long))
774 values[i].j = (jlong) va_arg (vargs, jlong);
775 else if (arg_elts[i] == JvPrimClass (float))
776 values[i].f = (jfloat) va_arg (vargs, double);
777 else if (arg_elts[i] == JvPrimClass (double))
778 values[i].d = (jdouble) va_arg (vargs, double);
779 else if (arg_elts[i] == JvPrimClass (boolean))
780 values[i].z = (jboolean) va_arg (vargs, int);
781 else if (arg_elts[i] == JvPrimClass (char))
782 values[i].c = (jchar) va_arg (vargs, int);
783 else
785 // An object.
786 values[i].l = unwrap (va_arg (vargs, jobject));
791 // This can call any sort of method: virtual, "nonvirtual", static, or
792 // constructor.
793 template<typename T, invocation_type style>
794 static T JNICALL
795 _Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
796 jmethodID id, va_list vargs)
798 obj = unwrap (obj);
799 klass = unwrap (klass);
801 jclass decl_class = klass ? klass : obj->getClass ();
802 JvAssert (decl_class != NULL);
804 jclass return_type;
805 JArray<jclass> *arg_types;
809 _Jv_GetTypesFromSignature (id, decl_class,
810 &arg_types, &return_type);
812 jvalue args[arg_types->length];
813 array_from_valist (args, arg_types, vargs);
815 // For constructors we need to pass the Class we are instantiating.
816 if (style == constructor)
817 return_type = klass;
819 jvalue result;
820 _Jv_CallAnyMethodA (obj, return_type, id,
821 style == constructor,
822 style == normal,
823 arg_types, args, &result);
825 return wrap_value (env, extract_from_jvalue<T>(result));
827 catch (jthrowable t)
829 env->ex = t;
832 return wrap_value (env, (T) 0);
835 template<typename T, invocation_type style>
836 static T JNICALL
837 _Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
838 jmethodID method, ...)
840 va_list args;
841 T result;
843 va_start (args, method);
844 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
845 va_end (args);
847 return result;
850 template<typename T, invocation_type style>
851 static T JNICALL
852 _Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
853 jmethodID id, jvalue *args)
855 obj = unwrap (obj);
856 klass = unwrap (klass);
858 jclass decl_class = klass ? klass : obj->getClass ();
859 JvAssert (decl_class != NULL);
861 jclass return_type;
862 JArray<jclass> *arg_types;
865 _Jv_GetTypesFromSignature (id, decl_class,
866 &arg_types, &return_type);
868 // For constructors we need to pass the Class we are instantiating.
869 if (style == constructor)
870 return_type = klass;
872 // Unwrap arguments as required. Eww.
873 jclass *type_elts = elements (arg_types);
874 jvalue arg_copy[arg_types->length];
875 for (int i = 0; i < arg_types->length; ++i)
877 if (type_elts[i]->isPrimitive ())
878 arg_copy[i] = args[i];
879 else
880 arg_copy[i].l = unwrap (args[i].l);
883 jvalue result;
884 _Jv_CallAnyMethodA (obj, return_type, id,
885 style == constructor,
886 style == normal,
887 arg_types, arg_copy, &result);
889 return wrap_value (env, extract_from_jvalue<T>(result));
891 catch (jthrowable t)
893 env->ex = t;
896 return wrap_value (env, (T) 0);
899 template<invocation_type style>
900 static void JNICALL
901 _Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
902 jmethodID id, va_list vargs)
904 obj = unwrap (obj);
905 klass = unwrap (klass);
907 jclass decl_class = klass ? klass : obj->getClass ();
908 JvAssert (decl_class != NULL);
910 jclass return_type;
911 JArray<jclass> *arg_types;
914 _Jv_GetTypesFromSignature (id, decl_class,
915 &arg_types, &return_type);
917 jvalue args[arg_types->length];
918 array_from_valist (args, arg_types, vargs);
920 // For constructors we need to pass the Class we are instantiating.
921 if (style == constructor)
922 return_type = klass;
924 _Jv_CallAnyMethodA (obj, return_type, id,
925 style == constructor,
926 style == normal,
927 arg_types, args, NULL);
929 catch (jthrowable t)
931 env->ex = t;
935 template<invocation_type style>
936 static void JNICALL
937 _Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
938 jmethodID method, ...)
940 va_list args;
942 va_start (args, method);
943 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
944 va_end (args);
947 template<invocation_type style>
948 static void JNICALL
949 _Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
950 jmethodID id, jvalue *args)
952 jclass decl_class = klass ? klass : obj->getClass ();
953 JvAssert (decl_class != NULL);
955 jclass return_type;
956 JArray<jclass> *arg_types;
959 _Jv_GetTypesFromSignature (id, decl_class,
960 &arg_types, &return_type);
962 // Unwrap arguments as required. Eww.
963 jclass *type_elts = elements (arg_types);
964 jvalue arg_copy[arg_types->length];
965 for (int i = 0; i < arg_types->length; ++i)
967 if (type_elts[i]->isPrimitive ())
968 arg_copy[i] = args[i];
969 else
970 arg_copy[i].l = unwrap (args[i].l);
973 _Jv_CallAnyMethodA (obj, return_type, id,
974 style == constructor,
975 style == normal,
976 arg_types, args, NULL);
978 catch (jthrowable t)
980 env->ex = t;
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_CallMethodV (JNIEnv *env, jobject obj,
989 jmethodID id, va_list args)
991 return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
994 // Functions with this signature are used to implement functions in
995 // the CallMethod family.
996 template<typename T>
997 static T JNICALL
998 _Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1000 va_list args;
1001 T result;
1003 va_start (args, id);
1004 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
1005 va_end (args);
1007 return result;
1010 // Functions with this signature are used to implement functions in
1011 // the CallMethod family.
1012 template<typename T>
1013 static T JNICALL
1014 _Jv_JNI_CallMethodA (JNIEnv *env, jobject obj,
1015 jmethodID id, jvalue *args)
1017 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
1020 static void JNICALL
1021 _Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj,
1022 jmethodID id, va_list args)
1024 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1027 static void JNICALL
1028 _Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1030 va_list args;
1032 va_start (args, id);
1033 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1034 va_end (args);
1037 static void JNICALL
1038 _Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj,
1039 jmethodID id, jvalue *args)
1041 _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1044 // Functions with this signature are used to implement functions in
1045 // the CallStaticMethod family.
1046 template<typename T>
1047 static T JNICALL
1048 _Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
1049 jmethodID id, va_list args)
1051 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1052 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1054 return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1057 // Functions with this signature are used to implement functions in
1058 // the CallStaticMethod family.
1059 template<typename T>
1060 static T JNICALL
1061 _Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass,
1062 jmethodID id, ...)
1064 va_list args;
1065 T result;
1067 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1068 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1070 va_start (args, id);
1071 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1072 id, args);
1073 va_end (args);
1075 return result;
1078 // Functions with this signature are used to implement functions in
1079 // the CallStaticMethod family.
1080 template<typename T>
1081 static T JNICALL
1082 _Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
1083 jvalue *args)
1085 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1086 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1088 return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1091 static void JNICALL
1092 _Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass,
1093 jmethodID id, va_list args)
1095 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1098 static void JNICALL
1099 _Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass,
1100 jmethodID id, ...)
1102 va_list args;
1104 va_start (args, id);
1105 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1106 va_end (args);
1109 static void JNICALL
1110 _Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass,
1111 jmethodID id, jvalue *args)
1113 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1116 static jobject JNICALL
1117 _Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
1118 jmethodID id, va_list args)
1120 JvAssert (klass && ! klass->isArray ());
1121 JvAssert (! strcmp (id->name->chars(), "<init>")
1122 && id->signature->len() > 2
1123 && id->signature->chars()[0] == '('
1124 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1125 ")V"));
1127 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1128 id, args);
1131 static jobject JNICALL
1132 _Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
1134 JvAssert (klass && ! klass->isArray ());
1135 JvAssert (! strcmp (id->name->chars(), "<init>")
1136 && id->signature->len() > 2
1137 && id->signature->chars()[0] == '('
1138 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1139 ")V"));
1141 va_list args;
1142 jobject result;
1144 va_start (args, id);
1145 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1146 id, args);
1147 va_end (args);
1149 return result;
1152 static jobject JNICALL
1153 _Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
1154 jvalue *args)
1156 JvAssert (klass && ! klass->isArray ());
1157 JvAssert (! strcmp (id->name->chars(), "<init>")
1158 && id->signature->len() > 2
1159 && id->signature->chars()[0] == '('
1160 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1161 ")V"));
1163 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1164 id, args);
1169 template<typename T>
1170 static T JNICALL
1171 _Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
1173 obj = unwrap (obj);
1174 JvAssert (obj);
1175 T *ptr = (T *) ((char *) obj + field->getOffset ());
1176 return wrap_value (env, *ptr);
1179 template<typename T>
1180 static void JNICALL
1181 _Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
1183 obj = unwrap (obj);
1184 value = unwrap (value);
1186 JvAssert (obj);
1187 T *ptr = (T *) ((char *) obj + field->getOffset ());
1188 *ptr = value;
1191 template<jboolean is_static>
1192 static jfieldID JNICALL
1193 _Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
1194 const char *name, const char *sig)
1198 clazz = unwrap (clazz);
1200 _Jv_InitClass (clazz);
1202 _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1204 // FIXME: assume that SIG isn't too long.
1205 int len = strlen (sig);
1206 char s[len + 1];
1207 for (int i = 0; i <= len; ++i)
1208 s[i] = (sig[i] == '/') ? '.' : sig[i];
1209 java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1210 jclass field_class = _Jv_FindClassFromSignature ((char *) s, loader);
1211 if (! field_class)
1212 throw new java::lang::ClassNotFoundException(JvNewStringUTF(s));
1214 while (clazz != NULL)
1216 // We acquire the class lock so that fields aren't resolved
1217 // while we are running.
1218 JvSynchronize sync (clazz);
1220 jint count = (is_static
1221 ? JvNumStaticFields (clazz)
1222 : JvNumInstanceFields (clazz));
1223 jfieldID field = (is_static
1224 ? JvGetFirstStaticField (clazz)
1225 : JvGetFirstInstanceField (clazz));
1226 for (jint i = 0; i < count; ++i)
1228 _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1230 // The field might be resolved or it might not be. It
1231 // is much simpler to always resolve it.
1232 _Jv_Linker::resolve_field (field, loader);
1233 if (_Jv_equalUtf8Consts (f_name, a_name)
1234 && field->getClass() == field_class)
1235 return field;
1237 field = field->getNextField ();
1240 clazz = clazz->getSuperclass ();
1243 env->ex = new java::lang::NoSuchFieldError ();
1245 catch (jthrowable t)
1247 env->ex = t;
1249 return NULL;
1252 template<typename T>
1253 static T JNICALL
1254 _Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
1256 T *ptr = (T *) field->u.addr;
1257 return wrap_value (env, *ptr);
1260 template<typename T>
1261 static void JNICALL
1262 _Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
1264 value = unwrap (value);
1265 T *ptr = (T *) field->u.addr;
1266 *ptr = value;
1269 static jstring JNICALL
1270 _Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
1274 jstring r = _Jv_NewString (unichars, len);
1275 return (jstring) wrap_value (env, r);
1277 catch (jthrowable t)
1279 env->ex = t;
1280 return NULL;
1284 static jsize JNICALL
1285 _Jv_JNI_GetStringLength (JNIEnv *, jstring string)
1287 return unwrap (string)->length();
1290 static const jchar * JNICALL
1291 _Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
1293 string = unwrap (string);
1294 jchar *result = _Jv_GetStringChars (string);
1295 mark_for_gc (string, global_ref_table);
1296 if (isCopy)
1297 *isCopy = false;
1298 return (const jchar *) result;
1301 static void JNICALL
1302 _Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
1304 unmark_for_gc (unwrap (string), global_ref_table);
1307 static jstring JNICALL
1308 _Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
1312 jstring result = JvNewStringUTF (bytes);
1313 return (jstring) wrap_value (env, result);
1315 catch (jthrowable t)
1317 env->ex = t;
1318 return NULL;
1322 static jsize JNICALL
1323 _Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
1325 return JvGetStringUTFLength (unwrap (string));
1328 static const char * JNICALL
1329 _Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string,
1330 jboolean *isCopy)
1334 string = unwrap (string);
1335 if (string == NULL)
1336 return NULL;
1337 jsize len = JvGetStringUTFLength (string);
1338 char *r = (char *) _Jv_Malloc (len + 1);
1339 JvGetStringUTFRegion (string, 0, string->length(), r);
1340 r[len] = '\0';
1342 if (isCopy)
1343 *isCopy = true;
1345 return (const char *) r;
1347 catch (jthrowable t)
1349 env->ex = t;
1350 return NULL;
1354 static void JNICALL
1355 _Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
1357 _Jv_Free ((void *) utf);
1360 static void JNICALL
1361 _Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start,
1362 jsize len, jchar *buf)
1364 string = unwrap (string);
1365 jchar *result = _Jv_GetStringChars (string);
1366 if (start < 0 || start > string->length ()
1367 || len < 0 || start + len > string->length ())
1371 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1373 catch (jthrowable t)
1375 env->ex = t;
1378 else
1379 memcpy (buf, &result[start], len * sizeof (jchar));
1382 static void JNICALL
1383 _Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
1384 jsize len, char *buf)
1386 str = unwrap (str);
1388 if (start < 0 || start > str->length ()
1389 || len < 0 || start + len > str->length ())
1393 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1395 catch (jthrowable t)
1397 env->ex = t;
1400 else
1401 _Jv_GetStringUTFRegion (str, start, len, buf);
1404 static const jchar * JNICALL
1405 _Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
1407 jchar *result = _Jv_GetStringChars (unwrap (str));
1408 if (isCopy)
1409 *isCopy = false;
1410 return result;
1413 static void JNICALL
1414 _Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
1416 // Nothing.
1419 static jsize JNICALL
1420 _Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
1422 return unwrap (array)->length;
1425 static jobjectArray JNICALL
1426 _Jv_JNI_NewObjectArray (JNIEnv *env, jsize length,
1427 jclass elementClass, jobject init)
1431 elementClass = unwrap (elementClass);
1432 init = unwrap (init);
1434 _Jv_CheckCast (elementClass, init);
1435 jarray result = JvNewObjectArray (length, elementClass, init);
1436 return (jobjectArray) wrap_value (env, result);
1438 catch (jthrowable t)
1440 env->ex = t;
1441 return NULL;
1445 static jobject JNICALL
1446 _Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array,
1447 jsize index)
1449 if ((unsigned) index >= (unsigned) array->length)
1450 _Jv_ThrowBadArrayIndex (index);
1451 jobject *elts = elements (unwrap (array));
1452 return wrap_value (env, elts[index]);
1455 static void JNICALL
1456 _Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array,
1457 jsize index, jobject value)
1461 array = unwrap (array);
1462 value = unwrap (value);
1464 _Jv_CheckArrayStore (array, value);
1465 if ((unsigned) index >= (unsigned) array->length)
1466 _Jv_ThrowBadArrayIndex (index);
1467 jobject *elts = elements (array);
1468 elts[index] = value;
1470 catch (jthrowable t)
1472 env->ex = t;
1476 template<typename T, jclass K>
1477 static JArray<T> * JNICALL
1478 _Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
1482 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1484 catch (jthrowable t)
1486 env->ex = t;
1487 return NULL;
1491 template<typename T, jclass K>
1492 static T * JNICALL
1493 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1494 jboolean *isCopy)
1496 array = unwrap (array);
1497 if (! _Jv_JNI_check_types (env, array, K))
1498 return NULL;
1499 T *elts = elements (array);
1500 if (isCopy)
1502 // We elect never to copy.
1503 *isCopy = false;
1505 mark_for_gc (array, global_ref_table);
1506 return elts;
1509 template<typename T, jclass K>
1510 static void JNICALL
1511 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1512 T *, jint /* mode */)
1514 array = unwrap (array);
1515 _Jv_JNI_check_types (env, array, K);
1516 // Note that we ignore MODE. We can do this because we never copy
1517 // the array elements. My reading of the JNI documentation is that
1518 // this is an option for the implementor.
1519 unmark_for_gc (array, global_ref_table);
1522 template<typename T, jclass K>
1523 static void JNICALL
1524 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1525 jsize start, jsize len,
1526 T *buf)
1528 array = unwrap (array);
1529 if (! _Jv_JNI_check_types (env, array, K))
1530 return;
1532 // The cast to unsigned lets us save a comparison.
1533 if (start < 0 || len < 0
1534 || (unsigned long) (start + len) > (unsigned long) array->length)
1538 // FIXME: index.
1539 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1541 catch (jthrowable t)
1543 // Could have thown out of memory error.
1544 env->ex = t;
1547 else
1549 T *elts = elements (array) + start;
1550 memcpy (buf, elts, len * sizeof (T));
1554 template<typename T, jclass K>
1555 static void JNICALL
1556 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1557 jsize start, jsize len, T *buf)
1559 array = unwrap (array);
1560 if (! _Jv_JNI_check_types (env, array, K))
1561 return;
1563 // The cast to unsigned lets us save a comparison.
1564 if (start < 0 || len < 0
1565 || (unsigned long) (start + len) > (unsigned long) array->length)
1569 // FIXME: index.
1570 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1572 catch (jthrowable t)
1574 env->ex = t;
1577 else
1579 T *elts = elements (array) + start;
1580 memcpy (elts, buf, len * sizeof (T));
1584 static void * JNICALL
1585 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
1586 jboolean *isCopy)
1588 array = unwrap (array);
1589 // FIXME: does this work?
1590 jclass klass = array->getClass()->getComponentType();
1591 JvAssert (klass->isPrimitive ());
1592 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1593 if (isCopy)
1594 *isCopy = false;
1595 return r;
1598 static void JNICALL
1599 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
1601 // Nothing.
1604 static jint JNICALL
1605 _Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
1609 _Jv_MonitorEnter (unwrap (obj));
1610 return 0;
1612 catch (jthrowable t)
1614 env->ex = t;
1616 return JNI_ERR;
1619 static jint JNICALL
1620 _Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
1624 _Jv_MonitorExit (unwrap (obj));
1625 return 0;
1627 catch (jthrowable t)
1629 env->ex = t;
1631 return JNI_ERR;
1634 // JDK 1.2
1635 jobject JNICALL
1636 _Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
1637 jboolean)
1641 cls = unwrap (cls);
1642 java::lang::reflect::Field *field = new java::lang::reflect::Field();
1643 field->declaringClass = cls;
1644 field->offset = (char*) fieldID - (char *) cls->fields;
1645 field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1646 return wrap_value (env, field);
1648 catch (jthrowable t)
1650 env->ex = t;
1652 return NULL;
1655 // JDK 1.2
1656 static jfieldID JNICALL
1657 _Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
1659 using namespace java::lang::reflect;
1661 f = unwrap (f);
1662 Field *field = reinterpret_cast<Field *> (f);
1663 return _Jv_FromReflectedField (field);
1666 jobject JNICALL
1667 _Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
1668 jboolean)
1670 using namespace java::lang::reflect;
1672 jobject result = NULL;
1673 klass = unwrap (klass);
1677 if (_Jv_equalUtf8Consts (id->name, init_name))
1679 // A constructor.
1680 Constructor *cons = new Constructor ();
1681 cons->offset = (char *) id - (char *) &klass->methods;
1682 cons->declaringClass = klass;
1683 result = cons;
1685 else
1687 Method *meth = new Method ();
1688 meth->offset = (char *) id - (char *) &klass->methods;
1689 meth->declaringClass = klass;
1690 result = meth;
1693 catch (jthrowable t)
1695 env->ex = t;
1698 return wrap_value (env, result);
1701 static jmethodID JNICALL
1702 _Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
1704 using namespace java::lang::reflect;
1705 method = unwrap (method);
1706 if (Method::class$.isInstance (method))
1707 return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1708 return
1709 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1712 // JDK 1.2.
1713 jweak JNICALL
1714 _Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
1716 using namespace gnu::gcj::runtime;
1717 JNIWeakRef *ref = NULL;
1721 // This seems weird but I think it is correct.
1722 obj = unwrap (obj);
1723 ref = new JNIWeakRef (obj);
1724 mark_for_gc (ref, global_ref_table);
1726 catch (jthrowable t)
1728 env->ex = t;
1731 return reinterpret_cast<jweak> (ref);
1734 void JNICALL
1735 _Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
1737 using namespace gnu::gcj::runtime;
1738 JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1739 unmark_for_gc (ref, global_ref_table);
1740 ref->clear ();
1745 // Direct byte buffers.
1747 static jobject JNICALL
1748 _Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
1750 using namespace gnu::gcj;
1751 using namespace java::nio;
1752 return new DirectByteBufferImpl$ReadWrite
1753 (reinterpret_cast<RawData *> (address), length);
1756 static void * JNICALL
1757 _Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
1759 using namespace java::nio;
1760 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1761 return NULL;
1762 Buffer *tmp = static_cast<Buffer *> (buffer);
1763 return reinterpret_cast<void *> (tmp->address);
1766 static jlong JNICALL
1767 _Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
1769 using namespace java::nio;
1770 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1771 return -1;
1772 Buffer *tmp = static_cast<Buffer *> (buffer);
1773 if (tmp->address == NULL)
1774 return -1;
1775 return tmp->capacity();
1780 // Hash table of native methods.
1781 static JNINativeMethod *nathash;
1782 // Number of slots used.
1783 static int nathash_count = 0;
1784 // Number of slots available. Must be power of 2.
1785 static int nathash_size = 0;
1787 #define DELETED_ENTRY ((char *) (~0))
1789 // Compute a hash value for a native method descriptor.
1790 static int
1791 hash (const JNINativeMethod *method)
1793 char *ptr;
1794 int hash = 0;
1796 ptr = method->name;
1797 while (*ptr)
1798 hash = (31 * hash) + *ptr++;
1800 ptr = method->signature;
1801 while (*ptr)
1802 hash = (31 * hash) + *ptr++;
1804 return hash;
1807 // Find the slot where a native method goes.
1808 static JNINativeMethod *
1809 nathash_find_slot (const JNINativeMethod *method)
1811 jint h = hash (method);
1812 int step = (h ^ (h >> 16)) | 1;
1813 int w = h & (nathash_size - 1);
1814 int del = -1;
1816 for (;;)
1818 JNINativeMethod *slotp = &nathash[w];
1819 if (slotp->name == NULL)
1821 if (del >= 0)
1822 return &nathash[del];
1823 else
1824 return slotp;
1826 else if (slotp->name == DELETED_ENTRY)
1827 del = w;
1828 else if (! strcmp (slotp->name, method->name)
1829 && ! strcmp (slotp->signature, method->signature))
1830 return slotp;
1831 w = (w + step) & (nathash_size - 1);
1835 // Find a method. Return NULL if it isn't in the hash table.
1836 static void *
1837 nathash_find (JNINativeMethod *method)
1839 if (nathash == NULL)
1840 return NULL;
1841 JNINativeMethod *slot = nathash_find_slot (method);
1842 if (slot->name == NULL || slot->name == DELETED_ENTRY)
1843 return NULL;
1844 return slot->fnPtr;
1847 static void
1848 natrehash ()
1850 if (nathash == NULL)
1852 nathash_size = 1024;
1853 nathash =
1854 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1855 * sizeof (JNINativeMethod));
1857 else
1859 int savesize = nathash_size;
1860 JNINativeMethod *savehash = nathash;
1861 nathash_size *= 2;
1862 nathash =
1863 (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1864 * sizeof (JNINativeMethod));
1866 for (int i = 0; i < savesize; ++i)
1868 if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1870 JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
1871 *slot = savehash[i];
1877 static void
1878 nathash_add (const JNINativeMethod *method)
1880 if (3 * nathash_count >= 2 * nathash_size)
1881 natrehash ();
1882 JNINativeMethod *slot = nathash_find_slot (method);
1883 // If the slot has a real entry in it, then there is no work to do.
1884 if (slot->name != NULL && slot->name != DELETED_ENTRY)
1885 return;
1886 // FIXME
1887 slot->name = strdup (method->name);
1888 // This was already strduped in _Jv_JNI_RegisterNatives.
1889 slot->signature = method->signature;
1890 slot->fnPtr = method->fnPtr;
1893 static jint JNICALL
1894 _Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
1895 const JNINativeMethod *methods,
1896 jint nMethods)
1898 // Synchronize while we do the work. This must match
1899 // synchronization in some other functions that manipulate or use
1900 // the nathash table.
1901 JvSynchronize sync (global_ref_table);
1903 JNINativeMethod dottedMethod;
1905 // Look at each descriptor given us, and find the corresponding
1906 // method in the class.
1907 for (int j = 0; j < nMethods; ++j)
1909 bool found = false;
1911 _Jv_Method *imeths = JvGetFirstMethod (klass);
1912 for (int i = 0; i < JvNumMethods (klass); ++i)
1914 _Jv_Method *self = &imeths[i];
1916 // Copy this JNINativeMethod and do a slash to dot
1917 // conversion on the signature.
1918 dottedMethod.name = methods[j].name;
1919 dottedMethod.signature = strdup (methods[j].signature);
1920 dottedMethod.fnPtr = methods[j].fnPtr;
1921 char *c = dottedMethod.signature;
1922 while (*c)
1924 if (*c == '/')
1925 *c = '.';
1926 c++;
1929 if (! strcmp (self->name->chars (), dottedMethod.name)
1930 && ! strcmp (self->signature->chars (), dottedMethod.signature))
1932 if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
1933 break;
1935 // Found a match that is native.
1936 found = true;
1937 nathash_add (&dottedMethod);
1939 break;
1943 if (! found)
1945 jstring m = JvNewStringUTF (methods[j].name);
1948 env->ex = new java::lang::NoSuchMethodError (m);
1950 catch (jthrowable t)
1952 env->ex = t;
1954 return JNI_ERR;
1958 return JNI_OK;
1961 static jint JNICALL
1962 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1964 // FIXME -- we could implement this.
1965 return JNI_ERR;
1970 // Add a character to the buffer, encoding properly.
1971 static void
1972 add_char (char *buf, jchar c, int *here)
1974 if (c == '_')
1976 buf[(*here)++] = '_';
1977 buf[(*here)++] = '1';
1979 else if (c == ';')
1981 buf[(*here)++] = '_';
1982 buf[(*here)++] = '2';
1984 else if (c == '[')
1986 buf[(*here)++] = '_';
1987 buf[(*here)++] = '3';
1990 // Also check for `.' here because we might be passed an internal
1991 // qualified class name like `foo.bar'.
1992 else if (c == '/' || c == '.')
1993 buf[(*here)++] = '_';
1994 else if ((c >= '0' && c <= '9')
1995 || (c >= 'a' && c <= 'z')
1996 || (c >= 'A' && c <= 'Z'))
1997 buf[(*here)++] = (char) c;
1998 else
2000 // "Unicode" character.
2001 buf[(*here)++] = '_';
2002 buf[(*here)++] = '0';
2003 for (int i = 0; i < 4; ++i)
2005 int val = c & 0x0f;
2006 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
2007 c >>= 4;
2009 *here += 4;
2013 // Compute a mangled name for a native function. This computes the
2014 // long name, and also returns an index which indicates where a NUL
2015 // can be placed to create the short name. This function assumes that
2016 // the buffer is large enough for its results.
2017 static void
2018 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
2019 _Jv_Utf8Const *signature, char *buf, int *long_start)
2021 strcpy (buf, "Java_");
2022 int here = 5;
2024 // Add fully qualified class name.
2025 jchar *chars = _Jv_GetStringChars (klass->getName ());
2026 jint len = klass->getName ()->length ();
2027 for (int i = 0; i < len; ++i)
2028 add_char (buf, chars[i], &here);
2030 // Don't use add_char because we need a literal `_'.
2031 buf[here++] = '_';
2033 const unsigned char *fn = (const unsigned char *) func_name->chars ();
2034 const unsigned char *limit = fn + func_name->len ();
2035 for (int i = 0; ; ++i)
2037 int ch = UTF8_GET (fn, limit);
2038 if (ch < 0)
2039 break;
2040 add_char (buf, ch, &here);
2043 // This is where the long signature begins.
2044 *long_start = here;
2045 buf[here++] = '_';
2046 buf[here++] = '_';
2048 const unsigned char *sig = (const unsigned char *) signature->chars ();
2049 limit = sig + signature->len ();
2050 JvAssert (sig[0] == '(');
2051 ++sig;
2052 while (1)
2054 int ch = UTF8_GET (sig, limit);
2055 if (ch == ')' || ch < 0)
2056 break;
2057 add_char (buf, ch, &here);
2060 buf[here] = '\0';
2063 // Return the current thread's JNIEnv; if one does not exist, create
2064 // it. Also create a new system frame for use. This is `extern "C"'
2065 // because the compiler calls it.
2066 extern "C" JNIEnv *
2067 _Jv_GetJNIEnvNewFrame (jclass klass)
2069 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2070 if (__builtin_expect (env == NULL, false))
2072 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2073 env->p = &_Jv_JNIFunctions;
2074 env->klass = klass;
2075 env->locals = NULL;
2076 // We set env->ex below.
2078 // Set up the bottom, reusable frame.
2079 env->bottom_locals = (_Jv_JNI_LocalFrame *)
2080 _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2081 + (FRAME_SIZE
2082 * sizeof (jobject)));
2084 env->bottom_locals->marker = MARK_SYSTEM;
2085 env->bottom_locals->size = FRAME_SIZE;
2086 env->bottom_locals->next = NULL;
2087 env->bottom_locals->allocated_p = 0;
2088 memset (&env->bottom_locals->vec[0], 0,
2089 env->bottom_locals->size * sizeof (jobject));
2091 _Jv_SetCurrentJNIEnv (env);
2094 // If we're in a simple JNI call (non-nested), we can just reuse the
2095 // locals frame we allocated many calls ago, back when the env was first
2096 // built, above.
2098 if (__builtin_expect (env->locals == NULL, true))
2099 env->locals = env->bottom_locals;
2101 else
2103 // Alternatively, we might be re-entering JNI, in which case we can't
2104 // reuse the bottom_locals frame, because it is already underneath
2105 // us. So we need to make a new one.
2107 _Jv_JNI_LocalFrame *frame
2108 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2109 + (FRAME_SIZE
2110 * sizeof (jobject)));
2112 frame->marker = MARK_SYSTEM;
2113 frame->size = FRAME_SIZE;
2114 frame->allocated_p = 0;
2115 frame->next = env->locals;
2117 memset (&frame->vec[0], 0,
2118 frame->size * sizeof (jobject));
2120 env->locals = frame;
2123 env->ex = NULL;
2125 return env;
2128 // Destroy the env's reusable resources. This is called from the thread
2129 // destructor "finalize_native" in natThread.cc
2130 void
2131 _Jv_FreeJNIEnv (_Jv_JNIEnv *env)
2133 if (env == NULL)
2134 return;
2136 if (env->bottom_locals != NULL)
2137 _Jv_Free (env->bottom_locals);
2139 _Jv_Free (env);
2142 // Return the function which implements a particular JNI method. If
2143 // we can't find the function, we throw the appropriate exception.
2144 // This is `extern "C"' because the compiler uses it.
2145 extern "C" void *
2146 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2147 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
2149 int name_length = name->len();
2150 int sig_length = signature->len();
2151 char buf[10 + 6 * (name_length + sig_length) + 12];
2152 int long_start;
2153 void *function;
2155 // Synchronize on something convenient. Right now we use the hash.
2156 JvSynchronize sync (global_ref_table);
2158 // First see if we have an override in the hash table.
2159 strncpy (buf, name->chars (), name_length);
2160 buf[name_length] = '\0';
2161 strncpy (buf + name_length + 1, signature->chars (), sig_length);
2162 buf[name_length + sig_length + 1] = '\0';
2163 JNINativeMethod meth;
2164 meth.name = buf;
2165 meth.signature = buf + name_length + 1;
2166 function = nathash_find (&meth);
2167 if (function != NULL)
2168 return function;
2170 // If there was no override, then look in the symbol table.
2171 buf[0] = '_';
2172 mangled_name (klass, name, signature, buf + 1, &long_start);
2173 char c = buf[long_start + 1];
2174 buf[long_start + 1] = '\0';
2176 function = _Jv_FindSymbolInExecutable (buf + 1);
2177 #ifdef WIN32
2178 // On Win32, we use the "stdcall" calling convention (see JNICALL
2179 // in jni.h).
2181 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2182 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2183 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2184 // take care of all these variations here.
2186 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2187 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2189 if (function == NULL)
2191 // We have tried searching for the 'fooBar' form (BCC) - now
2192 // try the others.
2194 // First, save the part of the long name that will be damaged
2195 // by appending '@nn'.
2196 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2198 sprintf (asz_buf, "@%d", args_size);
2199 strcat (buf, asz_buf);
2201 // Search for the '_fooBar@nn' form (MSVC).
2202 function = _Jv_FindSymbolInExecutable (buf);
2204 if (function == NULL)
2206 // Search for the 'fooBar@nn' form (MinGW GCC).
2207 function = _Jv_FindSymbolInExecutable (buf + 1);
2210 #endif /* WIN32 */
2212 if (function == NULL)
2214 buf[long_start + 1] = c;
2215 #ifdef WIN32
2216 // Restore the part of the long name that was damaged by
2217 // appending the '@nn'.
2218 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2219 #endif /* WIN32 */
2220 function = _Jv_FindSymbolInExecutable (buf + 1);
2221 if (function == NULL)
2223 #ifdef WIN32
2224 strcat (buf, asz_buf);
2225 function = _Jv_FindSymbolInExecutable (buf);
2226 if (function == NULL)
2227 function = _Jv_FindSymbolInExecutable (buf + 1);
2229 if (function == NULL)
2230 #endif /* WIN32 */
2232 jstring str = JvNewStringUTF (name->chars ());
2233 throw new java::lang::UnsatisfiedLinkError (str);
2238 return function;
2241 #ifdef INTERPRETER
2243 // This function is the stub which is used to turn an ordinary (CNI)
2244 // method call into a JNI call.
2245 void
2246 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2248 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2250 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2252 // FIXME: we should mark every reference parameter as a local. For
2253 // now we assume a conservative GC, and we assume that the
2254 // references are on the stack somewhere.
2256 // We cache the value that we find, of course, but if we don't find
2257 // a value we don't cache that fact -- we might subsequently load a
2258 // library which finds the function in question.
2260 // Synchronize on a convenient object to ensure sanity in case two
2261 // threads reach this point for the same function at the same
2262 // time.
2263 JvSynchronize sync (global_ref_table);
2264 if (_this->function == NULL)
2266 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2268 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2269 args_size += sizeof (_this->defining_class);
2271 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2272 _this->self->name,
2273 _this->self->signature,
2274 args_size);
2278 JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2279 ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2280 int offset = 0;
2282 // First argument is always the environment pointer.
2283 real_args[offset++].ptr = env;
2285 // For a static method, we pass in the Class. For non-static
2286 // methods, the `this' argument is already handled.
2287 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2288 real_args[offset++].ptr = _this->defining_class;
2290 // In libgcj, the callee synchronizes.
2291 jobject sync = NULL;
2292 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2294 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2295 sync = _this->defining_class;
2296 else
2297 sync = (jobject) args[0].ptr;
2298 _Jv_MonitorEnter (sync);
2301 // Copy over passed-in arguments.
2302 memcpy (&real_args[offset], args, _this->args_raw_size);
2304 // The actual call to the JNI function.
2305 #if FFI_NATIVE_RAW_API
2306 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2307 ret, real_args);
2308 #else
2309 ffi_java_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2310 ret, real_args);
2311 #endif
2313 // We might need to unwrap a JNI weak reference here.
2314 if (_this->jni_cif.rtype == &ffi_type_pointer)
2316 _Jv_value *val = (_Jv_value *) ret;
2317 val->object_value = unwrap (val->object_value);
2320 if (sync != NULL)
2321 _Jv_MonitorExit (sync);
2323 _Jv_JNI_PopSystemFrame (env);
2326 #endif /* INTERPRETER */
2331 // Invocation API.
2334 // An internal helper function.
2335 static jint
2336 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2337 void *args, jboolean is_daemon)
2339 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2340 java::lang::ThreadGroup *group = NULL;
2342 if (attach)
2344 // FIXME: do we really want to support 1.1?
2345 if (attach->version != JNI_VERSION_1_4
2346 && attach->version != JNI_VERSION_1_2
2347 && attach->version != JNI_VERSION_1_1)
2348 return JNI_EVERSION;
2350 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2351 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2354 // Attaching an already-attached thread is a no-op.
2355 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2356 if (env != NULL)
2358 *penv = reinterpret_cast<void *> (env);
2359 return 0;
2362 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2363 if (env == NULL)
2364 return JNI_ERR;
2365 env->p = &_Jv_JNIFunctions;
2366 env->ex = NULL;
2367 env->klass = NULL;
2368 env->bottom_locals
2369 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2370 + (FRAME_SIZE
2371 * sizeof (jobject)));
2372 env->locals = env->bottom_locals;
2373 if (env->locals == NULL)
2375 _Jv_Free (env);
2376 return JNI_ERR;
2379 env->locals->allocated_p = 0;
2380 env->locals->marker = MARK_SYSTEM;
2381 env->locals->size = FRAME_SIZE;
2382 env->locals->next = NULL;
2384 for (int i = 0; i < env->locals->size; ++i)
2385 env->locals->vec[i] = NULL;
2387 *penv = reinterpret_cast<void *> (env);
2389 // This thread might already be a Java thread -- this function might
2390 // have been called simply to set the new JNIEnv.
2391 if (_Jv_ThreadCurrent () == NULL)
2395 if (is_daemon)
2396 _Jv_AttachCurrentThreadAsDaemon (name, group);
2397 else
2398 _Jv_AttachCurrentThread (name, group);
2400 catch (jthrowable t)
2402 return JNI_ERR;
2405 _Jv_SetCurrentJNIEnv (env);
2407 return 0;
2410 // This is the one actually used by JNI.
2411 jint JNICALL
2412 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
2414 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2417 static jint JNICALL
2418 _Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
2419 void *args)
2421 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2424 static jint JNICALL
2425 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
2427 JvAssert (_Jv_the_vm && vm == _Jv_the_vm);
2429 union
2431 JNIEnv *env;
2432 void *env_p;
2435 if (_Jv_ThreadCurrent () != NULL)
2437 jstring main_name;
2438 // This sucks.
2441 main_name = JvNewStringLatin1 ("main");
2443 catch (jthrowable t)
2445 return JNI_ERR;
2448 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name, &env_p,
2449 NULL, false);
2450 if (r < 0)
2451 return r;
2453 else
2454 env = _Jv_GetCurrentJNIEnv ();
2456 _Jv_ThreadWait ();
2458 // Docs say that this always returns an error code.
2459 return JNI_ERR;
2462 jint JNICALL
2463 _Jv_JNI_DetachCurrentThread (JavaVM *)
2465 jint code = _Jv_DetachCurrentThread ();
2466 return code ? JNI_EDETACHED : 0;
2469 static jint JNICALL
2470 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
2472 if (_Jv_ThreadCurrent () == NULL)
2474 *penv = NULL;
2475 return JNI_EDETACHED;
2478 #ifdef ENABLE_JVMPI
2479 // Handle JVMPI requests.
2480 if (version == JVMPI_VERSION_1)
2482 *penv = (void *) &_Jv_JVMPI_Interface;
2483 return 0;
2485 #endif
2487 // FIXME: do we really want to support 1.1?
2488 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2489 && version != JNI_VERSION_1_1)
2491 *penv = NULL;
2492 return JNI_EVERSION;
2495 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2496 return 0;
2499 JavaVM *
2500 _Jv_GetJavaVM ()
2502 // FIXME: synchronize
2503 if (! _Jv_the_vm)
2505 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2506 if (nvm != NULL)
2507 nvm->functions = &_Jv_JNI_InvokeFunctions;
2508 _Jv_the_vm = nvm;
2511 // If this is a Java thread, we want to make sure it has an
2512 // associated JNIEnv.
2513 if (_Jv_ThreadCurrent () != NULL)
2515 void *ignore;
2516 _Jv_JNI_AttachCurrentThread (_Jv_the_vm, &ignore, NULL);
2519 return _Jv_the_vm;
2522 static jint JNICALL
2523 _Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
2525 *vm = _Jv_GetJavaVM ();
2526 return *vm == NULL ? JNI_ERR : JNI_OK;
2531 #define RESERVED NULL
2533 struct JNINativeInterface _Jv_JNIFunctions =
2535 RESERVED,
2536 RESERVED,
2537 RESERVED,
2538 RESERVED,
2539 _Jv_JNI_GetVersion, // GetVersion
2540 _Jv_JNI_DefineClass, // DefineClass
2541 _Jv_JNI_FindClass, // FindClass
2542 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2543 _Jv_JNI_FromReflectedField, // FromReflectedField
2544 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2545 _Jv_JNI_GetSuperclass, // GetSuperclass
2546 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2547 _Jv_JNI_ToReflectedField, // ToReflectedField
2548 _Jv_JNI_Throw, // Throw
2549 _Jv_JNI_ThrowNew, // ThrowNew
2550 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2551 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2552 _Jv_JNI_ExceptionClear, // ExceptionClear
2553 _Jv_JNI_FatalError, // FatalError
2555 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2556 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2557 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2558 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2559 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2561 _Jv_JNI_IsSameObject, // IsSameObject
2563 _Jv_JNI_NewLocalRef, // NewLocalRef
2564 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2566 _Jv_JNI_AllocObject, // AllocObject
2567 _Jv_JNI_NewObject, // NewObject
2568 _Jv_JNI_NewObjectV, // NewObjectV
2569 _Jv_JNI_NewObjectA, // NewObjectA
2570 _Jv_JNI_GetObjectClass, // GetObjectClass
2571 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2572 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2574 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2575 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2576 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2577 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2578 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2579 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2580 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2581 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2582 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2583 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2584 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2585 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2586 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2587 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2588 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2589 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2590 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2591 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2592 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2593 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2594 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2595 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2596 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2597 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2598 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2599 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2600 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2601 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2602 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2603 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
2605 // Nonvirtual method invocation functions follow.
2606 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2607 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2608 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2609 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2610 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2611 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2612 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2613 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2614 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2615 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2616 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2617 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2618 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2619 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2620 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2621 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2622 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2623 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2624 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2625 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2626 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2627 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2628 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2629 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2630 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2631 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2632 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2633 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2634 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2635 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2637 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2638 _Jv_JNI_GetField<jobject>, // GetObjectField
2639 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2640 _Jv_JNI_GetField<jbyte>, // GetByteField
2641 _Jv_JNI_GetField<jchar>, // GetCharField
2642 _Jv_JNI_GetField<jshort>, // GetShortField
2643 _Jv_JNI_GetField<jint>, // GetIntField
2644 _Jv_JNI_GetField<jlong>, // GetLongField
2645 _Jv_JNI_GetField<jfloat>, // GetFloatField
2646 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2647 _Jv_JNI_SetField, // SetObjectField
2648 _Jv_JNI_SetField, // SetBooleanField
2649 _Jv_JNI_SetField, // SetByteField
2650 _Jv_JNI_SetField, // SetCharField
2651 _Jv_JNI_SetField, // SetShortField
2652 _Jv_JNI_SetField, // SetIntField
2653 _Jv_JNI_SetField, // SetLongField
2654 _Jv_JNI_SetField, // SetFloatField
2655 _Jv_JNI_SetField, // SetDoubleField
2656 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2658 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2659 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2660 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2661 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2662 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2663 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2664 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2665 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2666 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2667 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2668 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2669 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2670 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2671 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2672 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2673 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2674 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2675 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2676 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2677 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2678 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2679 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2680 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2681 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2682 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2683 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2684 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2685 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2686 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2687 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2689 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2690 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2691 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2692 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2693 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2694 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2695 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2696 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2697 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2698 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2699 _Jv_JNI_SetStaticField, // SetStaticObjectField
2700 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2701 _Jv_JNI_SetStaticField, // SetStaticByteField
2702 _Jv_JNI_SetStaticField, // SetStaticCharField
2703 _Jv_JNI_SetStaticField, // SetStaticShortField
2704 _Jv_JNI_SetStaticField, // SetStaticIntField
2705 _Jv_JNI_SetStaticField, // SetStaticLongField
2706 _Jv_JNI_SetStaticField, // SetStaticFloatField
2707 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2708 _Jv_JNI_NewString, // NewString
2709 _Jv_JNI_GetStringLength, // GetStringLength
2710 _Jv_JNI_GetStringChars, // GetStringChars
2711 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2712 _Jv_JNI_NewStringUTF, // NewStringUTF
2713 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
2714 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
2715 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2716 _Jv_JNI_GetArrayLength, // GetArrayLength
2717 _Jv_JNI_NewObjectArray, // NewObjectArray
2718 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2719 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
2720 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2721 // NewBooleanArray
2722 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2723 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2724 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2725 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2726 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2727 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2728 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2729 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2730 // GetBooleanArrayElements
2731 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2732 // GetByteArrayElements
2733 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2734 // GetCharArrayElements
2735 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2736 // GetShortArrayElements
2737 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2738 // GetIntArrayElements
2739 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2740 // GetLongArrayElements
2741 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2742 // GetFloatArrayElements
2743 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2744 // GetDoubleArrayElements
2745 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2746 // ReleaseBooleanArrayElements
2747 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2748 // ReleaseByteArrayElements
2749 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2750 // ReleaseCharArrayElements
2751 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2752 // ReleaseShortArrayElements
2753 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2754 // ReleaseIntArrayElements
2755 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2756 // ReleaseLongArrayElements
2757 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2758 // ReleaseFloatArrayElements
2759 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2760 // ReleaseDoubleArrayElements
2761 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2762 // GetBooleanArrayRegion
2763 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2764 // GetByteArrayRegion
2765 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2766 // GetCharArrayRegion
2767 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2768 // GetShortArrayRegion
2769 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2770 // GetIntArrayRegion
2771 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2772 // GetLongArrayRegion
2773 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2774 // GetFloatArrayRegion
2775 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2776 // GetDoubleArrayRegion
2777 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2778 // SetBooleanArrayRegion
2779 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2780 // SetByteArrayRegion
2781 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2782 // SetCharArrayRegion
2783 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2784 // SetShortArrayRegion
2785 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2786 // SetIntArrayRegion
2787 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2788 // SetLongArrayRegion
2789 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2790 // SetFloatArrayRegion
2791 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2792 // SetDoubleArrayRegion
2793 _Jv_JNI_RegisterNatives, // RegisterNatives
2794 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2795 _Jv_JNI_MonitorEnter, // MonitorEnter
2796 _Jv_JNI_MonitorExit, // MonitorExit
2797 _Jv_JNI_GetJavaVM, // GetJavaVM
2799 _Jv_JNI_GetStringRegion, // GetStringRegion
2800 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2801 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2802 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2803 _Jv_JNI_GetStringCritical, // GetStringCritical
2804 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
2806 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2807 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
2809 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2811 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2812 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
2813 _Jv_JNI_GetDirectBufferCapacity // GetDirectBufferCapacity
2816 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2818 RESERVED,
2819 RESERVED,
2820 RESERVED,
2822 _Jv_JNI_DestroyJavaVM,
2823 _Jv_JNI_AttachCurrentThread,
2824 _Jv_JNI_DetachCurrentThread,
2825 _Jv_JNI_GetEnv,
2826 _Jv_JNI_AttachCurrentThreadAsDaemon