OSDN Git Service

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