OSDN Git Service

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