OSDN Git Service

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