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