PR target/29166
[official-gcc.git] / libjava / jni.cc
blobc5c2b0ff3cd9c1179bc8f2ac6edbff25db0e7e14
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
25 #include <jvmti.h>
27 #include <java/lang/Class.h>
28 #include <java/lang/ClassLoader.h>
29 #include <java/lang/Throwable.h>
30 #include <java/lang/ArrayIndexOutOfBoundsException.h>
31 #include <java/lang/StringIndexOutOfBoundsException.h>
32 #include <java/lang/StringBuffer.h>
33 #include <java/lang/UnsatisfiedLinkError.h>
34 #include <java/lang/InstantiationException.h>
35 #include <java/lang/NoSuchFieldError.h>
36 #include <java/lang/NoSuchMethodError.h>
37 #include <java/lang/reflect/Constructor.h>
38 #include <java/lang/reflect/Method.h>
39 #include <java/lang/reflect/Modifier.h>
40 #include <java/lang/OutOfMemoryError.h>
41 #include <java/lang/Integer.h>
42 #include <java/lang/ThreadGroup.h>
43 #include <java/lang/Thread.h>
44 #include <java/lang/IllegalAccessError.h>
45 #include <java/nio/Buffer.h>
46 #include <java/nio/DirectByteBufferImpl.h>
47 #include <java/nio/DirectByteBufferImpl$ReadWrite.h>
48 #include <java/util/IdentityHashMap.h>
49 #include <gnu/gcj/RawData.h>
50 #include <java/lang/ClassNotFoundException.h>
52 #include <gcj/method.h>
53 #include <gcj/field.h>
55 #include <java-interp.h>
56 #include <java-threads.h>
58 using namespace gcj;
60 // This enum is used to select different template instantiations in
61 // the invocation code.
62 enum invocation_type
64 normal,
65 nonvirtual,
66 static_type,
67 constructor
70 // Forward declarations.
71 extern struct JNINativeInterface _Jv_JNIFunctions;
72 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
74 // Number of slots in the default frame. The VM must allow at least
75 // 16.
76 #define FRAME_SIZE 16
78 // Mark value indicating this is an overflow frame.
79 #define MARK_NONE 0
80 // Mark value indicating this is a user frame.
81 #define MARK_USER 1
82 // Mark value indicating this is a system frame.
83 #define MARK_SYSTEM 2
85 // This structure is used to keep track of local references.
86 struct _Jv_JNI_LocalFrame
88 // This is true if this frame object represents a pushed frame (eg
89 // from PushLocalFrame).
90 int marker;
92 // Flag to indicate some locals were allocated.
93 int allocated_p;
95 // Number of elements in frame.
96 int size;
98 // Next frame in chain.
99 _Jv_JNI_LocalFrame *next;
101 // The elements. These are allocated using the C "struct hack".
102 jobject vec[0];
105 // This holds a reference count for all local references.
106 static java::util::IdentityHashMap *local_ref_table;
107 // This holds a reference count for all global references.
108 static java::util::IdentityHashMap *global_ref_table;
110 // The only VM.
111 JavaVM *_Jv_the_vm;
113 #ifdef ENABLE_JVMPI
114 // The only JVMPI interface description.
115 static JVMPI_Interface _Jv_JVMPI_Interface;
117 static jint
118 jvmpiEnableEvent (jint event_type, void *)
120 switch (event_type)
122 case JVMPI_EVENT_OBJECT_ALLOC:
123 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
124 break;
126 case JVMPI_EVENT_THREAD_START:
127 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
128 break;
130 case JVMPI_EVENT_THREAD_END:
131 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
132 break;
134 default:
135 return JVMPI_NOT_AVAILABLE;
138 return JVMPI_SUCCESS;
141 static jint
142 jvmpiDisableEvent (jint event_type, void *)
144 switch (event_type)
146 case JVMPI_EVENT_OBJECT_ALLOC:
147 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
148 break;
150 default:
151 return JVMPI_NOT_AVAILABLE;
154 return JVMPI_SUCCESS;
156 #endif
160 void
161 _Jv_JNI_Init (void)
163 local_ref_table = new java::util::IdentityHashMap;
164 global_ref_table = new java::util::IdentityHashMap;
166 #ifdef ENABLE_JVMPI
167 _Jv_JVMPI_Interface.version = 1;
168 _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
169 _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
170 _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
171 _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
172 _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
173 #endif
176 // Tell the GC that a certain pointer is live.
177 static void
178 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
180 JvSynchronize sync (ref_table);
182 using namespace java::lang;
183 Integer *refcount = (Integer *) ref_table->get (obj);
184 jint val = (refcount == NULL) ? 0 : refcount->intValue ();
185 // FIXME: what about out of memory error?
186 ref_table->put (obj, new Integer (val + 1));
189 // Unmark a pointer.
190 static void
191 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
193 JvSynchronize sync (ref_table);
195 using namespace java::lang;
196 Integer *refcount = (Integer *) ref_table->get (obj);
197 JvAssert (refcount);
198 jint val = refcount->intValue () - 1;
199 JvAssert (val >= 0);
200 if (val == 0)
201 ref_table->remove (obj);
202 else
203 // FIXME: what about out of memory error?
204 ref_table->put (obj, new Integer (val));
207 // "Unwrap" some random non-reference type. This exists to simplify
208 // other template functions.
209 template<typename T>
210 static T
211 unwrap (T val)
213 return val;
216 // Unwrap a weak reference, if required.
217 template<typename T>
218 static T *
219 unwrap (T *obj)
221 using namespace gnu::gcj::runtime;
222 // We can compare the class directly because JNIWeakRef is `final'.
223 // Doing it this way is much faster.
224 if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
225 return obj;
226 JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
227 return reinterpret_cast<T *> (wr->get ());
230 jobject
231 _Jv_UnwrapJNIweakReference (jobject obj)
233 return unwrap (obj);
238 static jobject JNICALL
239 _Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
241 // This seems weird but I think it is correct.
242 obj = unwrap (obj);
243 mark_for_gc (obj, global_ref_table);
244 return obj;
247 static void JNICALL
248 _Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
250 // This seems weird but I think it is correct.
251 obj = unwrap (obj);
253 // NULL is ok here -- the JNI specification doesn't say so, but this
254 // is a no-op.
255 if (! obj)
256 return;
258 unmark_for_gc (obj, global_ref_table);
261 static void JNICALL
262 _Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
264 _Jv_JNI_LocalFrame *frame;
266 // This seems weird but I think it is correct.
267 obj = unwrap (obj);
269 // NULL is ok here -- the JNI specification doesn't say so, but this
270 // is a no-op.
271 if (! obj)
272 return;
274 for (frame = env->locals; frame != NULL; frame = frame->next)
276 for (int i = 0; i < frame->size; ++i)
278 if (frame->vec[i] == obj)
280 frame->vec[i] = NULL;
281 unmark_for_gc (obj, local_ref_table);
282 return;
286 // Don't go past a marked frame.
287 JvAssert (frame->marker == MARK_NONE);
290 JvAssert (0);
293 static jint JNICALL
294 _Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
296 // It is easier to just always allocate a new frame of the requested
297 // size. This isn't the most efficient thing, but for now we don't
298 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
300 _Jv_JNI_LocalFrame *frame;
303 frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
304 + size * sizeof (jobject));
306 catch (jthrowable t)
308 env->ex = t;
309 return JNI_ERR;
312 frame->marker = MARK_NONE;
313 frame->size = size;
314 frame->allocated_p = 0;
315 memset (&frame->vec[0], 0, size * sizeof (jobject));
316 frame->next = env->locals;
317 env->locals = frame;
319 return 0;
322 static jint JNICALL
323 _Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
325 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
326 if (r < 0)
327 return r;
329 // The new frame is on top.
330 env->locals->marker = MARK_USER;
332 return 0;
335 static jobject JNICALL
336 _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
338 // This seems weird but I think it is correct.
339 obj = unwrap (obj);
341 // Try to find an open slot somewhere in the topmost frame.
342 _Jv_JNI_LocalFrame *frame = env->locals;
343 bool done = false, set = false;
344 for (; frame != NULL && ! done; frame = frame->next)
346 for (int i = 0; i < frame->size; ++i)
348 if (frame->vec[i] == NULL)
350 set = true;
351 done = true;
352 frame->vec[i] = obj;
353 frame->allocated_p = 1;
354 break;
358 // If we found a slot, or if the frame we just searched is the
359 // mark frame, then we are done.
360 if (done || frame == NULL || frame->marker != MARK_NONE)
361 break;
364 if (! set)
366 // No slots, so we allocate a new frame. According to the spec
367 // we could just die here. FIXME: return value.
368 _Jv_JNI_EnsureLocalCapacity (env, 16);
369 // We know the first element of the new frame will be ok.
370 env->locals->vec[0] = obj;
371 env->locals->allocated_p = 1;
374 mark_for_gc (obj, local_ref_table);
375 return obj;
378 static jobject JNICALL
379 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
381 _Jv_JNI_LocalFrame *rf = env->locals;
383 bool done = false;
384 while (rf != NULL && ! done)
386 for (int i = 0; i < rf->size; ++i)
387 if (rf->vec[i] != NULL)
388 unmark_for_gc (rf->vec[i], local_ref_table);
390 // If the frame we just freed is the marker frame, we are done.
391 done = (rf->marker == stop);
393 _Jv_JNI_LocalFrame *n = rf->next;
394 // When N==NULL, we've reached the reusable bottom_locals, and we must
395 // not free it. However, we must be sure to clear all its elements.
396 if (n == NULL)
398 if (rf->allocated_p)
399 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
400 rf->allocated_p = 0;
401 rf = NULL;
402 break;
405 _Jv_Free (rf);
406 rf = n;
409 // Update the local frame information.
410 env->locals = rf;
412 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
415 static jobject JNICALL
416 _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
418 return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
421 // Make sure an array's type is compatible with the type of the
422 // destination.
423 template<typename T>
424 static bool
425 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
427 jclass klass = array->getClass()->getComponentType();
428 if (__builtin_expect (klass != K, false))
430 env->ex = new java::lang::IllegalAccessError ();
431 return false;
433 else
434 return true;
437 // Pop a `system' frame from the stack. This is `extern "C"' as it is
438 // used by the compiler.
439 extern "C" void
440 _Jv_JNI_PopSystemFrame (JNIEnv *env)
442 // Only enter slow path when we're not at the bottom, or there have been
443 // allocations. Usually this is false and we can just null out the locals
444 // field.
446 if (__builtin_expect ((env->locals->next
447 || env->locals->allocated_p), false))
448 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
449 else
450 env->locals = NULL;
452 if (__builtin_expect (env->ex != NULL, false))
454 jthrowable t = env->ex;
455 env->ex = NULL;
456 throw t;
460 template<typename T> T extract_from_jvalue(jvalue const & t);
461 template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
462 template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
463 template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
464 template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
465 template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
466 template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
467 template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
468 template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
469 template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
472 // This function is used from other template functions. It wraps the
473 // return value appropriately; we specialize it so that object returns
474 // are turned into local references.
475 template<typename T>
476 static T
477 wrap_value (JNIEnv *, T value)
479 return value;
482 // This specialization is used for jobject, jclass, jstring, jarray,
483 // etc.
484 template<typename R, typename T>
485 static T *
486 wrap_value (JNIEnv *env, T *value)
488 return (value == NULL
489 ? value
490 : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
495 static jint JNICALL
496 _Jv_JNI_GetVersion (JNIEnv *)
498 return JNI_VERSION_1_4;
501 static jclass JNICALL
502 _Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
503 const jbyte *buf, jsize bufLen)
507 loader = unwrap (loader);
509 jstring sname = JvNewStringUTF (name);
510 jbyteArray bytes = JvNewByteArray (bufLen);
512 jbyte *elts = elements (bytes);
513 memcpy (elts, buf, bufLen * sizeof (jbyte));
515 java::lang::ClassLoader *l
516 = reinterpret_cast<java::lang::ClassLoader *> (loader);
518 jclass result = l->defineClass (sname, bytes, 0, bufLen);
519 return (jclass) wrap_value (env, result);
521 catch (jthrowable t)
523 env->ex = t;
524 return NULL;
528 static jclass JNICALL
529 _Jv_JNI_FindClass (JNIEnv *env, const char *name)
531 // FIXME: assume that NAME isn't too long.
532 int len = strlen (name);
533 char s[len + 1];
534 for (int i = 0; i <= len; ++i)
535 s[i] = (name[i] == '/') ? '.' : name[i];
537 jclass r = NULL;
540 // This might throw an out of memory exception.
541 jstring n = JvNewStringUTF (s);
543 java::lang::ClassLoader *loader = NULL;
544 if (env->klass != NULL)
545 loader = env->klass->getClassLoaderInternal ();
547 if (loader == NULL)
549 // FIXME: should use getBaseClassLoader, but we don't have that
550 // yet.
551 loader = java::lang::ClassLoader::getSystemClassLoader ();
554 r = loader->loadClass (n);
556 catch (jthrowable t)
558 env->ex = t;
561 return (jclass) wrap_value (env, r);
564 static jclass JNICALL
565 _Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
567 return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
570 static jboolean JNICALL
571 _Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
573 return unwrap (clazz2)->isAssignableFrom (unwrap (clazz1));
576 static jint JNICALL
577 _Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
579 // We check in case the user did some funky cast.
580 obj = unwrap (obj);
581 JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
582 env->ex = obj;
583 return 0;
586 static jint JNICALL
587 _Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
589 using namespace java::lang::reflect;
591 clazz = unwrap (clazz);
592 JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
594 int r = JNI_OK;
597 JArray<jclass> *argtypes
598 = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
599 NULL);
601 jclass *elts = elements (argtypes);
602 elts[0] = &java::lang::String::class$;
604 Constructor *cons = clazz->getConstructor (argtypes);
606 jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
607 NULL);
608 jobject *velts = elements (values);
609 velts[0] = JvNewStringUTF (message);
611 jobject obj = cons->newInstance (values);
613 env->ex = reinterpret_cast<jthrowable> (obj);
615 catch (jthrowable t)
617 env->ex = t;
618 r = JNI_ERR;
621 return r;
624 static jthrowable JNICALL
625 _Jv_JNI_ExceptionOccurred (JNIEnv *env)
627 return (jthrowable) wrap_value (env, env->ex);
630 static void JNICALL
631 _Jv_JNI_ExceptionDescribe (JNIEnv *env)
633 if (env->ex != NULL)
634 env->ex->printStackTrace();
637 static void JNICALL
638 _Jv_JNI_ExceptionClear (JNIEnv *env)
640 env->ex = NULL;
643 static jboolean JNICALL
644 _Jv_JNI_ExceptionCheck (JNIEnv *env)
646 return env->ex != NULL;
649 static void JNICALL
650 _Jv_JNI_FatalError (JNIEnv *, const char *message)
652 JvFail (message);
657 static jboolean JNICALL
658 _Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
660 return unwrap (obj1) == unwrap (obj2);
663 static jobject JNICALL
664 _Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
666 jobject obj = NULL;
667 using namespace java::lang::reflect;
671 clazz = unwrap (clazz);
672 JvAssert (clazz && ! clazz->isArray ());
673 if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
674 env->ex = new java::lang::InstantiationException ();
675 else
676 obj = _Jv_AllocObject (clazz);
678 catch (jthrowable t)
680 env->ex = t;
683 return wrap_value (env, obj);
686 static jclass JNICALL
687 _Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
689 obj = unwrap (obj);
690 JvAssert (obj);
691 return (jclass) wrap_value (env, obj->getClass());
694 static jboolean JNICALL
695 _Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
697 return unwrap (clazz)->isInstance(unwrap (obj));
703 // This section concerns method invocation.
706 template<jboolean is_static>
707 static jmethodID JNICALL
708 _Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
709 const char *name, const char *sig)
713 clazz = unwrap (clazz);
714 _Jv_InitClass (clazz);
716 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
718 // FIXME: assume that SIG isn't too long.
719 int len = strlen (sig);
720 char s[len + 1];
721 for (int i = 0; i <= len; ++i)
722 s[i] = (sig[i] == '/') ? '.' : sig[i];
723 _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
725 JvAssert (! clazz->isPrimitive());
727 using namespace java::lang::reflect;
729 while (clazz != NULL)
731 jint count = JvNumMethods (clazz);
732 jmethodID meth = JvGetFirstMethod (clazz);
734 for (jint i = 0; i < count; ++i)
736 if (((is_static && Modifier::isStatic (meth->accflags))
737 || (! is_static && ! Modifier::isStatic (meth->accflags)))
738 && _Jv_equalUtf8Consts (meth->name, name_u)
739 && _Jv_equalUtf8Consts (meth->signature, sig_u))
740 return meth;
742 meth = meth->getNextMethod();
745 clazz = clazz->getSuperclass ();
748 java::lang::StringBuffer *name_sig =
749 new java::lang::StringBuffer (JvNewStringUTF (name));
750 name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
751 env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
753 catch (jthrowable t)
755 env->ex = t;
758 return NULL;
761 // This is a helper function which turns a va_list into an array of
762 // `jvalue's. It needs signature information in order to do its work.
763 // The array of values must already be allocated.
764 static void
765 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
767 jclass *arg_elts = elements (arg_types);
768 for (int i = 0; i < arg_types->length; ++i)
770 // Here we assume that sizeof(int) >= sizeof(jint), because we
771 // use `int' when decoding the varargs. Likewise for
772 // float, and double. Also we assume that sizeof(jlong) >=
773 // sizeof(int), i.e. that jlong values are not further
774 // promoted.
775 JvAssert (sizeof (int) >= sizeof (jint));
776 JvAssert (sizeof (jlong) >= sizeof (int));
777 JvAssert (sizeof (double) >= sizeof (jfloat));
778 JvAssert (sizeof (double) >= sizeof (jdouble));
779 if (arg_elts[i] == JvPrimClass (byte))
780 values[i].b = (jbyte) va_arg (vargs, int);
781 else if (arg_elts[i] == JvPrimClass (short))
782 values[i].s = (jshort) va_arg (vargs, int);
783 else if (arg_elts[i] == JvPrimClass (int))
784 values[i].i = (jint) va_arg (vargs, int);
785 else if (arg_elts[i] == JvPrimClass (long))
786 values[i].j = (jlong) va_arg (vargs, jlong);
787 else if (arg_elts[i] == JvPrimClass (float))
788 values[i].f = (jfloat) va_arg (vargs, double);
789 else if (arg_elts[i] == JvPrimClass (double))
790 values[i].d = (jdouble) va_arg (vargs, double);
791 else if (arg_elts[i] == JvPrimClass (boolean))
792 values[i].z = (jboolean) va_arg (vargs, int);
793 else if (arg_elts[i] == JvPrimClass (char))
794 values[i].c = (jchar) va_arg (vargs, int);
795 else
797 // An object.
798 values[i].l = unwrap (va_arg (vargs, jobject));
803 // This can call any sort of method: virtual, "nonvirtual", static, or
804 // constructor.
805 template<typename T, invocation_type style>
806 static T JNICALL
807 _Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
808 jmethodID id, va_list vargs)
810 obj = unwrap (obj);
811 klass = unwrap (klass);
813 jclass decl_class = klass ? klass : obj->getClass ();
814 JvAssert (decl_class != NULL);
816 jclass return_type;
817 JArray<jclass> *arg_types;
821 _Jv_GetTypesFromSignature (id, decl_class,
822 &arg_types, &return_type);
824 jvalue args[arg_types->length];
825 array_from_valist (args, arg_types, vargs);
827 // For constructors we need to pass the Class we are instantiating.
828 if (style == constructor)
829 return_type = klass;
831 jvalue result;
832 _Jv_CallAnyMethodA (obj, return_type, id,
833 style == constructor,
834 style == normal,
835 arg_types, args, &result);
837 return wrap_value (env, extract_from_jvalue<T>(result));
839 catch (jthrowable t)
841 env->ex = t;
844 return wrap_value (env, (T) 0);
847 template<typename T, invocation_type style>
848 static T JNICALL
849 _Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
850 jmethodID method, ...)
852 va_list args;
853 T result;
855 va_start (args, method);
856 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
857 va_end (args);
859 return result;
862 template<typename T, invocation_type style>
863 static T JNICALL
864 _Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
865 jmethodID id, jvalue *args)
867 obj = unwrap (obj);
868 klass = unwrap (klass);
870 jclass decl_class = klass ? klass : obj->getClass ();
871 JvAssert (decl_class != NULL);
873 jclass return_type;
874 JArray<jclass> *arg_types;
877 _Jv_GetTypesFromSignature (id, decl_class,
878 &arg_types, &return_type);
880 // For constructors we need to pass the Class we are instantiating.
881 if (style == constructor)
882 return_type = klass;
884 // Unwrap arguments as required. Eww.
885 jclass *type_elts = elements (arg_types);
886 jvalue arg_copy[arg_types->length];
887 for (int i = 0; i < arg_types->length; ++i)
889 if (type_elts[i]->isPrimitive ())
890 arg_copy[i] = args[i];
891 else
892 arg_copy[i].l = unwrap (args[i].l);
895 jvalue result;
896 _Jv_CallAnyMethodA (obj, return_type, id,
897 style == constructor,
898 style == normal,
899 arg_types, arg_copy, &result);
901 return wrap_value (env, extract_from_jvalue<T>(result));
903 catch (jthrowable t)
905 env->ex = t;
908 return wrap_value (env, (T) 0);
911 template<invocation_type style>
912 static void JNICALL
913 _Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
914 jmethodID id, va_list vargs)
916 obj = unwrap (obj);
917 klass = unwrap (klass);
919 jclass decl_class = klass ? klass : obj->getClass ();
920 JvAssert (decl_class != NULL);
922 jclass return_type;
923 JArray<jclass> *arg_types;
926 _Jv_GetTypesFromSignature (id, decl_class,
927 &arg_types, &return_type);
929 jvalue args[arg_types->length];
930 array_from_valist (args, arg_types, vargs);
932 // For constructors we need to pass the Class we are instantiating.
933 if (style == constructor)
934 return_type = klass;
936 _Jv_CallAnyMethodA (obj, return_type, id,
937 style == constructor,
938 style == normal,
939 arg_types, args, NULL);
941 catch (jthrowable t)
943 env->ex = t;
947 template<invocation_type style>
948 static void JNICALL
949 _Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
950 jmethodID method, ...)
952 va_list args;
954 va_start (args, method);
955 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
956 va_end (args);
959 template<invocation_type style>
960 static void JNICALL
961 _Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
962 jmethodID id, jvalue *args)
964 jclass decl_class = klass ? klass : obj->getClass ();
965 JvAssert (decl_class != NULL);
967 jclass return_type;
968 JArray<jclass> *arg_types;
971 _Jv_GetTypesFromSignature (id, decl_class,
972 &arg_types, &return_type);
974 // Unwrap arguments as required. Eww.
975 jclass *type_elts = elements (arg_types);
976 jvalue arg_copy[arg_types->length];
977 for (int i = 0; i < arg_types->length; ++i)
979 if (type_elts[i]->isPrimitive ())
980 arg_copy[i] = args[i];
981 else
982 arg_copy[i].l = unwrap (args[i].l);
985 _Jv_CallAnyMethodA (obj, return_type, id,
986 style == constructor,
987 style == normal,
988 arg_types, args, NULL);
990 catch (jthrowable t)
992 env->ex = t;
996 // Functions with this signature are used to implement functions in
997 // the CallMethod family.
998 template<typename T>
999 static T JNICALL
1000 _Jv_JNI_CallMethodV (JNIEnv *env, jobject obj,
1001 jmethodID id, va_list args)
1003 return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
1006 // Functions with this signature are used to implement functions in
1007 // the CallMethod family.
1008 template<typename T>
1009 static T JNICALL
1010 _Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1012 va_list args;
1013 T result;
1015 va_start (args, id);
1016 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
1017 va_end (args);
1019 return result;
1022 // Functions with this signature are used to implement functions in
1023 // the CallMethod family.
1024 template<typename T>
1025 static T JNICALL
1026 _Jv_JNI_CallMethodA (JNIEnv *env, jobject obj,
1027 jmethodID id, jvalue *args)
1029 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
1032 static void JNICALL
1033 _Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj,
1034 jmethodID id, va_list args)
1036 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1039 static void JNICALL
1040 _Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
1042 va_list args;
1044 va_start (args, id);
1045 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1046 va_end (args);
1049 static void JNICALL
1050 _Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj,
1051 jmethodID id, jvalue *args)
1053 _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1056 // Functions with this signature are used to implement functions in
1057 // the CallStaticMethod family.
1058 template<typename T>
1059 static T JNICALL
1060 _Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
1061 jmethodID id, va_list args)
1063 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1064 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1066 return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1069 // Functions with this signature are used to implement functions in
1070 // the CallStaticMethod family.
1071 template<typename T>
1072 static T JNICALL
1073 _Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass,
1074 jmethodID id, ...)
1076 va_list args;
1077 T result;
1079 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1080 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1082 va_start (args, id);
1083 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1084 id, args);
1085 va_end (args);
1087 return result;
1090 // Functions with this signature are used to implement functions in
1091 // the CallStaticMethod family.
1092 template<typename T>
1093 static T JNICALL
1094 _Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
1095 jvalue *args)
1097 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1098 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1100 return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1103 static void JNICALL
1104 _Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass,
1105 jmethodID id, va_list args)
1107 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1110 static void JNICALL
1111 _Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass,
1112 jmethodID id, ...)
1114 va_list args;
1116 va_start (args, id);
1117 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1118 va_end (args);
1121 static void JNICALL
1122 _Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass,
1123 jmethodID id, jvalue *args)
1125 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1128 static jobject JNICALL
1129 _Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
1130 jmethodID id, va_list args)
1132 JvAssert (klass && ! klass->isArray ());
1133 JvAssert (! strcmp (id->name->chars(), "<init>")
1134 && id->signature->len() > 2
1135 && id->signature->chars()[0] == '('
1136 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1137 ")V"));
1139 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1140 id, args);
1143 static jobject JNICALL
1144 _Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
1146 JvAssert (klass && ! klass->isArray ());
1147 JvAssert (! strcmp (id->name->chars(), "<init>")
1148 && id->signature->len() > 2
1149 && id->signature->chars()[0] == '('
1150 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1151 ")V"));
1153 va_list args;
1154 jobject result;
1156 va_start (args, id);
1157 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1158 id, args);
1159 va_end (args);
1161 return result;
1164 static jobject JNICALL
1165 _Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
1166 jvalue *args)
1168 JvAssert (klass && ! klass->isArray ());
1169 JvAssert (! strcmp (id->name->chars(), "<init>")
1170 && id->signature->len() > 2
1171 && id->signature->chars()[0] == '('
1172 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1173 ")V"));
1175 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1176 id, args);
1181 template<typename T>
1182 static T JNICALL
1183 _Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
1185 obj = unwrap (obj);
1186 JvAssert (obj);
1187 T *ptr = (T *) ((char *) obj + field->getOffset ());
1188 return wrap_value (env, *ptr);
1191 template<typename T>
1192 static void JNICALL
1193 _Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
1195 obj = unwrap (obj);
1196 value = unwrap (value);
1198 JvAssert (obj);
1199 T *ptr = (T *) ((char *) obj + field->getOffset ());
1200 *ptr = value;
1203 template<jboolean is_static>
1204 static jfieldID JNICALL
1205 _Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
1206 const char *name, const char *sig)
1210 clazz = unwrap (clazz);
1212 _Jv_InitClass (clazz);
1214 _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1216 // FIXME: assume that SIG isn't too long.
1217 int len = strlen (sig);
1218 char s[len + 1];
1219 for (int i = 0; i <= len; ++i)
1220 s[i] = (sig[i] == '/') ? '.' : sig[i];
1221 java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1222 jclass field_class = _Jv_FindClassFromSignature ((char *) s, loader);
1223 if (! field_class)
1224 throw new java::lang::ClassNotFoundException(JvNewStringUTF(s));
1226 while (clazz != NULL)
1228 // We acquire the class lock so that fields aren't resolved
1229 // while we are running.
1230 JvSynchronize sync (clazz);
1232 jint count = (is_static
1233 ? JvNumStaticFields (clazz)
1234 : JvNumInstanceFields (clazz));
1235 jfieldID field = (is_static
1236 ? JvGetFirstStaticField (clazz)
1237 : JvGetFirstInstanceField (clazz));
1238 for (jint i = 0; i < count; ++i)
1240 _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1242 // The field might be resolved or it might not be. It
1243 // is much simpler to always resolve it.
1244 _Jv_Linker::resolve_field (field, loader);
1245 if (_Jv_equalUtf8Consts (f_name, a_name)
1246 && field->getClass() == field_class)
1247 return field;
1249 field = field->getNextField ();
1252 clazz = clazz->getSuperclass ();
1255 env->ex = new java::lang::NoSuchFieldError ();
1257 catch (jthrowable t)
1259 env->ex = t;
1261 return NULL;
1264 template<typename T>
1265 static T JNICALL
1266 _Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
1268 T *ptr = (T *) field->u.addr;
1269 return wrap_value (env, *ptr);
1272 template<typename T>
1273 static void JNICALL
1274 _Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
1276 value = unwrap (value);
1277 T *ptr = (T *) field->u.addr;
1278 *ptr = value;
1281 static jstring JNICALL
1282 _Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
1286 jstring r = _Jv_NewString (unichars, len);
1287 return (jstring) wrap_value (env, r);
1289 catch (jthrowable t)
1291 env->ex = t;
1292 return NULL;
1296 static jsize JNICALL
1297 _Jv_JNI_GetStringLength (JNIEnv *, jstring string)
1299 return unwrap (string)->length();
1302 static const jchar * JNICALL
1303 _Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
1305 string = unwrap (string);
1306 jchar *result = _Jv_GetStringChars (string);
1307 mark_for_gc (string, global_ref_table);
1308 if (isCopy)
1309 *isCopy = false;
1310 return (const jchar *) result;
1313 static void JNICALL
1314 _Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
1316 unmark_for_gc (unwrap (string), global_ref_table);
1319 static jstring JNICALL
1320 _Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
1324 jstring result = JvNewStringUTF (bytes);
1325 return (jstring) wrap_value (env, result);
1327 catch (jthrowable t)
1329 env->ex = t;
1330 return NULL;
1334 static jsize JNICALL
1335 _Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
1337 return JvGetStringUTFLength (unwrap (string));
1340 static const char * JNICALL
1341 _Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string,
1342 jboolean *isCopy)
1346 string = unwrap (string);
1347 if (string == NULL)
1348 return NULL;
1349 jsize len = JvGetStringUTFLength (string);
1350 char *r = (char *) _Jv_Malloc (len + 1);
1351 JvGetStringUTFRegion (string, 0, string->length(), r);
1352 r[len] = '\0';
1354 if (isCopy)
1355 *isCopy = true;
1357 return (const char *) r;
1359 catch (jthrowable t)
1361 env->ex = t;
1362 return NULL;
1366 static void JNICALL
1367 _Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
1369 _Jv_Free ((void *) utf);
1372 static void JNICALL
1373 _Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start,
1374 jsize len, jchar *buf)
1376 string = unwrap (string);
1377 jchar *result = _Jv_GetStringChars (string);
1378 if (start < 0 || start > string->length ()
1379 || len < 0 || start + len > string->length ())
1383 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1385 catch (jthrowable t)
1387 env->ex = t;
1390 else
1391 memcpy (buf, &result[start], len * sizeof (jchar));
1394 static void JNICALL
1395 _Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
1396 jsize len, char *buf)
1398 str = unwrap (str);
1400 if (start < 0 || start > str->length ()
1401 || len < 0 || start + len > str->length ())
1405 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1407 catch (jthrowable t)
1409 env->ex = t;
1412 else
1413 _Jv_GetStringUTFRegion (str, start, len, buf);
1416 static const jchar * JNICALL
1417 _Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
1419 jchar *result = _Jv_GetStringChars (unwrap (str));
1420 if (isCopy)
1421 *isCopy = false;
1422 return result;
1425 static void JNICALL
1426 _Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
1428 // Nothing.
1431 static jsize JNICALL
1432 _Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
1434 return unwrap (array)->length;
1437 static jobjectArray JNICALL
1438 _Jv_JNI_NewObjectArray (JNIEnv *env, jsize length,
1439 jclass elementClass, jobject init)
1443 elementClass = unwrap (elementClass);
1444 init = unwrap (init);
1446 _Jv_CheckCast (elementClass, init);
1447 jarray result = JvNewObjectArray (length, elementClass, init);
1448 return (jobjectArray) wrap_value (env, result);
1450 catch (jthrowable t)
1452 env->ex = t;
1453 return NULL;
1457 static jobject JNICALL
1458 _Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array,
1459 jsize index)
1461 if ((unsigned) index >= (unsigned) array->length)
1462 _Jv_ThrowBadArrayIndex (index);
1463 jobject *elts = elements (unwrap (array));
1464 return wrap_value (env, elts[index]);
1467 static void JNICALL
1468 _Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array,
1469 jsize index, jobject value)
1473 array = unwrap (array);
1474 value = unwrap (value);
1476 _Jv_CheckArrayStore (array, value);
1477 if ((unsigned) index >= (unsigned) array->length)
1478 _Jv_ThrowBadArrayIndex (index);
1479 jobject *elts = elements (array);
1480 elts[index] = value;
1482 catch (jthrowable t)
1484 env->ex = t;
1488 template<typename T, jclass K>
1489 static JArray<T> * JNICALL
1490 _Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
1494 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1496 catch (jthrowable t)
1498 env->ex = t;
1499 return NULL;
1503 template<typename T, jclass K>
1504 static T * JNICALL
1505 _Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1506 jboolean *isCopy)
1508 array = unwrap (array);
1509 if (! _Jv_JNI_check_types (env, array, K))
1510 return NULL;
1511 T *elts = elements (array);
1512 if (isCopy)
1514 // We elect never to copy.
1515 *isCopy = false;
1517 mark_for_gc (array, global_ref_table);
1518 return elts;
1521 template<typename T, jclass K>
1522 static void JNICALL
1523 _Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
1524 T *, jint /* mode */)
1526 array = unwrap (array);
1527 _Jv_JNI_check_types (env, array, K);
1528 // Note that we ignore MODE. We can do this because we never copy
1529 // the array elements. My reading of the JNI documentation is that
1530 // this is an option for the implementor.
1531 unmark_for_gc (array, global_ref_table);
1534 template<typename T, jclass K>
1535 static void JNICALL
1536 _Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1537 jsize start, jsize len,
1538 T *buf)
1540 array = unwrap (array);
1541 if (! _Jv_JNI_check_types (env, array, K))
1542 return;
1544 // The cast to unsigned lets us save a comparison.
1545 if (start < 0 || len < 0
1546 || (unsigned long) (start + len) > (unsigned long) array->length)
1550 // FIXME: index.
1551 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1553 catch (jthrowable t)
1555 // Could have thown out of memory error.
1556 env->ex = t;
1559 else
1561 T *elts = elements (array) + start;
1562 memcpy (buf, elts, len * sizeof (T));
1566 template<typename T, jclass K>
1567 static void JNICALL
1568 _Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
1569 jsize start, jsize len, T *buf)
1571 array = unwrap (array);
1572 if (! _Jv_JNI_check_types (env, array, K))
1573 return;
1575 // The cast to unsigned lets us save a comparison.
1576 if (start < 0 || len < 0
1577 || (unsigned long) (start + len) > (unsigned long) array->length)
1581 // FIXME: index.
1582 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1584 catch (jthrowable t)
1586 env->ex = t;
1589 else
1591 T *elts = elements (array) + start;
1592 memcpy (elts, buf, len * sizeof (T));
1596 static void * JNICALL
1597 _Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
1598 jboolean *isCopy)
1600 array = unwrap (array);
1601 // FIXME: does this work?
1602 jclass klass = array->getClass()->getComponentType();
1603 JvAssert (klass->isPrimitive ());
1604 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1605 if (isCopy)
1606 *isCopy = false;
1607 return r;
1610 static void JNICALL
1611 _Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
1613 // Nothing.
1616 static jint JNICALL
1617 _Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
1621 _Jv_MonitorEnter (unwrap (obj));
1622 return 0;
1624 catch (jthrowable t)
1626 env->ex = t;
1628 return JNI_ERR;
1631 static jint JNICALL
1632 _Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
1636 _Jv_MonitorExit (unwrap (obj));
1637 return 0;
1639 catch (jthrowable t)
1641 env->ex = t;
1643 return JNI_ERR;
1646 // JDK 1.2
1647 jobject JNICALL
1648 _Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
1649 jboolean)
1653 cls = unwrap (cls);
1654 java::lang::reflect::Field *field = new java::lang::reflect::Field();
1655 field->declaringClass = cls;
1656 field->offset = (char*) fieldID - (char *) cls->fields;
1657 field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1658 return wrap_value (env, field);
1660 catch (jthrowable t)
1662 env->ex = t;
1664 return NULL;
1667 // JDK 1.2
1668 static jfieldID JNICALL
1669 _Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
1671 using namespace java::lang::reflect;
1673 f = unwrap (f);
1674 Field *field = reinterpret_cast<Field *> (f);
1675 return _Jv_FromReflectedField (field);
1678 jobject JNICALL
1679 _Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
1680 jboolean)
1682 using namespace java::lang::reflect;
1684 jobject result = NULL;
1685 klass = unwrap (klass);
1689 if (_Jv_equalUtf8Consts (id->name, init_name))
1691 // A constructor.
1692 Constructor *cons = new Constructor ();
1693 cons->offset = (char *) id - (char *) &klass->methods;
1694 cons->declaringClass = klass;
1695 result = cons;
1697 else
1699 Method *meth = new Method ();
1700 meth->offset = (char *) id - (char *) &klass->methods;
1701 meth->declaringClass = klass;
1702 result = meth;
1705 catch (jthrowable t)
1707 env->ex = t;
1710 return wrap_value (env, result);
1713 static jmethodID JNICALL
1714 _Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
1716 using namespace java::lang::reflect;
1717 method = unwrap (method);
1718 if (Method::class$.isInstance (method))
1719 return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1720 return
1721 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1724 // JDK 1.2.
1725 jweak JNICALL
1726 _Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
1728 using namespace gnu::gcj::runtime;
1729 JNIWeakRef *ref = NULL;
1733 // This seems weird but I think it is correct.
1734 obj = unwrap (obj);
1735 ref = new JNIWeakRef (obj);
1736 mark_for_gc (ref, global_ref_table);
1738 catch (jthrowable t)
1740 env->ex = t;
1743 return reinterpret_cast<jweak> (ref);
1746 void JNICALL
1747 _Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
1749 using namespace gnu::gcj::runtime;
1750 JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1751 unmark_for_gc (ref, global_ref_table);
1752 ref->clear ();
1757 // Direct byte buffers.
1759 static jobject JNICALL
1760 _Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
1762 using namespace gnu::gcj;
1763 using namespace java::nio;
1764 return new DirectByteBufferImpl$ReadWrite
1765 (reinterpret_cast<RawData *> (address), length);
1768 static void * JNICALL
1769 _Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
1771 using namespace java::nio;
1772 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1773 return NULL;
1774 Buffer *tmp = static_cast<Buffer *> (buffer);
1775 return reinterpret_cast<void *> (tmp->address);
1778 static jlong JNICALL
1779 _Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
1781 using namespace java::nio;
1782 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1783 return -1;
1784 Buffer *tmp = static_cast<Buffer *> (buffer);
1785 if (tmp->address == NULL)
1786 return -1;
1787 return tmp->capacity();
1792 struct NativeMethodCacheEntry : public JNINativeMethod
1794 char *className;
1797 // Hash table of native methods.
1798 static NativeMethodCacheEntry *nathash;
1799 // Number of slots used.
1800 static int nathash_count = 0;
1801 // Number of slots available. Must be power of 2.
1802 static int nathash_size = 0;
1804 #define DELETED_ENTRY ((char *) (~0))
1806 // Compute a hash value for a native method descriptor.
1807 static int
1808 hash (const NativeMethodCacheEntry *method)
1810 char *ptr;
1811 int hash = 0;
1813 ptr = method->className;
1814 while (*ptr)
1815 hash = (31 * hash) + *ptr++;
1817 ptr = method->name;
1818 while (*ptr)
1819 hash = (31 * hash) + *ptr++;
1821 ptr = method->signature;
1822 while (*ptr)
1823 hash = (31 * hash) + *ptr++;
1825 return hash;
1828 // Find the slot where a native method goes.
1829 static NativeMethodCacheEntry *
1830 nathash_find_slot (const NativeMethodCacheEntry *method)
1832 jint h = hash (method);
1833 int step = (h ^ (h >> 16)) | 1;
1834 int w = h & (nathash_size - 1);
1835 int del = -1;
1837 for (;;)
1839 NativeMethodCacheEntry *slotp = &nathash[w];
1840 if (slotp->name == NULL)
1842 if (del >= 0)
1843 return &nathash[del];
1844 else
1845 return slotp;
1847 else if (slotp->name == DELETED_ENTRY)
1848 del = w;
1849 else if (! strcmp (slotp->name, method->name)
1850 && ! strcmp (slotp->signature, method->signature)
1851 && ! strcmp (slotp->className, method->className))
1852 return slotp;
1853 w = (w + step) & (nathash_size - 1);
1857 // Find a method. Return NULL if it isn't in the hash table.
1858 static void *
1859 nathash_find (NativeMethodCacheEntry *method)
1861 if (nathash == NULL)
1862 return NULL;
1863 NativeMethodCacheEntry *slot = nathash_find_slot (method);
1864 if (slot->name == NULL || slot->name == DELETED_ENTRY)
1865 return NULL;
1866 return slot->fnPtr;
1869 static void
1870 natrehash ()
1872 if (nathash == NULL)
1874 nathash_size = 1024;
1875 nathash =
1876 (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
1877 * sizeof (NativeMethodCacheEntry));
1879 else
1881 int savesize = nathash_size;
1882 NativeMethodCacheEntry *savehash = nathash;
1883 nathash_size *= 2;
1884 nathash =
1885 (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
1886 * sizeof (NativeMethodCacheEntry));
1888 for (int i = 0; i < savesize; ++i)
1890 if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1892 NativeMethodCacheEntry *slot = nathash_find_slot (&savehash[i]);
1893 *slot = savehash[i];
1899 static void
1900 nathash_add (const NativeMethodCacheEntry *method)
1902 if (3 * nathash_count >= 2 * nathash_size)
1903 natrehash ();
1904 NativeMethodCacheEntry *slot = nathash_find_slot (method);
1905 // If the slot has a real entry in it, then there is no work to do.
1906 if (slot->name != NULL && slot->name != DELETED_ENTRY)
1907 return;
1908 // FIXME: memory leak?
1909 slot->name = strdup (method->name);
1910 slot->className = strdup (method->className);
1911 // This was already strduped in _Jv_JNI_RegisterNatives.
1912 slot->signature = method->signature;
1913 slot->fnPtr = method->fnPtr;
1916 static jint JNICALL
1917 _Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
1918 const JNINativeMethod *methods,
1919 jint nMethods)
1921 // Synchronize while we do the work. This must match
1922 // synchronization in some other functions that manipulate or use
1923 // the nathash table.
1924 JvSynchronize sync (global_ref_table);
1926 NativeMethodCacheEntry dottedMethod;
1928 // Look at each descriptor given us, and find the corresponding
1929 // method in the class.
1930 for (int j = 0; j < nMethods; ++j)
1932 bool found = false;
1934 _Jv_Method *imeths = JvGetFirstMethod (klass);
1935 for (int i = 0; i < JvNumMethods (klass); ++i)
1937 _Jv_Method *self = &imeths[i];
1939 // Copy this JNINativeMethod and do a slash to dot
1940 // conversion on the signature.
1941 dottedMethod.name = methods[j].name;
1942 // FIXME: we leak a little memory here if the method
1943 // is not found.
1944 dottedMethod.signature = strdup (methods[j].signature);
1945 dottedMethod.fnPtr = methods[j].fnPtr;
1946 dottedMethod.className = _Jv_GetClassNameUtf8 (klass)->chars();
1947 char *c = dottedMethod.signature;
1948 while (*c)
1950 if (*c == '/')
1951 *c = '.';
1952 c++;
1955 if (! strcmp (self->name->chars (), dottedMethod.name)
1956 && ! strcmp (self->signature->chars (), dottedMethod.signature))
1958 if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
1959 break;
1961 // Found a match that is native.
1962 found = true;
1963 nathash_add (&dottedMethod);
1965 break;
1969 if (! found)
1971 jstring m = JvNewStringUTF (methods[j].name);
1974 env->ex = new java::lang::NoSuchMethodError (m);
1976 catch (jthrowable t)
1978 env->ex = t;
1980 return JNI_ERR;
1984 return JNI_OK;
1987 static jint JNICALL
1988 _Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
1990 // FIXME -- we could implement this.
1991 return JNI_ERR;
1996 // Add a character to the buffer, encoding properly.
1997 static void
1998 add_char (char *buf, jchar c, int *here)
2000 if (c == '_')
2002 buf[(*here)++] = '_';
2003 buf[(*here)++] = '1';
2005 else if (c == ';')
2007 buf[(*here)++] = '_';
2008 buf[(*here)++] = '2';
2010 else if (c == '[')
2012 buf[(*here)++] = '_';
2013 buf[(*here)++] = '3';
2016 // Also check for `.' here because we might be passed an internal
2017 // qualified class name like `foo.bar'.
2018 else if (c == '/' || c == '.')
2019 buf[(*here)++] = '_';
2020 else if ((c >= '0' && c <= '9')
2021 || (c >= 'a' && c <= 'z')
2022 || (c >= 'A' && c <= 'Z'))
2023 buf[(*here)++] = (char) c;
2024 else
2026 // "Unicode" character.
2027 buf[(*here)++] = '_';
2028 buf[(*here)++] = '0';
2029 for (int i = 0; i < 4; ++i)
2031 int val = c & 0x0f;
2032 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
2033 c >>= 4;
2035 *here += 4;
2039 // Compute a mangled name for a native function. This computes the
2040 // long name, and also returns an index which indicates where a NUL
2041 // can be placed to create the short name. This function assumes that
2042 // the buffer is large enough for its results.
2043 static void
2044 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
2045 _Jv_Utf8Const *signature, char *buf, int *long_start)
2047 strcpy (buf, "Java_");
2048 int here = 5;
2050 // Add fully qualified class name.
2051 jchar *chars = _Jv_GetStringChars (klass->getName ());
2052 jint len = klass->getName ()->length ();
2053 for (int i = 0; i < len; ++i)
2054 add_char (buf, chars[i], &here);
2056 // Don't use add_char because we need a literal `_'.
2057 buf[here++] = '_';
2059 const unsigned char *fn = (const unsigned char *) func_name->chars ();
2060 const unsigned char *limit = fn + func_name->len ();
2061 for (int i = 0; ; ++i)
2063 int ch = UTF8_GET (fn, limit);
2064 if (ch < 0)
2065 break;
2066 add_char (buf, ch, &here);
2069 // This is where the long signature begins.
2070 *long_start = here;
2071 buf[here++] = '_';
2072 buf[here++] = '_';
2074 const unsigned char *sig = (const unsigned char *) signature->chars ();
2075 limit = sig + signature->len ();
2076 JvAssert (sig[0] == '(');
2077 ++sig;
2078 while (1)
2080 int ch = UTF8_GET (sig, limit);
2081 if (ch == ')' || ch < 0)
2082 break;
2083 add_char (buf, ch, &here);
2086 buf[here] = '\0';
2089 // Return the current thread's JNIEnv; if one does not exist, create
2090 // it. Also create a new system frame for use. This is `extern "C"'
2091 // because the compiler calls it.
2092 extern "C" JNIEnv *
2093 _Jv_GetJNIEnvNewFrame (jclass klass)
2095 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2096 if (__builtin_expect (env == NULL, false))
2098 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2099 env->p = &_Jv_JNIFunctions;
2100 env->klass = klass;
2101 env->locals = NULL;
2102 // We set env->ex below.
2104 // Set up the bottom, reusable frame.
2105 env->bottom_locals = (_Jv_JNI_LocalFrame *)
2106 _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2107 + (FRAME_SIZE
2108 * sizeof (jobject)));
2110 env->bottom_locals->marker = MARK_SYSTEM;
2111 env->bottom_locals->size = FRAME_SIZE;
2112 env->bottom_locals->next = NULL;
2113 env->bottom_locals->allocated_p = 0;
2114 memset (&env->bottom_locals->vec[0], 0,
2115 env->bottom_locals->size * sizeof (jobject));
2117 _Jv_SetCurrentJNIEnv (env);
2120 // If we're in a simple JNI call (non-nested), we can just reuse the
2121 // locals frame we allocated many calls ago, back when the env was first
2122 // built, above.
2124 if (__builtin_expect (env->locals == NULL, true))
2125 env->locals = env->bottom_locals;
2127 else
2129 // Alternatively, we might be re-entering JNI, in which case we can't
2130 // reuse the bottom_locals frame, because it is already underneath
2131 // us. So we need to make a new one.
2133 _Jv_JNI_LocalFrame *frame
2134 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2135 + (FRAME_SIZE
2136 * sizeof (jobject)));
2138 frame->marker = MARK_SYSTEM;
2139 frame->size = FRAME_SIZE;
2140 frame->allocated_p = 0;
2141 frame->next = env->locals;
2143 memset (&frame->vec[0], 0,
2144 frame->size * sizeof (jobject));
2146 env->locals = frame;
2149 env->ex = NULL;
2151 return env;
2154 // Destroy the env's reusable resources. This is called from the thread
2155 // destructor "finalize_native" in natThread.cc
2156 void
2157 _Jv_FreeJNIEnv (_Jv_JNIEnv *env)
2159 if (env == NULL)
2160 return;
2162 if (env->bottom_locals != NULL)
2163 _Jv_Free (env->bottom_locals);
2165 _Jv_Free (env);
2168 // Return the function which implements a particular JNI method. If
2169 // we can't find the function, we throw the appropriate exception.
2170 // This is `extern "C"' because the compiler uses it.
2171 extern "C" void *
2172 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2173 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
2175 int name_length = name->len();
2176 int sig_length = signature->len();
2177 char buf[10 + 6 * (name_length + sig_length) + 12];
2178 int long_start;
2179 void *function;
2181 // Synchronize on something convenient. Right now we use the hash.
2182 JvSynchronize sync (global_ref_table);
2184 // First see if we have an override in the hash table.
2185 strncpy (buf, name->chars (), name_length);
2186 buf[name_length] = '\0';
2187 strncpy (buf + name_length + 1, signature->chars (), sig_length);
2188 buf[name_length + sig_length + 1] = '\0';
2189 NativeMethodCacheEntry meth;
2190 meth.name = buf;
2191 meth.signature = buf + name_length + 1;
2192 meth.className = _Jv_GetClassNameUtf8(klass)->chars();
2193 function = nathash_find (&meth);
2194 if (function != NULL)
2195 return function;
2197 // If there was no override, then look in the symbol table.
2198 buf[0] = '_';
2199 mangled_name (klass, name, signature, buf + 1, &long_start);
2200 char c = buf[long_start + 1];
2201 buf[long_start + 1] = '\0';
2203 function = _Jv_FindSymbolInExecutable (buf + 1);
2204 #ifdef WIN32
2205 // On Win32, we use the "stdcall" calling convention (see JNICALL
2206 // in jni.h).
2208 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2209 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2210 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2211 // take care of all these variations here.
2213 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2214 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2216 if (function == NULL)
2218 // We have tried searching for the 'fooBar' form (BCC) - now
2219 // try the others.
2221 // First, save the part of the long name that will be damaged
2222 // by appending '@nn'.
2223 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2225 sprintf (asz_buf, "@%d", args_size);
2226 strcat (buf, asz_buf);
2228 // Search for the '_fooBar@nn' form (MSVC).
2229 function = _Jv_FindSymbolInExecutable (buf);
2231 if (function == NULL)
2233 // Search for the 'fooBar@nn' form (MinGW GCC).
2234 function = _Jv_FindSymbolInExecutable (buf + 1);
2237 #endif /* WIN32 */
2239 if (function == NULL)
2241 buf[long_start + 1] = c;
2242 #ifdef WIN32
2243 // Restore the part of the long name that was damaged by
2244 // appending the '@nn'.
2245 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2246 #endif /* WIN32 */
2247 function = _Jv_FindSymbolInExecutable (buf + 1);
2248 if (function == NULL)
2250 #ifdef WIN32
2251 strcat (buf, asz_buf);
2252 function = _Jv_FindSymbolInExecutable (buf);
2253 if (function == NULL)
2254 function = _Jv_FindSymbolInExecutable (buf + 1);
2256 if (function == NULL)
2257 #endif /* WIN32 */
2259 jstring str = JvNewStringUTF (name->chars ());
2260 throw new java::lang::UnsatisfiedLinkError (str);
2265 return function;
2268 #ifdef INTERPRETER
2270 // This function is the stub which is used to turn an ordinary (CNI)
2271 // method call into a JNI call.
2272 void
2273 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2275 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2277 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2279 // FIXME: we should mark every reference parameter as a local. For
2280 // now we assume a conservative GC, and we assume that the
2281 // references are on the stack somewhere.
2283 // We cache the value that we find, of course, but if we don't find
2284 // a value we don't cache that fact -- we might subsequently load a
2285 // library which finds the function in question.
2287 // Synchronize on a convenient object to ensure sanity in case two
2288 // threads reach this point for the same function at the same
2289 // time.
2290 JvSynchronize sync (global_ref_table);
2291 if (_this->function == NULL)
2293 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2295 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2296 args_size += sizeof (_this->defining_class);
2298 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2299 _this->self->name,
2300 _this->self->signature,
2301 args_size);
2305 JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2306 ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2307 int offset = 0;
2309 // First argument is always the environment pointer.
2310 real_args[offset++].ptr = env;
2312 // For a static method, we pass in the Class. For non-static
2313 // methods, the `this' argument is already handled.
2314 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2315 real_args[offset++].ptr = _this->defining_class;
2317 // In libgcj, the callee synchronizes.
2318 jobject sync = NULL;
2319 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2321 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2322 sync = _this->defining_class;
2323 else
2324 sync = (jobject) args[0].ptr;
2325 _Jv_MonitorEnter (sync);
2328 // Copy over passed-in arguments.
2329 memcpy (&real_args[offset], args, _this->args_raw_size);
2331 // The actual call to the JNI function.
2332 #if FFI_NATIVE_RAW_API
2333 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2334 ret, real_args);
2335 #else
2336 ffi_java_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2337 ret, real_args);
2338 #endif
2340 // We might need to unwrap a JNI weak reference here.
2341 if (_this->jni_cif.rtype == &ffi_type_pointer)
2343 _Jv_value *val = (_Jv_value *) ret;
2344 val->object_value = unwrap (val->object_value);
2347 if (sync != NULL)
2348 _Jv_MonitorExit (sync);
2350 _Jv_JNI_PopSystemFrame (env);
2353 #endif /* INTERPRETER */
2358 // Invocation API.
2361 // An internal helper function.
2362 static jint
2363 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2364 void *args, jboolean is_daemon)
2366 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2367 java::lang::ThreadGroup *group = NULL;
2369 if (attach)
2371 // FIXME: do we really want to support 1.1?
2372 if (attach->version != JNI_VERSION_1_4
2373 && attach->version != JNI_VERSION_1_2
2374 && attach->version != JNI_VERSION_1_1)
2375 return JNI_EVERSION;
2377 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2378 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2381 // Attaching an already-attached thread is a no-op.
2382 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2383 if (env != NULL)
2385 *penv = reinterpret_cast<void *> (env);
2386 return 0;
2389 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2390 if (env == NULL)
2391 return JNI_ERR;
2392 env->p = &_Jv_JNIFunctions;
2393 env->ex = NULL;
2394 env->klass = NULL;
2395 env->bottom_locals
2396 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2397 + (FRAME_SIZE
2398 * sizeof (jobject)));
2399 env->locals = env->bottom_locals;
2400 if (env->locals == NULL)
2402 _Jv_Free (env);
2403 return JNI_ERR;
2406 env->locals->allocated_p = 0;
2407 env->locals->marker = MARK_SYSTEM;
2408 env->locals->size = FRAME_SIZE;
2409 env->locals->next = NULL;
2411 for (int i = 0; i < env->locals->size; ++i)
2412 env->locals->vec[i] = NULL;
2414 *penv = reinterpret_cast<void *> (env);
2416 // This thread might already be a Java thread -- this function might
2417 // have been called simply to set the new JNIEnv.
2418 if (_Jv_ThreadCurrent () == NULL)
2422 if (is_daemon)
2423 _Jv_AttachCurrentThreadAsDaemon (name, group);
2424 else
2425 _Jv_AttachCurrentThread (name, group);
2427 catch (jthrowable t)
2429 return JNI_ERR;
2432 _Jv_SetCurrentJNIEnv (env);
2434 return 0;
2437 // This is the one actually used by JNI.
2438 jint JNICALL
2439 _Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
2441 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2444 static jint JNICALL
2445 _Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
2446 void *args)
2448 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2451 static jint JNICALL
2452 _Jv_JNI_DestroyJavaVM (JavaVM *vm)
2454 JvAssert (_Jv_the_vm && vm == _Jv_the_vm);
2456 union
2458 JNIEnv *env;
2459 void *env_p;
2462 if (_Jv_ThreadCurrent () != NULL)
2464 jstring main_name;
2465 // This sucks.
2468 main_name = JvNewStringLatin1 ("main");
2470 catch (jthrowable t)
2472 return JNI_ERR;
2475 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name, &env_p,
2476 NULL, false);
2477 if (r < 0)
2478 return r;
2480 else
2481 env = _Jv_GetCurrentJNIEnv ();
2483 _Jv_ThreadWait ();
2485 // Docs say that this always returns an error code.
2486 return JNI_ERR;
2489 jint JNICALL
2490 _Jv_JNI_DetachCurrentThread (JavaVM *)
2492 jint code = _Jv_DetachCurrentThread ();
2493 return code ? JNI_EDETACHED : 0;
2496 static jint JNICALL
2497 _Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
2499 if (_Jv_ThreadCurrent () == NULL)
2501 *penv = NULL;
2502 return JNI_EDETACHED;
2505 #ifdef ENABLE_JVMPI
2506 // Handle JVMPI requests.
2507 if (version == JVMPI_VERSION_1)
2509 *penv = (void *) &_Jv_JVMPI_Interface;
2510 return 0;
2512 #endif
2514 // Handle JVMTI requests
2515 if (version == JVMTI_VERSION_1_0)
2517 *penv = (void *) _Jv_GetJVMTIEnv ();
2518 return 0;
2521 // FIXME: do we really want to support 1.1?
2522 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2523 && version != JNI_VERSION_1_1)
2525 *penv = NULL;
2526 return JNI_EVERSION;
2529 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2530 return 0;
2533 JavaVM *
2534 _Jv_GetJavaVM ()
2536 // FIXME: synchronize
2537 if (! _Jv_the_vm)
2539 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2540 if (nvm != NULL)
2541 nvm->functions = &_Jv_JNI_InvokeFunctions;
2542 _Jv_the_vm = nvm;
2545 // If this is a Java thread, we want to make sure it has an
2546 // associated JNIEnv.
2547 if (_Jv_ThreadCurrent () != NULL)
2549 void *ignore;
2550 _Jv_JNI_AttachCurrentThread (_Jv_the_vm, &ignore, NULL);
2553 return _Jv_the_vm;
2556 static jint JNICALL
2557 _Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
2559 *vm = _Jv_GetJavaVM ();
2560 return *vm == NULL ? JNI_ERR : JNI_OK;
2565 #define RESERVED NULL
2567 struct JNINativeInterface _Jv_JNIFunctions =
2569 RESERVED,
2570 RESERVED,
2571 RESERVED,
2572 RESERVED,
2573 _Jv_JNI_GetVersion, // GetVersion
2574 _Jv_JNI_DefineClass, // DefineClass
2575 _Jv_JNI_FindClass, // FindClass
2576 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2577 _Jv_JNI_FromReflectedField, // FromReflectedField
2578 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2579 _Jv_JNI_GetSuperclass, // GetSuperclass
2580 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2581 _Jv_JNI_ToReflectedField, // ToReflectedField
2582 _Jv_JNI_Throw, // Throw
2583 _Jv_JNI_ThrowNew, // ThrowNew
2584 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2585 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2586 _Jv_JNI_ExceptionClear, // ExceptionClear
2587 _Jv_JNI_FatalError, // FatalError
2589 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2590 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2591 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2592 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2593 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2595 _Jv_JNI_IsSameObject, // IsSameObject
2597 _Jv_JNI_NewLocalRef, // NewLocalRef
2598 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2600 _Jv_JNI_AllocObject, // AllocObject
2601 _Jv_JNI_NewObject, // NewObject
2602 _Jv_JNI_NewObjectV, // NewObjectV
2603 _Jv_JNI_NewObjectA, // NewObjectA
2604 _Jv_JNI_GetObjectClass, // GetObjectClass
2605 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2606 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2608 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2609 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2610 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2611 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2612 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2613 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2614 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2615 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2616 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2617 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2618 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2619 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2620 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2621 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2622 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2623 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2624 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2625 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2626 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2627 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2628 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2629 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2630 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2631 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2632 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2633 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2634 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2635 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2636 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2637 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
2639 // Nonvirtual method invocation functions follow.
2640 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2641 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2642 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2643 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2644 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2645 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2646 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2647 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2648 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2649 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2650 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2651 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2652 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2653 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2654 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2655 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2656 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2657 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2658 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2659 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2660 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2661 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2662 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2663 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2664 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2665 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2666 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2667 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2668 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2669 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2671 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2672 _Jv_JNI_GetField<jobject>, // GetObjectField
2673 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2674 _Jv_JNI_GetField<jbyte>, // GetByteField
2675 _Jv_JNI_GetField<jchar>, // GetCharField
2676 _Jv_JNI_GetField<jshort>, // GetShortField
2677 _Jv_JNI_GetField<jint>, // GetIntField
2678 _Jv_JNI_GetField<jlong>, // GetLongField
2679 _Jv_JNI_GetField<jfloat>, // GetFloatField
2680 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2681 _Jv_JNI_SetField, // SetObjectField
2682 _Jv_JNI_SetField, // SetBooleanField
2683 _Jv_JNI_SetField, // SetByteField
2684 _Jv_JNI_SetField, // SetCharField
2685 _Jv_JNI_SetField, // SetShortField
2686 _Jv_JNI_SetField, // SetIntField
2687 _Jv_JNI_SetField, // SetLongField
2688 _Jv_JNI_SetField, // SetFloatField
2689 _Jv_JNI_SetField, // SetDoubleField
2690 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2692 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2693 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2694 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2695 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2696 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2697 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2698 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2699 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2700 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2701 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2702 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2703 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2704 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2705 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2706 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2707 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2708 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2709 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2710 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2711 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2712 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2713 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2714 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2715 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2716 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2717 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2718 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2719 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2720 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2721 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2723 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2724 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2725 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2726 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2727 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2728 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2729 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2730 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2731 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2732 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2733 _Jv_JNI_SetStaticField, // SetStaticObjectField
2734 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2735 _Jv_JNI_SetStaticField, // SetStaticByteField
2736 _Jv_JNI_SetStaticField, // SetStaticCharField
2737 _Jv_JNI_SetStaticField, // SetStaticShortField
2738 _Jv_JNI_SetStaticField, // SetStaticIntField
2739 _Jv_JNI_SetStaticField, // SetStaticLongField
2740 _Jv_JNI_SetStaticField, // SetStaticFloatField
2741 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2742 _Jv_JNI_NewString, // NewString
2743 _Jv_JNI_GetStringLength, // GetStringLength
2744 _Jv_JNI_GetStringChars, // GetStringChars
2745 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2746 _Jv_JNI_NewStringUTF, // NewStringUTF
2747 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
2748 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
2749 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2750 _Jv_JNI_GetArrayLength, // GetArrayLength
2751 _Jv_JNI_NewObjectArray, // NewObjectArray
2752 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2753 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
2754 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2755 // NewBooleanArray
2756 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2757 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2758 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2759 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2760 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2761 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2762 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2763 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2764 // GetBooleanArrayElements
2765 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2766 // GetByteArrayElements
2767 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2768 // GetCharArrayElements
2769 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2770 // GetShortArrayElements
2771 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2772 // GetIntArrayElements
2773 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2774 // GetLongArrayElements
2775 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2776 // GetFloatArrayElements
2777 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2778 // GetDoubleArrayElements
2779 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2780 // ReleaseBooleanArrayElements
2781 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2782 // ReleaseByteArrayElements
2783 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2784 // ReleaseCharArrayElements
2785 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2786 // ReleaseShortArrayElements
2787 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2788 // ReleaseIntArrayElements
2789 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2790 // ReleaseLongArrayElements
2791 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2792 // ReleaseFloatArrayElements
2793 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2794 // ReleaseDoubleArrayElements
2795 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2796 // GetBooleanArrayRegion
2797 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2798 // GetByteArrayRegion
2799 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2800 // GetCharArrayRegion
2801 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2802 // GetShortArrayRegion
2803 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2804 // GetIntArrayRegion
2805 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2806 // GetLongArrayRegion
2807 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2808 // GetFloatArrayRegion
2809 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2810 // GetDoubleArrayRegion
2811 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2812 // SetBooleanArrayRegion
2813 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2814 // SetByteArrayRegion
2815 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2816 // SetCharArrayRegion
2817 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2818 // SetShortArrayRegion
2819 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2820 // SetIntArrayRegion
2821 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2822 // SetLongArrayRegion
2823 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2824 // SetFloatArrayRegion
2825 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2826 // SetDoubleArrayRegion
2827 _Jv_JNI_RegisterNatives, // RegisterNatives
2828 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2829 _Jv_JNI_MonitorEnter, // MonitorEnter
2830 _Jv_JNI_MonitorExit, // MonitorExit
2831 _Jv_JNI_GetJavaVM, // GetJavaVM
2833 _Jv_JNI_GetStringRegion, // GetStringRegion
2834 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2835 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2836 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2837 _Jv_JNI_GetStringCritical, // GetStringCritical
2838 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
2840 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2841 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
2843 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2845 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2846 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
2847 _Jv_JNI_GetDirectBufferCapacity // GetDirectBufferCapacity
2850 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2852 RESERVED,
2853 RESERVED,
2854 RESERVED,
2856 _Jv_JNI_DestroyJavaVM,
2857 _Jv_JNI_AttachCurrentThread,
2858 _Jv_JNI_DetachCurrentThread,
2859 _Jv_JNI_GetEnv,
2860 _Jv_JNI_AttachCurrentThreadAsDaemon