OSDN Git Service

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