Paul Fisher <[EMAIL PROTECTED]> writes:

> While it's important that Classpath continue to support JNI,

The first question is why?  What is the goal?  (Obviously JVMs
should support JNI; that is not my question.)  Presumably, the
goal is that Classpath can be used as the standard Java library
for non-Gcj JVMs.  The question then is how portable do we
need to/want to be?

(1) Is it OK to write Classpath in C++ rather than C?  Doing so rules
out architectures that don't have a C++ compiler.  If a platform does
not support C++, then it doesn't support Gcc.  From a GNU point of
view, I don't think it is important to support such platforms.  So C++
is it, as it makes it easier to use CNI or a JNI/CNI hybrid.

(2) Is it OK for Classpath to depend on G++ extensions?  For example,
G++ has some special knowledge of Java, to support CNI.  Whether
these extensions are going to help with JNI is another matter.

Tom Tromey suggested that the ideal way to let people write code
that is both JNI and CNI-compliant is to have a compiler that can
read CNI and emit JNI.  See:
http://sourceware.cygnus.com/ml/java-discuss/1999-q4/msg00570.html
My first reaction was that this was too difficult.  But it becomes
simpler if we remember that JNI is an Application *Binary* Interface.
All we need do is have G++ have an option to generate JNI calls.

For example, let is call this option -femit-jni.  First, note that
G++ already has the concept that certain classes have the "Java
property".  (In practice, thes are the classes that inherit from
java::lang::Object.)  If the compiler sees a field reference
        VAL->FIELDNAME
*and* -femit-jni has been specified *and* the type of VAL is a
pointer to a class that has the Java property, *then* the compiler
instead generates (code *as if* it had seen):
        static jfieldID f1 =
          (*__jnienv)->GetFieldID(__jnienv, CLS, "FIELDNAME", ...);
        (*__jnienv)->GetObjectField(__jnienv, VAL, f1);

Method invocation can be handled similarly.

What about the __jnienv mentioned before?  That is the JNIEnv pointer
needed by JNI.  It is provided by the JNI call.  The compiler
has to translate the CNI method declaration to a JNI method.
For example:
        jclass java::lang::Object::getClass()
        {
          return ...
        }
would get translated by the compiler to:
        jclass Java_java_lang_Object_getClass(JNIEnv* __jnienv, jobject this)
        {
          return ...;
        }

That is not a big deal.  Basically, it's a special name mangling,
plus an extra hidden argument.

Finally, consider the body of Object::getClass().  We want to be able to
write:
        return Jv_GetObjectClass(this);
We want to have the compiler generate:
        return (*__jnienv)->GetObjectClass(__jnienv, this);
We don't want to teach the compiler about all these CNJ/JNI methods.
Instead, we put it in the cni.h header file:
        #ifdef __JNI
        #define Jv_GetObjectClass(OBJ) \
          (*__jnienv)->GetObjectClass(__jnienv, OBJ) 
        #else
        #define Jv_GetObjectClass(OBJ) _Jv_GetObjectClass(OBJ)
        #endif
Now all we have to do if make sure -femit-jni causes __JNI to be defined.
-- 
        --Per Bothner
[EMAIL PROTECTED]   http://www.bothner.com/~per/

Reply via email to