I've been working on coming up with an "acceptable" solution for
dealing with our current merger problems regarding the use of CNI and
JNI.  While it's important that Classpath continue to support JNI, CNI
does have significant benefits, and I'd like to start porting
Classpath to CNI, assuming we can get some sort of sane API mapping
worked out.

In the ideal world, I'd like to be able to write a Java class, mark
appropriate native methods, and then write CNI code.  gcjh would be
able to generate appropriate wrapper code that would translate CNI
calls into JNI calls at runtime.

The process seems to be rather straightforward, with the exception of
accessing fields.  The only fool proof method (that I know of) to
solve the problem is to trap reads and writes to a section of memory
set aside for fields when using JNI.  This would be unacceptably slow.
So I've written a bit of C++ which tries to turn the transparent
access of fields in CNI into something that the compiler can insert
appropriate JNI calls when needed.

Assuming we follow a few rules, we can treat fields as being
"mostly-transparent" and clue the compiler into inserting JNI calls at
appropriate locations.

Where transparency fails:
1) sizeof (field) != sizeof (basic data type)
2) reading/writing directly to the field must occur after the field
   has been "accessed"
3) fields are LocalRefs

Entering a C method when using JNI, would bundle up all the Java
fields into JNIField<type> objects stored in a CNI-like looking
structure.  So a jint field, would turn into a JNIField<jint> field.
JNIFields contain an automatic typecast for their appropriate basic
data type, such that in most cases, the object can be treated as
though it were a basic data type.  Any C++ operators that contain side
effects are overloaded.  JNIFields do not hold the value stored in the
equivalent Java field, until the JNIField is accessed through either
the automatic typecast or one of the overloaded operators.  For any
reading and writing that occurs w.r.t the address of the JNIField
directly, the JNIField destructor compares the initial field value
with the field value held when the current scope exits.

For cases where this transparency isn't good enough (I doubt this
would happen often), we can always just #ifdef out CNI and JNI
versions of a native function.

Assuming these losses are acceptable, I'll continue on with this
project.  Thoughts?

Here's a brief look at the current C++ class that I'm experimenting
with for the basic JNIField:

template<class T>
class JNIField
{
  T val, old_val;

  JNIEnv *env;
  jobject obj;
  jfieldID id;
  char *clazz, *field, *sig;

public:
  JNIField<T> (JNIEnv *env, jobject obj, 
               char *clazz, char *field, char *sig);
  ~JNIField ();

  operator T();

  JNIField<T>& operator = (const T);
  JNIField<T>& operator += (const T);
  JNIField<T>& operator -= (const T);
  JNIField<T>& operator *= (const T);
  JNIField<T>& operator /= (const T);
  
  JNIField<T>& operator %= (const T);
  JNIField<T>& operator ^= (const T);
  JNIField<T>& operator &= (const T);
  JNIField<T>& operator |= (const T);
  JNIField<T>& operator >>= (const T);
  JNIField<T>& operator <<= (const T);

  const JNIField<T>& operator++ ();
  const JNIField<T>  operator++ (int);
  const JNIField<T>& operator-- ();
  const JNIField<T>  operator-- (int);

  void resolve_field ();
  void set_field (T new_val);
  T get_field ();
};

Reply via email to