OSDN Git Service

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