OSDN Git Service

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