OSDN Git Service

2005-02-13 Anthony Green <green@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / jni.cc
index 68aeb5d..b7c2084 100644 (file)
@@ -1,6 +1,7 @@
 // jni.cc - JNI implementation, including the jump table.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Free Software Foundation
 
    This file is part of libgcj.
 
@@ -27,6 +28,7 @@ details.  */
 #include <java/lang/Throwable.h>
 #include <java/lang/ArrayIndexOutOfBoundsException.h>
 #include <java/lang/StringIndexOutOfBoundsException.h>
+#include <java/lang/StringBuffer.h>
 #include <java/lang/UnsatisfiedLinkError.h>
 #include <java/lang/InstantiationException.h>
 #include <java/lang/NoSuchFieldError.h>
@@ -35,11 +37,15 @@ details.  */
 #include <java/lang/reflect/Method.h>
 #include <java/lang/reflect/Modifier.h>
 #include <java/lang/OutOfMemoryError.h>
-#include <java/util/IdentityHashMap.h>
 #include <java/lang/Integer.h>
 #include <java/lang/ThreadGroup.h>
 #include <java/lang/Thread.h>
 #include <java/lang/IllegalAccessError.h>
+#include <java/nio/Buffer.h>
+#include <java/nio/DirectByteBufferImpl.h>
+#include <java/nio/DirectByteBufferImpl$ReadWrite.h>
+#include <java/util/IdentityHashMap.h>
+#include <gnu/gcj/RawData.h>
 
 #include <gcj/method.h>
 #include <gcj/field.h>
@@ -65,7 +71,7 @@ extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions;
 
 // Number of slots in the default frame.  The VM must allow at least
 // 16.
-#define FRAME_SIZE 32
+#define FRAME_SIZE 16
 
 // Mark value indicating this is an overflow frame.
 #define MARK_NONE    0
@@ -79,10 +85,13 @@ struct _Jv_JNI_LocalFrame
 {
   // This is true if this frame object represents a pushed frame (eg
   // from PushLocalFrame).
-  int marker :  2;
+  int marker;
+
+  // Flag to indicate some locals were allocated.
+  int allocated_p;
 
   // Number of elements in frame.
-  int size   : 30;
+  int size;
 
   // Next frame in chain.
   _Jv_JNI_LocalFrame *next;
@@ -218,8 +227,8 @@ unwrap (T *obj)
 
 \f
 
-static jobject
-(JNICALL _Jv_JNI_NewGlobalRef) (JNIEnv *, jobject obj)
+static jobject JNICALL
+_Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
 {
   // This seems weird but I think it is correct.
   obj = unwrap (obj);
@@ -227,16 +236,16 @@ static jobject
   return obj;
 }
 
-static void
-(JNICALL _Jv_JNI_DeleteGlobalRef) (JNIEnv *, jobject obj)
+static void JNICALL
+_Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
 {
   // This seems weird but I think it is correct.
   obj = unwrap (obj);
   unmark_for_gc (obj, global_ref_table);
 }
 
-static void
-(JNICALL _Jv_JNI_DeleteLocalRef) (JNIEnv *env, jobject obj)
+static void JNICALL
+_Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
 {
   _Jv_JNI_LocalFrame *frame;
 
@@ -262,8 +271,8 @@ static void
   JvAssert (0);
 }
 
-static jint
-(JNICALL _Jv_JNI_EnsureLocalCapacity) (JNIEnv *env, jint size)
+static jint JNICALL
+_Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
 {
   // It is easier to just always allocate a new frame of the requested
   // size.  This isn't the most efficient thing, but for now we don't
@@ -283,6 +292,7 @@ static jint
 
   frame->marker = MARK_NONE;
   frame->size = size;
+  frame->allocated_p = 0;
   memset (&frame->vec[0], 0, size * sizeof (jobject));
   frame->next = env->locals;
   env->locals = frame;
@@ -290,8 +300,8 @@ static jint
   return 0;
 }
 
-static jint
-(JNICALL _Jv_JNI_PushLocalFrame) (JNIEnv *env, jint size)
+static jint JNICALL
+_Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
 {
   jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
   if (r < 0)
@@ -303,8 +313,8 @@ static jint
   return 0;
 }
 
-static jobject
-(JNICALL _Jv_JNI_NewLocalRef) (JNIEnv *env, jobject obj)
+static jobject JNICALL
+_Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
 {
   // This seems weird but I think it is correct.
   obj = unwrap (obj);
@@ -321,6 +331,7 @@ static jobject
              set = true;
              done = true;
              frame->vec[i] = obj;
+             frame->allocated_p = 1;
              break;
            }
        }
@@ -338,14 +349,15 @@ static jobject
       _Jv_JNI_EnsureLocalCapacity (env, 16);
       // We know the first element of the new frame will be ok.
       env->locals->vec[0] = obj;
+      env->locals->allocated_p = 1;
     }
 
   mark_for_gc (obj, local_ref_table);
   return obj;
 }
 
-static jobject
-(JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result, int stop)
+static jobject JNICALL
+_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
 {
   _Jv_JNI_LocalFrame *rf = env->locals;
 
@@ -360,12 +372,14 @@ static jobject
       done = (rf->marker == stop);
 
       _Jv_JNI_LocalFrame *n = rf->next;
-      // When N==NULL, we've reached the stack-allocated frame, and we
-      // must not free it.  However, we must be sure to clear all its
-      // elements, since we might conceivably reuse it.
+      // When N==NULL, we've reached the reusable bottom_locals, and we must
+      // not free it.  However, we must be sure to clear all its elements.
       if (n == NULL)
        {
-         memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
+         if (rf->allocated_p)
+           memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
+         rf->allocated_p = 0;
+         rf = NULL;
          break;
        }
 
@@ -379,8 +393,8 @@ static jobject
   return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
 }
 
-static jobject
-(JNICALL _Jv_JNI_PopLocalFrame) (JNIEnv *env, jobject result)
+static jobject JNICALL
+_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
 {
   return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
 }
@@ -406,9 +420,17 @@ _Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
 extern "C" void
 _Jv_JNI_PopSystemFrame (JNIEnv *env)
 {
-  _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
+  // Only enter slow path when we're not at the bottom, or there have been
+  // allocations. Usually this is false and we can just null out the locals
+  // field.
 
-  if (env->ex)
+  if (__builtin_expect ((env->locals->next 
+                        || env->locals->allocated_p), false))
+    _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
+  else
+    env->locals = NULL;
+  
+  if (__builtin_expect (env->ex != NULL, false))
     {
       jthrowable t = env->ex;
       env->ex = NULL;
@@ -416,6 +438,18 @@ _Jv_JNI_PopSystemFrame (JNIEnv *env)
     }
 }
 
+template<typename T> T extract_from_jvalue(jvalue const & t);
+template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
+template<> jbyte    extract_from_jvalue(jvalue const & jv) { return jv.b; }
+template<> jchar    extract_from_jvalue(jvalue const & jv) { return jv.c; }
+template<> jshort   extract_from_jvalue(jvalue const & jv) { return jv.s; }
+template<> jint     extract_from_jvalue(jvalue const & jv) { return jv.i; }
+template<> jlong    extract_from_jvalue(jvalue const & jv) { return jv.j; }
+template<> jfloat   extract_from_jvalue(jvalue const & jv) { return jv.f; }
+template<> jdouble  extract_from_jvalue(jvalue const & jv) { return jv.d; }
+template<> jobject  extract_from_jvalue(jvalue const & jv) { return jv.l; }
+
+
 // This function is used from other template functions.  It wraps the
 // return value appropriately; we specialize it so that object returns
 // are turned into local references.
@@ -428,7 +462,7 @@ wrap_value (JNIEnv *, T value)
 
 // This specialization is used for jobject, jclass, jstring, jarray,
 // etc.
-template<typename T>
+template<typename R, typename T>
 static T *
 wrap_value (JNIEnv *env, T *value)
 {
@@ -439,15 +473,15 @@ wrap_value (JNIEnv *env, T *value)
 
 \f
 
-static jint
-(JNICALL _Jv_JNI_GetVersion) (JNIEnv *)
+static jint JNICALL
+_Jv_JNI_GetVersion (JNIEnv *)
 {
   return JNI_VERSION_1_4;
 }
 
-static jclass
-(JNICALL _Jv_JNI_DefineClass) (JNIEnv *env, const char *name, jobject loader,
-                              const jbyte *buf, jsize bufLen)
+static jclass JNICALL
+_Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
+                    const jbyte *buf, jsize bufLen)
 {
   try
     {
@@ -472,8 +506,8 @@ static jclass
     }
 }
 
-static jclass
-(JNICALL _Jv_JNI_FindClass) (JNIEnv *env, const char *name)
+static jclass JNICALL
+_Jv_JNI_FindClass (JNIEnv *env, const char *name)
 {
   // FIXME: assume that NAME isn't too long.
   int len = strlen (name);
@@ -489,7 +523,7 @@ static jclass
 
       java::lang::ClassLoader *loader = NULL;
       if (env->klass != NULL)
-       loader = env->klass->getClassLoader ();
+       loader = env->klass->getClassLoaderInternal ();
 
       if (loader == NULL)
        {
@@ -508,20 +542,20 @@ static jclass
   return (jclass) wrap_value (env, r);
 }
 
-static jclass
-(JNICALL _Jv_JNI_GetSuperclass) (JNIEnv *env, jclass clazz)
+static jclass JNICALL
+_Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
 {
   return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
 }
 
-static jboolean
-(JNICALL _Jv_JNI_IsAssignableFrom) (JNIEnv *, jclass clazz1, jclass clazz2)
+static jboolean JNICALL
+_Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
 {
   return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
 }
 
-static jint
-(JNICALL _Jv_JNI_Throw) (JNIEnv *env, jthrowable obj)
+static jint JNICALL
+_Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
 {
   // We check in case the user did some funky cast.
   obj = unwrap (obj);
@@ -530,8 +564,8 @@ static jint
   return 0;
 }
 
-static jint
-(JNICALL _Jv_JNI_ThrowNew) (JNIEnv *env, jclass clazz, const char *message)
+static jint JNICALL
+_Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
 {
   using namespace java::lang::reflect;
 
@@ -546,11 +580,12 @@ static jint
                                               NULL);
 
       jclass *elts = elements (argtypes);
-      elts[0] = &StringClass;
+      elts[0] = &java::lang::String::class$;
 
       Constructor *cons = clazz->getConstructor (argtypes);
 
-      jobjectArray values = JvNewObjectArray (1, &StringClass, NULL);
+      jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
+                                             NULL);
       jobject *velts = elements (values);
       velts[0] = JvNewStringUTF (message);
 
@@ -567,47 +602,47 @@ static jint
   return r;
 }
 
-static jthrowable
-(JNICALL _Jv_JNI_ExceptionOccurred) (JNIEnv *env)
+static jthrowable JNICALL
+_Jv_JNI_ExceptionOccurred (JNIEnv *env)
 {
   return (jthrowable) wrap_value (env, env->ex);
 }
 
-static void
-(JNICALL _Jv_JNI_ExceptionDescribe) (JNIEnv *env)
+static void JNICALL
+_Jv_JNI_ExceptionDescribe (JNIEnv *env)
 {
   if (env->ex != NULL)
     env->ex->printStackTrace();
 }
 
-static void
-(JNICALL _Jv_JNI_ExceptionClear) (JNIEnv *env)
+static void JNICALL
+_Jv_JNI_ExceptionClear (JNIEnv *env)
 {
   env->ex = NULL;
 }
 
-static jboolean
-(JNICALL _Jv_JNI_ExceptionCheck) (JNIEnv *env)
+static jboolean JNICALL
+_Jv_JNI_ExceptionCheck (JNIEnv *env)
 {
   return env->ex != NULL;
 }
 
-static void
-(JNICALL _Jv_JNI_FatalError) (JNIEnv *, const char *message)
+static void JNICALL
+_Jv_JNI_FatalError (JNIEnv *, const char *message)
 {
   JvFail (message);
 }
 
 \f
 
-static jboolean
-(JNICALL _Jv_JNI_IsSameObject) (JNIEnv *, jobject obj1, jobject obj2)
+static jboolean JNICALL
+_Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
 {
   return unwrap (obj1) == unwrap (obj2);
 }
 
-static jobject
-(JNICALL _Jv_JNI_AllocObject) (JNIEnv *env, jclass clazz)
+static jobject JNICALL
+_Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
 {
   jobject obj = NULL;
   using namespace java::lang::reflect;
@@ -619,7 +654,7 @@ static jobject
       if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
        env->ex = new java::lang::InstantiationException ();
       else
-       obj = JvAllocObject (clazz);
+       obj = _Jv_AllocObject (clazz);
     }
   catch (jthrowable t)
     {
@@ -629,16 +664,16 @@ static jobject
   return wrap_value (env, obj);
 }
 
-static jclass
-(JNICALL _Jv_JNI_GetObjectClass) (JNIEnv *env, jobject obj)
+static jclass JNICALL
+_Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
 {
   obj = unwrap (obj);
   JvAssert (obj);
   return (jclass) wrap_value (env, obj->getClass());
 }
 
-static jboolean
-(JNICALL _Jv_JNI_IsInstanceOf) (JNIEnv *, jobject obj, jclass clazz)
+static jboolean JNICALL
+_Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
 {
   return unwrap (clazz)->isInstance(unwrap (obj));
 }
@@ -650,9 +685,9 @@ static jboolean
 //
 
 template<jboolean is_static>
-static jmethodID
-(JNICALL _Jv_JNI_GetAnyMethodID) (JNIEnv *env, jclass clazz,
-                                 const char *name, const char *sig)
+static jmethodID JNICALL
+_Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
+                       const char *name, const char *sig)
 {
   try
     {
@@ -691,7 +726,10 @@ static jmethodID
          clazz = clazz->getSuperclass ();
        }
 
-      env->ex = new java::lang::NoSuchMethodError ();
+      java::lang::StringBuffer *name_sig =
+        new java::lang::StringBuffer (JvNewStringUTF (name));
+      name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
+      env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
     }
   catch (jthrowable t)
     {
@@ -746,16 +784,13 @@ array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
 // This can call any sort of method: virtual, "nonvirtual", static, or
 // constructor.
 template<typename T, invocation_type style>
-static T
-(JNICALL _Jv_JNI_CallAnyMethodV) (JNIEnv *env, jobject obj, jclass klass,
-                                 jmethodID id, va_list vargs)
+static T JNICALL
+_Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
+                       jmethodID id, va_list vargs)
 {
   obj = unwrap (obj);
   klass = unwrap (klass);
 
-  if (style == normal)
-    id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
-
   jclass decl_class = klass ? klass : obj->getClass ();
   JvAssert (decl_class != NULL);
 
@@ -775,15 +810,12 @@ static T
        return_type = klass;
 
       jvalue result;
-      jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
-                                         style == constructor,
-                                         arg_types, args, &result);
+      _Jv_CallAnyMethodA (obj, return_type, id,
+                         style == constructor,
+                         style == normal,
+                         arg_types, args, &result);
 
-      if (ex != NULL)
-       env->ex = ex;
-
-      // We cheat a little here.  FIXME.
-      return wrap_value (env, * (T *) &result);
+      return wrap_value (env, extract_from_jvalue<T>(result));
     }
   catch (jthrowable t)
     {
@@ -794,9 +826,9 @@ static T
 }
 
 template<typename T, invocation_type style>
-static T
-(JNICALL _Jv_JNI_CallAnyMethod) (JNIEnv *env, jobject obj, jclass klass,
-                                jmethodID method, ...)
+static T JNICALL
+_Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
+                      jmethodID method, ...)
 {
   va_list args;
   T result;
@@ -809,16 +841,13 @@ static T
 }
 
 template<typename T, invocation_type style>
-static T
-(JNICALL _Jv_JNI_CallAnyMethodA) (JNIEnv *env, jobject obj, jclass klass,
-                                 jmethodID id, jvalue *args)
+static T JNICALL
+_Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
+                       jmethodID id, jvalue *args)
 {
   obj = unwrap (obj);
   klass = unwrap (klass);
 
-  if (style == normal)
-    id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
-
   jclass decl_class = klass ? klass : obj->getClass ();
   JvAssert (decl_class != NULL);
 
@@ -845,15 +874,12 @@ static T
        }
 
       jvalue result;
-      jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
-                                         style == constructor,
-                                         arg_types, arg_copy, &result);
-
-      if (ex != NULL)
-       env->ex = ex;
+      _Jv_CallAnyMethodA (obj, return_type, id,
+                         style == constructor,
+                         style == normal,
+                         arg_types, arg_copy, &result);
 
-      // We cheat a little here.  FIXME.
-      return wrap_value (env, * (T *) &result);
+      return wrap_value (env, extract_from_jvalue<T>(result));
     }
   catch (jthrowable t)
     {
@@ -864,16 +890,13 @@ static T
 }
 
 template<invocation_type style>
-static void
-(JNICALL _Jv_JNI_CallAnyVoidMethodV) (JNIEnv *env, jobject obj, jclass klass,
-                                     jmethodID id, va_list vargs)
+static void JNICALL
+_Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
+                           jmethodID id, va_list vargs)
 {
   obj = unwrap (obj);
   klass = unwrap (klass);
 
-  if (style == normal)
-    id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
-
   jclass decl_class = klass ? klass : obj->getClass ();
   JvAssert (decl_class != NULL);
 
@@ -891,12 +914,10 @@ static void
       if (style == constructor)
        return_type = klass;
 
-      jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
-                                         style == constructor,
-                                         arg_types, args, NULL);
-
-      if (ex != NULL)
-       env->ex = ex;
+      _Jv_CallAnyMethodA (obj, return_type, id,
+                         style == constructor,
+                         style == normal,
+                         arg_types, args, NULL);
     }
   catch (jthrowable t)
     {
@@ -905,9 +926,9 @@ static void
 }
 
 template<invocation_type style>
-static void
-(JNICALL _Jv_JNI_CallAnyVoidMethod) (JNIEnv *env, jobject obj, jclass klass,
-                                    jmethodID method, ...)
+static void JNICALL
+_Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
+                          jmethodID method, ...)
 {
   va_list args;
 
@@ -917,13 +938,10 @@ static void
 }
 
 template<invocation_type style>
-static void
-(JNICALL _Jv_JNI_CallAnyVoidMethodA) (JNIEnv *env, jobject obj, jclass klass,
-                                     jmethodID id, jvalue *args)
+static void JNICALL
+_Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
+                           jmethodID id, jvalue *args)
 {
-  if (style == normal)
-    id = _Jv_LookupDeclaredMethod (obj->getClass (), id->name, id->signature);
-
   jclass decl_class = klass ? klass : obj->getClass ();
   JvAssert (decl_class != NULL);
 
@@ -945,12 +963,10 @@ static void
            arg_copy[i].l = unwrap (args[i].l);
        }
 
-      jthrowable ex = _Jv_CallAnyMethodA (obj, return_type, id,
-                                         style == constructor,
-                                         arg_types, args, NULL);
-
-      if (ex != NULL)
-       env->ex = ex;
+      _Jv_CallAnyMethodA (obj, return_type, id,
+                         style == constructor,
+                         style == normal,
+                         arg_types, args, NULL);
     }
   catch (jthrowable t)
     {
@@ -961,9 +977,9 @@ static void
 // Functions with this signature are used to implement functions in
 // the CallMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallMethodV) (JNIEnv *env, jobject obj, 
-                               jmethodID id, va_list args)
+static T JNICALL
+_Jv_JNI_CallMethodV (JNIEnv *env, jobject obj, 
+                    jmethodID id, va_list args)
 {
   return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
 }
@@ -971,8 +987,8 @@ static T
 // Functions with this signature are used to implement functions in
 // the CallMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
+static T JNICALL
+_Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
 {
   va_list args;
   T result;
@@ -987,22 +1003,22 @@ static T
 // Functions with this signature are used to implement functions in
 // the CallMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallMethodA) (JNIEnv *env, jobject obj, 
-                               jmethodID id, jvalue *args)
+static T JNICALL
+_Jv_JNI_CallMethodA (JNIEnv *env, jobject obj, 
+                    jmethodID id, jvalue *args)
 {
   return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallVoidMethodV) (JNIEnv *env, jobject obj, 
-                                   jmethodID id, va_list args)
+static void JNICALL
+_Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj, 
+                        jmethodID id, va_list args)
 {
   _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallVoidMethod) (JNIEnv *env, jobject obj, jmethodID id, ...)
+static void JNICALL
+_Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
 {
   va_list args;
 
@@ -1011,9 +1027,9 @@ static void
   va_end (args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallVoidMethodA) (JNIEnv *env, jobject obj, 
-                                   jmethodID id, jvalue *args)
+static void JNICALL
+_Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj, 
+                        jmethodID id, jvalue *args)
 {
   _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
 }
@@ -1021,9 +1037,9 @@ static void
 // Functions with this signature are used to implement functions in
 // the CallStaticMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallStaticMethodV) (JNIEnv *env, jclass klass,
-                                    jmethodID id, va_list args)
+static T JNICALL
+_Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
+                          jmethodID id, va_list args)
 {
   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
@@ -1034,9 +1050,9 @@ static T
 // Functions with this signature are used to implement functions in
 // the CallStaticMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallStaticMethod) (JNIEnv *env, jclass klass, 
-                                    jmethodID id, ...)
+static T JNICALL
+_Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass, 
+                         jmethodID id, ...)
 {
   va_list args;
   T result;
@@ -1055,9 +1071,9 @@ static T
 // Functions with this signature are used to implement functions in
 // the CallStaticMethod family.
 template<typename T>
-static T
-(JNICALL _Jv_JNI_CallStaticMethodA) (JNIEnv *env, jclass klass, jmethodID id,
-                                    jvalue *args)
+static T JNICALL
+_Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
+                          jvalue *args)
 {
   JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
   JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
@@ -1065,16 +1081,16 @@ static T
   return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallStaticVoidMethodV) (JNIEnv *env, jclass klass, 
-                                         jmethodID id, va_list args)
+static void JNICALL
+_Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass, 
+                              jmethodID id, va_list args)
 {
   _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallStaticVoidMethod) (JNIEnv *env, jclass klass, 
-                                        jmethodID id, ...)
+static void JNICALL
+_Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass, 
+                             jmethodID id, ...)
 {
   va_list args;
 
@@ -1083,16 +1099,16 @@ static void
   va_end (args);
 }
 
-static void
-(JNICALL _Jv_JNI_CallStaticVoidMethodA) (JNIEnv *env, jclass klass, 
-                                         jmethodID id, jvalue *args)
+static void JNICALL
+_Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass, 
+                              jmethodID id, jvalue *args)
 {
   _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
 }
 
-static jobject
-(JNICALL _Jv_JNI_NewObjectV) (JNIEnv *env, jclass klass,
-                             jmethodID id, va_list args)
+static jobject JNICALL
+_Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
+                   jmethodID id, va_list args)
 {
   JvAssert (klass && ! klass->isArray ());
   JvAssert (! strcmp (id->name->data, "<init>")
@@ -1105,8 +1121,8 @@ static jobject
                                                       id, args);
 }
 
-static jobject
-(JNICALL _Jv_JNI_NewObject) (JNIEnv *env, jclass klass, jmethodID id, ...)
+static jobject JNICALL
+_Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
 {
   JvAssert (klass && ! klass->isArray ());
   JvAssert (! strcmp (id->name->data, "<init>")
@@ -1126,9 +1142,9 @@ static jobject
   return result;
 }
 
-static jobject
-(JNICALL _Jv_JNI_NewObjectA) (JNIEnv *env, jclass klass, jmethodID id,
-                             jvalue *args)
+static jobject JNICALL
+_Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
+                   jvalue *args)
 {
   JvAssert (klass && ! klass->isArray ());
   JvAssert (! strcmp (id->name->data, "<init>")
@@ -1144,8 +1160,8 @@ static jobject
 \f
 
 template<typename T>
-static T
-(JNICALL _Jv_JNI_GetField) (JNIEnv *env, jobject obj, jfieldID field)
+static T JNICALL
+_Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
 {
   obj = unwrap (obj);
   JvAssert (obj);
@@ -1154,8 +1170,8 @@ static T
 }
 
 template<typename T>
-static void
-(JNICALL _Jv_JNI_SetField) (JNIEnv *, jobject obj, jfieldID field, T value)
+static void JNICALL
+_Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
 {
   obj = unwrap (obj);
   value = unwrap (value);
@@ -1166,9 +1182,9 @@ static void
 }
 
 template<jboolean is_static>
-static jfieldID
-(JNICALL _Jv_JNI_GetAnyFieldID) (JNIEnv *env, jclass clazz,
-                                const char *name, const char *sig)
+static jfieldID JNICALL
+_Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
+                      const char *name, const char *sig)
 {
   try
     {
@@ -1187,7 +1203,7 @@ static jfieldID
 
       // FIXME: what if field_class == NULL?
 
-      java::lang::ClassLoader *loader = clazz->getClassLoader ();
+      java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
       while (clazz != NULL)
        {
          // We acquire the class lock so that fields aren't resolved
@@ -1206,7 +1222,7 @@ static jfieldID
 
              // The field might be resolved or it might not be.  It
              // is much simpler to always resolve it.
-             _Jv_ResolveField (field, loader);
+             _Jv_Linker::resolve_field (field, loader);
              if (_Jv_equalUtf8Consts (f_name, a_name)
                  && field->getClass() == field_class)
                return field;
@@ -1227,24 +1243,24 @@ static jfieldID
 }
 
 template<typename T>
-static T
-(JNICALL _Jv_JNI_GetStaticField) (JNIEnv *env, jclass, jfieldID field)
+static T JNICALL
+_Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
 {
   T *ptr = (T *) field->u.addr;
   return wrap_value (env, *ptr);
 }
 
 template<typename T>
-static void
-(JNICALL _Jv_JNI_SetStaticField) (JNIEnv *, jclass, jfieldID field, T value)
+static void JNICALL
+_Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
 {
   value = unwrap (value);
   T *ptr = (T *) field->u.addr;
   *ptr = value;
 }
 
-static jstring
-(JNICALL _Jv_JNI_NewString) (JNIEnv *env, const jchar *unichars, jsize len)
+static jstring JNICALL
+_Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
 {
   try
     {
@@ -1258,14 +1274,14 @@ static jstring
     }
 }
 
-static jsize
-(JNICALL _Jv_JNI_GetStringLength) (JNIEnv *, jstring string)
+static jsize JNICALL
+_Jv_JNI_GetStringLength (JNIEnv *, jstring string)
 {
   return unwrap (string)->length();
 }
 
-static const jchar *
-(JNICALL _Jv_JNI_GetStringChars) (JNIEnv *, jstring string, jboolean *isCopy)
+static const jchar * JNICALL
+_Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
 {
   string = unwrap (string);
   jchar *result = _Jv_GetStringChars (string);
@@ -1275,14 +1291,14 @@ static const jchar *
   return (const jchar *) result;
 }
 
-static void
-(JNICALL _Jv_JNI_ReleaseStringChars) (JNIEnv *, jstring string, const jchar *)
+static void JNICALL
+_Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
 {
   unmark_for_gc (unwrap (string), global_ref_table);
 }
 
-static jstring
-(JNICALL _Jv_JNI_NewStringUTF) (JNIEnv *env, const char *bytes)
+static jstring JNICALL
+_Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
 {
   try
     {
@@ -1296,22 +1312,24 @@ static jstring
     }
 }
 
-static jsize
-(JNICALL _Jv_JNI_GetStringUTFLength) (JNIEnv *, jstring string)
+static jsize JNICALL
+_Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
 {
   return JvGetStringUTFLength (unwrap (string));
 }
 
-static const char *
-(JNICALL _Jv_JNI_GetStringUTFChars) (JNIEnv *env, jstring string, 
-                                     jboolean *isCopy)
+static const char * JNICALL
+_Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string, 
+                          jboolean *isCopy)
 {
-  string = unwrap (string);
-  jsize len = JvGetStringUTFLength (string);
   try
     {
+      string = unwrap (string);
+      if (string == NULL)
+       return NULL;
+      jsize len = JvGetStringUTFLength (string);
       char *r = (char *) _Jv_Malloc (len + 1);
-      JvGetStringUTFRegion (string, 0, len, r);
+      JvGetStringUTFRegion (string, 0, string->length(), r);
       r[len] = '\0';
 
       if (isCopy)
@@ -1326,15 +1344,15 @@ static const char *
     }
 }
 
-static void
-(JNICALL _Jv_JNI_ReleaseStringUTFChars) (JNIEnv *, jstring, const char *utf)
+static void JNICALL
+_Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
 {
   _Jv_Free ((void *) utf);
 }
 
-static void
-(JNICALL _Jv_JNI_GetStringRegion) (JNIEnv *env, jstring string, jsize start, 
-                                   jsize len, jchar *buf)
+static void JNICALL
+_Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start, 
+                        jsize len, jchar *buf)
 {
   string = unwrap (string);
   jchar *result = _Jv_GetStringChars (string);
@@ -1354,9 +1372,9 @@ static void
     memcpy (buf, &result[start], len * sizeof (jchar));
 }
 
-static void
-(JNICALL _Jv_JNI_GetStringUTFRegion) (JNIEnv *env, jstring str, jsize start,
-                                     jsize len, char *buf)
+static void JNICALL
+_Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
+                           jsize len, char *buf)
 {
   str = unwrap (str);
     
@@ -1376,8 +1394,8 @@ static void
     _Jv_GetStringUTFRegion (str, start, len, buf);
 }
 
-static const jchar *
-(JNICALL _Jv_JNI_GetStringCritical) (JNIEnv *, jstring str, jboolean *isCopy)
+static const jchar * JNICALL
+_Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
 {
   jchar *result = _Jv_GetStringChars (unwrap (str));
   if (isCopy)
@@ -1385,21 +1403,21 @@ static const jchar *
   return result;
 }
 
-static void
-(JNICALL _Jv_JNI_ReleaseStringCritical) (JNIEnv *, jstring, const jchar *)
+static void JNICALL
+_Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
 {
   // Nothing.
 }
 
-static jsize
-(JNICALL _Jv_JNI_GetArrayLength) (JNIEnv *, jarray array)
+static jsize JNICALL
+_Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
 {
   return unwrap (array)->length;
 }
 
-static jarray
-(JNICALL _Jv_JNI_NewObjectArray) (JNIEnv *env, jsize length, 
-                                  jclass elementClass, jobject init)
+static jobjectArray JNICALL
+_Jv_JNI_NewObjectArray (JNIEnv *env, jsize length, 
+                       jclass elementClass, jobject init)
 {
   try
     {
@@ -1408,7 +1426,7 @@ static jarray
 
       _Jv_CheckCast (elementClass, init);
       jarray result = JvNewObjectArray (length, elementClass, init);
-      return (jarray) wrap_value (env, result);
+      return (jobjectArray) wrap_value (env, result);
     }
   catch (jthrowable t)
     {
@@ -1417,9 +1435,9 @@ static jarray
     }
 }
 
-static jobject
-(JNICALL _Jv_JNI_GetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
-                                         jsize index)
+static jobject JNICALL
+_Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array, 
+                              jsize index)
 {
   if ((unsigned) index >= (unsigned) array->length)
     _Jv_ThrowBadArrayIndex (index);
@@ -1427,9 +1445,9 @@ static jobject
   return wrap_value (env, elts[index]);
 }
 
-static void
-(JNICALL _Jv_JNI_SetObjectArrayElement) (JNIEnv *env, jobjectArray array, 
-                                         jsize index, jobject value)
+static void JNICALL
+_Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array, 
+                              jsize index, jobject value)
 {
   try
     {
@@ -1449,8 +1467,8 @@ static void
 }
 
 template<typename T, jclass K>
-static JArray<T> *
-(JNICALL _Jv_JNI_NewPrimitiveArray) (JNIEnv *env, jsize length)
+static JArray<T> * JNICALL
+_Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
 {
   try
     {
@@ -1464,9 +1482,9 @@ static JArray<T> *
 }
 
 template<typename T, jclass K>
-static T *
-(JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
-                                            jboolean *isCopy)
+static T * JNICALL
+_Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
+                                  jboolean *isCopy)
 {
   array = unwrap (array);
   if (! _Jv_JNI_check_types (env, array, K))
@@ -1482,9 +1500,9 @@ static T *
 }
 
 template<typename T, jclass K>
-static void
-(JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
-                                                T *, jint /* mode */)
+static void JNICALL
+_Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
+                                      T *, jint /* mode */)
 {
   array = unwrap (array);
   _Jv_JNI_check_types (env, array, K);
@@ -1495,9 +1513,9 @@ static void
 }
 
 template<typename T, jclass K>
-static void
-(JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
-                                          jsize start, jsize len,
+static void JNICALL
+_Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
+                                jsize start, jsize len,
                                 T *buf)
 {
   array = unwrap (array);
@@ -1527,9 +1545,9 @@ static void
 }
 
 template<typename T, jclass K>
-static void
-(JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
-                                          jsize start, jsize len, T *buf)
+static void JNICALL
+_Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
+                                jsize start, jsize len, T *buf)
 {
   array = unwrap (array);
   if (! _Jv_JNI_check_types (env, array, K))
@@ -1556,9 +1574,9 @@ static void
     }
 }
 
-static void *
-(JNICALL _Jv_JNI_GetPrimitiveArrayCritical) (JNIEnv *, jarray array,
-                                            jboolean *isCopy)
+static void * JNICALL
+_Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
+                                  jboolean *isCopy)
 {
   array = unwrap (array);
   // FIXME: does this work?
@@ -1570,14 +1588,14 @@ static void *
   return r;
 }
 
-static void
-(JNICALL _Jv_JNI_ReleasePrimitiveArrayCritical) (JNIEnv *, jarray, void *, jint)
+static void JNICALL
+_Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
 {
   // Nothing.
 }
 
-static jint
-(JNICALL _Jv_JNI_MonitorEnter) (JNIEnv *env, jobject obj)
+static jint JNICALL
+_Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
 {
   try
     {
@@ -1591,8 +1609,8 @@ static jint
   return JNI_ERR;
 }
 
-static jint
-(JNICALL _Jv_JNI_MonitorExit) (JNIEnv *env, jobject obj)
+static jint JNICALL
+_Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
 {
   try
     {
@@ -1607,9 +1625,9 @@ static jint
 }
 
 // JDK 1.2
-jobject
-(JNICALL _Jv_JNI_ToReflectedField) (JNIEnv *env, jclass cls, jfieldID fieldID,
-                                   jboolean)
+jobject JNICALL
+_Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
+                         jboolean)
 {
   try
     {
@@ -1628,8 +1646,8 @@ jobject
 }
 
 // JDK 1.2
-static jfieldID
-(JNICALL _Jv_JNI_FromReflectedField) (JNIEnv *, jobject f)
+static jfieldID JNICALL
+_Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
 {
   using namespace java::lang::reflect;
 
@@ -1638,9 +1656,9 @@ static jfieldID
   return _Jv_FromReflectedField (field);
 }
 
-jobject
-(JNICALL _Jv_JNI_ToReflectedMethod) (JNIEnv *env, jclass klass, jmethodID id,
-                                    jboolean)
+jobject JNICALL
+_Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
+                          jboolean)
 {
   using namespace java::lang::reflect;
 
@@ -1673,8 +1691,8 @@ jobject
   return wrap_value (env, result);
 }
 
-static jmethodID
-(JNICALL _Jv_JNI_FromReflectedMethod) (JNIEnv *, jobject method)
+static jmethodID JNICALL
+_Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
 {
   using namespace java::lang::reflect;
   method = unwrap (method);
@@ -1685,8 +1703,8 @@ static jmethodID
 }
 
 // JDK 1.2.
-jweak
-(JNICALL _Jv_JNI_NewWeakGlobalRef) (JNIEnv *env, jobject obj)
+jweak JNICALL
+_Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
 {
   using namespace gnu::gcj::runtime;
   JNIWeakRef *ref = NULL;
@@ -1706,8 +1724,8 @@ jweak
   return reinterpret_cast<jweak> (ref);
 }
 
-void
-(JNICALL _Jv_JNI_DeleteWeakGlobalRef) (JNIEnv *, jweak obj)
+void JNICALL
+_Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
 {
   using namespace gnu::gcj::runtime;
   JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
@@ -1719,25 +1737,35 @@ void
 
 // Direct byte buffers.
 
-static jobject
-(JNICALL _Jv_JNI_NewDirectByteBuffer) (JNIEnv *, void *, jlong)
+static jobject JNICALL
+_Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
 {
-  // For now we don't support this.
-  return NULL;
+  using namespace gnu::gcj;
+  using namespace java::nio;
+  return new DirectByteBufferImpl$ReadWrite
+    (reinterpret_cast<RawData *> (address), length);
 }
 
-static void *
-(JNICALL _Jv_JNI_GetDirectBufferAddress) (JNIEnv *, jobject)
+static void * JNICALL
+_Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
 {
-  // For now we don't support this.
-  return NULL;
+  using namespace java::nio;
+  if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
+    return NULL;
+  Buffer *tmp = static_cast<Buffer *> (buffer);
+  return reinterpret_cast<void *> (tmp->address);
 }
 
-static jlong
-(JNICALL _Jv_JNI_GetDirectBufferCapacity) (JNIEnv *, jobject)
+static jlong JNICALL
+_Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
 {
-  // For now we don't support this.
-  return -1;
+  using namespace java::nio;
+  if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
+    return -1;
+  Buffer *tmp = static_cast<Buffer *> (buffer);
+  if (tmp->address == NULL)
+    return -1;
+  return tmp->capacity();
 }
 
 \f
@@ -1852,20 +1880,23 @@ nathash_add (const JNINativeMethod *method)
     return;
   // FIXME
   slot->name = strdup (method->name);
-  slot->signature = strdup (method->signature);
+  // This was already strduped in _Jv_JNI_RegisterNatives.
+  slot->signature = method->signature;
   slot->fnPtr = method->fnPtr;
 }
 
-static jint
-(JNICALL _Jv_JNI_RegisterNatives) (JNIEnv *env, jclass klass,
-                                  const JNINativeMethod *methods,
-                                  jint nMethods)
+static jint JNICALL
+_Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
+                        const JNINativeMethod *methods,
+                        jint nMethods)
 {
   // Synchronize while we do the work.  This must match
   // synchronization in some other functions that manipulate or use
   // the nathash table.
   JvSynchronize sync (global_ref_table);
 
+  JNINativeMethod dottedMethod;
+
   // Look at each descriptor given us, and find the corresponding
   // method in the class.
   for (int j = 0; j < nMethods; ++j)
@@ -1877,16 +1908,28 @@ static jint
        {
          _Jv_Method *self = &imeths[i];
 
-         if (! strcmp (self->name->data, methods[j].name)
-             && ! strcmp (self->signature->data, methods[j].signature))
+         // Copy this JNINativeMethod and do a slash to dot
+         // conversion on the signature.
+         dottedMethod.name = methods[j].name;
+         dottedMethod.signature = strdup (methods[j].signature);
+         dottedMethod.fnPtr = methods[j].fnPtr;
+         char *c = dottedMethod.signature;
+         while (*c)
            {
-             if (! (self->accflags
-                    & java::lang::reflect::Modifier::NATIVE))
+             if (*c == '/')
+               *c = '.';
+             c++;
+           }
+
+         if (! strcmp (self->name->chars (), dottedMethod.name)
+             && ! strcmp (self->signature->chars (), dottedMethod.signature))
+           {
+             if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
                break;
 
              // Found a match that is native.
              found = true;
-             nathash_add (&methods[j]);
+             nathash_add (&dottedMethod);
 
              break;
            }
@@ -1897,7 +1940,7 @@ static jint
          jstring m = JvNewStringUTF (methods[j].name);
          try
            {
-             env->ex =new java::lang::NoSuchMethodError (m);
+             env->ex = new java::lang::NoSuchMethodError (m);
            }
          catch (jthrowable t)
            {
@@ -1910,8 +1953,8 @@ static jint
   return JNI_OK;
 }
 
-static jint
-(JNICALL _Jv_JNI_UnregisterNatives) (JNIEnv *, jclass)
+static jint JNICALL
+_Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
 {
   // FIXME -- we could implement this.
   return JNI_ERR;
@@ -1982,8 +2025,8 @@ mangled_name (jclass klass, _Jv_Utf8Const *func_name,
   // Don't use add_char because we need a literal `_'.
   buf[here++] = '_';
 
-  const unsigned char *fn = (const unsigned char *) func_name->data;
-  const unsigned char *limit = fn + func_name->length;
+  const unsigned char *fn = (const unsigned char *) func_name->chars ();
+  const unsigned char *limit = fn + func_name->len ();
   for (int i = 0; ; ++i)
     {
       int ch = UTF8_GET (fn, limit);
@@ -1997,8 +2040,8 @@ mangled_name (jclass klass, _Jv_Utf8Const *func_name,
   buf[here++] = '_';
   buf[here++] = '_';
 
-  const unsigned char *sig = (const unsigned char *) signature->data;
-  limit = sig + signature->length;
+  const unsigned char *sig = (const unsigned char *) signature->chars ();
+  limit = sig + signature->len ();
   JvAssert (sig[0] == '(');
   ++sig;
   while (1)
@@ -2019,7 +2062,7 @@ extern "C" JNIEnv *
 _Jv_GetJNIEnvNewFrame (jclass klass)
 {
   JNIEnv *env = _Jv_GetCurrentJNIEnv ();
-  if (env == NULL)
+  if (__builtin_expect (env == NULL, false))
     {
       env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
       env->p = &_Jv_JNIFunctions;
@@ -2027,35 +2070,80 @@ _Jv_GetJNIEnvNewFrame (jclass klass)
       env->locals = NULL;
       // We set env->ex below.
 
+      // Set up the bottom, reusable frame.
+      env->bottom_locals = (_Jv_JNI_LocalFrame *) 
+       _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
+                            + (FRAME_SIZE
+                               * sizeof (jobject)));
+      
+      env->bottom_locals->marker = MARK_SYSTEM;
+      env->bottom_locals->size = FRAME_SIZE;
+      env->bottom_locals->next = NULL;
+      env->bottom_locals->allocated_p = 0;
+      memset (&env->bottom_locals->vec[0], 0, 
+             env->bottom_locals->size * sizeof (jobject));
+
       _Jv_SetCurrentJNIEnv (env);
     }
 
-  _Jv_JNI_LocalFrame *frame
-    = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
-                                                 + (FRAME_SIZE
-                                                    * sizeof (jobject)));
+  // If we're in a simple JNI call (non-nested), we can just reuse the
+  // locals frame we allocated many calls ago, back when the env was first
+  // built, above.
 
-  frame->marker = MARK_SYSTEM;
-  frame->size = FRAME_SIZE;
-  frame->next = env->locals;
+  if (__builtin_expect (env->locals == NULL, true))
+    env->locals = env->bottom_locals;
 
-  for (int i = 0; i < frame->size; ++i)
-    frame->vec[i] = NULL;
+  else
+    {
+      // Alternatively, we might be re-entering JNI, in which case we can't
+      // reuse the bottom_locals frame, because it is already underneath
+      // us. So we need to make a new one.
+
+      _Jv_JNI_LocalFrame *frame
+       = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
+                                                     + (FRAME_SIZE
+                                                        * sizeof (jobject)));
+      
+      frame->marker = MARK_SYSTEM;
+      frame->size = FRAME_SIZE;
+      frame->allocated_p = 0;
+      frame->next = env->locals;
+
+      memset (&frame->vec[0], 0, 
+             frame->size * sizeof (jobject));
+
+      env->locals = frame;
+    }
 
-  env->locals = frame;
   env->ex = NULL;
 
   return env;
 }
 
+// Destroy the env's reusable resources. This is called from the thread
+// destructor "finalize_native" in natThread.cc
+void 
+_Jv_FreeJNIEnv (_Jv_JNIEnv *env)
+{
+  if (env == NULL)
+    return;
+
+  if (env->bottom_locals != NULL)
+    _Jv_Free (env->bottom_locals);
+
+  _Jv_Free (env);
+}
+
 // Return the function which implements a particular JNI method.  If
 // we can't find the function, we throw the appropriate exception.
 // This is `extern "C"' because the compiler uses it.
 extern "C" void *
 _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
-                    _Jv_Utf8Const *signature, int args_size)
+                    _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
 {
-  char buf[10 + 6 * (name->length + signature->length) + 12];
+  int name_length = name->len();
+  int sig_length = signature->len();
+  char buf[10 + 6 * (name_length + sig_length) + 12];
   int long_start;
   void *function;
 
@@ -2063,13 +2151,13 @@ _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
   JvSynchronize sync (global_ref_table);
 
   // First see if we have an override in the hash table.
-  strncpy (buf, name->data, name->length);
-  buf[name->length] = '\0';
-  strncpy (buf + name->length + 1, signature->data, signature->length);
-  buf[name->length + signature->length + 1] = '\0';
+  strncpy (buf, name->chars (), name_length);
+  buf[name_length] = '\0';
+  strncpy (buf + name_length + 1, signature->chars (), sig_length);
+  buf[name_length + sig_length + 1] = '\0';
   JNINativeMethod meth;
   meth.name = buf;
-  meth.signature = buf + name->length + 1;
+  meth.signature = buf + name_length + 1;
   function = nathash_find (&meth);
   if (function != NULL)
     return function;
@@ -2114,9 +2202,7 @@ _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
           function = _Jv_FindSymbolInExecutable (buf + 1);
         }
     }
-#else /* WIN32 */
-  args_size;  /* Dummy statement to avoid unused parameter warning */
-#endif /* ! WIN32 */
+#endif /* WIN32 */
 
   if (function == NULL)
     {
@@ -2138,7 +2224,7 @@ _Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
           if (function == NULL)
 #endif /* WIN32 */
             {
-              jstring str = JvNewStringUTF (name->data);
+              jstring str = JvNewStringUTF (name->chars ());
               throw new java::lang::UnsatisfiedLinkError (str);
             }
        }
@@ -2211,8 +2297,13 @@ _Jv_JNIMethod::call (ffi_cif *, void *ret, ffi_raw *args, void *__this)
   memcpy (&real_args[offset], args, _this->args_raw_size);
 
   // The actual call to the JNI function.
+#if FFI_NATIVE_RAW_API
   ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
                ret, real_args);
+#else
+  ffi_java_raw_call (&_this->jni_cif, (void (*)()) _this->function,
+                    ret, real_args);
+#endif
 
   if (sync != NULL)
     _Jv_MonitorExit (sync);
@@ -2258,16 +2349,18 @@ _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
   env->p = &_Jv_JNIFunctions;
   env->ex = NULL;
   env->klass = NULL;
-  env->locals
+  env->bottom_locals
     = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
                                                  + (FRAME_SIZE
                                                     * sizeof (jobject)));
+  env->locals = env->bottom_locals;
   if (env->locals == NULL)
     {
       _Jv_Free (env);
       return JNI_ERR;
     }
 
+  env->locals->allocated_p = 0;
   env->locals->marker = MARK_SYSTEM;
   env->locals->size = FRAME_SIZE;
   env->locals->next = NULL;
@@ -2299,21 +2392,21 @@ _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
 }
 
 // This is the one actually used by JNI.
-static jint
-(JNICALL _Jv_JNI_AttachCurrentThread) (JavaVM *vm, void **penv, void *args)
+static jint JNICALL
+_Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
 {
   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
 }
 
-static jint
-(JNICALL _Jv_JNI_AttachCurrentThreadAsDaemon) (JavaVM *vm, void **penv, 
-                                               void *args)
+static jint JNICALL
+_Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv, 
+                                    void *args)
 {
   return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
 }
 
-static jint
-(JNICALL _Jv_JNI_DestroyJavaVM) (JavaVM *vm)
+static jint JNICALL
+_Jv_JNI_DestroyJavaVM (JavaVM *vm)
 {
   JvAssert (the_vm && vm == the_vm);
 
@@ -2346,15 +2439,15 @@ static jint
   return JNI_ERR;
 }
 
-jint
-(JNICALL _Jv_JNI_DetachCurrentThread) (JavaVM *)
+jint JNICALL
+_Jv_JNI_DetachCurrentThread (JavaVM *)
 {
   jint code = _Jv_DetachCurrentThread ();
   return code  ? JNI_EDETACHED : 0;
 }
 
-static jint
-(JNICALL _Jv_JNI_GetEnv) (JavaVM *, void **penv, jint version)
+static jint JNICALL
+_Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
 {
   if (_Jv_ThreadCurrent () == NULL)
     {
@@ -2383,7 +2476,7 @@ static jint
   return 0;
 }
 
-JNIEXPORT jint JNICALL
+jint JNICALL
 JNI_GetDefaultJavaVMInitArgs (void *args)
 {
   jint version = * (jint *) args;
@@ -2400,7 +2493,7 @@ JNI_GetDefaultJavaVMInitArgs (void *args)
   return 0;
 }
 
-JNIEXPORT jint JNICALL
+jint JNICALL
 JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
 {
   JvAssert (! the_vm);
@@ -2465,7 +2558,7 @@ JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
   return 0;
 }
 
-JNIEXPORT jint JNICALL
+jint JNICALL
 JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
 {
   if (buf_len <= 0)
@@ -2505,8 +2598,8 @@ _Jv_GetJavaVM ()
   return the_vm;
 }
 
-static jint
-(JNICALL _Jv_JNI_GetJavaVM) (JNIEnv *, JavaVM **vm)
+static jint JNICALL
+_Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
 {
   *vm = _Jv_GetJavaVM ();
   return *vm == NULL ? JNI_ERR : JNI_OK;