OSDN Git Service

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