This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: CNI namespace


Per Bothner <per@bothner.com> writes:

> Paul sent me some private email, which didn't really explain the
> idea.  For one thing, I don't see how the JNIEnv can get passed.

Sorry for the delay in replying -- been sick of late.

The JNIEnv and jobject (this) is passed into a function from the
foreign VM, so it's always around for calling member functions and
accessing fields.  When it's not possible to pass around the JNIEnv
(ie. when calling a non-member function), it can be simply retrieved
as thread local data.  No changes to g++ should be necessary.

gcjh could be modified to churn out appropriate code to handle calling
CNI functions from JNI entry points, and Java functions via JNI from
CNI entry points.

For example, a native routine for Object.hashCode () written in CNI,
and a Java routine for Object.equals (), gcjh might generate code that
looks something like:

class Object
{
  static jmethodID *method_ids;

  jobject obj;
  JNIEnv *env;
  
 public:
  Object (JNIEnv *env, jobject obj);
  
  int hashCode ();
  jboolean equals (Object *o);
};

// foreign VM JNI entry point -- passes in JNIEnv and `this'
jint
Java_java_lang_Object_hashCode (JNIEnv *env, jobject obj)
{
  Object o = Object (env, obj);
  return o.hashCode ();
}

// user written CNI routine
jint
hashCode ()
{
  return 1;
}

// turns a CNI/Java call into a JNI/Java call
jboolean
Object::equals (Object *o)
{
  if (!method_ids[EQUALS_ID])
    {
      method_ids[EQUALS_ID] =
        env->GetMethodID (env->FindClass ("java/lang/Object"),
                             "equals", "(Ljava/lang/Object;)Z");
    }

  return env->CallBooleanMethod (obj, method_ids[EQUALS_ID], o->obj);
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]