source: vendor/gcc/3.2.2/libjava/jni.cc

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

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