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