OSDN Git Service

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