OSDN Git Service

2003-06-11 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / jni.cc
1 // jni.cc - JNI implementation, including the jump table.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <stdio.h>
14 #include <stddef.h>
15 #include <string.h>
16
17 #include <gcj/cni.h>
18 #include <jvm.h>
19 #include <java-assert.h>
20 #include <jni.h>
21 #ifdef ENABLE_JVMPI
22 #include <jvmpi.h>
23 #endif
24
25 #include <java/lang/Class.h>
26 #include <java/lang/ClassLoader.h>
27 #include <java/lang/Throwable.h>
28 #include <java/lang/ArrayIndexOutOfBoundsException.h>
29 #include <java/lang/StringIndexOutOfBoundsException.h>
30 #include <java/lang/UnsatisfiedLinkError.h>
31 #include <java/lang/InstantiationException.h>
32 #include <java/lang/NoSuchFieldError.h>
33 #include <java/lang/NoSuchMethodError.h>
34 #include <java/lang/reflect/Constructor.h>
35 #include <java/lang/reflect/Method.h>
36 #include <java/lang/reflect/Modifier.h>
37 #include <java/lang/OutOfMemoryError.h>
38 #include <java/util/IdentityHashMap.h>
39 #include <java/lang/Integer.h>
40 #include <java/lang/ThreadGroup.h>
41 #include <java/lang/Thread.h>
42 #include <java/lang/IllegalAccessError.h>
43
44 #include <gcj/method.h>
45 #include <gcj/field.h>
46
47 #include <java-interp.h>
48 #include <java-threads.h>
49
50 using namespace gcj;
51
52 // This enum is used to select different template instantiations in
53 // the invocation code.
54 enum invocation_type
55 {
56   normal,
57   nonvirtual,
58   static_type,
59   constructor
60 };
61
62 // Forward declarations.
63 extern struct JNINativeInterface _Jv_JNIFunctions;
64 extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
65
66 // Number of slots in the default frame.  The VM must allow at least
67 // 16.
68 #define FRAME_SIZE 32
69
70 // Mark value indicating this is an overflow frame.
71 #define MARK_NONE    0
72 // Mark value indicating this is a user frame.
73 #define MARK_USER    1
74 // Mark value indicating this is a system frame.
75 #define MARK_SYSTEM  2
76
77 // This structure is used to keep track of local references.
78 struct _Jv_JNI_LocalFrame
79 {
80   // This is true if this frame object represents a pushed frame (eg
81   // from PushLocalFrame).
82   int marker :  2;
83
84   // Number of elements in frame.
85   int size   : 30;
86
87   // Next frame in chain.
88   _Jv_JNI_LocalFrame *next;
89
90   // The elements.  These are allocated using the C "struct hack".
91   jobject vec[0];
92 };
93
94 // This holds a reference count for all local references.
95 static java::util::IdentityHashMap *local_ref_table;
96 // This holds a reference count for all global references.
97 static java::util::IdentityHashMap *global_ref_table;
98
99 // The only VM.
100 static JavaVM *the_vm;
101
102 #ifdef ENABLE_JVMPI
103 // The only JVMPI interface description.
104 static JVMPI_Interface _Jv_JVMPI_Interface;
105
106 static jint
107 jvmpiEnableEvent (jint event_type, void *)
108 {
109   switch (event_type)
110     {
111     case JVMPI_EVENT_OBJECT_ALLOC:
112       _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
113       break;
114
115     case JVMPI_EVENT_THREAD_START:
116       _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
117       break;
118
119     case JVMPI_EVENT_THREAD_END:
120       _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
121       break;
122
123     default:
124       return JVMPI_NOT_AVAILABLE;
125     }
126
127   return JVMPI_SUCCESS;
128 }
129
130 static jint
131 jvmpiDisableEvent (jint event_type, void *)
132 {
133   switch (event_type)
134     {
135     case JVMPI_EVENT_OBJECT_ALLOC:
136       _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
137       break;
138
139     default:
140       return JVMPI_NOT_AVAILABLE;
141     }
142
143   return JVMPI_SUCCESS;
144 }
145 #endif
146
147 \f
148
149 void
150 _Jv_JNI_Init (void)
151 {
152   local_ref_table = new java::util::IdentityHashMap;
153   global_ref_table = new java::util::IdentityHashMap;
154
155 #ifdef ENABLE_JVMPI
156   _Jv_JVMPI_Interface.version = 1;
157   _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
158   _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
159   _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
160   _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
161   _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
162 #endif
163 }
164
165 // Tell the GC that a certain pointer is live.
166 static void
167 mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
168 {
169   JvSynchronize sync (ref_table);
170
171   using namespace java::lang;
172   Integer *refcount = (Integer *) ref_table->get (obj);
173   jint val = (refcount == NULL) ? 0 : refcount->intValue ();
174   // FIXME: what about out of memory error?
175   ref_table->put (obj, new Integer (val + 1));
176 }
177
178 // Unmark a pointer.
179 static void
180 unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
181 {
182   JvSynchronize sync (ref_table);
183
184   using namespace java::lang;
185   Integer *refcount = (Integer *) ref_table->get (obj);
186   JvAssert (refcount);
187   jint val = refcount->intValue () - 1;
188   JvAssert (val >= 0);
189   if (val == 0)
190     ref_table->remove (obj);
191   else
192     // FIXME: what about out of memory error?
193     ref_table->put (obj, new Integer (val));
194 }
195
196 // "Unwrap" some random non-reference type.  This exists to simplify
197 // other template functions.
198 template<typename T>
199 static T
200 unwrap (T val)
201 {
202   return val;
203 }
204
205 // Unwrap a weak reference, if required.
206 template<typename T>
207 static T *
208 unwrap (T *obj)
209 {
210   using namespace gnu::gcj::runtime;
211   // We can compare the class directly because JNIWeakRef is `final'.
212   // Doing it this way is much faster.
213   if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
214     return obj;
215   JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
216   return reinterpret_cast<T *> (wr->get ());
217 }
218
219 \f
220
221 static jobject
222 (JNICALL _Jv_JNI_NewGlobalRef) (JNIEnv *, jobject obj)
223 {
224   // This seems weird but I think it is correct.
225   obj = unwrap (obj);
226   mark_for_gc (obj, global_ref_table);
227   return obj;
228 }
229
230 static void
231 (JNICALL _Jv_JNI_DeleteGlobalRef) (JNIEnv *, jobject obj)
232 {
233   // This seems weird but I think it is correct.
234   obj = unwrap (obj);
235   unmark_for_gc (obj, global_ref_table);
236 }
237
238 static void
239 (JNICALL _Jv_JNI_DeleteLocalRef) (JNIEnv *env, jobject obj)
240 {
241   _Jv_JNI_LocalFrame *frame;
242
243   // This seems weird but I think it is correct.
244   obj = unwrap (obj);
245
246   for (frame = env->locals; frame != NULL; frame = frame->next)
247     {
248       for (int i = 0; i < frame->size; ++i)
249         {
250           if (frame->vec[i] == obj)
251             {
252               frame->vec[i] = NULL;
253               unmark_for_gc (obj, local_ref_table);
254               return;
255             }
256         }
257
258       // Don't go past a marked frame.
259       JvAssert (frame->marker == MARK_NONE);
260     }
261
262   JvAssert (0);
263 }
264
265 static jint
266 (JNICALL _Jv_JNI_EnsureLocalCapacity) (JNIEnv *env, jint size)
267 {
268   // It is easier to just always allocate a new frame of the requested
269   // size.  This isn't the most efficient thing, but for now we don't
270   // care.  Note that _Jv_JNI_PushLocalFrame relies on this right now.
271
272   _Jv_JNI_LocalFrame *frame;
273   try
274     {
275       frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
276                                                  + size * sizeof (jobject));
277     }
278   catch (jthrowable t)
279     {
280       env->ex = t;
281       return JNI_ERR;
282     }
283
284   frame->marker = MARK_NONE;
285   frame->size = size;
286   memset (&frame->vec[0], 0, size * sizeof (jobject));
287   frame->next = env->locals;
288   env->locals = frame;
289
290   return 0;
291 }
292
293 static jint
294 (JNICALL _Jv_JNI_PushLocalFrame) (JNIEnv *env, jint size)
295 {
296   jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
297   if (r < 0)
298     return r;
299
300   // The new frame is on top.
301   env->locals->marker = MARK_USER;
302
303   return 0;
304 }
305
306 static jobject
307 (JNICALL _Jv_JNI_NewLocalRef) (JNIEnv *env, jobject obj)
308 {
309   // This seems weird but I think it is correct.
310   obj = unwrap (obj);
311
312   // Try to find an open slot somewhere in the topmost frame.
313   _Jv_JNI_LocalFrame *frame = env->locals;
314   bool done = false, set = false;
315   for (; frame != NULL && ! done; frame = frame->next)
316     {
317       for (int i = 0; i < frame->size; ++i)
318         {
319           if (frame->vec[i] == NULL)
320             {
321               set = true;
322               done = true;
323               frame->vec[i] = obj;
324               break;
325             }
326         }
327
328       // If we found a slot, or if the frame we just searched is the
329       // mark frame, then we are done.
330       if (done || frame == NULL || frame->marker != MARK_NONE)
331         break;
332     }
333
334   if (! set)
335     {
336       // No slots, so we allocate a new frame.  According to the spec
337       // we could just die here.  FIXME: return value.
338       _Jv_JNI_EnsureLocalCapacity (env, 16);
339       // We know the first element of the new frame will be ok.
340       env->locals->vec[0] = obj;
341     }
342
343   mark_for_gc (obj, local_ref_table);
344   return obj;
345 }
346
347 static jobject
348 (JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result, int stop)
349 {
350   _Jv_JNI_LocalFrame *rf = env->locals;
351
352   bool done = false;
353   while (rf != NULL && ! done)
354     {
355       for (int i = 0; i < rf->size; ++i)
356         if (rf->vec[i] != NULL)
357           unmark_for_gc (rf->vec[i], local_ref_table);
358
359       // If the frame we just freed is the marker frame, we are done.
360       done = (rf->marker == stop);
361
362       _Jv_JNI_LocalFrame *n = rf->next;
363       // When N==NULL, we've reached the stack-allocated frame, and we
364       // must not free it.  However, we must be sure to clear all its
365       // elements, since we might conceivably reuse it.
366       if (n == NULL)
367         {
368           memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
369           break;
370         }
371
372       _Jv_Free (rf);
373       rf = n;
374     }
375
376   // Update the local frame information.
377   env->locals = rf;
378
379   return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
380 }
381
382 static jobject
383 (JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result)
384 {
385   return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
386 }
387
388 // Make sure an array's type is compatible with the type of the
389 // destination.
390 template<typename T>
391 static bool
392 _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
393 {
394   jclass klass = array->getClass()->getComponentType();
395   if (__builtin_expect (klass != K, false))
396     {
397       env->ex = new java::lang::IllegalAccessError ();
398       return false;
399     }
400   else
401     return true;
402 }
403
404 // Pop a `system' frame from the stack.  This is `extern "C"' as it is
405 // used by the compiler.
406 extern "C" void
407 _Jv_JNI_PopSystemFrame (JNIEnv *env)
408 {
409   _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
410
411   if (env->ex)
412     {
413       jthrowable t = env->ex;
414       env->ex = NULL;
415       throw t;
416     }
417 }
418
419 // This function is used from other template functions.  It wraps the
420 // return value appropriately; we specialize it so that object returns
421 // are turned into local references.
422 template<typename T>
423 static T
424 wrap_value (JNIEnv *, T value)
425 {
426   return value;
427 }
428
429 // This specialization is used for jobject, jclass, jstring, jarray,
430 // etc.
431 template<typename T>
432 static T *
433 wrap_value (JNIEnv *env, T *value)
434 {
435   return (value == NULL
436           ? value
437           : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
438 }
439
440 \f
441
442 static jint
443 (JNICALL _Jv_JNI_GetVersion) (JNIEnv *)
444 {
445   return JNI_VERSION_1_4;
446 }
447
448 static jclass
449 (JNICALL _Jv_JNI_DefineClass) (JNIEnv *env, const char *name, jobject loader,
450                                const jbyte *buf, jsize bufLen)
451 {
452   try
453     {
454       loader = unwrap (loader);
455
456       jstring sname = JvNewStringUTF (name);
457       jbyteArray bytes = JvNewByteArray (bufLen);
458
459       jbyte *elts = elements (bytes);
460       memcpy (elts, buf, bufLen * sizeof (jbyte));
461
462       java::lang::ClassLoader *l
463         = reinterpret_cast<java::lang::ClassLoader *> (loader);
464
465       jclass result = l->defineClass (sname, bytes, 0, bufLen);
466       return (jclass) wrap_value (env, result);
467     }
468   catch (jthrowable t)
469     {
470       env->ex = t;
471       return NULL;
472     }
473 }
474
475 static jclass
476 (JNICALL _Jv_JNI_FindClass) (JNIEnv *env, const char *name)
477 {
478   // FIXME: assume that NAME isn't too long.
479   int len = strlen (name);
480   char s[len + 1];
481   for (int i = 0; i <= len; ++i)
482     s[i] = (name[i] == '/') ? '.' : name[i];
483
484   jclass r = NULL;
485   try
486     {
487       // This might throw an out of memory exception.
488       jstring n = JvNewStringUTF (s);
489
490       java::lang::ClassLoader *loader = NULL;
491       if (env->klass != NULL)
492         loader = env->klass->getClassLoader ();
493
494       if (loader == NULL)
495         {
496           // FIXME: should use getBaseClassLoader, but we don't have that
497           // yet.
498           loader = java::lang::ClassLoader::getSystemClassLoader ();
499         }
500
501       r = loader->loadClass (n);
502     }
503   catch (jthrowable t)
504     {
505       env->ex = t;
506     }
507
508   return (jclass) wrap_value (env, r);
509 }
510
511 static jclass
512 (JNICALL _Jv_JNI_GetSuperclass) (JNIEnv *env, jclass clazz)
513 {
514   return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
515 }
516
517 static jboolean
518 (JNICALL _Jv_JNI_IsAssignableFrom) (JNIEnv *, jclass clazz1, jclass clazz2)
519 {
520   return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
521 }
522
523 static jint
524 (JNICALL _Jv_JNI_Throw) (JNIEnv *env, jthrowable obj)
525 {
526   // We check in case the user did some funky cast.
527   obj = unwrap (obj);
528   JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
529   env->ex = obj;
530   return 0;
531 }
532
533 static jint
534 (JNICALL _Jv_JNI_ThrowNew) (JNIEnv *env, jclass clazz, const char *message)
535 {
536   using namespace java::lang::reflect;
537
538   clazz = unwrap (clazz);
539   JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
540
541   int r = JNI_OK;
542   try
543     {
544       JArray<jclass> *argtypes
545         = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
546                                                NULL);
547
548       jclass *elts = elements (argtypes);
549       elts[0] = &StringClass;
550
551       Constructor *cons = clazz->getConstructor (argtypes);
552
553       jobjectArray values = JvNewObjectArray (1, &StringClass, NULL);
554       jobject *velts = elements (values);
555       velts[0] = JvNewStringUTF (message);
556
557       jobject obj = cons->newInstance (values);
558
559       env->ex = reinterpret_cast<jthrowable> (obj);
560     }
561   catch (jthrowable t)
562     {
563       env->ex = t;
564       r = JNI_ERR;
565     }
566
567   return r;
568 }
569
570 static jthrowable
571 (JNICALL _Jv_JNI_ExceptionOccurred) (JNIEnv *env)
572 {
573   return (jthrowable) wrap_value (env, env->ex);
574 }
575
576 static void
577 (JNICALL _Jv_JNI_ExceptionDescribe) (JNIEnv *env)
578 {
579   if (env->ex != NULL)
580     env->ex->printStackTrace();
581 }
582
583 static void
584 (JNICALL _Jv_JNI_ExceptionClear) (JNIEnv *env)
585 {
586   env->ex = NULL;
587 }
588
589 static jboolean
590 (JNICALL _Jv_JNI_ExceptionCheck) (JNIEnv *env)
591 {
592   return env->ex != NULL;
593 }
594
595 static void
596 (JNICALL _Jv_JNI_FatalError) (JNIEnv *, const char *message)
597 {
598   JvFail (message);
599 }
600
601 \f
602
603 static jboolean
604 (JNICALL _Jv_JNI_IsSameObject) (JNIEnv *, jobject obj1, jobject obj2)
605 {
606   return unwrap (obj1) == unwrap (obj2);
607 }
608
609 static jobject
610 (JNICALL _Jv_JNI_AllocObject) (JNIEnv *env, jclass clazz)
611 {
612   jobject obj = NULL;
613   using namespace java::lang::reflect;
614
615   try
616     {
617       clazz = unwrap (clazz);
618       JvAssert (clazz && ! clazz->isArray ());
619       if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
620         env->ex = new java::lang::InstantiationException ();
621       else
622         obj = JvAllocObject (clazz);
623     }
624   catch (jthrowable t)
625     {
626       env->ex = t;
627     }
628
629   return wrap_value (env, obj);
630 }
631
632 static jclass
633 (JNICALL _Jv_JNI_GetObjectClass) (JNIEnv *env, jobject obj)
634 {
635   obj = unwrap (obj);
636   JvAssert (obj);
637   return (jclass) wrap_value (env, obj->getClass());
638 }
639
640 static jboolean
641 (JNICALL _Jv_JNI_IsInstanceOf) (JNIEnv *, jobject obj, jclass clazz)
642 {
643   return unwrap (clazz)->isInstance(unwrap (obj));
644 }
645
646 \f
647
648 //
649 // This section concerns method invocation.
650 //
651
652 template<jboolean is_static>
653 static jmethodID
654 (JNICALL _Jv_JNI_GetAnyMethodID) (JNIEnv *env, jclass clazz,
655                                   const char *name, const char *sig)
656 {
657   try
658     {
659       clazz = unwrap (clazz);
660       _Jv_InitClass (clazz);
661
662       _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
663
664       // FIXME: assume that SIG isn't too long.
665       int len = strlen (sig);
666       char s[len + 1];
667       for (int i = 0; i <= len; ++i)
668         s[i] = (sig[i] == '/') ? '.' : sig[i];
669       _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
670
671       JvAssert (! clazz->isPrimitive());
672
673       using namespace java::lang::reflect;
674
675       while (clazz != NULL)
676         {
677           jint count = JvNumMethods (clazz);
678           jmethodID meth = JvGetFirstMethod (clazz);
679
680           for (jint i = 0; i < count; ++i)
681             {
682               if (((is_static && Modifier::isStatic (meth->accflags))
683                    || (! is_static && ! Modifier::isStatic (meth->accflags)))
684                   && _Jv_equalUtf8Consts (meth->name, name_u)
685                   && _Jv_equalUtf8Consts (meth->signature, sig_u))
686                 return meth;
687
688               meth = meth->getNextMethod();
689             }
690
691           clazz = clazz->getSuperclass ();
692         }
693
694       env->ex = new java::lang::NoSuchMethodError ();
695     }
696   catch (jthrowable t)
697     {
698       env->ex = t;
699     }
700
701   return NULL;
702 }
703
704 // This is a helper function which turns a va_list into an array of
705 // `jvalue's.  It needs signature information in order to do its work.
706 // The array of values must already be allocated.
707 static void
708 array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
709 {
710   jclass *arg_elts = elements (arg_types);
711   for (int i = 0; i < arg_types->length; ++i)
712     {
713       // Here we assume that sizeof(int) >= sizeof(jint), because we
714       // use `int' when decoding the varargs.  Likewise for
715       // float, and double.  Also we assume that sizeof(jlong) >=
716       // sizeof(int), i.e. that jlong values are not further
717       // promoted.
718       JvAssert (sizeof (int) >= sizeof (jint));
719       JvAssert (sizeof (jlong) >= sizeof (int));
720       JvAssert (sizeof (double) >= sizeof (jfloat));
721       JvAssert (sizeof (double) >= sizeof (jdouble));
722       if (arg_elts[i] == JvPrimClass (byte))
723         values[i].b = (jbyte) va_arg (vargs, int);
724       else if (arg_elts[i] == JvPrimClass (short))
725         values[i].s = (jshort) va_arg (vargs, int);
726       else if (arg_elts[i] == JvPrimClass (int))
727         values[i].i = (jint) va_arg (vargs, int);
728       else if (arg_elts[i] == JvPrimClass (long))
729         values[i].j = (jlong) va_arg (vargs, jlong);
730       else if (arg_elts[i] == JvPrimClass (float))
731         values[i].f = (jfloat) va_arg (vargs, double);
732       else if (arg_elts[i] == JvPrimClass (double))
733         values[i].d = (jdouble) va_arg (vargs, double);
734       else if (arg_elts[i] == JvPrimClass (boolean))
735         values[i].z = (jboolean) va_arg (vargs, int);
736       else if (arg_elts[i] == JvPrimClass (char))
737         values[i].c = (jchar) va_arg (vargs, int);
738       else
739         {
740           // An object.
741           values[i].l = unwrap (va_arg (vargs, jobject));
742         }
743     }
744 }
745
746 // This can call any sort of method: virtual, "nonvirtual", static, or
747 // constructor.
748 template<typename T, invocation_type style>
749 static T
750 (JNICALL _Jv_JNI_CallAnyMethodV) (JNIEnv *env, jobject obj, jclass klass,
751                                   jmethodID id, va_list vargs)
752 {
753   obj = unwrap (obj);
754   klass = unwrap (klass);
755
756   if (style == normal)
757     id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
758
759   jclass decl_class = klass ? klass : obj->getClass ();
760   JvAssert (decl_class != NULL);
761
762   jclass return_type;
763   JArray<jclass> *arg_types;
764
765   try
766     {
767       _Jv_GetTypesFromSignature (id, decl_class,
768                                  &arg_types, &return_type);
769
770       jvalue args[arg_types->length];
771       array_from_valist (args, arg_types, vargs);
772
773       // For constructors we need to pass the Class we are instantiating.
774       if (style == constructor)
775         return_type = klass;
776
777       jvalue result;
778       jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
779                                           style == constructor,
780                                           arg_types, args, &result);
781
782       if (ex != NULL)
783         env->ex = ex;
784
785       // We cheat a little here.  FIXME.
786       return wrap_value (env, * (T *) &result);
787     }
788   catch (jthrowable t)
789     {
790       env->ex = t;
791     }
792
793   return wrap_value (env, (T) 0);
794 }
795
796 template<typename T, invocation_type style>
797 static T
798 (JNICALL _Jv_JNI_CallAnyMethod) (JNIEnv *env, jobject obj, jclass klass,
799                                  jmethodID method, ...)
800 {
801   va_list args;
802   T result;
803
804   va_start (args, method);
805   result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
806   va_end (args);
807
808   return result;
809 }
810
811 template<typename T, invocation_type style>
812 static T
813 (JNICALL _Jv_JNI_CallAnyMethodA) (JNIEnv *env, jobject obj, jclass klass,
814                                   jmethodID id, jvalue *args)
815 {
816   obj = unwrap (obj);
817   klass = unwrap (klass);
818
819   if (style == normal)
820     id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
821
822   jclass decl_class = klass ? klass : obj->getClass ();
823   JvAssert (decl_class != NULL);
824
825   jclass return_type;
826   JArray<jclass> *arg_types;
827   try
828     {
829       _Jv_GetTypesFromSignature (id, decl_class,
830                                  &arg_types, &return_type);
831
832       // For constructors we need to pass the Class we are instantiating.
833       if (style == constructor)
834         return_type = klass;
835
836       // Unwrap arguments as required.  Eww.
837       jclass *type_elts = elements (arg_types);
838       jvalue arg_copy[arg_types->length];
839       for (int i = 0; i < arg_types->length; ++i)
840         {
841           if (type_elts[i]->isPrimitive ())
842             arg_copy[i] = args[i];
843           else
844             arg_copy[i].l = unwrap (args[i].l);
845         }
846
847       jvalue result;
848       jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
849                                           style == constructor,
850                                           arg_types, arg_copy, &result);
851
852       if (ex != NULL)
853         env->ex = ex;
854
855       // We cheat a little here.  FIXME.
856       return wrap_value (env, * (T *) &result);
857     }
858   catch (jthrowable t)
859     {
860       env->ex = t;
861     }
862
863   return wrap_value (env, (T) 0);
864 }
865
866 template<invocation_type style>
867 static void
868 (JNICALL _Jv_JNI_CallAnyVoidMethodV) (JNIEnv *env, jobject obj, jclass klass,
869                                       jmethodID id, va_list vargs)
870 {
871   obj = unwrap (obj);
872   klass = unwrap (klass);
873
874   if (style == normal)
875     id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
876
877   jclass decl_class = klass ? klass : obj->getClass ();
878   JvAssert (decl_class != NULL);
879
880   jclass return_type;
881   JArray<jclass> *arg_types;
882   try
883     {
884       _Jv_GetTypesFromSignature (id, decl_class,
885                                  &arg_types, &return_type);
886
887       jvalue args[arg_types->length];
888       array_from_valist (args, arg_types, vargs);
889
890       // For constructors we need to pass the Class we are instantiating.
891       if (style == constructor)
892         return_type = klass;
893
894       jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
895                                           style == constructor,
896                                           arg_types, args, NULL);
897
898       if (ex != NULL)
899         env->ex = ex;
900     }
901   catch (jthrowable t)
902     {
903       env->ex = t;
904     }
905 }
906
907 template<invocation_type style>
908 static void
909 (JNICALL _Jv_JNI_CallAnyVoidMethod) (JNIEnv *env, jobject obj, jclass klass,
910                                      jmethodID method, ...)
911 {
912   va_list args;
913
914   va_start (args, method);
915   _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
916   va_end (args);
917 }
918
919 template<invocation_type style>
920 static void
921 (JNICALL _Jv_JNI_CallAnyVoidMethodA) (JNIEnv *env, jobject obj, jclass klass,
922                                       jmethodID id, jvalue *args)
923 {
924   if (style == normal)
925     id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
926
927   jclass decl_class = klass ? klass : obj->getClass ();
928   JvAssert (decl_class != NULL);
929
930   jclass return_type;
931   JArray<jclass> *arg_types;
932   try
933     {
934       _Jv_GetTypesFromSignature (id, decl_class,
935                                  &arg_types, &return_type);
936
937       // Unwrap arguments as required.  Eww.
938       jclass *type_elts = elements (arg_types);
939       jvalue arg_copy[arg_types->length];
940       for (int i = 0; i < arg_types->length; ++i)
941         {
942           if (type_elts[i]->isPrimitive ())
943             arg_copy[i] = args[i];
944           else
945             arg_copy[i].l = unwrap (args[i].l);
946         }
947
948       jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
949                                           style == constructor,
950                                           arg_types, args, NULL);
951
952       if (ex != NULL)
953         env->ex = ex;
954     }
955   catch (jthrowable t)
956     {
957       env->ex = t;
958     }
959 }
960
961 // Functions with this signature are used to implement functions in
962 // the CallMethod family.
963 template<typename T>
964 static T
965 (JNICALL _Jv_JNI_CallMethodV) (JNIEnv *env, jobject obj, 
966                                jmethodID id, va_list args)
967 {
968   return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
969 }
970
971 // Functions with this signature are used to implement functions in
972 // the CallMethod family.
973 template<typename T>
974 static T
975 (JNICALL _Jv_JNI_CallMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
976 {
977   va_list args;
978   T result;
979
980   va_start (args, id);
981   result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
982   va_end (args);
983
984   return result;
985 }
986
987 // Functions with this signature are used to implement functions in
988 // the CallMethod family.
989 template<typename T>
990 static T
991 (JNICALL _Jv_JNI_CallMethodA) (JNIEnv *env, jobject obj, 
992                                jmethodID id, jvalue *args)
993 {
994   return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
995 }
996
997 static void
998 (JNICALL _Jv_JNI_CallVoidMethodV) (JNIEnv *env, jobject obj, 
999                                    jmethodID id, va_list args)
1000 {
1001   _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1002 }
1003
1004 static void
1005 (JNICALL _Jv_JNI_CallVoidMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
1006 {
1007   va_list args;
1008
1009   va_start (args, id);
1010   _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1011   va_end (args);
1012 }
1013
1014 static void
1015 (JNICALL _Jv_JNI_CallVoidMethodA) (JNIEnv *env, jobject obj, 
1016                                    jmethodID id, jvalue *args)
1017 {
1018   _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1019 }
1020
1021 // Functions with this signature are used to implement functions in
1022 // the CallStaticMethod family.
1023 template<typename T>
1024 static T
1025 (JNICALL _Jv_JNI_CallStaticMethodV) (JNIEnv *env, jclass klass,
1026                                      jmethodID id, va_list args)
1027 {
1028   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1029   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1030
1031   return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1032 }
1033
1034 // Functions with this signature are used to implement functions in
1035 // the CallStaticMethod family.
1036 template<typename T>
1037 static T
1038 (JNICALL _Jv_JNI_CallStaticMethod) (JNIEnv *env, jclass klass, 
1039                                     jmethodID id, ...)
1040 {
1041   va_list args;
1042   T result;
1043
1044   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1045   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1046
1047   va_start (args, id);
1048   result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1049                                                    id, args);
1050   va_end (args);
1051
1052   return result;
1053 }
1054
1055 // Functions with this signature are used to implement functions in
1056 // the CallStaticMethod family.
1057 template<typename T>
1058 static T
1059 (JNICALL _Jv_JNI_CallStaticMethodA) (JNIEnv *env, jclass klass, jmethodID id,
1060                                      jvalue *args)
1061 {
1062   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
1063   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
1064
1065   return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1066 }
1067
1068 static void
1069 (JNICALL _Jv_JNI_CallStaticVoidMethodV) (JNIEnv *env, jclass klass, 
1070                                          jmethodID id, va_list args)
1071 {
1072   _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1073 }
1074
1075 static void
1076 (JNICALL _Jv_JNI_CallStaticVoidMethod) (JNIEnv *env, jclass klass, 
1077                                         jmethodID id, ...)
1078 {
1079   va_list args;
1080
1081   va_start (args, id);
1082   _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1083   va_end (args);
1084 }
1085
1086 static void
1087 (JNICALL _Jv_JNI_CallStaticVoidMethodA) (JNIEnv *env, jclass klass, 
1088                                          jmethodID id, jvalue *args)
1089 {
1090   _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1091 }
1092
1093 static jobject
1094 (JNICALL _Jv_JNI_NewObjectV) (JNIEnv *env, jclass klass,
1095                               jmethodID id, va_list args)
1096 {
1097   JvAssert (klass && ! klass->isArray ());
1098   JvAssert (! strcmp (id->name->data, "<init>")
1099             && id->signature->length > 2
1100             && id->signature->data[0] == '('
1101             && ! strcmp (&id->signature->data[id->signature->length - 2],
1102                          ")V"));
1103
1104   return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1105                                                        id, args);
1106 }
1107
1108 static jobject
1109 (JNICALL _Jv_JNI_NewObject) (JNIEnv *env, jclass klass, jmethodID id, ...)
1110 {
1111   JvAssert (klass && ! klass->isArray ());
1112   JvAssert (! strcmp (id->name->data, "<init>")
1113             && id->signature->length > 2
1114             && id->signature->data[0] == '('
1115             && ! strcmp (&id->signature->data[id->signature->length - 2],
1116                          ")V"));
1117
1118   va_list args;
1119   jobject result;
1120
1121   va_start (args, id);
1122   result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1123                                                          id, args);
1124   va_end (args);
1125
1126   return result;
1127 }
1128
1129 static jobject
1130 (JNICALL _Jv_JNI_NewObjectA) (JNIEnv *env, jclass klass, jmethodID id,
1131                               jvalue *args)
1132 {
1133   JvAssert (klass && ! klass->isArray ());
1134   JvAssert (! strcmp (id->name->data, "<init>")
1135             && id->signature->length > 2
1136             && id->signature->data[0] == '('
1137             && ! strcmp (&id->signature->data[id->signature->length - 2],
1138                          ")V"));
1139
1140   return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1141                                                        id, args);
1142 }
1143
1144 \f
1145
1146 template<typename T>
1147 static T
1148 (JNICALL _Jv_JNI_GetField) (JNIEnv *env, jobject obj, jfieldID field)
1149 {
1150   obj = unwrap (obj);
1151   JvAssert (obj);
1152   T *ptr = (T *) ((char *) obj + field->getOffset ());
1153   return wrap_value (env, *ptr);
1154 }
1155
1156 template<typename T>
1157 static void
1158 (JNICALL _Jv_JNI_SetField) (JNIEnv *, jobject obj, jfieldID field, T value)
1159 {
1160   obj = unwrap (obj);
1161   value = unwrap (value);
1162
1163   JvAssert (obj);
1164   T *ptr = (T *) ((char *) obj + field->getOffset ());
1165   *ptr = value;
1166 }
1167
1168 template<jboolean is_static>
1169 static jfieldID
1170 (JNICALL _Jv_JNI_GetAnyFieldID) (JNIEnv *env, jclass clazz,
1171                                  const char *name, const char *sig)
1172 {
1173   try
1174     {
1175       clazz = unwrap (clazz);
1176
1177       _Jv_InitClass (clazz);
1178
1179       _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
1180
1181       // FIXME: assume that SIG isn't too long.
1182       int len = strlen (sig);
1183       char s[len + 1];
1184       for (int i = 0; i <= len; ++i)
1185         s[i] = (sig[i] == '/') ? '.' : sig[i];
1186       jclass field_class = _Jv_FindClassFromSignature ((char *) s, NULL);
1187
1188       // FIXME: what if field_class == NULL?
1189
1190       java::lang::ClassLoader *loader = clazz->getClassLoader ();
1191       while (clazz != NULL)
1192         {
1193           // We acquire the class lock so that fields aren't resolved
1194           // while we are running.
1195           JvSynchronize sync (clazz);
1196
1197           jint count = (is_static
1198                         ? JvNumStaticFields (clazz)
1199                         : JvNumInstanceFields (clazz));
1200           jfieldID field = (is_static
1201                             ? JvGetFirstStaticField (clazz)
1202                             : JvGetFirstInstanceField (clazz));
1203           for (jint i = 0; i < count; ++i)
1204             {
1205               _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
1206
1207               // The field might be resolved or it might not be.  It
1208               // is much simpler to always resolve it.
1209               _Jv_ResolveField (field, loader);
1210               if (_Jv_equalUtf8Consts (f_name, a_name)
1211                   && field->getClass() == field_class)
1212                 return field;
1213
1214               field = field->getNextField ();
1215             }
1216
1217           clazz = clazz->getSuperclass ();
1218         }
1219
1220       env->ex = new java::lang::NoSuchFieldError ();
1221     }
1222   catch (jthrowable t)
1223     {
1224       env->ex = t;
1225     }
1226   return NULL;
1227 }
1228
1229 template<typename T>
1230 static T
1231 (JNICALL _Jv_JNI_GetStaticField) (JNIEnv *env, jclass, jfieldID field)
1232 {
1233   T *ptr = (T *) field->u.addr;
1234   return wrap_value (env, *ptr);
1235 }
1236
1237 template<typename T>
1238 static void
1239 (JNICALL _Jv_JNI_SetStaticField) (JNIEnv *, jclass, jfieldID field, T value)
1240 {
1241   value = unwrap (value);
1242   T *ptr = (T *) field->u.addr;
1243   *ptr = value;
1244 }
1245
1246 static jstring
1247 (JNICALL _Jv_JNI_NewString) (JNIEnv *env, const jchar *unichars, jsize len)
1248 {
1249   try
1250     {
1251       jstring r = _Jv_NewString (unichars, len);
1252       return (jstring) wrap_value (env, r);
1253     }
1254   catch (jthrowable t)
1255     {
1256       env->ex = t;
1257       return NULL;
1258     }
1259 }
1260
1261 static jsize
1262 (JNICALL _Jv_JNI_GetStringLength) (JNIEnv *, jstring string)
1263 {
1264   return unwrap (string)->length();
1265 }
1266
1267 static const jchar *
1268 (JNICALL _Jv_JNI_GetStringChars) (JNIEnv *, jstring string, jboolean *isCopy)
1269 {
1270   string = unwrap (string);
1271   jchar *result = _Jv_GetStringChars (string);
1272   mark_for_gc (string, global_ref_table);
1273   if (isCopy)
1274     *isCopy = false;
1275   return (const jchar *) result;
1276 }
1277
1278 static void
1279 (JNICALL _Jv_JNI_ReleaseStringChars) (JNIEnv *, jstring string, const jchar *)
1280 {
1281   unmark_for_gc (unwrap (string), global_ref_table);
1282 }
1283
1284 static jstring
1285 (JNICALL _Jv_JNI_NewStringUTF) (JNIEnv *env, const char *bytes)
1286 {
1287   try
1288     {
1289       jstring result = JvNewStringUTF (bytes);
1290       return (jstring) wrap_value (env, result);
1291     }
1292   catch (jthrowable t)
1293     {
1294       env->ex = t;
1295       return NULL;
1296     }
1297 }
1298
1299 static jsize
1300 (JNICALL _Jv_JNI_GetStringUTFLength) (JNIEnv *, jstring string)
1301 {
1302   return JvGetStringUTFLength (unwrap (string));
1303 }
1304
1305 static const char *
1306 (JNICALL _Jv_JNI_GetStringUTFChars) (JNIEnv *env, jstring string, 
1307                                      jboolean *isCopy)
1308 {
1309   string = unwrap (string);
1310   jsize len = JvGetStringUTFLength (string);
1311   try
1312     {
1313       char *r = (char *) _Jv_Malloc (len + 1);
1314       JvGetStringUTFRegion (string, 0, len, r);
1315       r[len] = '\0';
1316
1317       if (isCopy)
1318         *isCopy = true;
1319
1320       return (const char *) r;
1321     }
1322   catch (jthrowable t)
1323     {
1324       env->ex = t;
1325       return NULL;
1326     }
1327 }
1328
1329 static void
1330 (JNICALL _Jv_JNI_ReleaseStringUTFChars) (JNIEnv *, jstring, const char *utf)
1331 {
1332   _Jv_Free ((void *) utf);
1333 }
1334
1335 static void
1336 (JNICALL _Jv_JNI_GetStringRegion) (JNIEnv *env, jstring string, jsize start, 
1337                                    jsize len, jchar *buf)
1338 {
1339   string = unwrap (string);
1340   jchar *result = _Jv_GetStringChars (string);
1341   if (start < 0 || start > string->length ()
1342       || len < 0 || start + len > string->length ())
1343     {
1344       try
1345         {
1346           env->ex = new java::lang::StringIndexOutOfBoundsException ();
1347         }
1348       catch (jthrowable t)
1349         {
1350           env->ex = t;
1351         }
1352     }
1353   else
1354     memcpy (buf, &result[start], len * sizeof (jchar));
1355 }
1356
1357 static void
1358 (JNICALL _Jv_JNI_GetStringUTFRegion) (JNIEnv *env, jstring str, jsize start,
1359                                       jsize len, char *buf)
1360 {
1361   str = unwrap (str);
1362     
1363   if (start < 0 || start > str->length ()
1364       || len < 0 || start + len > str->length ())
1365     {
1366       try
1367         {
1368           env->ex = new java::lang::StringIndexOutOfBoundsException ();
1369         }
1370       catch (jthrowable t)
1371         {
1372           env->ex = t;
1373         }
1374     }
1375   else
1376     _Jv_GetStringUTFRegion (str, start, len, buf);
1377 }
1378
1379 static const jchar *
1380 (JNICALL _Jv_JNI_GetStringCritical) (JNIEnv *, jstring str, jboolean *isCopy)
1381 {
1382   jchar *result = _Jv_GetStringChars (unwrap (str));
1383   if (isCopy)
1384     *isCopy = false;
1385   return result;
1386 }
1387
1388 static void
1389 (JNICALL _Jv_JNI_ReleaseStringCritical) (JNIEnv *, jstring, const jchar *)
1390 {
1391   // Nothing.
1392 }
1393
1394 static jsize
1395 (JNICALL _Jv_JNI_GetArrayLength) (JNIEnv *, jarray array)
1396 {
1397   return unwrap (array)->length;
1398 }
1399
1400 static jarray
1401 (JNICALL _Jv_JNI_NewObjectArray) (JNIEnv *env, jsize length, 
1402                                   jclass elementClass, jobject init)
1403 {
1404   try
1405     {
1406       elementClass = unwrap (elementClass);
1407       init = unwrap (init);
1408
1409       _Jv_CheckCast (elementClass, init);
1410       jarray result = JvNewObjectArray (length, elementClass, init);
1411       return (jarray) wrap_value (env, result);
1412     }
1413   catch (jthrowable t)
1414     {
1415       env->ex = t;
1416       return NULL;
1417     }
1418 }
1419
1420 static jobject
1421 (JNICALL _Jv_JNI_GetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
1422                                          jsize index)
1423 {
1424   if ((unsigned) index >= (unsigned) array->length)
1425     _Jv_ThrowBadArrayIndex (index);
1426   jobject *elts = elements (unwrap (array));
1427   return wrap_value (env, elts[index]);
1428 }
1429
1430 static void
1431 (JNICALL _Jv_JNI_SetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
1432                                          jsize index, jobject value)
1433 {
1434   try
1435     {
1436       array = unwrap (array);
1437       value = unwrap (value);
1438
1439       _Jv_CheckArrayStore (array, value);
1440       if ((unsigned) index >= (unsigned) array->length)
1441         _Jv_ThrowBadArrayIndex (index);
1442       jobject *elts = elements (array);
1443       elts[index] = value;
1444     }
1445   catch (jthrowable t)
1446     {
1447       env->ex = t;
1448     }
1449 }
1450
1451 template<typename T, jclass K>
1452 static JArray<T> *
1453 (JNICALL _Jv_JNI_NewPrimitiveArray) (JNIEnv *env, jsize length)
1454 {
1455   try
1456     {
1457       return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1458     }
1459   catch (jthrowable t)
1460     {
1461       env->ex = t;
1462       return NULL;
1463     }
1464 }
1465
1466 template<typename T, jclass K>
1467 static T *
1468 (JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
1469                                              jboolean *isCopy)
1470 {
1471   array = unwrap (array);
1472   if (! _Jv_JNI_check_types (env, array, K))
1473     return NULL;
1474   T *elts = elements (array);
1475   if (isCopy)
1476     {
1477       // We elect never to copy.
1478       *isCopy = false;
1479     }
1480   mark_for_gc (array, global_ref_table);
1481   return elts;
1482 }
1483
1484 template<typename T, jclass K>
1485 static void
1486 (JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
1487                                                  T *, jint /* mode */)
1488 {
1489   array = unwrap (array);
1490   _Jv_JNI_check_types (env, array, K);
1491   // Note that we ignore MODE.  We can do this because we never copy
1492   // the array elements.  My reading of the JNI documentation is that
1493   // this is an option for the implementor.
1494   unmark_for_gc (array, global_ref_table);
1495 }
1496
1497 template<typename T, jclass K>
1498 static void
1499 (JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
1500                                            jsize start, jsize len,
1501                                  T *buf)
1502 {
1503   array = unwrap (array);
1504   if (! _Jv_JNI_check_types (env, array, K))
1505     return;
1506
1507   // The cast to unsigned lets us save a comparison.
1508   if (start < 0 || len < 0
1509       || (unsigned long) (start + len) > (unsigned long) array->length)
1510     {
1511       try
1512         {
1513           // FIXME: index.
1514           env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1515         }
1516       catch (jthrowable t)
1517         {
1518           // Could have thown out of memory error.
1519           env->ex = t;
1520         }
1521     }
1522   else
1523     {
1524       T *elts = elements (array) + start;
1525       memcpy (buf, elts, len * sizeof (T));
1526     }
1527 }
1528
1529 template<typename T, jclass K>
1530 static void
1531 (JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
1532                                            jsize start, jsize len, T *buf)
1533 {
1534   array = unwrap (array);
1535   if (! _Jv_JNI_check_types (env, array, K))
1536     return;
1537
1538   // The cast to unsigned lets us save a comparison.
1539   if (start < 0 || len < 0
1540       || (unsigned long) (start + len) > (unsigned long) array->length)
1541     {
1542       try
1543         {
1544           // FIXME: index.
1545           env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1546         }
1547       catch (jthrowable t)
1548         {
1549           env->ex = t;
1550         }
1551     }
1552   else
1553     {
1554       T *elts = elements (array) + start;
1555       memcpy (elts, buf, len * sizeof (T));
1556     }
1557 }
1558
1559 static void *
1560 (JNICALL _Jv_JNI_GetPrimitiveArrayCritical) (JNIEnv *, jarray array,
1561                                              jboolean *isCopy)
1562 {
1563   array = unwrap (array);
1564   // FIXME: does this work?
1565   jclass klass = array->getClass()->getComponentType();
1566   JvAssert (klass->isPrimitive ());
1567   char *r = _Jv_GetArrayElementFromElementType (array, klass);
1568   if (isCopy)
1569     *isCopy = false;
1570   return r;
1571 }
1572
1573 static void
1574 (JNICALL _Jv_JNI_ReleasePrimitiveArrayCritical) (JNIEnv *, jarray, void *, jint)
1575 {
1576   // Nothing.
1577 }
1578
1579 static jint
1580 (JNICALL _Jv_JNI_MonitorEnter) (JNIEnv *env, jobject obj)
1581 {
1582   try
1583     {
1584       _Jv_MonitorEnter (unwrap (obj));
1585       return 0;
1586     }
1587   catch (jthrowable t)
1588     {
1589       env->ex = t;
1590     }
1591   return JNI_ERR;
1592 }
1593
1594 static jint
1595 (JNICALL _Jv_JNI_MonitorExit) (JNIEnv *env, jobject obj)
1596 {
1597   try
1598     {
1599       _Jv_MonitorExit (unwrap (obj));
1600       return 0;
1601     }
1602   catch (jthrowable t)
1603     {
1604       env->ex = t;
1605     }
1606   return JNI_ERR;
1607 }
1608
1609 // JDK 1.2
1610 jobject
1611 (JNICALL _Jv_JNI_ToReflectedField) (JNIEnv *env, jclass cls, jfieldID fieldID,
1612                                     jboolean)
1613 {
1614   try
1615     {
1616       cls = unwrap (cls);
1617       java::lang::reflect::Field *field = new java::lang::reflect::Field();
1618       field->declaringClass = cls;
1619       field->offset = (char*) fieldID - (char *) cls->fields;
1620       field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1621       return wrap_value (env, field);
1622     }
1623   catch (jthrowable t)
1624     {
1625       env->ex = t;
1626     }
1627   return NULL;
1628 }
1629
1630 // JDK 1.2
1631 static jfieldID
1632 (JNICALL _Jv_JNI_FromReflectedField) (JNIEnv *, jobject f)
1633 {
1634   using namespace java::lang::reflect;
1635
1636   f = unwrap (f);
1637   Field *field = reinterpret_cast<Field *> (f);
1638   return _Jv_FromReflectedField (field);
1639 }
1640
1641 jobject
1642 (JNICALL _Jv_JNI_ToReflectedMethod) (JNIEnv *env, jclass klass, jmethodID id,
1643                                      jboolean)
1644 {
1645   using namespace java::lang::reflect;
1646
1647   jobject result = NULL;
1648   klass = unwrap (klass);
1649
1650   try
1651     {
1652       if (_Jv_equalUtf8Consts (id->name, init_name))
1653         {
1654           // A constructor.
1655           Constructor *cons = new Constructor ();
1656           cons->offset = (char *) id - (char *) &klass->methods;
1657           cons->declaringClass = klass;
1658           result = cons;
1659         }
1660       else
1661         {
1662           Method *meth = new Method ();
1663           meth->offset = (char *) id - (char *) &klass->methods;
1664           meth->declaringClass = klass;
1665           result = meth;
1666         }
1667     }
1668   catch (jthrowable t)
1669     {
1670       env->ex = t;
1671     }
1672
1673   return wrap_value (env, result);
1674 }
1675
1676 static jmethodID
1677 (JNICALL _Jv_JNI_FromReflectedMethod) (JNIEnv *, jobject method)
1678 {
1679   using namespace java::lang::reflect;
1680   method = unwrap (method);
1681   if (Method::class$.isInstance (method))
1682     return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1683   return
1684     _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1685 }
1686
1687 // JDK 1.2.
1688 jweak
1689 (JNICALL _Jv_JNI_NewWeakGlobalRef) (JNIEnv *env, jobject obj)
1690 {
1691   using namespace gnu::gcj::runtime;
1692   JNIWeakRef *ref = NULL;
1693
1694   try
1695     {
1696       // This seems weird but I think it is correct.
1697       obj = unwrap (obj);
1698       ref = new JNIWeakRef (obj);
1699       mark_for_gc (ref, global_ref_table);
1700     }
1701   catch (jthrowable t)
1702     {
1703       env->ex = t;
1704     }
1705
1706   return reinterpret_cast<jweak> (ref);
1707 }
1708
1709 void
1710 (JNICALL _Jv_JNI_DeleteWeakGlobalRef) (JNIEnv *, jweak obj)
1711 {
1712   using namespace gnu::gcj::runtime;
1713   JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1714   unmark_for_gc (ref, global_ref_table);
1715   ref->clear ();
1716 }
1717
1718 \f
1719
1720 // Direct byte buffers.
1721
1722 static jobject
1723 (JNICALL _Jv_JNI_NewDirectByteBuffer) (JNIEnv *, void *, jlong)
1724 {
1725   // For now we don't support this.
1726   return NULL;
1727 }
1728
1729 static void *
1730 (JNICALL _Jv_JNI_GetDirectBufferAddress) (JNIEnv *, jobject)
1731 {
1732   // For now we don't support this.
1733   return NULL;
1734 }
1735
1736 static jlong
1737 (JNICALL _Jv_JNI_GetDirectBufferCapacity) (JNIEnv *, jobject)
1738 {
1739   // For now we don't support this.
1740   return -1;
1741 }
1742
1743 \f
1744
1745 // Hash table of native methods.
1746 static JNINativeMethod *nathash;
1747 // Number of slots used.
1748 static int nathash_count = 0;
1749 // Number of slots available.  Must be power of 2.
1750 static int nathash_size = 0;
1751
1752 #define DELETED_ENTRY ((char *) (~0))
1753
1754 // Compute a hash value for a native method descriptor.
1755 static int
1756 hash (const JNINativeMethod *method)
1757 {
1758   char *ptr;
1759   int hash = 0;
1760
1761   ptr = method->name;
1762   while (*ptr)
1763     hash = (31 * hash) + *ptr++;
1764
1765   ptr = method->signature;
1766   while (*ptr)
1767     hash = (31 * hash) + *ptr++;
1768
1769   return hash;
1770 }
1771
1772 // Find the slot where a native method goes.
1773 static JNINativeMethod *
1774 nathash_find_slot (const JNINativeMethod *method)
1775 {
1776   jint h = hash (method);
1777   int step = (h ^ (h >> 16)) | 1;
1778   int w = h & (nathash_size - 1);
1779   int del = -1;
1780
1781   for (;;)
1782     {
1783       JNINativeMethod *slotp = &nathash[w];
1784       if (slotp->name == NULL)
1785         {
1786           if (del >= 0)
1787             return &nathash[del];
1788           else
1789             return slotp;
1790         }
1791       else if (slotp->name == DELETED_ENTRY)
1792         del = w;
1793       else if (! strcmp (slotp->name, method->name)
1794                && ! strcmp (slotp->signature, method->signature))
1795         return slotp;
1796       w = (w + step) & (nathash_size - 1);
1797     }
1798 }
1799
1800 // Find a method.  Return NULL if it isn't in the hash table.
1801 static void *
1802 nathash_find (JNINativeMethod *method)
1803 {
1804   if (nathash == NULL)
1805     return NULL;
1806   JNINativeMethod *slot = nathash_find_slot (method);
1807   if (slot->name == NULL || slot->name == DELETED_ENTRY)
1808     return NULL;
1809   return slot->fnPtr;
1810 }
1811
1812 static void
1813 natrehash ()
1814 {
1815   if (nathash == NULL)
1816     {
1817       nathash_size = 1024;
1818       nathash =
1819         (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1820                                             * sizeof (JNINativeMethod));
1821       memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1822     }
1823   else
1824     {
1825       int savesize = nathash_size;
1826       JNINativeMethod *savehash = nathash;
1827       nathash_size *= 2;
1828       nathash =
1829         (JNINativeMethod *) _Jv_AllocBytes (nathash_size
1830                                             * sizeof (JNINativeMethod));
1831       memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
1832
1833       for (int i = 0; i < savesize; ++i)
1834         {
1835           if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1836             {
1837               JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
1838               *slot = savehash[i];
1839             }
1840         }
1841     }
1842 }
1843
1844 static void
1845 nathash_add (const JNINativeMethod *method)
1846 {
1847   if (3 * nathash_count >= 2 * nathash_size)
1848     natrehash ();
1849   JNINativeMethod *slot = nathash_find_slot (method);
1850   // If the slot has a real entry in it, then there is no work to do.
1851   if (slot->name != NULL && slot->name != DELETED_ENTRY)
1852     return;
1853   // FIXME
1854   slot->name = strdup (method->name);
1855   slot->signature = strdup (method->signature);
1856   slot->fnPtr = method->fnPtr;
1857 }
1858
1859 static jint
1860 (JNICALL _Jv_JNI_RegisterNatives) (JNIEnv *env, jclass klass,
1861                                    const JNINativeMethod *methods,
1862                                    jint nMethods)
1863 {
1864   // Synchronize while we do the work.  This must match
1865   // synchronization in some other functions that manipulate or use
1866   // the nathash table.
1867   JvSynchronize sync (global_ref_table);
1868
1869   // Look at each descriptor given us, and find the corresponding
1870   // method in the class.
1871   for (int j = 0; j < nMethods; ++j)
1872     {
1873       bool found = false;
1874
1875       _Jv_Method *imeths = JvGetFirstMethod (klass);
1876       for (int i = 0; i < JvNumMethods (klass); ++i)
1877         {
1878           _Jv_Method *self = &imeths[i];
1879
1880           if (! strcmp (self->name->data, methods[j].name)
1881               && ! strcmp (self->signature->data, methods[j].signature))
1882             {
1883               if (! (self->accflags
1884                      & java::lang::reflect::Modifier::NATIVE))
1885                 break;
1886
1887               // Found a match that is native.
1888               found = true;
1889               nathash_add (&methods[j]);
1890
1891               break;
1892             }
1893         }
1894
1895       if (! found)
1896         {
1897           jstring m = JvNewStringUTF (methods[j].name);
1898           try
1899             {
1900               env->ex =new java::lang::NoSuchMethodError (m);
1901             }
1902           catch (jthrowable t)
1903             {
1904               env->ex = t;
1905             }
1906           return JNI_ERR;
1907         }
1908     }
1909
1910   return JNI_OK;
1911 }
1912
1913 static jint
1914 (JNICALL _Jv_JNI_UnregisterNatives) (JNIEnv *, jclass)
1915 {
1916   // FIXME -- we could implement this.
1917   return JNI_ERR;
1918 }
1919
1920 \f
1921
1922 // Add a character to the buffer, encoding properly.
1923 static void
1924 add_char (char *buf, jchar c, int *here)
1925 {
1926   if (c == '_')
1927     {
1928       buf[(*here)++] = '_';
1929       buf[(*here)++] = '1';
1930     }
1931   else if (c == ';')
1932     {
1933       buf[(*here)++] = '_';
1934       buf[(*here)++] = '2';
1935     }
1936   else if (c == '[')
1937     {
1938       buf[(*here)++] = '_';
1939       buf[(*here)++] = '3';
1940     }
1941
1942   // Also check for `.' here because we might be passed an internal
1943   // qualified class name like `foo.bar'.
1944   else if (c == '/' || c == '.')
1945     buf[(*here)++] = '_';
1946   else if ((c >= '0' && c <= '9')
1947            || (c >= 'a' && c <= 'z')
1948            || (c >= 'A' && c <= 'Z'))
1949     buf[(*here)++] = (char) c;
1950   else
1951     {
1952       // "Unicode" character.
1953       buf[(*here)++] = '_';
1954       buf[(*here)++] = '0';
1955       for (int i = 0; i < 4; ++i)
1956         {
1957           int val = c & 0x0f;
1958           buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
1959           c >>= 4;
1960         }
1961       *here += 4;
1962     }
1963 }
1964
1965 // Compute a mangled name for a native function.  This computes the
1966 // long name, and also returns an index which indicates where a NUL
1967 // can be placed to create the short name.  This function assumes that
1968 // the buffer is large enough for its results.
1969 static void
1970 mangled_name (jclass klass, _Jv_Utf8Const *func_name,
1971               _Jv_Utf8Const *signature, char *buf, int *long_start)
1972 {
1973   strcpy (buf, "Java_");
1974   int here = 5;
1975
1976   // Add fully qualified class name.
1977   jchar *chars = _Jv_GetStringChars (klass->getName ());
1978   jint len = klass->getName ()->length ();
1979   for (int i = 0; i < len; ++i)
1980     add_char (buf, chars[i], &here);
1981
1982   // Don't use add_char because we need a literal `_'.
1983   buf[here++] = '_';
1984
1985   const unsigned char *fn = (const unsigned char *) func_name->data;
1986   const unsigned char *limit = fn + func_name->length;
1987   for (int i = 0; ; ++i)
1988     {
1989       int ch = UTF8_GET (fn, limit);
1990       if (ch < 0)
1991         break;
1992       add_char (buf, ch, &here);
1993     }
1994
1995   // This is where the long signature begins.
1996   *long_start = here;
1997   buf[here++] = '_';
1998   buf[here++] = '_';
1999
2000   const unsigned char *sig = (const unsigned char *) signature->data;
2001   limit = sig + signature->length;
2002   JvAssert (sig[0] == '(');
2003   ++sig;
2004   while (1)
2005     {
2006       int ch = UTF8_GET (sig, limit);
2007       if (ch == ')' || ch < 0)
2008         break;
2009       add_char (buf, ch, &here);
2010     }
2011
2012   buf[here] = '\0';
2013 }
2014
2015 // Return the current thread's JNIEnv; if one does not exist, create
2016 // it.  Also create a new system frame for use.  This is `extern "C"'
2017 // because the compiler calls it.
2018 extern "C" JNIEnv *
2019 _Jv_GetJNIEnvNewFrame (jclass klass)
2020 {
2021   JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2022   if (env == NULL)
2023     {
2024       env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2025       env->p = &_Jv_JNIFunctions;
2026       env->klass = klass;
2027       env->locals = NULL;
2028       // We set env->ex below.
2029
2030       _Jv_SetCurrentJNIEnv (env);
2031     }
2032
2033   _Jv_JNI_LocalFrame *frame
2034     = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2035                                                   + (FRAME_SIZE
2036                                                      * sizeof (jobject)));
2037
2038   frame->marker = MARK_SYSTEM;
2039   frame->size = FRAME_SIZE;
2040   frame->next = env->locals;
2041
2042   for (int i = 0; i < frame->size; ++i)
2043     frame->vec[i] = NULL;
2044
2045   env->locals = frame;
2046   env->ex = NULL;
2047
2048   return env;
2049 }
2050
2051 // Return the function which implements a particular JNI method.  If
2052 // we can't find the function, we throw the appropriate exception.
2053 // This is `extern "C"' because the compiler uses it.
2054 extern "C" void *
2055 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
2056                      _Jv_Utf8Const *signature, int args_size)
2057 {
2058   char buf[10 + 6 * (name->length + signature->length) + 12];
2059   int long_start;
2060   void *function;
2061
2062   // Synchronize on something convenient.  Right now we use the hash.
2063   JvSynchronize sync (global_ref_table);
2064
2065   // First see if we have an override in the hash table.
2066   strncpy (buf, name->data, name->length);
2067   buf[name->length] = '\0';
2068   strncpy (buf + name->length + 1, signature->data, signature->length);
2069   buf[name->length + signature->length + 1] = '\0';
2070   JNINativeMethod meth;
2071   meth.name = buf;
2072   meth.signature = buf + name->length + 1;
2073   function = nathash_find (&meth);
2074   if (function != NULL)
2075     return function;
2076
2077   // If there was no override, then look in the symbol table.
2078   buf[0] = '_';
2079   mangled_name (klass, name, signature, buf + 1, &long_start);
2080   char c = buf[long_start + 1];
2081   buf[long_start + 1] = '\0';
2082
2083   function = _Jv_FindSymbolInExecutable (buf + 1);
2084 #ifdef WIN32
2085   // On Win32, we use the "stdcall" calling convention (see JNICALL
2086   // in jni.h).
2087   // 
2088   // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2089   // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2090   // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2091   // take care of all these variations here.
2092
2093   char asz_buf[12];    /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2094   char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2095
2096   if (function == NULL)
2097     {
2098       // We have tried searching for the 'fooBar' form (BCC) - now
2099       // try the others.
2100
2101       // First, save the part of the long name that will be damaged
2102       // by appending '@nn'.
2103       memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2104
2105       sprintf (asz_buf, "@%d", args_size);
2106       strcat (buf, asz_buf);
2107
2108       // Search for the '_fooBar@nn' form (MSVC).
2109       function = _Jv_FindSymbolInExecutable (buf);
2110
2111       if (function == NULL)
2112         {
2113           // Search for the 'fooBar@nn' form (MinGW GCC).
2114           function = _Jv_FindSymbolInExecutable (buf + 1);
2115         }
2116     }
2117 #else /* WIN32 */
2118   args_size;  /* Dummy statement to avoid unused parameter warning */
2119 #endif /* ! WIN32 */
2120
2121   if (function == NULL)
2122     {
2123       buf[long_start + 1] = c;
2124 #ifdef WIN32
2125       // Restore the part of the long name that was damaged by 
2126       // appending the '@nn'.
2127       memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2128 #endif /* WIN32 */
2129       function = _Jv_FindSymbolInExecutable (buf + 1);
2130       if (function == NULL)
2131         {
2132 #ifdef WIN32
2133           strcat (buf, asz_buf);
2134           function = _Jv_FindSymbolInExecutable (buf);
2135           if (function == NULL)
2136             function = _Jv_FindSymbolInExecutable (buf + 1);
2137
2138           if (function == NULL)
2139 #endif /* WIN32 */
2140             {
2141               jstring str = JvNewStringUTF (name->data);
2142               throw new java::lang::UnsatisfiedLinkError (str);
2143             }
2144         }
2145     }
2146
2147   return function;
2148 }
2149
2150 #ifdef INTERPRETER
2151
2152 // This function is the stub which is used to turn an ordinary (CNI)
2153 // method call into a JNI call.
2154 void
2155 _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
2156 {
2157   _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2158
2159   JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2160
2161   // FIXME: we should mark every reference parameter as a local.  For
2162   // now we assume a conservative GC, and we assume that the
2163   // references are on the stack somewhere.
2164
2165   // We cache the value that we find, of course, but if we don't find
2166   // a value we don't cache that fact -- we might subsequently load a
2167   // library which finds the function in question.
2168   {
2169     // Synchronize on a convenient object to ensure sanity in case two
2170     // threads reach this point for the same function at the same
2171     // time.
2172     JvSynchronize sync (global_ref_table);
2173     if (_this->function == NULL)
2174       {
2175         int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2176
2177         if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2178           args_size += sizeof (_this->defining_class);
2179
2180         _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2181                                                _this->self->name,
2182                                                _this->self->signature,
2183                                                args_size);
2184       }
2185   }
2186
2187   JvAssert (_this->args_raw_size % sizeof (ffi_raw) == 0);
2188   ffi_raw real_args[2 + _this->args_raw_size / sizeof (ffi_raw)];
2189   int offset = 0;
2190
2191   // First argument is always the environment pointer.
2192   real_args[offset++].ptr = env;
2193
2194   // For a static method, we pass in the Class.  For non-static
2195   // methods, the `this' argument is already handled.
2196   if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2197     real_args[offset++].ptr = _this->defining_class;
2198
2199   // In libgcj, the callee synchronizes.
2200   jobject sync = NULL;
2201   if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2202     {
2203       if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2204         sync = _this->defining_class;
2205       else
2206         sync = (jobject) args[0].ptr;
2207       _Jv_MonitorEnter (sync);
2208     }
2209
2210   // Copy over passed-in arguments.
2211   memcpy (&real_args[offset], args, _this->args_raw_size);
2212
2213   // The actual call to the JNI function.
2214   ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2215                 ret, real_args);
2216
2217   if (sync != NULL)
2218     _Jv_MonitorExit (sync);
2219
2220   _Jv_JNI_PopSystemFrame (env);
2221 }
2222
2223 #endif /* INTERPRETER */
2224
2225 \f
2226
2227 //
2228 // Invocation API.
2229 //
2230
2231 // An internal helper function.
2232 static jint
2233 _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2234                              void *args, jboolean is_daemon)
2235 {
2236   JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2237   java::lang::ThreadGroup *group = NULL;
2238
2239   if (attach)
2240     {
2241       // FIXME: do we really want to support 1.1?
2242       if (attach->version != JNI_VERSION_1_4
2243           && attach->version != JNI_VERSION_1_2
2244           && attach->version != JNI_VERSION_1_1)
2245         return JNI_EVERSION;
2246
2247       JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
2248       group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2249     }
2250
2251   // Attaching an already-attached thread is a no-op.
2252   if (_Jv_GetCurrentJNIEnv () != NULL)
2253     return 0;
2254
2255   JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2256   if (env == NULL)
2257     return JNI_ERR;
2258   env->p = &_Jv_JNIFunctions;
2259   env->ex = NULL;
2260   env->klass = NULL;
2261   env->locals
2262     = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2263                                                   + (FRAME_SIZE
2264                                                      * sizeof (jobject)));
2265   if (env->locals == NULL)
2266     {
2267       _Jv_Free (env);
2268       return JNI_ERR;
2269     }
2270
2271   env->locals->marker = MARK_SYSTEM;
2272   env->locals->size = FRAME_SIZE;
2273   env->locals->next = NULL;
2274
2275   for (int i = 0; i < env->locals->size; ++i)
2276     env->locals->vec[i] = NULL;
2277
2278   *penv = reinterpret_cast<void *> (env);
2279
2280   // This thread might already be a Java thread -- this function might
2281   // have been called simply to set the new JNIEnv.
2282   if (_Jv_ThreadCurrent () == NULL)
2283     {
2284       try
2285         {
2286           if (is_daemon)
2287             _Jv_AttachCurrentThreadAsDaemon (name, group);
2288           else
2289             _Jv_AttachCurrentThread (name, group);
2290         }
2291       catch (jthrowable t)
2292         {
2293           return JNI_ERR;
2294         }
2295     }
2296   _Jv_SetCurrentJNIEnv (env);
2297
2298   return 0;
2299 }
2300
2301 // This is the one actually used by JNI.
2302 static jint
2303 (JNICALL _Jv_JNI_AttachCurrentThread) (JavaVM *vm, void **penv, void *args)
2304 {
2305   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2306 }
2307
2308 static jint
2309 (JNICALL _Jv_JNI_AttachCurrentThreadAsDaemon) (JavaVM *vm, void **penv, 
2310                                                void *args)
2311 {
2312   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
2313 }
2314
2315 static jint
2316 (JNICALL _Jv_JNI_DestroyJavaVM) (JavaVM *vm)
2317 {
2318   JvAssert (the_vm && vm == the_vm);
2319
2320   JNIEnv *env;
2321   if (_Jv_ThreadCurrent () != NULL)
2322     {
2323       jstring main_name;
2324       // This sucks.
2325       try
2326         {
2327           main_name = JvNewStringLatin1 ("main");
2328         }
2329       catch (jthrowable t)
2330         {
2331           return JNI_ERR;
2332         }
2333
2334       jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
2335                                             reinterpret_cast<void **> (&env),
2336                                             NULL, false);
2337       if (r < 0)
2338         return r;
2339     }
2340   else
2341     env = _Jv_GetCurrentJNIEnv ();
2342
2343   _Jv_ThreadWait ();
2344
2345   // Docs say that this always returns an error code.
2346   return JNI_ERR;
2347 }
2348
2349 jint
2350 (JNICALL _Jv_JNI_DetachCurrentThread) (JavaVM *)
2351 {
2352   jint code = _Jv_DetachCurrentThread ();
2353   return code  ? JNI_EDETACHED : 0;
2354 }
2355
2356 static jint
2357 (JNICALL _Jv_JNI_GetEnv) (JavaVM *, void **penv, jint version)
2358 {
2359   if (_Jv_ThreadCurrent () == NULL)
2360     {
2361       *penv = NULL;
2362       return JNI_EDETACHED;
2363     }
2364
2365 #ifdef ENABLE_JVMPI
2366   // Handle JVMPI requests.
2367   if (version == JVMPI_VERSION_1)
2368     {
2369       *penv = (void *) &_Jv_JVMPI_Interface;
2370       return 0;
2371     }
2372 #endif
2373
2374   // FIXME: do we really want to support 1.1?
2375   if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2376       && version != JNI_VERSION_1_1)
2377     {
2378       *penv = NULL;
2379       return JNI_EVERSION;
2380     }
2381
2382   *penv = (void *) _Jv_GetCurrentJNIEnv ();
2383   return 0;
2384 }
2385
2386 JNIEXPORT jint JNICALL
2387 JNI_GetDefaultJavaVMInitArgs (void *args)
2388 {
2389   jint version = * (jint *) args;
2390   // Here we only support 1.2 and 1.4.
2391   if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2392     return JNI_EVERSION;
2393
2394   JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2395   ia->version = JNI_VERSION_1_4;
2396   ia->nOptions = 0;
2397   ia->options = NULL;
2398   ia->ignoreUnrecognized = true;
2399
2400   return 0;
2401 }
2402
2403 JNIEXPORT jint JNICALL
2404 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
2405 {
2406   JvAssert (! the_vm);
2407
2408   _Jv_CreateJavaVM (NULL);
2409
2410   // FIXME: synchronize
2411   JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2412   if (nvm == NULL)
2413     return JNI_ERR;
2414   nvm->functions = &_Jv_JNI_InvokeFunctions;
2415
2416   // Parse the arguments.
2417   if (args != NULL)
2418     {
2419       jint version = * (jint *) args;
2420       // We only support 1.2 and 1.4.
2421       if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
2422         return JNI_EVERSION;
2423       JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
2424       for (int i = 0; i < ia->nOptions; ++i)
2425         {
2426           if (! strcmp (ia->options[i].optionString, "vfprintf")
2427               || ! strcmp (ia->options[i].optionString, "exit")
2428               || ! strcmp (ia->options[i].optionString, "abort"))
2429             {
2430               // We are required to recognize these, but for now we
2431               // don't handle them in any way.  FIXME.
2432               continue;
2433             }
2434           else if (! strncmp (ia->options[i].optionString,
2435                               "-verbose", sizeof ("-verbose") - 1))
2436             {
2437               // We don't do anything with this option either.  We
2438               // might want to make sure the argument is valid, but we
2439               // don't really care all that much for now.
2440               continue;
2441             }
2442           else if (! strncmp (ia->options[i].optionString, "-D", 2))
2443             {
2444               // FIXME.
2445               continue;
2446             }
2447           else if (ia->ignoreUnrecognized)
2448             {
2449               if (ia->options[i].optionString[0] == '_'
2450                   || ! strncmp (ia->options[i].optionString, "-X", 2))
2451                 continue;
2452             }
2453
2454           return JNI_ERR;
2455         }
2456     }
2457
2458   jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
2459   if (r < 0)
2460     return r;
2461
2462   the_vm = nvm;
2463   *vm = the_vm;
2464
2465   return 0;
2466 }
2467
2468 JNIEXPORT jint JNICALL
2469 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
2470 {
2471   if (buf_len <= 0)
2472     return JNI_ERR;
2473
2474   // We only support a single VM.
2475   if (the_vm != NULL)
2476     {
2477       vm_buffer[0] = the_vm;
2478       *n_vms = 1;
2479     }
2480   else
2481     *n_vms = 0;
2482   return 0;
2483 }
2484
2485 JavaVM *
2486 _Jv_GetJavaVM ()
2487 {
2488   // FIXME: synchronize
2489   if (! the_vm)
2490     {
2491       JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
2492       if (nvm != NULL)
2493         nvm->functions = &_Jv_JNI_InvokeFunctions;
2494       the_vm = nvm;
2495     }
2496
2497   // If this is a Java thread, we want to make sure it has an
2498   // associated JNIEnv.
2499   if (_Jv_ThreadCurrent () != NULL)
2500     {
2501       void *ignore;
2502       _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
2503     }
2504
2505   return the_vm;
2506 }
2507
2508 static jint
2509 (JNICALL _Jv_JNI_GetJavaVM) (JNIEnv *, JavaVM **vm)
2510 {
2511   *vm = _Jv_GetJavaVM ();
2512   return *vm == NULL ? JNI_ERR : JNI_OK;
2513 }
2514
2515 \f
2516
2517 #define RESERVED NULL
2518
2519 struct JNINativeInterface _Jv_JNIFunctions =
2520 {
2521   RESERVED,
2522   RESERVED,
2523   RESERVED,
2524   RESERVED,
2525   _Jv_JNI_GetVersion,           // GetVersion
2526   _Jv_JNI_DefineClass,          // DefineClass
2527   _Jv_JNI_FindClass,            // FindClass
2528   _Jv_JNI_FromReflectedMethod,  // FromReflectedMethod
2529   _Jv_JNI_FromReflectedField,   // FromReflectedField
2530   _Jv_JNI_ToReflectedMethod,    // ToReflectedMethod
2531   _Jv_JNI_GetSuperclass,        // GetSuperclass
2532   _Jv_JNI_IsAssignableFrom,     // IsAssignableFrom
2533   _Jv_JNI_ToReflectedField,     // ToReflectedField
2534   _Jv_JNI_Throw,                // Throw
2535   _Jv_JNI_ThrowNew,             // ThrowNew
2536   _Jv_JNI_ExceptionOccurred,    // ExceptionOccurred
2537   _Jv_JNI_ExceptionDescribe,    // ExceptionDescribe
2538   _Jv_JNI_ExceptionClear,       // ExceptionClear
2539   _Jv_JNI_FatalError,           // FatalError
2540
2541   _Jv_JNI_PushLocalFrame,       // PushLocalFrame
2542   _Jv_JNI_PopLocalFrame,        // PopLocalFrame
2543   _Jv_JNI_NewGlobalRef,         // NewGlobalRef
2544   _Jv_JNI_DeleteGlobalRef,      // DeleteGlobalRef
2545   _Jv_JNI_DeleteLocalRef,       // DeleteLocalRef
2546
2547   _Jv_JNI_IsSameObject,         // IsSameObject
2548
2549   _Jv_JNI_NewLocalRef,          // NewLocalRef
2550   _Jv_JNI_EnsureLocalCapacity,  // EnsureLocalCapacity
2551
2552   _Jv_JNI_AllocObject,              // AllocObject
2553   _Jv_JNI_NewObject,                // NewObject
2554   _Jv_JNI_NewObjectV,               // NewObjectV
2555   _Jv_JNI_NewObjectA,               // NewObjectA
2556   _Jv_JNI_GetObjectClass,           // GetObjectClass
2557   _Jv_JNI_IsInstanceOf,             // IsInstanceOf
2558   _Jv_JNI_GetAnyMethodID<false>,    // GetMethodID
2559
2560   _Jv_JNI_CallMethod<jobject>,          // CallObjectMethod
2561   _Jv_JNI_CallMethodV<jobject>,         // CallObjectMethodV
2562   _Jv_JNI_CallMethodA<jobject>,         // CallObjectMethodA
2563   _Jv_JNI_CallMethod<jboolean>,         // CallBooleanMethod
2564   _Jv_JNI_CallMethodV<jboolean>,        // CallBooleanMethodV
2565   _Jv_JNI_CallMethodA<jboolean>,        // CallBooleanMethodA
2566   _Jv_JNI_CallMethod<jbyte>,            // CallByteMethod
2567   _Jv_JNI_CallMethodV<jbyte>,           // CallByteMethodV
2568   _Jv_JNI_CallMethodA<jbyte>,           // CallByteMethodA
2569   _Jv_JNI_CallMethod<jchar>,            // CallCharMethod
2570   _Jv_JNI_CallMethodV<jchar>,           // CallCharMethodV
2571   _Jv_JNI_CallMethodA<jchar>,           // CallCharMethodA
2572   _Jv_JNI_CallMethod<jshort>,           // CallShortMethod
2573   _Jv_JNI_CallMethodV<jshort>,          // CallShortMethodV
2574   _Jv_JNI_CallMethodA<jshort>,          // CallShortMethodA
2575   _Jv_JNI_CallMethod<jint>,             // CallIntMethod
2576   _Jv_JNI_CallMethodV<jint>,            // CallIntMethodV
2577   _Jv_JNI_CallMethodA<jint>,            // CallIntMethodA
2578   _Jv_JNI_CallMethod<jlong>,            // CallLongMethod
2579   _Jv_JNI_CallMethodV<jlong>,           // CallLongMethodV
2580   _Jv_JNI_CallMethodA<jlong>,           // CallLongMethodA
2581   _Jv_JNI_CallMethod<jfloat>,           // CallFloatMethod
2582   _Jv_JNI_CallMethodV<jfloat>,          // CallFloatMethodV
2583   _Jv_JNI_CallMethodA<jfloat>,          // CallFloatMethodA
2584   _Jv_JNI_CallMethod<jdouble>,          // CallDoubleMethod
2585   _Jv_JNI_CallMethodV<jdouble>,         // CallDoubleMethodV
2586   _Jv_JNI_CallMethodA<jdouble>,         // CallDoubleMethodA
2587   _Jv_JNI_CallVoidMethod,               // CallVoidMethod
2588   _Jv_JNI_CallVoidMethodV,              // CallVoidMethodV
2589   _Jv_JNI_CallVoidMethodA,              // CallVoidMethodA
2590
2591   // Nonvirtual method invocation functions follow.
2592   _Jv_JNI_CallAnyMethod<jobject, nonvirtual>,   // CallNonvirtualObjectMethod
2593   _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>,  // CallNonvirtualObjectMethodV
2594   _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>,  // CallNonvirtualObjectMethodA
2595   _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>,  // CallNonvirtualBooleanMethod
2596   _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2597   _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2598   _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>,     // CallNonvirtualByteMethod
2599   _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>,    // CallNonvirtualByteMethodV
2600   _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>,    // CallNonvirtualByteMethodA
2601   _Jv_JNI_CallAnyMethod<jchar, nonvirtual>,     // CallNonvirtualCharMethod
2602   _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>,    // CallNonvirtualCharMethodV
2603   _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>,    // CallNonvirtualCharMethodA
2604   _Jv_JNI_CallAnyMethod<jshort, nonvirtual>,    // CallNonvirtualShortMethod
2605   _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>,   // CallNonvirtualShortMethodV
2606   _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>,   // CallNonvirtualShortMethodA
2607   _Jv_JNI_CallAnyMethod<jint, nonvirtual>,      // CallNonvirtualIntMethod
2608   _Jv_JNI_CallAnyMethodV<jint, nonvirtual>,     // CallNonvirtualIntMethodV
2609   _Jv_JNI_CallAnyMethodA<jint, nonvirtual>,     // CallNonvirtualIntMethodA
2610   _Jv_JNI_CallAnyMethod<jlong, nonvirtual>,     // CallNonvirtualLongMethod
2611   _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>,    // CallNonvirtualLongMethodV
2612   _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>,    // CallNonvirtualLongMethodA
2613   _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>,    // CallNonvirtualFloatMethod
2614   _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>,   // CallNonvirtualFloatMethodV
2615   _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>,   // CallNonvirtualFloatMethodA
2616   _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>,   // CallNonvirtualDoubleMethod
2617   _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>,  // CallNonvirtualDoubleMethodV
2618   _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>,  // CallNonvirtualDoubleMethodA
2619   _Jv_JNI_CallAnyVoidMethod<nonvirtual>,        // CallNonvirtualVoidMethod
2620   _Jv_JNI_CallAnyVoidMethodV<nonvirtual>,       // CallNonvirtualVoidMethodV
2621   _Jv_JNI_CallAnyVoidMethodA<nonvirtual>,       // CallNonvirtualVoidMethodA
2622
2623   _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2624   _Jv_JNI_GetField<jobject>,    // GetObjectField
2625   _Jv_JNI_GetField<jboolean>,   // GetBooleanField
2626   _Jv_JNI_GetField<jbyte>,      // GetByteField
2627   _Jv_JNI_GetField<jchar>,      // GetCharField
2628   _Jv_JNI_GetField<jshort>,     // GetShortField
2629   _Jv_JNI_GetField<jint>,       // GetIntField
2630   _Jv_JNI_GetField<jlong>,      // GetLongField
2631   _Jv_JNI_GetField<jfloat>,     // GetFloatField
2632   _Jv_JNI_GetField<jdouble>,    // GetDoubleField
2633   _Jv_JNI_SetField,             // SetObjectField
2634   _Jv_JNI_SetField,             // SetBooleanField
2635   _Jv_JNI_SetField,             // SetByteField
2636   _Jv_JNI_SetField,             // SetCharField
2637   _Jv_JNI_SetField,             // SetShortField
2638   _Jv_JNI_SetField,             // SetIntField
2639   _Jv_JNI_SetField,             // SetLongField
2640   _Jv_JNI_SetField,             // SetFloatField
2641   _Jv_JNI_SetField,             // SetDoubleField
2642   _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2643
2644   _Jv_JNI_CallStaticMethod<jobject>,      // CallStaticObjectMethod
2645   _Jv_JNI_CallStaticMethodV<jobject>,     // CallStaticObjectMethodV
2646   _Jv_JNI_CallStaticMethodA<jobject>,     // CallStaticObjectMethodA
2647   _Jv_JNI_CallStaticMethod<jboolean>,     // CallStaticBooleanMethod
2648   _Jv_JNI_CallStaticMethodV<jboolean>,    // CallStaticBooleanMethodV
2649   _Jv_JNI_CallStaticMethodA<jboolean>,    // CallStaticBooleanMethodA
2650   _Jv_JNI_CallStaticMethod<jbyte>,        // CallStaticByteMethod
2651   _Jv_JNI_CallStaticMethodV<jbyte>,       // CallStaticByteMethodV
2652   _Jv_JNI_CallStaticMethodA<jbyte>,       // CallStaticByteMethodA
2653   _Jv_JNI_CallStaticMethod<jchar>,        // CallStaticCharMethod
2654   _Jv_JNI_CallStaticMethodV<jchar>,       // CallStaticCharMethodV
2655   _Jv_JNI_CallStaticMethodA<jchar>,       // CallStaticCharMethodA
2656   _Jv_JNI_CallStaticMethod<jshort>,       // CallStaticShortMethod
2657   _Jv_JNI_CallStaticMethodV<jshort>,      // CallStaticShortMethodV
2658   _Jv_JNI_CallStaticMethodA<jshort>,      // CallStaticShortMethodA
2659   _Jv_JNI_CallStaticMethod<jint>,         // CallStaticIntMethod
2660   _Jv_JNI_CallStaticMethodV<jint>,        // CallStaticIntMethodV
2661   _Jv_JNI_CallStaticMethodA<jint>,        // CallStaticIntMethodA
2662   _Jv_JNI_CallStaticMethod<jlong>,        // CallStaticLongMethod
2663   _Jv_JNI_CallStaticMethodV<jlong>,       // CallStaticLongMethodV
2664   _Jv_JNI_CallStaticMethodA<jlong>,       // CallStaticLongMethodA
2665   _Jv_JNI_CallStaticMethod<jfloat>,       // CallStaticFloatMethod
2666   _Jv_JNI_CallStaticMethodV<jfloat>,      // CallStaticFloatMethodV
2667   _Jv_JNI_CallStaticMethodA<jfloat>,      // CallStaticFloatMethodA
2668   _Jv_JNI_CallStaticMethod<jdouble>,      // CallStaticDoubleMethod
2669   _Jv_JNI_CallStaticMethodV<jdouble>,     // CallStaticDoubleMethodV
2670   _Jv_JNI_CallStaticMethodA<jdouble>,     // CallStaticDoubleMethodA
2671   _Jv_JNI_CallStaticVoidMethod,           // CallStaticVoidMethod
2672   _Jv_JNI_CallStaticVoidMethodV,          // CallStaticVoidMethodV
2673   _Jv_JNI_CallStaticVoidMethodA,          // CallStaticVoidMethodA
2674
2675   _Jv_JNI_GetAnyFieldID<true>,         // GetStaticFieldID
2676   _Jv_JNI_GetStaticField<jobject>,     // GetStaticObjectField
2677   _Jv_JNI_GetStaticField<jboolean>,    // GetStaticBooleanField
2678   _Jv_JNI_GetStaticField<jbyte>,       // GetStaticByteField
2679   _Jv_JNI_GetStaticField<jchar>,       // GetStaticCharField
2680   _Jv_JNI_GetStaticField<jshort>,      // GetStaticShortField
2681   _Jv_JNI_GetStaticField<jint>,        // GetStaticIntField
2682   _Jv_JNI_GetStaticField<jlong>,       // GetStaticLongField
2683   _Jv_JNI_GetStaticField<jfloat>,      // GetStaticFloatField
2684   _Jv_JNI_GetStaticField<jdouble>,     // GetStaticDoubleField
2685   _Jv_JNI_SetStaticField,              // SetStaticObjectField
2686   _Jv_JNI_SetStaticField,              // SetStaticBooleanField
2687   _Jv_JNI_SetStaticField,              // SetStaticByteField
2688   _Jv_JNI_SetStaticField,              // SetStaticCharField
2689   _Jv_JNI_SetStaticField,              // SetStaticShortField
2690   _Jv_JNI_SetStaticField,              // SetStaticIntField
2691   _Jv_JNI_SetStaticField,              // SetStaticLongField
2692   _Jv_JNI_SetStaticField,              // SetStaticFloatField
2693   _Jv_JNI_SetStaticField,              // SetStaticDoubleField
2694   _Jv_JNI_NewString,                   // NewString
2695   _Jv_JNI_GetStringLength,             // GetStringLength
2696   _Jv_JNI_GetStringChars,              // GetStringChars
2697   _Jv_JNI_ReleaseStringChars,          // ReleaseStringChars
2698   _Jv_JNI_NewStringUTF,                // NewStringUTF
2699   _Jv_JNI_GetStringUTFLength,          // GetStringUTFLength
2700   _Jv_JNI_GetStringUTFChars,           // GetStringUTFChars
2701   _Jv_JNI_ReleaseStringUTFChars,       // ReleaseStringUTFChars
2702   _Jv_JNI_GetArrayLength,              // GetArrayLength
2703   _Jv_JNI_NewObjectArray,              // NewObjectArray
2704   _Jv_JNI_GetObjectArrayElement,       // GetObjectArrayElement
2705   _Jv_JNI_SetObjectArrayElement,       // SetObjectArrayElement
2706   _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
2707                                                             // NewBooleanArray
2708   _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>,     // NewByteArray
2709   _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>,     // NewCharArray
2710   _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>,   // NewShortArray
2711   _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>,       // NewIntArray
2712   _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>,     // NewLongArray
2713   _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>,   // NewFloatArray
2714   _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
2715   _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,       
2716                                             // GetBooleanArrayElements
2717   _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,  
2718                                             // GetByteArrayElements
2719   _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2720                                             // GetCharArrayElements
2721   _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,           
2722                                             // GetShortArrayElements
2723   _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,               
2724                                             // GetIntArrayElements
2725   _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,             
2726                                             // GetLongArrayElements
2727   _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,           
2728                                             // GetFloatArrayElements
2729   _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,         
2730                                             // GetDoubleArrayElements
2731   _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,    
2732                                             // ReleaseBooleanArrayElements
2733   _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,    
2734                                             // ReleaseByteArrayElements
2735   _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,    
2736                                             // ReleaseCharArrayElements
2737   _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,    
2738                                             // ReleaseShortArrayElements
2739   _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,    
2740                                             // ReleaseIntArrayElements
2741   _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,    
2742                                             // ReleaseLongArrayElements
2743   _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,    
2744                                             // ReleaseFloatArrayElements
2745   _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,    
2746                                             // ReleaseDoubleArrayElements
2747   _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,         
2748                                             // GetBooleanArrayRegion
2749   _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,       
2750                                             // GetByteArrayRegion
2751   _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,       
2752                                             // GetCharArrayRegion
2753   _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,     
2754                                             // GetShortArrayRegion
2755   _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,         
2756                                             // GetIntArrayRegion
2757   _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,       
2758                                             // GetLongArrayRegion
2759   _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,     
2760                                             // GetFloatArrayRegion
2761   _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,           
2762                                             // GetDoubleArrayRegion
2763   _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,         
2764                                             // SetBooleanArrayRegion
2765   _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,       
2766                                             // SetByteArrayRegion
2767   _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,       
2768                                             // SetCharArrayRegion
2769   _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,     
2770                                             // SetShortArrayRegion
2771   _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,         
2772                                             // SetIntArrayRegion
2773   _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,       
2774                                             // SetLongArrayRegion
2775   _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,     
2776                                             // SetFloatArrayRegion
2777   _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,           
2778                                             // SetDoubleArrayRegion
2779   _Jv_JNI_RegisterNatives,                  // RegisterNatives
2780   _Jv_JNI_UnregisterNatives,                // UnregisterNatives
2781   _Jv_JNI_MonitorEnter,                     // MonitorEnter
2782   _Jv_JNI_MonitorExit,                      // MonitorExit
2783   _Jv_JNI_GetJavaVM,                        // GetJavaVM
2784
2785   _Jv_JNI_GetStringRegion,                  // GetStringRegion
2786   _Jv_JNI_GetStringUTFRegion,               // GetStringUTFRegion
2787   _Jv_JNI_GetPrimitiveArrayCritical,        // GetPrimitiveArrayCritical
2788   _Jv_JNI_ReleasePrimitiveArrayCritical,    // ReleasePrimitiveArrayCritical
2789   _Jv_JNI_GetStringCritical,                // GetStringCritical
2790   _Jv_JNI_ReleaseStringCritical,            // ReleaseStringCritical
2791
2792   _Jv_JNI_NewWeakGlobalRef,                 // NewWeakGlobalRef
2793   _Jv_JNI_DeleteWeakGlobalRef,              // DeleteWeakGlobalRef
2794
2795   _Jv_JNI_ExceptionCheck,                   // ExceptionCheck
2796
2797   _Jv_JNI_NewDirectByteBuffer,              // NewDirectByteBuffer
2798   _Jv_JNI_GetDirectBufferAddress,           // GetDirectBufferAddress
2799   _Jv_JNI_GetDirectBufferCapacity           // GetDirectBufferCapacity
2800 };
2801
2802 struct JNIInvokeInterface _Jv_JNI_InvokeFunctions =
2803 {
2804   RESERVED,
2805   RESERVED,
2806   RESERVED,
2807
2808   _Jv_JNI_DestroyJavaVM,
2809   _Jv_JNI_AttachCurrentThread,
2810   _Jv_JNI_DetachCurrentThread,
2811   _Jv_JNI_GetEnv,
2812   _Jv_JNI_AttachCurrentThreadAsDaemon
2813 };