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