OSDN Git Service

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