OSDN Git Service

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