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