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