OSDN Git Service

* gnu/java/net/natPlainDatagramSocketImplWin32.cc:
[pf3gnuchains/gcc-fork.git] / libjava / jni.cc
1 // jni.cc - JNI implementation, including the jump table.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <stdio.h>
14 #include <stddef.h>
15 #include <string.h>
16
17 #include <gcj/cni.h>
18 #include <jvm.h>
19 #include <java-assert.h>
20 #include <jni.h>
21 #ifdef ENABLE_JVMPI
22 #include <jvmpi.h>
23 #endif
24
25 #include <java/lang/Class.h>
26 #include <java/lang/ClassLoader.h>
27 #include <java/lang/Throwable.h>
28 #include <java/lang/ArrayIndexOutOfBoundsException.h>
29 #include <java/lang/StringIndexOutOfBoundsException.h>
30 #include <java/lang/UnsatisfiedLinkError.h>
31 #include <java/lang/InstantiationException.h>
32 #include <java/lang/NoSuchFieldError.h>
33 #include <java/lang/NoSuchMethodError.h>
34 #include <java/lang/reflect/Constructor.h>
35 #include <java/lang/reflect/Method.h>
36 #include <java/lang/reflect/Modifier.h>
37 #include <java/lang/OutOfMemoryError.h>
38 #include <java/lang/Integer.h>
39 #include <java/lang/ThreadGroup.h>
40 #include <java/lang/Thread.h>
41 #include <java/lang/IllegalAccessError.h>
42 #include <java/nio/DirectByteBufferImpl.h>
43 #include <java/util/IdentityHashMap.h>
44 #include <gnu/gcj/RawData.h>
45
46 #include <gcj/method.h>
47 #include <gcj/field.h>
48
49 #include <java-interp.h>
50 #include <java-threads.h>
51
52 using namespace gcj;
53
54 // This enum is used to select different template instantiations in
55 // the invocation code.
56 enum invocation_type
57 {
58   normal,
59   nonvirtual,
60   static_type,
61   constructor
62 };
63
64 // Forward declarations.
65 extern struct JNINativeInterface _Jv_JNIFunctions;
66 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
67
68 // Number of slots in the default frame.  The VM must allow at least
69 // 16.
70 #define FRAME_SIZE 32
71
72 // Mark value indicating this is an overflow frame.
73 #define MARK_NONE    0
74 // Mark value indicating this is a user frame.
75 #define MARK_USER    1
76 // Mark value indicating this is a system frame.
77 #define MARK_SYSTEM  2
78
79 // This structure is used to keep track of local references.
80 struct _Jv_JNI_LocalFrame
81 {
82   // This is true if this frame object represents a pushed frame (eg
83   // from PushLocalFrame).
84   int marker :  2;
85
86   // Number of elements in frame.
87   int size   : 30;
88
89   // Next frame in chain.
90   _Jv_JNI_LocalFrame *next;
91
92   // The elements.  These are allocated using the C "struct hack".
93   jobject vec[0];
94 };
95
96 // This holds a reference count for all local references.
97 static java::util::IdentityHashMap *local_ref_table;
98 // This holds a reference count for all global references.
99 static java::util::IdentityHashMap *global_ref_table;
100
101 // The only VM.
102 static JavaVM *the_vm;
103
104 #ifdef ENABLE_JVMPI
105 // The only JVMPI interface description.
106 static JVMPI_Interface _Jv_JVMPI_Interface;
107
108 static jint
109 jvmpiEnableEvent (jint event_type, void *)
110 {
111   switch (event_type)
112     {
113     case JVMPI_EVENT_OBJECT_ALLOC:
114       _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
115       break;
116
117     case JVMPI_EVENT_THREAD_START:
118       _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
119       break;
120
121     case JVMPI_EVENT_THREAD_END:
122       _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
123       break;
124
125     default:
126       return JVMPI_NOT_AVAILABLE;
127     }
128
129   return JVMPI_SUCCESS;
130 }
131
132 static jint
133 jvmpiDisableEvent (jint event_type, void *)
134 {
135   switch (event_type)
136     {
137     case JVMPI_EVENT_OBJECT_ALLOC:
138       _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
139       break;
140
141     default:
142       return JVMPI_NOT_AVAILABLE;
143     }
144
145   return JVMPI_SUCCESS;
146 }
147 #endif
148
149 \f
150
151 void
152 _Jv_JNI_Init (void)
153 {
154   local_ref_table = new java::util::IdentityHashMap;
155   global_ref_table = new java::util::IdentityHashMap;
156
157 #ifdef ENABLE_JVMPI
158   _Jv_JVMPI_Interface.version = 1;
159   _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
160   _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
161   _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
162   _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
163   _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
164 #endif
165 }
166
167 // Tell the GC that a certain pointer is live.
168 static void
169 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
170 {
171   JvSynchronize sync (ref_table);
172
173   using namespace java::lang;
174   Integer *refcount = (Integer *) ref_table->get (obj);
175   jint val = (refcount == NULL) ? 0 : refcount->intValue ();
176   // FIXME: what about out of memory error?
177   ref_table->put (obj, new Integer (val + 1));
178 }
179
180 // Unmark a pointer.
181 static void
182 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
183 {
184   JvSynchronize sync (ref_table);
185
186   using namespace java::lang;
187   Integer *refcount = (Integer *) ref_table->get (obj);
188   JvAssert (refcount);
189   jint val = refcount->intValue () - 1;
190   JvAssert (val >= 0);
191   if (val == 0)
192     ref_table->remove (obj);
193   else
194     // FIXME: what about out of memory error?
195     ref_table->put (obj, new Integer (val));
196 }
197
198 // "Unwrap" some random non-reference type.  This exists to simplify
199 // other template functions.
200 template<typename T>
201 static T
202 unwrap (T val)
203 {
204   return val;
205 }
206
207 // Unwrap a weak reference, if required.
208 template<typename T>
209 static T *
210 unwrap (T *obj)
211 {
212   using namespace gnu::gcj::runtime;
213   // We can compare the class directly because JNIWeakRef is `final'.
214   // Doing it this way is much faster.
215   if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
216     return obj;
217   JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
218   return reinterpret_cast<T *> (wr->get ());
219 }
220
221 \f
222
223 static jobject
224 (JNICALL _Jv_JNI_NewGlobalRef) (JNIEnv *, jobject obj)
225 {
226   // This seems weird but I think it is correct.
227   obj = unwrap (obj);
228   mark_for_gc (obj, global_ref_table);
229   return obj;
230 }
231
232 static void
233 (JNICALL _Jv_JNI_DeleteGlobalRef) (JNIEnv *, jobject obj)
234 {
235   // This seems weird but I think it is correct.
236   obj = unwrap (obj);
237   unmark_for_gc (obj, global_ref_table);
238 }
239
240 static void
241 (JNICALL _Jv_JNI_DeleteLocalRef) (JNIEnv *env, jobject obj)
242 {
243   _Jv_JNI_LocalFrame *frame;
244
245   // This seems weird but I think it is correct.
246   obj = unwrap (obj);
247
248   for (frame = env->locals; frame != NULL; frame = frame->next)
249     {
250       for (int i = 0; i < frame->size; ++i)
251         {
252           if (frame->vec[i] == obj)
253             {
254               frame->vec[i] = NULL;
255               unmark_for_gc (obj, local_ref_table);
256               return;
257             }
258         }
259
260       // Don't go past a marked frame.
261       JvAssert (frame->marker == MARK_NONE);
262     }
263
264   JvAssert (0);
265 }
266
267 static jint
268 (JNICALL _Jv_JNI_EnsureLocalCapacity) (JNIEnv *env, jint size)
269 {
270   // It is easier to just always allocate a new frame of the requested
271   // size.  This isn't the most efficient thing, but for now we don't
272   // care.  Note that _Jv_JNI_PushLocalFrame relies on this right now.
273
274   _Jv_JNI_LocalFrame *frame;
275   try
276     {
277       frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
278                                                  + size * sizeof (jobject));
279     }
280   catch (jthrowable t)
281     {
282       env->ex = t;
283       return JNI_ERR;
284     }
285
286   frame->marker = MARK_NONE;
287   frame->size = size;
288   memset (&frame->vec[0], 0, size * sizeof (jobject));
289   frame->next = env->locals;
290   env->locals = frame;
291
292   return 0;
293 }
294
295 static jint
296 (JNICALL _Jv_JNI_PushLocalFrame) (JNIEnv *env, jint size)
297 {
298   jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
299   if (r < 0)
300     return r;
301
302   // The new frame is on top.
303   env->locals->marker = MARK_USER;
304
305   return 0;
306 }
307
308 static jobject
309 (JNICALL _Jv_JNI_NewLocalRef) (JNIEnv *env, jobject obj)
310 {
311   // This seems weird but I think it is correct.
312   obj = unwrap (obj);
313
314   // Try to find an open slot somewhere in the topmost frame.
315   _Jv_JNI_LocalFrame *frame = env->locals;
316   bool done = false, set = false;
317   for (; frame != NULL && ! done; frame = frame->next)
318     {
319       for (int i = 0; i < frame->size; ++i)
320         {
321           if (frame->vec[i] == NULL)
322             {
323               set = true;
324               done = true;
325               frame->vec[i] = obj;
326               break;
327             }
328         }
329
330       // If we found a slot, or if the frame we just searched is the
331       // mark frame, then we are done.
332       if (done || frame == NULL || frame->marker != MARK_NONE)
333         break;
334     }
335
336   if (! set)
337     {
338       // No slots, so we allocate a new frame.  According to the spec
339       // we could just die here.  FIXME: return value.
340       _Jv_JNI_EnsureLocalCapacity (env, 16);
341       // We know the first element of the new frame will be ok.
342       env->locals->vec[0] = obj;
343     }
344
345   mark_for_gc (obj, local_ref_table);
346   return obj;
347 }
348
349 static jobject
350 (JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result, int stop)
351 {
352   _Jv_JNI_LocalFrame *rf = env->locals;
353
354   bool done = false;
355   while (rf != NULL && ! done)
356     {
357       for (int i = 0; i < rf->size; ++i)
358         if (rf->vec[i] != NULL)
359           unmark_for_gc (rf->vec[i], local_ref_table);
360
361       // If the frame we just freed is the marker frame, we are done.
362       done = (rf->marker == stop);
363
364       _Jv_JNI_LocalFrame *n = rf->next;
365       // When N==NULL, we've reached the stack-allocated frame, and we
366       // must not free it.  However, we must be sure to clear all its
367       // elements, since we might conceivably reuse it.
368       if (n == NULL)
369         {
370           memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
371           break;
372         }
373
374       _Jv_Free (rf);
375       rf = n;
376     }
377
378   // Update the local frame information.
379   env->locals = rf;
380
381   return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
382 }
383
384 static jobject
385 (JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result)
386 {
387   return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
388 }
389
390 // Make sure an array's type is compatible with the type of the
391 // destination.
392 template<typename T>
393 static bool
394 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
395 {
396   jclass klass = array->getClass()->getComponentType();
397   if (__builtin_expect (klass != K, false))
398     {
399       env->ex = new java::lang::IllegalAccessError ();
400       return false;
401     }
402   else
403     return true;
404 }
405
406 // Pop a `system' frame from the stack.  This is `extern "C"' as it is
407 // used by the compiler.
408 extern "C" void
409 _Jv_JNI_PopSystemFrame (JNIEnv *env)
410 {
411   _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
412
413   if (env->ex)
414     {
415       jthrowable t = env->ex;
416       env->ex = NULL;
417       throw t;
418     }
419 }
420
421 template<typename T> T extract_from_jvalue(jvalue const & t);
422 template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
423 template<> jbyte    extract_from_jvalue(jvalue const & jv) { return jv.b; }
424 template<> jchar    extract_from_jvalue(jvalue const & jv) { return jv.c; }
425 template<> jshort   extract_from_jvalue(jvalue const & jv) { return jv.s; }
426 template<> jint     extract_from_jvalue(jvalue const & jv) { return jv.i; }
427 template<> jlong    extract_from_jvalue(jvalue const & jv) { return jv.j; }
428 template<> jfloat   extract_from_jvalue(jvalue const & jv) { return jv.f; }
429 template<> jdouble  extract_from_jvalue(jvalue const & jv) { return jv.d; }
430 template<> jobject  extract_from_jvalue(jvalue const & jv) { return jv.l; }
431
432
433 // This function is used from other template functions.  It wraps the
434 // return value appropriately; we specialize it so that object returns
435 // are turned into local references.
436 template<typename T>
437 static T
438 wrap_value (JNIEnv *, T value)
439 {
440   return value;
441 }
442
443 // This specialization is used for jobject, jclass, jstring, jarray,
444 // etc.
445 template<typename R, typename T>
446 static T *
447 wrap_value (JNIEnv *env, T *value)
448 {
449   return (value == NULL
450           ? value
451           : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
452 }
453
454 \f
455
456 static jint
457 (JNICALL _Jv_JNI_GetVersion) (JNIEnv *)
458 {
459   return JNI_VERSION_1_4;
460 }
461
462 static jclass
463 (JNICALL _Jv_JNI_DefineClass) (JNIEnv *env, const char *name, jobject loader,
464                                const jbyte *buf, jsize bufLen)
465 {
466   try
467     {
468       loader = unwrap (loader);
469
470       jstring sname = JvNewStringUTF (name);
471       jbyteArray bytes = JvNewByteArray (bufLen);
472
473       jbyte *elts = elements (bytes);
474       memcpy (elts, buf, bufLen * sizeof (jbyte));
475
476       java::lang::ClassLoader *l
477         = reinterpret_cast<java::lang::ClassLoader *> (loader);
478
479       jclass result = l->defineClass (sname, bytes, 0, bufLen);
480       return (jclass) wrap_value (env, result);
481     }
482   catch (jthrowable t)
483     {
484       env->ex = t;
485       return NULL;
486     }
487 }
488
489 static jclass
490 (JNICALL _Jv_JNI_FindClass) (JNIEnv *env, const char *name)
491 {
492   // FIXME: assume that NAME isn't too long.
493   int len = strlen (name);
494   char s[len + 1];
495   for (int i = 0; i <= len; ++i)
496     s[i] = (name[i] == '/') ? '.' : name[i];
497
498   jclass r = NULL;
499   try
500     {
501       // This might throw an out of memory exception.
502       jstring n = JvNewStringUTF (s);
503
504       java::lang::ClassLoader *loader = NULL;
505       if (env->klass != NULL)
506         loader = env->klass->getClassLoaderInternal ();
507
508       if (loader == NULL)
509         {
510           // FIXME: should use getBaseClassLoader, but we don't have that
511           // yet.
512           loader = java::lang::ClassLoader::getSystemClassLoader ();
513         }
514
515       r = loader->loadClass (n);
516     }
517   catch (jthrowable t)
518     {
519       env->ex = t;
520     }
521
522   return (jclass) wrap_value (env, r);
523 }
524
525 static jclass
526 (JNICALL _Jv_JNI_GetSuperclass) (JNIEnv *env, jclass clazz)
527 {
528   return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
529 }
530
531 static jboolean
532 (JNICALL _Jv_JNI_IsAssignableFrom) (JNIEnv *, jclass clazz1, jclass clazz2)
533 {
534   return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
535 }
536
537 static jint
538 (JNICALL _Jv_JNI_Throw) (JNIEnv *env, jthrowable obj)
539 {
540   // We check in case the user did some funky cast.
541   obj = unwrap (obj);
542   JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
543   env->ex = obj;
544   return 0;
545 }
546
547 static jint
548 (JNICALL _Jv_JNI_ThrowNew) (JNIEnv *env, jclass clazz, const char *message)
549 {
550   using namespace java::lang::reflect;
551
552   clazz = unwrap (clazz);
553   JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
554
555   int r = JNI_OK;
556   try
557     {
558       JArray<jclass> *argtypes
559         = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
560                                                NULL);
561
562       jclass *elts = elements (argtypes);
563       elts[0] = &StringClass;
564
565       Constructor *cons = clazz->getConstructor (argtypes);
566
567       jobjectArray values = JvNewObjectArray (1, &StringClass, NULL);
568       jobject *velts = elements (values);
569       velts[0] = JvNewStringUTF (message);
570
571       jobject obj = cons->newInstance (values);
572
573       env->ex = reinterpret_cast<jthrowable> (obj);
574     }
575   catch (jthrowable t)
576     {
577       env->ex = t;
578       r = JNI_ERR;
579     }
580
581   return r;
582 }
583
584 static jthrowable
585 (JNICALL _Jv_JNI_ExceptionOccurred) (JNIEnv *env)
586 {
587   return (jthrowable) wrap_value (env, env->ex);
588 }
589
590 static void
591 (JNICALL _Jv_JNI_ExceptionDescribe) (JNIEnv *env)
592 {
593   if (env->ex != NULL)
594     env->ex->printStackTrace();
595 }
596
597 static void
598 (JNICALL _Jv_JNI_ExceptionClear) (JNIEnv *env)
599 {
600   env->ex = NULL;
601 }
602
603 static jboolean
604 (JNICALL _Jv_JNI_ExceptionCheck) (JNIEnv *env)
605 {
606   return env->ex != NULL;
607 }
608
609 static void
610 (JNICALL _Jv_JNI_FatalError) (JNIEnv *, const char *message)
611 {
612   JvFail (message);
613 }
614
615 \f
616
617 static jboolean
618 (JNICALL _Jv_JNI_IsSameObject) (JNIEnv *, jobject obj1, jobject obj2)
619 {
620   return unwrap (obj1) == unwrap (obj2);
621 }
622
623 static jobject
624 (JNICALL _Jv_JNI_AllocObject) (JNIEnv *env, jclass clazz)
625 {
626   jobject obj = NULL;
627   using namespace java::lang::reflect;
628
629   try
630     {
631       clazz = unwrap (clazz);
632       JvAssert (clazz && ! clazz->isArray ());
633       if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
634         env->ex = new java::lang::InstantiationException ();
635       else
636         obj = JvAllocObject (clazz);
637     }
638   catch (jthrowable t)
639     {
640       env->ex = t;
641     }
642
643   return wrap_value (env, obj);
644 }
645
646 static jclass
647 (JNICALL _Jv_JNI_GetObjectClass) (JNIEnv *env, jobject obj)
648 {
649   obj = unwrap (obj);
650   JvAssert (obj);
651   return (jclass) wrap_value (env, obj->getClass());
652 }
653
654 static jboolean
655 (JNICALL _Jv_JNI_IsInstanceOf) (JNIEnv *, jobject obj, jclass clazz)
656 {
657   return unwrap (clazz)->isInstance(unwrap (obj));
658 }
659
660 \f
661
662 //
663 // This section concerns method invocation.
664 //
665
666 template<jboolean is_static>
667 static jmethodID
668 (JNICALL _Jv_JNI_GetAnyMethodID) (JNIEnv *env, jclass clazz,
669                                   const char *name, const char *sig)
670 {
671   try
672     {
673       clazz = unwrap (clazz);
674       _Jv_InitClass (clazz);
675
676       _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
677
678       // FIXME: assume that SIG isn't too long.
679       int len = strlen (sig);
680       char s[len + 1];
681       for (int i = 0; i <= len; ++i)
682         s[i] = (sig[i] == '/') ? '.' : sig[i];
683       _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
684
685       JvAssert (! clazz->isPrimitive());
686
687       using namespace java::lang::reflect;
688
689       while (clazz != NULL)
690         {
691           jint count = JvNumMethods (clazz);
692           jmethodID meth = JvGetFirstMethod (clazz);
693
694           for (jint i = 0; i < count; ++i)
695             {
696               if (((is_static && Modifier::isStatic (meth->accflags))
697                    || (! is_static && ! Modifier::isStatic (meth->accflags)))
698                   && _Jv_equalUtf8Consts (meth->name, name_u)
699                   && _Jv_equalUtf8Consts (meth->signature, sig_u))
700                 return meth;
701
702               meth = meth->getNextMethod();
703             }
704
705           clazz = clazz->getSuperclass ();
706         }
707
708       env->ex = new java::lang::NoSuchMethodError ();
709     }
710   catch (jthrowable t)
711     {
712       env->ex = t;
713     }
714
715   return NULL;
716 }
717
718 // This is a helper function which turns a va_list into an array of
719 // `jvalue's.  It needs signature information in order to do its work.
720 // The array of values must already be allocated.
721 static void
722 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
723 {
724   jclass *arg_elts = elements (arg_types);
725   for (int i = 0; i < arg_types->length; ++i)
726     {
727       // Here we assume that sizeof(int) >= sizeof(jint), because we
728       // use `int' when decoding the varargs.  Likewise for
729       // float, and double.  Also we assume that sizeof(jlong) >=
730       // sizeof(int), i.e. that jlong values are not further
731       // promoted.
732       JvAssert (sizeof (int) >= sizeof (jint));
733       JvAssert (sizeof (jlong) >= sizeof (int));
734       JvAssert (sizeof (double) >= sizeof (jfloat));
735       JvAssert (sizeof (double) >= sizeof (jdouble));
736       if (arg_elts[i] == JvPrimClass (byte))
737         values[i].b = (jbyte) va_arg (vargs, int);
738       else if (arg_elts[i] == JvPrimClass (short))
739         values[i].s = (jshort) va_arg (vargs, int);
740       else if (arg_elts[i] == JvPrimClass (int))
741         values[i].i = (jint) va_arg (vargs, int);
742       else if (arg_elts[i] == JvPrimClass (long))
743         values[i].j = (jlong) va_arg (vargs, jlong);
744       else if (arg_elts[i] == JvPrimClass (float))
745         values[i].f = (jfloat) va_arg (vargs, double);
746       else if (arg_elts[i] == JvPrimClass (double))
747         values[i].d = (jdouble) va_arg (vargs, double);
748       else if (arg_elts[i] == JvPrimClass (boolean))
749         values[i].z = (jboolean) va_arg (vargs, int);
750       else if (arg_elts[i] == JvPrimClass (char))
751         values[i].c = (jchar) va_arg (vargs, int);
752       else
753         {
754           // An object.
755           values[i].l = unwrap (va_arg (vargs, jobject));
756         }
757     }
758 }
759
760 // This can call any sort of method: virtual, "nonvirtual", static, or
761 // constructor.
762 template<typename T, invocation_type style>
763 static T
764 (JNICALL _Jv_JNI_CallAnyMethodV) (JNIEnv *env, jobject obj, jclass klass,
765                                   jmethodID id, va_list vargs)
766 {
767   obj = unwrap (obj);
768   klass = unwrap (klass);
769
770   jclass decl_class = klass ? klass : obj->getClass ();
771   JvAssert (decl_class != NULL);
772
773   jclass return_type;
774   JArray<jclass> *arg_types;
775
776   try
777     {
778       _Jv_GetTypesFromSignature (id, decl_class,
779                                  &arg_types, &return_type);
780
781       jvalue args[arg_types->length];
782       array_from_valist (args, arg_types, vargs);
783
784       // For constructors we need to pass the Class we are instantiating.
785       if (style == constructor)
786         return_type = klass;
787
788       jvalue result;
789       _Jv_CallAnyMethodA (obj, return_type, id,
790                           style == constructor,
791                           style == normal,
792                           arg_types, args, &result);
793
794       return wrap_value (env, extract_from_jvalue<T>(result));
795     }
796   catch (jthrowable t)
797     {
798       env->ex = t;
799     }
800
801   return wrap_value (env, (T) 0);
802 }
803
804 template<typename T, invocation_type style>
805 static T
806 (JNICALL _Jv_JNI_CallAnyMethod) (JNIEnv *env, jobject obj, jclass klass,
807                                  jmethodID method, ...)
808 {
809   va_list args;
810   T result;
811
812   va_start (args, method);
813   result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
814   va_end (args);
815
816   return result;
817 }
818
819 template<typename T, invocation_type style>
820 static T
821 (JNICALL _Jv_JNI_CallAnyMethodA) (JNIEnv *env, jobject obj, jclass klass,
822                                   jmethodID id, jvalue *args)
823 {
824   obj = unwrap (obj);
825   klass = unwrap (klass);
826
827   jclass decl_class = klass ? klass : obj->getClass ();
828   JvAssert (decl_class != NULL);
829
830   jclass return_type;
831   JArray<jclass> *arg_types;
832   try
833     {
834       _Jv_GetTypesFromSignature (id, decl_class,
835                                  &arg_types, &return_type);
836
837       // For constructors we need to pass the Class we are instantiating.
838       if (style == constructor)
839         return_type = klass;
840
841       // Unwrap arguments as required.  Eww.
842       jclass *type_elts = elements (arg_types);
843       jvalue arg_copy[arg_types->length];
844       for (int i = 0; i < arg_types->length; ++i)
845         {
846           if (type_elts[i]->isPrimitive ())
847             arg_copy[i] = args[i];
848           else
849             arg_copy[i].l = unwrap (args[i].l);
850         }
851
852       jvalue result;
853       _Jv_CallAnyMethodA (obj, return_type, id,
854                           style == constructor,
855                           style == normal,
856                           arg_types, arg_copy, &result);
857
858       return wrap_value (env, extract_from_jvalue<T>(result));
859     }
860   catch (jthrowable t)
861     {
862       env->ex = t;
863     }
864
865   return wrap_value (env, (T) 0);
866 }
867
868 template<invocation_type style>
869 static void
870 (JNICALL _Jv_JNI_CallAnyVoidMethodV) (JNIEnv *env, jobject obj, jclass klass,
871                                       jmethodID id, va_list vargs)
872 {
873   obj = unwrap (obj);
874   klass = unwrap (klass);
875
876   jclass decl_class = klass ? klass : obj->getClass ();
877   JvAssert (decl_class != NULL);
878
879   jclass return_type;
880   JArray<jclass> *arg_types;
881   try
882     {
883       _Jv_GetTypesFromSignature (id, decl_class,
884                                  &arg_types, &return_type);
885
886       jvalue args[arg_types->length];
887       array_from_valist (args, arg_types, vargs);
888
889       // For constructors we need to pass the Class we are instantiating.
890       if (style == constructor)
891         return_type = klass;
892
893       _Jv_CallAnyMethodA (obj, return_type, id,
894                           style == constructor,
895                           style == normal,
896                           arg_types, args, NULL);
897     }
898   catch (jthrowable t)
899     {
900       env->ex = t;
901     }
902 }
903
904 template<invocation_type style>
905 static void
906 (JNICALL _Jv_JNI_CallAnyVoidMethod) (JNIEnv *env, jobject obj, jclass klass,
907                                      jmethodID method, ...)
908 {
909   va_list args;
910
911   va_start (args, method);
912   _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
913   va_end (args);
914 }
915
916 template<invocation_type style>
917 static void
918 (JNICALL _Jv_JNI_CallAnyVoidMethodA) (JNIEnv *env, jobject obj, jclass klass,
919                                       jmethodID id, jvalue *args)
920 {
921   jclass decl_class = klass ? klass : obj->getClass ();
922   JvAssert (decl_class != NULL);
923
924   jclass return_type;
925   JArray<jclass> *arg_types;
926   try
927     {
928       _Jv_GetTypesFromSignature (id, decl_class,
929                                  &arg_types, &return_type);
930
931       // Unwrap arguments as required.  Eww.
932       jclass *type_elts = elements (arg_types);
933       jvalue arg_copy[arg_types->length];
934       for (int i = 0; i < arg_types->length; ++i)
935         {
936           if (type_elts[i]->isPrimitive ())
937             arg_copy[i] = args[i];
938           else
939             arg_copy[i].l = unwrap (args[i].l);
940         }
941
942       _Jv_CallAnyMethodA (obj, return_type, id,
943                           style == constructor,
944                           style == normal,
945                           arg_types, args, NULL);
946     }
947   catch (jthrowable t)
948     {
949       env->ex = t;
950     }
951 }
952
953 // Functions with this signature are used to implement functions in
954 // the CallMethod family.
955 template<typename T>
956 static T
957 (JNICALL _Jv_JNI_CallMethodV) (JNIEnv *env, jobject obj, 
958                                jmethodID id, va_list args)
959 {
960   return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
961 }
962
963 // Functions with this signature are used to implement functions in
964 // the CallMethod family.
965 template<typename T>
966 static T
967 (JNICALL _Jv_JNI_CallMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
968 {
969   va_list args;
970   T result;
971
972   va_start (args, id);
973   result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
974   va_end (args);
975
976   return result;
977 }
978
979 // Functions with this signature are used to implement functions in
980 // the CallMethod family.
981 template<typename T>
982 static T
983 (JNICALL _Jv_JNI_CallMethodA) (JNIEnv *env, jobject obj, 
984                                jmethodID id, jvalue *args)
985 {
986   return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
987 }
988
989 static void
990 (JNICALL _Jv_JNI_CallVoidMethodV) (JNIEnv *env, jobject obj, 
991                                    jmethodID id, va_list args)
992 {
993   _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
994 }
995
996 static void
997 (JNICALL _Jv_JNI_CallVoidMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
998 {
999   va_list args;
1000
1001   va_start (args, id);
1002   _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1003   va_end (args);
1004 }
1005
1006 static void
1007 (JNICALL _Jv_JNI_CallVoidMethodA) (JNIEnv *env, jobject obj, 
1008                                    jmethodID id, jvalue *args)
1009 {
1010   _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1011 }
1012
1013 // Functions with this signature are used to implement functions in
1014 // the CallStaticMethod family.
1015 template<typename T>
1016 static T
1017 (JNICALL _Jv_JNI_CallStaticMethodV) (JNIEnv *env, jclass klass,
1018                                      jmethodID id, va_list args)
1019 {
1020   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1021   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1022
1023   return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1024 }
1025
1026 // Functions with this signature are used to implement functions in
1027 // the CallStaticMethod family.
1028 template<typename T>
1029 static T
1030 (JNICALL _Jv_JNI_CallStaticMethod) (JNIEnv *env, jclass klass, 
1031                                     jmethodID id, ...)
1032 {
1033   va_list args;
1034   T result;
1035
1036   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1037   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1038
1039   va_start (args, id);
1040   result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1041                                                    id, args);
1042   va_end (args);
1043
1044   return result;
1045 }
1046
1047 // Functions with this signature are used to implement functions in
1048 // the CallStaticMethod family.
1049 template<typename T>
1050 static T
1051 (JNICALL _Jv_JNI_CallStaticMethodA) (JNIEnv *env, jclass klass, jmethodID id,
1052                                      jvalue *args)
1053 {
1054   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1055   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1056
1057   return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1058 }
1059
1060 static void
1061 (JNICALL _Jv_JNI_CallStaticVoidMethodV) (JNIEnv *env, jclass klass, 
1062                                          jmethodID id, va_list args)
1063 {
1064   _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1065 }
1066
1067 static void
1068 (JNICALL _Jv_JNI_CallStaticVoidMethod) (JNIEnv *env, jclass klass, 
1069                                         jmethodID id, ...)
1070 {
1071   va_list args;
1072
1073   va_start (args, id);
1074   _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1075   va_end (args);
1076 }
1077
1078 static void
1079 (JNICALL _Jv_JNI_CallStaticVoidMethodA) (JNIEnv *env, jclass klass, 
1080                                          jmethodID id, jvalue *args)
1081 {
1082   _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1083 }
1084
1085 static jobject
1086 (JNICALL _Jv_JNI_NewObjectV) (JNIEnv *env, jclass klass,
1087                               jmethodID id, va_list args)
1088 {
1089   JvAssert (klass && ! klass->isArray ());
1090   JvAssert (! strcmp (id->name->data, "<init>")
1091             && id->signature->length > 2
1092             && id->signature->data[0] == '('
1093             && ! strcmp (&id->signature->data[id->signature->length - 2],
1094                          ")V"));
1095
1096   return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1097                                                        id, args);
1098 }
1099
1100 static jobject
1101 (JNICALL _Jv_JNI_NewObject) (JNIEnv *env, jclass klass, jmethodID id, ...)
1102 {
1103   JvAssert (klass && ! klass->isArray ());
1104   JvAssert (! strcmp (id->name->data, "<init>")
1105             && id->signature->length > 2
1106             && id->signature->data[0] == '('
1107             && ! strcmp (&id->signature->data[id->signature->length - 2],
1108                          ")V"));
1109
1110   va_list args;
1111   jobject result;
1112
1113   va_start (args, id);
1114   result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1115                                                          id, args);
1116   va_end (args);
1117
1118   return result;
1119 }
1120
1121 static jobject
1122 (JNICALL _Jv_JNI_NewObjectA) (JNIEnv *env, jclass klass, jmethodID id,
1123                               jvalue *args)
1124 {
1125   JvAssert (klass && ! klass->isArray ());
1126   JvAssert (! strcmp (id->name->data, "<init>")
1127             && id->signature->length > 2
1128             && id->signature->data[0] == '('
1129             && ! strcmp (&id->signature->data[id->signature->length - 2],
1130                          ")V"));
1131
1132   return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1133                                                        id, args);
1134 }
1135
1136 \f
1137
1138 template<typename T>
1139 static T
1140 (JNICALL _Jv_JNI_GetField) (JNIEnv *env, jobject obj, jfieldID field)
1141 {
1142   obj = unwrap (obj);
1143   JvAssert (obj);
1144   T *ptr = (T *) ((char *) obj + field->getOffset ());
1145   return wrap_value (env, *ptr);
1146 }
1147
1148 template<typename T>
1149 static void
1150 (JNICALL _Jv_JNI_SetField) (JNIEnv *, jobject obj, jfieldID field, T value)
1151 {
1152   obj = unwrap (obj);
1153   value = unwrap (value);
1154
1155   JvAssert (obj);
1156   T *ptr = (T *) ((char *) obj + field->getOffset ());
1157   *ptr = value;
1158 }
1159
1160 template<jboolean is_static>
1161 static jfieldID
1162 (JNICALL _Jv_JNI_GetAnyFieldID) (JNIEnv *env, jclass clazz,
1163                                  const char *name, const char *sig)
1164 {
1165   try
1166     {
1167       clazz = unwrap (clazz);
1168
1169       _Jv_InitClass (clazz);
1170
1171       _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1172
1173       // FIXME: assume that SIG isn't too long.
1174       int len = strlen (sig);
1175       char s[len + 1];
1176       for (int i = 0; i <= len; ++i)
1177         s[i] = (sig[i] == '/') ? '.' : sig[i];
1178       jclass field_class = _Jv_FindClassFromSignature ((char *) s, NULL);
1179
1180       // FIXME: what if field_class == NULL?
1181
1182       java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1183       while (clazz != NULL)
1184         {
1185           // We acquire the class lock so that fields aren't resolved
1186           // while we are running.
1187           JvSynchronize sync (clazz);
1188
1189           jint count = (is_static
1190                         ? JvNumStaticFields (clazz)
1191                         : JvNumInstanceFields (clazz));
1192           jfieldID field = (is_static
1193                             ? JvGetFirstStaticField (clazz)
1194                             : JvGetFirstInstanceField (clazz));
1195           for (jint i = 0; i < count; ++i)
1196             {
1197               _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1198
1199               // The field might be resolved or it might not be.  It
1200               // is much simpler to always resolve it.
1201               _Jv_ResolveField (field, loader);
1202               if (_Jv_equalUtf8Consts (f_name, a_name)
1203                   && field->getClass() == field_class)
1204                 return field;
1205
1206               field = field->getNextField ();
1207             }
1208
1209           clazz = clazz->getSuperclass ();
1210         }
1211
1212       env->ex = new java::lang::NoSuchFieldError ();
1213     }
1214   catch (jthrowable t)
1215     {
1216       env->ex = t;
1217     }
1218   return NULL;
1219 }
1220
1221 template<typename T>
1222 static T
1223 (JNICALL _Jv_JNI_GetStaticField) (JNIEnv *env, jclass, jfieldID field)
1224 {
1225   T *ptr = (T *) field->u.addr;
1226   return wrap_value (env, *ptr);
1227 }
1228
1229 template<typename T>
1230 static void
1231 (JNICALL _Jv_JNI_SetStaticField) (JNIEnv *, jclass, jfieldID field, T value)
1232 {
1233   value = unwrap (value);
1234   T *ptr = (T *) field->u.addr;
1235   *ptr = value;
1236 }
1237
1238 static jstring
1239 (JNICALL _Jv_JNI_NewString) (JNIEnv *env, const jchar *unichars, jsize len)
1240 {
1241   try
1242     {
1243       jstring r = _Jv_NewString (unichars, len);
1244       return (jstring) wrap_value (env, r);
1245     }
1246   catch (jthrowable t)
1247     {
1248       env->ex = t;
1249       return NULL;
1250     }
1251 }
1252
1253 static jsize
1254 (JNICALL _Jv_JNI_GetStringLength) (JNIEnv *, jstring string)
1255 {
1256   return unwrap (string)->length();
1257 }
1258
1259 static const jchar *
1260 (JNICALL _Jv_JNI_GetStringChars) (JNIEnv *, jstring string, jboolean *isCopy)
1261 {
1262   string = unwrap (string);
1263   jchar *result = _Jv_GetStringChars (string);
1264   mark_for_gc (string, global_ref_table);
1265   if (isCopy)
1266     *isCopy = false;
1267   return (const jchar *) result;
1268 }
1269
1270 static void
1271 (JNICALL _Jv_JNI_ReleaseStringChars) (JNIEnv *, jstring string, const jchar *)
1272 {
1273   unmark_for_gc (unwrap (string), global_ref_table);
1274 }
1275
1276 static jstring
1277 (JNICALL _Jv_JNI_NewStringUTF) (JNIEnv *env, const char *bytes)
1278 {
1279   try
1280     {
1281       jstring result = JvNewStringUTF (bytes);
1282       return (jstring) wrap_value (env, result);
1283     }
1284   catch (jthrowable t)
1285     {
1286       env->ex = t;
1287       return NULL;
1288     }
1289 }
1290
1291 static jsize
1292 (JNICALL _Jv_JNI_GetStringUTFLength) (JNIEnv *, jstring string)
1293 {
1294   return JvGetStringUTFLength (unwrap (string));
1295 }
1296
1297 static const char *
1298 (JNICALL _Jv_JNI_GetStringUTFChars) (JNIEnv *env, jstring string, 
1299                                      jboolean *isCopy)
1300 {
1301   try
1302     {
1303       string = unwrap (string);
1304       if (string == NULL)
1305         return NULL;
1306       jsize len = JvGetStringUTFLength (string);
1307       char *r = (char *) _Jv_Malloc (len + 1);
1308       JvGetStringUTFRegion (string, 0, string->length(), r);
1309       r[len] = '\0';
1310
1311       if (isCopy)
1312         *isCopy = true;
1313
1314       return (const char *) r;
1315     }
1316   catch (jthrowable t)
1317     {
1318       env->ex = t;
1319       return NULL;
1320     }
1321 }
1322
1323 static void
1324 (JNICALL _Jv_JNI_ReleaseStringUTFChars) (JNIEnv *, jstring, const char *utf)
1325 {
1326   _Jv_Free ((void *) utf);
1327 }
1328
1329 static void
1330 (JNICALL _Jv_JNI_GetStringRegion) (JNIEnv *env, jstring string, jsize start, 
1331                                    jsize len, jchar *buf)
1332 {
1333   string = unwrap (string);
1334   jchar *result = _Jv_GetStringChars (string);
1335   if (start < 0 || start > string->length ()
1336       || len < 0 || start + len > string->length ())
1337     {
1338       try
1339         {
1340           env->ex = new java::lang::StringIndexOutOfBoundsException ();
1341         }
1342       catch (jthrowable t)
1343         {
1344           env->ex = t;
1345         }
1346     }
1347   else
1348     memcpy (buf, &result[start], len * sizeof (jchar));
1349 }
1350
1351 static void
1352 (JNICALL _Jv_JNI_GetStringUTFRegion) (JNIEnv *env, jstring str, jsize start,
1353                                       jsize len, char *buf)
1354 {
1355   str = unwrap (str);
1356     
1357   if (start < 0 || start > str->length ()
1358       || len < 0 || start + len > str->length ())
1359     {
1360       try
1361         {
1362           env->ex = new java::lang::StringIndexOutOfBoundsException ();
1363         }
1364       catch (jthrowable t)
1365         {
1366           env->ex = t;
1367         }
1368     }
1369   else
1370     _Jv_GetStringUTFRegion (str, start, len, buf);
1371 }
1372
1373 static const jchar *
1374 (JNICALL _Jv_JNI_GetStringCritical) (JNIEnv *, jstring str, jboolean *isCopy)
1375 {
1376   jchar *result = _Jv_GetStringChars (unwrap (str));
1377   if (isCopy)
1378     *isCopy = false;
1379   return result;
1380 }
1381
1382 static void
1383 (JNICALL _Jv_JNI_ReleaseStringCritical) (JNIEnv *, jstring, const jchar *)
1384 {
1385   // Nothing.
1386 }
1387
1388 static jsize
1389 (JNICALL _Jv_JNI_GetArrayLength) (JNIEnv *, jarray array)
1390 {
1391   return unwrap (array)->length;
1392 }
1393
1394 static jarray
1395 (JNICALL _Jv_JNI_NewObjectArray) (JNIEnv *env, jsize length, 
1396                                   jclass elementClass, jobject init)
1397 {
1398   try
1399     {
1400       elementClass = unwrap (elementClass);
1401       init = unwrap (init);
1402
1403       _Jv_CheckCast (elementClass, init);
1404       jarray result = JvNewObjectArray (length, elementClass, init);
1405       return (jarray) wrap_value (env, result);
1406     }
1407   catch (jthrowable t)
1408     {
1409       env->ex = t;
1410       return NULL;
1411     }
1412 }
1413
1414 static jobject
1415 (JNICALL _Jv_JNI_GetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
1416                                          jsize index)
1417 {
1418   if ((unsigned) index >= (unsigned) array->length)
1419     _Jv_ThrowBadArrayIndex (index);
1420   jobject *elts = elements (unwrap (array));
1421   return wrap_value (env, elts[index]);
1422 }
1423
1424 static void
1425 (JNICALL _Jv_JNI_SetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
1426                                          jsize index, jobject value)
1427 {
1428   try
1429     {
1430       array = unwrap (array);
1431       value = unwrap (value);
1432
1433       _Jv_CheckArrayStore (array, value);
1434       if ((unsigned) index >= (unsigned) array->length)
1435         _Jv_ThrowBadArrayIndex (index);
1436       jobject *elts = elements (array);
1437       elts[index] = value;
1438     }
1439   catch (jthrowable t)
1440     {
1441       env->ex = t;
1442     }
1443 }
1444
1445 template<typename T, jclass K>
1446 static JArray<T> *
1447 (JNICALL _Jv_JNI_NewPrimitiveArray) (JNIEnv *env, jsize length)
1448 {
1449   try
1450     {
1451       return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1452     }
1453   catch (jthrowable t)
1454     {
1455       env->ex = t;
1456       return NULL;
1457     }
1458 }
1459
1460 template<typename T, jclass K>
1461 static T *
1462 (JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
1463                                              jboolean *isCopy)
1464 {
1465   array = unwrap (array);
1466   if (! _Jv_JNI_check_types (env, array, K))
1467     return NULL;
1468   T *elts = elements (array);
1469   if (isCopy)
1470     {
1471       // We elect never to copy.
1472       *isCopy = false;
1473     }
1474   mark_for_gc (array, global_ref_table);
1475   return elts;
1476 }
1477
1478 template<typename T, jclass K>
1479 static void
1480 (JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
1481                                                  T *, jint /* mode */)
1482 {
1483   array = unwrap (array);
1484   _Jv_JNI_check_types (env, array, K);
1485   // Note that we ignore MODE.  We can do this because we never copy
1486   // the array elements.  My reading of the JNI documentation is that
1487   // this is an option for the implementor.
1488   unmark_for_gc (array, global_ref_table);
1489 }
1490
1491 template<typename T, jclass K>
1492 static void
1493 (JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
1494                                            jsize start, jsize len,
1495                                  T *buf)
1496 {
1497   array = unwrap (array);
1498   if (! _Jv_JNI_check_types (env, array, K))
1499     return;
1500
1501   // The cast to unsigned lets us save a comparison.
1502   if (start < 0 || len < 0
1503       || (unsigned long) (start + len) > (unsigned long) array->length)
1504     {
1505       try
1506         {
1507           // FIXME: index.
1508           env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1509         }
1510       catch (jthrowable t)
1511         {
1512           // Could have thown out of memory error.
1513           env->ex = t;
1514         }
1515     }
1516   else
1517     {
1518       T *elts = elements (array) + start;
1519       memcpy (buf, elts, len * sizeof (T));
1520     }
1521 }
1522
1523 template<typename T, jclass K>
1524 static void
1525 (JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
1526                                            jsize start, jsize len, T *buf)
1527 {
1528   array = unwrap (array);
1529   if (! _Jv_JNI_check_types (env, array, K))
1530     return;
1531
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)
1535     {
1536       try
1537         {
1538           // FIXME: index.
1539           env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1540         }
1541       catch (jthrowable t)
1542         {
1543           env->ex = t;
1544         }
1545     }
1546   else
1547     {
1548       T *elts = elements (array) + start;
1549       memcpy (elts, buf, len * sizeof (T));
1550     }
1551 }
1552
1553 static void *
1554 (JNICALL _Jv_JNI_GetPrimitiveArrayCritical) (JNIEnv *, jarray array,
1555                                              jboolean *isCopy)
1556 {
1557   array = unwrap (array);
1558   // FIXME: does this work?
1559   jclass klass = array->getClass()->getComponentType();
1560   JvAssert (klass->isPrimitive ());
1561   char *r = _Jv_GetArrayElementFromElementType (array, klass);
1562   if (isCopy)
1563     *isCopy = false;
1564   return r;
1565 }
1566
1567 static void
1568 (JNICALL _Jv_JNI_ReleasePrimitiveArrayCritical) (JNIEnv *, jarray, void *, jint)
1569 {
1570   // Nothing.
1571 }
1572
1573 static jint
1574 (JNICALL _Jv_JNI_MonitorEnter) (JNIEnv *env, jobject obj)
1575 {
1576   try
1577     {
1578       _Jv_MonitorEnter (unwrap (obj));
1579       return 0;
1580     }
1581   catch (jthrowable t)
1582     {
1583       env->ex = t;
1584     }
1585   return JNI_ERR;
1586 }
1587
1588 static jint
1589 (JNICALL _Jv_JNI_MonitorExit) (JNIEnv *env, jobject obj)
1590 {
1591   try
1592     {
1593       _Jv_MonitorExit (unwrap (obj));
1594       return 0;
1595     }
1596   catch (jthrowable t)
1597     {
1598       env->ex = t;
1599     }
1600   return JNI_ERR;
1601 }
1602
1603 // JDK 1.2
1604 jobject
1605 (JNICALL _Jv_JNI_ToReflectedField) (JNIEnv *env, jclass cls, jfieldID fieldID,
1606                                     jboolean)
1607 {
1608   try
1609     {
1610       cls = unwrap (cls);
1611       java::lang::reflect::Field *field = new java::lang::reflect::Field();
1612       field->declaringClass = cls;
1613       field->offset = (char*) fieldID - (char *) cls->fields;
1614       field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1615       return wrap_value (env, field);
1616     }
1617   catch (jthrowable t)
1618     {
1619       env->ex = t;
1620     }
1621   return NULL;
1622 }
1623
1624 // JDK 1.2
1625 static jfieldID
1626 (JNICALL _Jv_JNI_FromReflectedField) (JNIEnv *, jobject f)
1627 {
1628   using namespace java::lang::reflect;
1629
1630   f = unwrap (f);
1631   Field *field = reinterpret_cast<Field *> (f);
1632   return _Jv_FromReflectedField (field);
1633 }
1634
1635 jobject
1636 (JNICALL _Jv_JNI_ToReflectedMethod) (JNIEnv *env, jclass klass, jmethodID id,
1637                                      jboolean)
1638 {
1639   using namespace java::lang::reflect;
1640
1641   jobject result = NULL;
1642   klass = unwrap (klass);
1643
1644   try
1645     {
1646       if (_Jv_equalUtf8Consts (id->name, init_name))
1647         {
1648           // A constructor.
1649           Constructor *cons = new Constructor ();
1650           cons->offset = (char *) id - (char *) &klass->methods;
1651           cons->declaringClass = klass;
1652           result = cons;
1653         }
1654       else
1655         {
1656           Method *meth = new Method ();
1657           meth->offset = (char *) id - (char *) &klass->methods;
1658           meth->declaringClass = klass;
1659           result = meth;
1660         }
1661     }
1662   catch (jthrowable t)
1663     {
1664       env->ex = t;
1665     }
1666
1667   return wrap_value (env, result);
1668 }
1669
1670 static jmethodID
1671 (JNICALL _Jv_JNI_FromReflectedMethod) (JNIEnv *, jobject method)
1672 {
1673   using namespace java::lang::reflect;
1674   method = unwrap (method);
1675   if (Method::class$.isInstance (method))
1676     return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1677   return
1678     _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1679 }
1680
1681 // JDK 1.2.
1682 jweak
1683 (JNICALL _Jv_JNI_NewWeakGlobalRef) (JNIEnv *env, jobject obj)
1684 {
1685   using namespace gnu::gcj::runtime;
1686   JNIWeakRef *ref = NULL;
1687
1688   try
1689     {
1690       // This seems weird but I think it is correct.
1691       obj = unwrap (obj);
1692       ref = new JNIWeakRef (obj);
1693       mark_for_gc (ref, global_ref_table);
1694     }
1695   catch (jthrowable t)
1696     {
1697       env->ex = t;
1698     }
1699
1700   return reinterpret_cast<jweak> (ref);
1701 }
1702
1703 void
1704 (JNICALL _Jv_JNI_DeleteWeakGlobalRef) (JNIEnv *, jweak obj)
1705 {
1706   using namespace gnu::gcj::runtime;
1707   JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1708   unmark_for_gc (ref, global_ref_table);
1709   ref->clear ();
1710 }
1711
1712 \f
1713
1714 // Direct byte buffers.
1715
1716 static jobject
1717 (JNICALL _Jv_JNI_NewDirectByteBuffer) (JNIEnv *, void *address, jlong length)
1718 {
1719   using namespace gnu::gcj;
1720   using namespace java::nio;
1721   return new DirectByteBufferImpl (reinterpret_cast<RawData *> (address),
1722                                    length);
1723 }
1724
1725 static void *
1726 (JNICALL _Jv_JNI_GetDirectBufferAddress) (JNIEnv *, jobject buffer)
1727 {
1728   using namespace java::nio;
1729   DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
1730   return reinterpret_cast<void *> (bb->address);
1731 }
1732
1733 static jlong
1734 (JNICALL _Jv_JNI_GetDirectBufferCapacity) (JNIEnv *, jobject buffer)
1735 {
1736   using namespace java::nio;
1737   DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
1738   return bb->capacity();
1739 }
1740
1741 \f
1742
1743 // Hash table of native methods.
1744 static JNINativeMethod *nathash;
1745 // Number of slots used.
1746 static int nathash_count = 0;
1747 // Number of slots available.  Must be power of 2.
1748 static int nathash_size = 0;
1749
1750 #define DELETED_ENTRY ((char *) (~0))
1751
1752 // Compute a hash value for a native method descriptor.
1753 static int
1754 hash (const JNINativeMethod *method)
1755 {
1756   char *ptr;
1757   int hash = 0;
1758
1759   ptr = method->name;
1760   while (*ptr)
1761     hash = (31 * hash) + *ptr++;
1762
1763   ptr = method->signature;
1764   while (*ptr)
1765     hash = (31 * hash) + *ptr++;
1766
1767   return hash;
1768 }
1769
1770 // Find the slot where a native method goes.
1771 static JNINativeMethod *
1772 nathash_find_slot (const JNINativeMethod *method)
1773 {
1774   jint h = hash (method);
1775   int step = (h ^ (h >> 16)) | 1;
1776   int w = h & (nathash_size - 1);
1777   int del = -1;
1778
1779   for (;;)
1780     {
1781       JNINativeMethod *slotp = &nathash[w];
1782       if (slotp->name == NULL)
1783         {
1784           if (del >= 0)
1785             return &nathash[del];
1786           else
1787             return slotp;
1788         }
1789       else if (slotp->name == DELETED_ENTRY)
1790         del = w;
1791       else if (! strcmp (slotp->name, method->name)
1792                && ! strcmp (slotp->signature, method->signature))
1793         return slotp;
1794       w = (w + step) & (nathash_size - 1);
1795     }
1796 }
1797
1798 // Find a method.  Return NULL if it isn't in the hash table.
1799 static void *
1800 nathash_find (JNINativeMethod *method)
1801 {
1802   if (nathash == NULL)
1803     return NULL;
1804   JNINativeMethod *slot = nathash_find_slot (method);
1805   if (slot->name == NULL || slot->name == DELETED_ENTRY)
1806     return NULL;
1807   return slot->fnPtr;
1808 }
1809
1810 static void
1811 natrehash ()
1812 {
1813   if (nathash == NULL)
1814     {
1815       nathash_size = 1024;
1816       nathash =
1817         (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1818                                             * sizeof (JNINativeMethod));
1819       memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1820     }
1821   else
1822     {
1823       int savesize = nathash_size;
1824       JNINativeMethod *savehash = nathash;
1825       nathash_size *= 2;
1826       nathash =
1827         (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1828                                             * sizeof (JNINativeMethod));
1829       memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1830
1831       for (int i = 0; i < savesize; ++i)
1832         {
1833           if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1834             {
1835               JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
1836               *slot = savehash[i];
1837             }
1838         }
1839     }
1840 }
1841
1842 static void
1843 nathash_add (const JNINativeMethod *method)
1844 {
1845   if (3 * nathash_count >= 2 * nathash_size)
1846     natrehash ();
1847   JNINativeMethod *slot = nathash_find_slot (method);
1848   // If the slot has a real entry in it, then there is no work to do.
1849   if (slot->name != NULL && slot->name != DELETED_ENTRY)
1850     return;
1851   // FIXME
1852   slot->name = strdup (method->name);
1853   slot->signature = strdup (method->signature);
1854   slot->fnPtr = method->fnPtr;
1855 }
1856
1857 static jint
1858 (JNICALL _Jv_JNI_RegisterNatives) (JNIEnv *env, jclass klass,
1859                                    const JNINativeMethod *methods,
1860                                    jint nMethods)
1861 {
1862   // Synchronize while we do the work.  This must match
1863   // synchronization in some other functions that manipulate or use
1864   // the nathash table.
1865   JvSynchronize sync (global_ref_table);
1866
1867   // Look at each descriptor given us, and find the corresponding
1868   // method in the class.
1869   for (int j = 0; j < nMethods; ++j)
1870     {
1871       bool found = false;
1872
1873       _Jv_Method *imeths = JvGetFirstMethod (klass);
1874       for (int i = 0; i < JvNumMethods (klass); ++i)
1875         {
1876           _Jv_Method *self = &imeths[i];
1877
1878           if (! strcmp (self->name->data, methods[j].name)
1879               && ! strcmp (self->signature->data, methods[j].signature))
1880             {
1881               if (! (self->accflags
1882                      & java::lang::reflect::Modifier::NATIVE))
1883                 break;
1884
1885               // Found a match that is native.
1886               found = true;
1887               nathash_add (&methods[j]);
1888
1889               break;
1890             }
1891         }
1892
1893       if (! found)
1894         {
1895           jstring m = JvNewStringUTF (methods[j].name);
1896           try
1897             {
1898               env->ex =new java::lang::NoSuchMethodError (m);
1899             }
1900           catch (jthrowable t)
1901             {
1902               env->ex = t;
1903             }
1904           return JNI_ERR;
1905         }
1906     }
1907
1908   return JNI_OK;
1909 }
1910
1911 static jint
1912 (JNICALL _Jv_JNI_UnregisterNatives) (JNIEnv *, jclass)
1913 {
1914   // FIXME -- we could implement this.
1915   return JNI_ERR;
1916 }
1917
1918 \f
1919
1920 // Add a character to the buffer, encoding properly.
1921 static void
1922 add_char (char *buf, jchar c, int *here)
1923 {
1924   if (c == '_')
1925     {
1926       buf[(*here)++] = '_';
1927       buf[(*here)++] = '1';
1928     }
1929   else if (c == ';')
1930     {
1931       buf[(*here)++] = '_';
1932       buf[(*here)++] = '2';
1933     }
1934   else if (c == '[')
1935     {
1936       buf[(*here)++] = '_';
1937       buf[(*here)++] = '3';
1938     }
1939
1940   // Also check for `.' here because we might be passed an internal
1941   // qualified class name like `foo.bar'.
1942   else if (c == '/' || c == '.')
1943     buf[(*here)++] = '_';
1944   else if ((c >= '0' && c <= '9')
1945            || (c >= 'a' && c <= 'z')
1946            || (c >= 'A' && c <= 'Z'))
1947     buf[(*here)++] = (char) c;
1948   else
1949     {
1950       // "Unicode" character.
1951       buf[(*here)++] = '_';
1952       buf[(*here)++] = '0';
1953       for (int i = 0; i < 4; ++i)
1954         {
1955           int val = c & 0x0f;
1956           buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1957           c >>= 4;
1958         }
1959       *here += 4;
1960     }
1961 }
1962
1963 // Compute a mangled name for a native function.  This computes the
1964 // long name, and also returns an index which indicates where a NUL
1965 // can be placed to create the short name.  This function assumes that
1966 // the buffer is large enough for its results.
1967 static void
1968 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1969               _Jv_Utf8Const *signature, char *buf, int *long_start)
1970 {
1971   strcpy (buf, "Java_");
1972   int here = 5;
1973
1974   // Add fully qualified class name.
1975   jchar *chars = _Jv_GetStringChars (klass->getName ());
1976   jint len = klass->getName ()->length ();
1977   for (int i = 0; i < len; ++i)
1978     add_char (buf, chars[i], &here);
1979
1980   // Don't use add_char because we need a literal `_'.
1981   buf[here++] = '_';
1982
1983   const unsigned char *fn = (const unsigned char *) func_name->data;
1984   const unsigned char *limit = fn + func_name->length;
1985   for (int i = 0; ; ++i)
1986     {
1987       int ch = UTF8_GET (fn, limit);
1988       if (ch < 0)
1989         break;
1990       add_char (buf, ch, &here);
1991     }
1992
1993   // This is where the long signature begins.
1994   *long_start = here;
1995   buf[here++] = '_';
1996   buf[here++] = '_';
1997
1998   const unsigned char *sig = (const unsigned char *) signature->data;
1999   limit = sig + signature->length;
2000   JvAssert (sig[0] == '(');
2001   ++sig;
2002   while (1)
2003     {
2004       int ch = UTF8_GET (sig, limit);
2005       if (ch == ')' || ch < 0)
2006         break;
2007       add_char (buf, ch, &here);
2008     }
2009
2010   buf[here] = '\0';
2011 }
2012
2013 // Return the current thread's JNIEnv; if one does not exist, create
2014 // it.  Also create a new system frame for use.  This is `extern "C"'
2015 // because the compiler calls it.
2016 extern "C" JNIEnv *
2017 _Jv_GetJNIEnvNewFrame (jclass klass)
2018 {
2019   JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2020   if (env == NULL)
2021     {
2022       env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2023       env->p = &_Jv_JNIFunctions;
2024       env->klass = klass;
2025       env->locals = NULL;
2026       // We set env->ex below.
2027
2028       _Jv_SetCurrentJNIEnv (env);
2029     }
2030
2031   _Jv_JNI_LocalFrame *frame
2032     = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2033                                                   + (FRAME_SIZE
2034                                                      * sizeof (jobject)));
2035
2036   frame->marker = MARK_SYSTEM;
2037   frame->size = FRAME_SIZE;
2038   frame->next = env->locals;
2039
2040   for (int i = 0; i < frame->size; ++i)
2041     frame->vec[i] = NULL;
2042
2043   env->locals = frame;
2044   env->ex = NULL;
2045
2046   return env;
2047 }
2048
2049 // Return the function which implements a particular JNI method.  If
2050 // we can't find the function, we throw the appropriate exception.
2051 // This is `extern "C"' because the compiler uses it.
2052 extern "C" void *
2053 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2054                      _Jv_Utf8Const *signature, int args_size)
2055 {
2056   char buf[10 + 6 * (name->length + signature->length) + 12];
2057   int long_start;
2058   void *function;
2059
2060   // Synchronize on something convenient.  Right now we use the hash.
2061   JvSynchronize sync (global_ref_table);
2062
2063   // First see if we have an override in the hash table.
2064   strncpy (buf, name->data, name->length);
2065   buf[name->length] = '\0';
2066   strncpy (buf + name->length + 1, signature->data, signature->length);
2067   buf[name->length + signature->length + 1] = '\0';
2068   JNINativeMethod meth;
2069   meth.name = buf;
2070   meth.signature = buf + name->length + 1;
2071   function = nathash_find (&meth);
2072   if (function != NULL)
2073     return function;
2074
2075   // If there was no override, then look in the symbol table.
2076   buf[0] = '_';
2077   mangled_name (klass, name, signature, buf + 1, &long_start);
2078   char c = buf[long_start + 1];
2079   buf[long_start + 1] = '\0';
2080
2081   function = _Jv_FindSymbolInExecutable (buf + 1);
2082 #ifdef WIN32
2083   // On Win32, we use the "stdcall" calling convention (see JNICALL
2084   // in jni.h).
2085   // 
2086   // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2087   // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2088   // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2089   // take care of all these variations here.
2090
2091   char asz_buf[12];    /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2092   char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2093
2094   if (function == NULL)
2095     {
2096       // We have tried searching for the 'fooBar' form (BCC) - now
2097       // try the others.
2098
2099       // First, save the part of the long name that will be damaged
2100       // by appending '@nn'.
2101       memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2102
2103       sprintf (asz_buf, "@%d", args_size);
2104       strcat (buf, asz_buf);
2105
2106       // Search for the '_fooBar@nn' form (MSVC).
2107       function = _Jv_FindSymbolInExecutable (buf);
2108
2109       if (function == NULL)
2110         {
2111           // Search for the 'fooBar@nn' form (MinGW GCC).
2112           function = _Jv_FindSymbolInExecutable (buf + 1);
2113         }
2114     }
2115 #endif /* WIN32 */
2116
2117   if (function == NULL)
2118     {
2119       buf[long_start + 1] = c;
2120 #ifdef WIN32
2121       // Restore the part of the long name that was damaged by 
2122       // appending the '@nn'.
2123       memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2124 #endif /* WIN32 */
2125       function = _Jv_FindSymbolInExecutable (buf + 1);
2126       if (function == NULL)
2127         {
2128 #ifdef WIN32
2129           strcat (buf, asz_buf);
2130           function = _Jv_FindSymbolInExecutable (buf);
2131           if (function == NULL)
2132             function = _Jv_FindSymbolInExecutable (buf + 1);
2133
2134           if (function == NULL)
2135 #endif /* WIN32 */
2136             {
2137               jstring str = JvNewStringUTF (name->data);
2138               throw new java::lang::UnsatisfiedLinkError (str);
2139             }
2140         }
2141     }
2142
2143   return function;
2144 }
2145
2146 #ifdef INTERPRETER
2147
2148 // This function is the stub which is used to turn an ordinary (CNI)
2149 // method call into a JNI call.
2150 void
2151 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2152 {
2153   _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2154
2155   JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2156
2157   // FIXME: we should mark every reference parameter as a local.  For
2158   // now we assume a conservative GC, and we assume that the
2159   // references are on the stack somewhere.
2160
2161   // We cache the value that we find, of course, but if we don't find
2162   // a value we don't cache that fact -- we might subsequently load a
2163   // library which finds the function in question.
2164   {
2165     // Synchronize on a convenient object to ensure sanity in case two
2166     // threads reach this point for the same function at the same
2167     // time.
2168     JvSynchronize sync (global_ref_table);
2169     if (_this->function == NULL)
2170       {
2171         int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2172
2173         if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2174           args_size += sizeof (_this->defining_class);
2175
2176         _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2177                                                _this->self->name,
2178                                                _this->self->signature,
2179                                                args_size);
2180       }
2181   }
2182
2183   JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2184   ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2185   int offset = 0;
2186
2187   // First argument is always the environment pointer.
2188   real_args[offset++].ptr = env;
2189
2190   // For a static method, we pass in the Class.  For non-static
2191   // methods, the `this' argument is already handled.
2192   if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2193     real_args[offset++].ptr = _this->defining_class;
2194
2195   // In libgcj, the callee synchronizes.
2196   jobject sync = NULL;
2197   if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2198     {
2199       if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2200         sync = _this->defining_class;
2201       else
2202         sync = (jobject) args[0].ptr;
2203       _Jv_MonitorEnter (sync);
2204     }
2205
2206   // Copy over passed-in arguments.
2207   memcpy (&real_args[offset], args, _this->args_raw_size);
2208
2209   // The actual call to the JNI function.
2210   ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2211                 ret, real_args);
2212
2213   if (sync != NULL)
2214     _Jv_MonitorExit (sync);
2215
2216   _Jv_JNI_PopSystemFrame (env);
2217 }
2218
2219 #endif /* INTERPRETER */
2220
2221 \f
2222
2223 //
2224 // Invocation API.
2225 //
2226
2227 // An internal helper function.
2228 static jint
2229 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2230                              void *args, jboolean is_daemon)
2231 {
2232   JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2233   java::lang::ThreadGroup *group = NULL;
2234
2235   if (attach)
2236     {
2237       // FIXME: do we really want to support 1.1?
2238       if (attach->version != JNI_VERSION_1_4
2239           && attach->version != JNI_VERSION_1_2
2240           && attach->version != JNI_VERSION_1_1)
2241         return JNI_EVERSION;
2242
2243       JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2244       group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2245     }
2246
2247   // Attaching an already-attached thread is a no-op.
2248   if (_Jv_GetCurrentJNIEnv () != NULL)
2249     return 0;
2250
2251   JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2252   if (env == NULL)
2253     return JNI_ERR;
2254   env->p = &_Jv_JNIFunctions;
2255   env->ex = NULL;
2256   env->klass = NULL;
2257   env->locals
2258     = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2259                                                   + (FRAME_SIZE
2260                                                      * sizeof (jobject)));
2261   if (env->locals == NULL)
2262     {
2263       _Jv_Free (env);
2264       return JNI_ERR;
2265     }
2266
2267   env->locals->marker = MARK_SYSTEM;
2268   env->locals->size = FRAME_SIZE;
2269   env->locals->next = NULL;
2270
2271   for (int i = 0; i < env->locals->size; ++i)
2272     env->locals->vec[i] = NULL;
2273
2274   *penv = reinterpret_cast<void *> (env);
2275
2276   // This thread might already be a Java thread -- this function might
2277   // have been called simply to set the new JNIEnv.
2278   if (_Jv_ThreadCurrent () == NULL)
2279     {
2280       try
2281         {
2282           if (is_daemon)
2283             _Jv_AttachCurrentThreadAsDaemon (name, group);
2284           else
2285             _Jv_AttachCurrentThread (name, group);
2286         }
2287       catch (jthrowable t)
2288         {
2289           return JNI_ERR;
2290         }
2291     }
2292   _Jv_SetCurrentJNIEnv (env);
2293
2294   return 0;
2295 }
2296
2297 // This is the one actually used by JNI.
2298 static jint
2299 (JNICALL _Jv_JNI_AttachCurrentThread) (JavaVM *vm, void **penv, void *args)
2300 {
2301   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2302 }
2303
2304 static jint
2305 (JNICALL _Jv_JNI_AttachCurrentThreadAsDaemon) (JavaVM *vm, void **penv, 
2306                                                void *args)
2307 {
2308   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2309 }
2310
2311 static jint
2312 (JNICALL _Jv_JNI_DestroyJavaVM) (JavaVM *vm)
2313 {
2314   JvAssert (the_vm && vm == the_vm);
2315
2316   JNIEnv *env;
2317   if (_Jv_ThreadCurrent () != NULL)
2318     {
2319       jstring main_name;
2320       // This sucks.
2321       try
2322         {
2323           main_name = JvNewStringLatin1 ("main");
2324         }
2325       catch (jthrowable t)
2326         {
2327           return JNI_ERR;
2328         }
2329
2330       jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
2331                                             reinterpret_cast<void **> (&env),
2332                                             NULL, false);
2333       if (r < 0)
2334         return r;
2335     }
2336   else
2337     env = _Jv_GetCurrentJNIEnv ();
2338
2339   _Jv_ThreadWait ();
2340
2341   // Docs say that this always returns an error code.
2342   return JNI_ERR;
2343 }
2344
2345 jint
2346 (JNICALL _Jv_JNI_DetachCurrentThread) (JavaVM *)
2347 {
2348   jint code = _Jv_DetachCurrentThread ();
2349   return code  ? JNI_EDETACHED : 0;
2350 }
2351
2352 static jint
2353 (JNICALL _Jv_JNI_GetEnv) (JavaVM *, void **penv, jint version)
2354 {
2355   if (_Jv_ThreadCurrent () == NULL)
2356     {
2357       *penv = NULL;
2358       return JNI_EDETACHED;
2359     }
2360
2361 #ifdef ENABLE_JVMPI
2362   // Handle JVMPI requests.
2363   if (version == JVMPI_VERSION_1)
2364     {
2365       *penv = (void *) &_Jv_JVMPI_Interface;
2366       return 0;
2367     }
2368 #endif
2369
2370   // FIXME: do we really want to support 1.1?
2371   if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2372       && version != JNI_VERSION_1_1)
2373     {
2374       *penv = NULL;
2375       return JNI_EVERSION;
2376     }
2377
2378   *penv = (void *) _Jv_GetCurrentJNIEnv ();
2379   return 0;
2380 }
2381
2382 jint JNICALL
2383 JNI_GetDefaultJavaVMInitArgs (void *args)
2384 {
2385   jint version = * (jint *) args;
2386   // Here we only support 1.2 and 1.4.
2387   if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2388     return JNI_EVERSION;
2389
2390   JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2391   ia->version = JNI_VERSION_1_4;
2392   ia->nOptions = 0;
2393   ia->options = NULL;
2394   ia->ignoreUnrecognized = true;
2395
2396   return 0;
2397 }
2398
2399 jint JNICALL
2400 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
2401 {
2402   JvAssert (! the_vm);
2403
2404   _Jv_CreateJavaVM (NULL);
2405
2406   // FIXME: synchronize
2407   JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2408   if (nvm == NULL)
2409     return JNI_ERR;
2410   nvm->functions = &_Jv_JNI_InvokeFunctions;
2411
2412   // Parse the arguments.
2413   if (args != NULL)
2414     {
2415       jint version = * (jint *) args;
2416       // We only support 1.2 and 1.4.
2417       if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2418         return JNI_EVERSION;
2419       JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2420       for (int i = 0; i < ia->nOptions; ++i)
2421         {
2422           if (! strcmp (ia->options[i].optionString, "vfprintf")
2423               || ! strcmp (ia->options[i].optionString, "exit")
2424               || ! strcmp (ia->options[i].optionString, "abort"))
2425             {
2426               // We are required to recognize these, but for now we
2427               // don't handle them in any way.  FIXME.
2428               continue;
2429             }
2430           else if (! strncmp (ia->options[i].optionString,
2431                               "-verbose", sizeof ("-verbose") - 1))
2432             {
2433               // We don't do anything with this option either.  We
2434               // might want to make sure the argument is valid, but we
2435               // don't really care all that much for now.
2436               continue;
2437             }
2438           else if (! strncmp (ia->options[i].optionString, "-D", 2))
2439             {
2440               // FIXME.
2441               continue;
2442             }
2443           else if (ia->ignoreUnrecognized)
2444             {
2445               if (ia->options[i].optionString[0] == '_'
2446                   || ! strncmp (ia->options[i].optionString, "-X", 2))
2447                 continue;
2448             }
2449
2450           return JNI_ERR;
2451         }
2452     }
2453
2454   jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2455   if (r < 0)
2456     return r;
2457
2458   the_vm = nvm;
2459   *vm = the_vm;
2460
2461   return 0;
2462 }
2463
2464 jint JNICALL
2465 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2466 {
2467   if (buf_len <= 0)
2468     return JNI_ERR;
2469
2470   // We only support a single VM.
2471   if (the_vm != NULL)
2472     {
2473       vm_buffer[0] = the_vm;
2474       *n_vms = 1;
2475     }
2476   else
2477     *n_vms = 0;
2478   return 0;
2479 }
2480
2481 JavaVM *
2482 _Jv_GetJavaVM ()
2483 {
2484   // FIXME: synchronize
2485   if (! the_vm)
2486     {
2487       JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2488       if (nvm != NULL)
2489         nvm->functions = &_Jv_JNI_InvokeFunctions;
2490       the_vm = nvm;
2491     }
2492
2493   // If this is a Java thread, we want to make sure it has an
2494   // associated JNIEnv.
2495   if (_Jv_ThreadCurrent () != NULL)
2496     {
2497       void *ignore;
2498       _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2499     }
2500
2501   return the_vm;
2502 }
2503
2504 static jint
2505 (JNICALL _Jv_JNI_GetJavaVM) (JNIEnv *, JavaVM **vm)
2506 {
2507   *vm = _Jv_GetJavaVM ();
2508   return *vm == NULL ? JNI_ERR : JNI_OK;
2509 }
2510
2511 \f
2512
2513 #define RESERVED NULL
2514
2515 struct JNINativeInterface _Jv_JNIFunctions =
2516 {
2517   RESERVED,
2518   RESERVED,
2519   RESERVED,
2520   RESERVED,
2521   _Jv_JNI_GetVersion,           // GetVersion
2522   _Jv_JNI_DefineClass,          // DefineClass
2523   _Jv_JNI_FindClass,            // FindClass
2524   _Jv_JNI_FromReflectedMethod,  // FromReflectedMethod
2525   _Jv_JNI_FromReflectedField,   // FromReflectedField
2526   _Jv_JNI_ToReflectedMethod,    // ToReflectedMethod
2527   _Jv_JNI_GetSuperclass,        // GetSuperclass
2528   _Jv_JNI_IsAssignableFrom,     // IsAssignableFrom
2529   _Jv_JNI_ToReflectedField,     // ToReflectedField
2530   _Jv_JNI_Throw,                // Throw
2531   _Jv_JNI_ThrowNew,             // ThrowNew
2532   _Jv_JNI_ExceptionOccurred,    // ExceptionOccurred
2533   _Jv_JNI_ExceptionDescribe,    // ExceptionDescribe
2534   _Jv_JNI_ExceptionClear,       // ExceptionClear
2535   _Jv_JNI_FatalError,           // FatalError
2536
2537   _Jv_JNI_PushLocalFrame,       // PushLocalFrame
2538   _Jv_JNI_PopLocalFrame,        // PopLocalFrame
2539   _Jv_JNI_NewGlobalRef,         // NewGlobalRef
2540   _Jv_JNI_DeleteGlobalRef,      // DeleteGlobalRef
2541   _Jv_JNI_DeleteLocalRef,       // DeleteLocalRef
2542
2543   _Jv_JNI_IsSameObject,         // IsSameObject
2544
2545   _Jv_JNI_NewLocalRef,          // NewLocalRef
2546   _Jv_JNI_EnsureLocalCapacity,  // EnsureLocalCapacity
2547
2548   _Jv_JNI_AllocObject,              // AllocObject
2549   _Jv_JNI_NewObject,                // NewObject
2550   _Jv_JNI_NewObjectV,               // NewObjectV
2551   _Jv_JNI_NewObjectA,               // NewObjectA
2552   _Jv_JNI_GetObjectClass,           // GetObjectClass
2553   _Jv_JNI_IsInstanceOf,             // IsInstanceOf
2554   _Jv_JNI_GetAnyMethodID<false>,    // GetMethodID
2555
2556   _Jv_JNI_CallMethod<jobject>,          // CallObjectMethod
2557   _Jv_JNI_CallMethodV<jobject>,         // CallObjectMethodV
2558   _Jv_JNI_CallMethodA<jobject>,         // CallObjectMethodA
2559   _Jv_JNI_CallMethod<jboolean>,         // CallBooleanMethod
2560   _Jv_JNI_CallMethodV<jboolean>,        // CallBooleanMethodV
2561   _Jv_JNI_CallMethodA<jboolean>,        // CallBooleanMethodA
2562   _Jv_JNI_CallMethod<jbyte>,            // CallByteMethod
2563   _Jv_JNI_CallMethodV<jbyte>,           // CallByteMethodV
2564   _Jv_JNI_CallMethodA<jbyte>,           // CallByteMethodA
2565   _Jv_JNI_CallMethod<jchar>,            // CallCharMethod
2566   _Jv_JNI_CallMethodV<jchar>,           // CallCharMethodV
2567   _Jv_JNI_CallMethodA<jchar>,           // CallCharMethodA
2568   _Jv_JNI_CallMethod<jshort>,           // CallShortMethod
2569   _Jv_JNI_CallMethodV<jshort>,          // CallShortMethodV
2570   _Jv_JNI_CallMethodA<jshort>,          // CallShortMethodA
2571   _Jv_JNI_CallMethod<jint>,             // CallIntMethod
2572   _Jv_JNI_CallMethodV<jint>,            // CallIntMethodV
2573   _Jv_JNI_CallMethodA<jint>,            // CallIntMethodA
2574   _Jv_JNI_CallMethod<jlong>,            // CallLongMethod
2575   _Jv_JNI_CallMethodV<jlong>,           // CallLongMethodV
2576   _Jv_JNI_CallMethodA<jlong>,           // CallLongMethodA
2577   _Jv_JNI_CallMethod<jfloat>,           // CallFloatMethod
2578   _Jv_JNI_CallMethodV<jfloat>,          // CallFloatMethodV
2579   _Jv_JNI_CallMethodA<jfloat>,          // CallFloatMethodA
2580   _Jv_JNI_CallMethod<jdouble>,          // CallDoubleMethod
2581   _Jv_JNI_CallMethodV<jdouble>,         // CallDoubleMethodV
2582   _Jv_JNI_CallMethodA<jdouble>,         // CallDoubleMethodA
2583   _Jv_JNI_CallVoidMethod,               // CallVoidMethod
2584   _Jv_JNI_CallVoidMethodV,              // CallVoidMethodV
2585   _Jv_JNI_CallVoidMethodA,              // CallVoidMethodA
2586
2587   // Nonvirtual method invocation functions follow.
2588   _Jv_JNI_CallAnyMethod<jobject, nonvirtual>,   // CallNonvirtualObjectMethod
2589   _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>,  // CallNonvirtualObjectMethodV
2590   _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>,  // CallNonvirtualObjectMethodA
2591   _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>,  // CallNonvirtualBooleanMethod
2592   _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2593   _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2594   _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>,     // CallNonvirtualByteMethod
2595   _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>,    // CallNonvirtualByteMethodV
2596   _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>,    // CallNonvirtualByteMethodA
2597   _Jv_JNI_CallAnyMethod<jchar, nonvirtual>,     // CallNonvirtualCharMethod
2598   _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>,    // CallNonvirtualCharMethodV
2599   _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>,    // CallNonvirtualCharMethodA
2600   _Jv_JNI_CallAnyMethod<jshort, nonvirtual>,    // CallNonvirtualShortMethod
2601   _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>,   // CallNonvirtualShortMethodV
2602   _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>,   // CallNonvirtualShortMethodA
2603   _Jv_JNI_CallAnyMethod<jint, nonvirtual>,      // CallNonvirtualIntMethod
2604   _Jv_JNI_CallAnyMethodV<jint, nonvirtual>,     // CallNonvirtualIntMethodV
2605   _Jv_JNI_CallAnyMethodA<jint, nonvirtual>,     // CallNonvirtualIntMethodA
2606   _Jv_JNI_CallAnyMethod<jlong, nonvirtual>,     // CallNonvirtualLongMethod
2607   _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>,    // CallNonvirtualLongMethodV
2608   _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>,    // CallNonvirtualLongMethodA
2609   _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>,    // CallNonvirtualFloatMethod
2610   _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>,   // CallNonvirtualFloatMethodV
2611   _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>,   // CallNonvirtualFloatMethodA
2612   _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>,   // CallNonvirtualDoubleMethod
2613   _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>,  // CallNonvirtualDoubleMethodV
2614   _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>,  // CallNonvirtualDoubleMethodA
2615   _Jv_JNI_CallAnyVoidMethod<nonvirtual>,        // CallNonvirtualVoidMethod
2616   _Jv_JNI_CallAnyVoidMethodV<nonvirtual>,       // CallNonvirtualVoidMethodV
2617   _Jv_JNI_CallAnyVoidMethodA<nonvirtual>,       // CallNonvirtualVoidMethodA
2618
2619   _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2620   _Jv_JNI_GetField<jobject>,    // GetObjectField
2621   _Jv_JNI_GetField<jboolean>,   // GetBooleanField
2622   _Jv_JNI_GetField<jbyte>,      // GetByteField
2623   _Jv_JNI_GetField<jchar>,      // GetCharField
2624   _Jv_JNI_GetField<jshort>,     // GetShortField
2625   _Jv_JNI_GetField<jint>,       // GetIntField
2626   _Jv_JNI_GetField<jlong>,      // GetLongField
2627   _Jv_JNI_GetField<jfloat>,     // GetFloatField
2628   _Jv_JNI_GetField<jdouble>,    // GetDoubleField
2629   _Jv_JNI_SetField,             // SetObjectField
2630   _Jv_JNI_SetField,             // SetBooleanField
2631   _Jv_JNI_SetField,             // SetByteField
2632   _Jv_JNI_SetField,             // SetCharField
2633   _Jv_JNI_SetField,             // SetShortField
2634   _Jv_JNI_SetField,             // SetIntField
2635   _Jv_JNI_SetField,             // SetLongField
2636   _Jv_JNI_SetField,             // SetFloatField
2637   _Jv_JNI_SetField,             // SetDoubleField
2638   _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2639
2640   _Jv_JNI_CallStaticMethod<jobject>,      // CallStaticObjectMethod
2641   _Jv_JNI_CallStaticMethodV<jobject>,     // CallStaticObjectMethodV
2642   _Jv_JNI_CallStaticMethodA<jobject>,     // CallStaticObjectMethodA
2643   _Jv_JNI_CallStaticMethod<jboolean>,     // CallStaticBooleanMethod
2644   _Jv_JNI_CallStaticMethodV<jboolean>,    // CallStaticBooleanMethodV
2645   _Jv_JNI_CallStaticMethodA<jboolean>,    // CallStaticBooleanMethodA
2646   _Jv_JNI_CallStaticMethod<jbyte>,        // CallStaticByteMethod
2647   _Jv_JNI_CallStaticMethodV<jbyte>,       // CallStaticByteMethodV
2648   _Jv_JNI_CallStaticMethodA<jbyte>,       // CallStaticByteMethodA
2649   _Jv_JNI_CallStaticMethod<jchar>,        // CallStaticCharMethod
2650   _Jv_JNI_CallStaticMethodV<jchar>,       // CallStaticCharMethodV
2651   _Jv_JNI_CallStaticMethodA<jchar>,       // CallStaticCharMethodA
2652   _Jv_JNI_CallStaticMethod<jshort>,       // CallStaticShortMethod
2653   _Jv_JNI_CallStaticMethodV<jshort>,      // CallStaticShortMethodV
2654   _Jv_JNI_CallStaticMethodA<jshort>,      // CallStaticShortMethodA
2655   _Jv_JNI_CallStaticMethod<jint>,         // CallStaticIntMethod
2656   _Jv_JNI_CallStaticMethodV<jint>,        // CallStaticIntMethodV
2657   _Jv_JNI_CallStaticMethodA<jint>,        // CallStaticIntMethodA
2658   _Jv_JNI_CallStaticMethod<jlong>,        // CallStaticLongMethod
2659   _Jv_JNI_CallStaticMethodV<jlong>,       // CallStaticLongMethodV
2660   _Jv_JNI_CallStaticMethodA<jlong>,       // CallStaticLongMethodA
2661   _Jv_JNI_CallStaticMethod<jfloat>,       // CallStaticFloatMethod
2662   _Jv_JNI_CallStaticMethodV<jfloat>,      // CallStaticFloatMethodV
2663   _Jv_JNI_CallStaticMethodA<jfloat>,      // CallStaticFloatMethodA
2664   _Jv_JNI_CallStaticMethod<jdouble>,      // CallStaticDoubleMethod
2665   _Jv_JNI_CallStaticMethodV<jdouble>,     // CallStaticDoubleMethodV
2666   _Jv_JNI_CallStaticMethodA<jdouble>,     // CallStaticDoubleMethodA
2667   _Jv_JNI_CallStaticVoidMethod,           // CallStaticVoidMethod
2668   _Jv_JNI_CallStaticVoidMethodV,          // CallStaticVoidMethodV
2669   _Jv_JNI_CallStaticVoidMethodA,          // CallStaticVoidMethodA
2670
2671   _Jv_JNI_GetAnyFieldID<true>,         // GetStaticFieldID
2672   _Jv_JNI_GetStaticField<jobject>,     // GetStaticObjectField
2673   _Jv_JNI_GetStaticField<jboolean>,    // GetStaticBooleanField
2674   _Jv_JNI_GetStaticField<jbyte>,       // GetStaticByteField
2675   _Jv_JNI_GetStaticField<jchar>,       // GetStaticCharField
2676   _Jv_JNI_GetStaticField<jshort>,      // GetStaticShortField
2677   _Jv_JNI_GetStaticField<jint>,        // GetStaticIntField
2678   _Jv_JNI_GetStaticField<jlong>,       // GetStaticLongField
2679   _Jv_JNI_GetStaticField<jfloat>,      // GetStaticFloatField
2680   _Jv_JNI_GetStaticField<jdouble>,     // GetStaticDoubleField
2681   _Jv_JNI_SetStaticField,              // SetStaticObjectField
2682   _Jv_JNI_SetStaticField,              // SetStaticBooleanField
2683   _Jv_JNI_SetStaticField,              // SetStaticByteField
2684   _Jv_JNI_SetStaticField,              // SetStaticCharField
2685   _Jv_JNI_SetStaticField,              // SetStaticShortField
2686   _Jv_JNI_SetStaticField,              // SetStaticIntField
2687   _Jv_JNI_SetStaticField,              // SetStaticLongField
2688   _Jv_JNI_SetStaticField,              // SetStaticFloatField
2689   _Jv_JNI_SetStaticField,              // SetStaticDoubleField
2690   _Jv_JNI_NewString,                   // NewString
2691   _Jv_JNI_GetStringLength,             // GetStringLength
2692   _Jv_JNI_GetStringChars,              // GetStringChars
2693   _Jv_JNI_ReleaseStringChars,          // ReleaseStringChars
2694   _Jv_JNI_NewStringUTF,                // NewStringUTF
2695   _Jv_JNI_GetStringUTFLength,          // GetStringUTFLength
2696   _Jv_JNI_GetStringUTFChars,           // GetStringUTFChars
2697   _Jv_JNI_ReleaseStringUTFChars,       // ReleaseStringUTFChars
2698   _Jv_JNI_GetArrayLength,              // GetArrayLength
2699   _Jv_JNI_NewObjectArray,              // NewObjectArray
2700   _Jv_JNI_GetObjectArrayElement,       // GetObjectArrayElement
2701   _Jv_JNI_SetObjectArrayElement,       // SetObjectArrayElement
2702   _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2703                                                             // NewBooleanArray
2704   _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>,     // NewByteArray
2705   _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>,     // NewCharArray
2706   _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>,   // NewShortArray
2707   _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>,       // NewIntArray
2708   _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>,     // NewLongArray
2709   _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>,   // NewFloatArray
2710   _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2711   _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,       
2712                                             // GetBooleanArrayElements
2713   _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,  
2714                                             // GetByteArrayElements
2715   _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2716                                             // GetCharArrayElements
2717   _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,           
2718                                             // GetShortArrayElements
2719   _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,               
2720                                             // GetIntArrayElements
2721   _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,             
2722                                             // GetLongArrayElements
2723   _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,           
2724                                             // GetFloatArrayElements
2725   _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,         
2726                                             // GetDoubleArrayElements
2727   _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,    
2728                                             // ReleaseBooleanArrayElements
2729   _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,    
2730                                             // ReleaseByteArrayElements
2731   _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,    
2732                                             // ReleaseCharArrayElements
2733   _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,    
2734                                             // ReleaseShortArrayElements
2735   _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,    
2736                                             // ReleaseIntArrayElements
2737   _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,    
2738                                             // ReleaseLongArrayElements
2739   _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,    
2740                                             // ReleaseFloatArrayElements
2741   _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,    
2742                                             // ReleaseDoubleArrayElements
2743   _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,         
2744                                             // GetBooleanArrayRegion
2745   _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,       
2746                                             // GetByteArrayRegion
2747   _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,       
2748                                             // GetCharArrayRegion
2749   _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,     
2750                                             // GetShortArrayRegion
2751   _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,         
2752                                             // GetIntArrayRegion
2753   _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,       
2754                                             // GetLongArrayRegion
2755   _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,     
2756                                             // GetFloatArrayRegion
2757   _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,           
2758                                             // GetDoubleArrayRegion
2759   _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,         
2760                                             // SetBooleanArrayRegion
2761   _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,       
2762                                             // SetByteArrayRegion
2763   _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,       
2764                                             // SetCharArrayRegion
2765   _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,     
2766                                             // SetShortArrayRegion
2767   _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,         
2768                                             // SetIntArrayRegion
2769   _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,       
2770                                             // SetLongArrayRegion
2771   _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,     
2772                                             // SetFloatArrayRegion
2773   _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,           
2774                                             // SetDoubleArrayRegion
2775   _Jv_JNI_RegisterNatives,                  // RegisterNatives
2776   _Jv_JNI_UnregisterNatives,                // UnregisterNatives
2777   _Jv_JNI_MonitorEnter,                     // MonitorEnter
2778   _Jv_JNI_MonitorExit,                      // MonitorExit
2779   _Jv_JNI_GetJavaVM,                        // GetJavaVM
2780
2781   _Jv_JNI_GetStringRegion,                  // GetStringRegion
2782   _Jv_JNI_GetStringUTFRegion,               // GetStringUTFRegion
2783   _Jv_JNI_GetPrimitiveArrayCritical,        // GetPrimitiveArrayCritical
2784   _Jv_JNI_ReleasePrimitiveArrayCritical,    // ReleasePrimitiveArrayCritical
2785   _Jv_JNI_GetStringCritical,                // GetStringCritical
2786   _Jv_JNI_ReleaseStringCritical,            // ReleaseStringCritical
2787
2788   _Jv_JNI_NewWeakGlobalRef,                 // NewWeakGlobalRef
2789   _Jv_JNI_DeleteWeakGlobalRef,              // DeleteWeakGlobalRef
2790
2791   _Jv_JNI_ExceptionCheck,                   // ExceptionCheck
2792
2793   _Jv_JNI_NewDirectByteBuffer,              // NewDirectByteBuffer
2794   _Jv_JNI_GetDirectBufferAddress,           // GetDirectBufferAddress
2795   _Jv_JNI_GetDirectBufferCapacity           // GetDirectBufferCapacity
2796 };
2797
2798 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2799 {
2800   RESERVED,
2801   RESERVED,
2802   RESERVED,
2803
2804   _Jv_JNI_DestroyJavaVM,
2805   _Jv_JNI_AttachCurrentThread,
2806   _Jv_JNI_DetachCurrentThread,
2807   _Jv_JNI_GetEnv,
2808   _Jv_JNI_AttachCurrentThreadAsDaemon
2809 };