using C++ EH personality for Java
Per Bothner
per@bothner.com
Mon Sep 9 13:59:00 GMT 2002
I've been looking at how to better merge the C++ and Java
exception handling. I'd like to merge the Java and C++
"personality" routines. The most obvious advantage is
we'd remove the confusing restriction in CNI that prevents
throwing or catching both Java and C++ exceptions in the
same compilation unit. More indirectly we can also
remove some of the pecularities that prevent Java references
from being full C++ values, adding some RTTI support.
My suggestion would be to drop the Java personality function,
instead using the C++ one. The latter uses C++ typeinfo
objects to determine of the type of the thrown object matches
the type of the handler, therefore we need to implement some
typeinfo support for Java too. An appealing option would be
to use the C++ support for classes with single inheritance.
However, C++ exception type matching is based on the
compile-time type of the thrown value being a sub-type of
the type specified in the handler. For Java, we need to match
the run-time class of the Throwable value against the handler
type. One way to to do that is for _Jv_Throw is to extract
the typeinfo descriptor from the Throwable object. This works,
assuming we can easily map from objects to typeinfo descriptors.
My current preference is to instead create new typeinfo
sub-classes that use _Jv_IsInstanceOf, as shown below.
This approach appears simple, and doesn't requre changing any
of the Java meta-class data, though we have the option of
doing so, if we want to support C++ rtti.
class __java_pointer_type_info : public std::type_info
{
public:
jclass clas;
explicit __java_pointer_type_info (const char *__n, jclass cls)
: std::type_info (__n), clas(cls)
{ }
protected:
virtual bool __is_pointer_p () const;
virtual bool __do_catch (const std::type_info *__thr_type,
void **__thr_obj,
unsigned __outer) const;
};
bool __java_pointer_type_info::
__is_pointer_p () const
{
return true;
}
bool __java_pointer_type_info::__do_catch
(const type_info *thr_type, void **thr_obj, unsigned /* outer*/) const
{
if (*this == *thr_type)
return true; // same type
if (typeid (*this) != typeid (*thr_type))
return false; // not both same kind of pointers
return _Jv_IsInstanceOf ((jobject) *thr_obj, clas);
}
One possible downside of using the C++ personality routine is
that the gcj command would have to link in some C++ routines
in libsup that I don't believe we currently link in. Would it
be worth it?
--
--Per Bothner
per@bothner.com http://www.bothner.com/per/
More information about the Java
mailing list