Updated Ruby Exception handling. Classes that are specified in throws clauses, or are marked as %exceptionclass, are now inherited from rb_eRuntimeError. This allows instances of these classes to be returned to Ruby as exceptions. Thus if a C++ method throws an instance of MyException, the calling Ruby method will get back a MyException object. To see an example, look at ruby/examples/exception_class.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8353 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Charlie Savage 2006-01-10 19:19:19 +00:00
commit ee63d5276c
3 changed files with 36 additions and 2 deletions

View file

@ -35,6 +35,15 @@ VALUE getObjectPreviouslyDeletedError() {
return rb_eObjectPreviouslyDeleted;
}
VALUE getExceptionClass() {
static int init = 0;
static VALUE rubyExceptionClass ;
if (!init) {
init = 1;
rubyExceptionClass = rb_const_get(_mSWIG, rb_intern("Exception"));
}
return rubyExceptionClass;
}
SWIGINTERN VALUE
SWIG_Ruby_ErrorType(int SWIG_code) {
@ -88,4 +97,19 @@ SWIG_Ruby_ErrorType(int SWIG_code) {
return type;
}
/* This code checks to see if the Ruby object being raised as part
of an exception inherits from the Ruby class Exception. If so,
the object is simply returned. If not, then a new Ruby exception
object is created and that will be returned to Ruby.*/
SWIGINTERN VALUE
SWIG_Ruby_ExceptionType(swig_type_info *desc, VALUE obj) {
VALUE exceptionClass = getExceptionClass();
if (rb_obj_is_kind_of(obj, exceptionClass)) {
return obj;
} else {
return rb_exc_new3(rb_eRuntimeError, rb_obj_as_string(obj));
}
}
%}

View file

@ -42,7 +42,11 @@
/* Simple overload of the output/constant/exception handling */
#define SWIG_AppendOutput(result,obj) SWIG_Ruby_AppendOutput(result, obj)
#define SWIG_SetConstant(name, obj) rb_define_const($module, name, obj)
#define SWIG_Raise(obj, type, desc) rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_obj_as_string(obj)))
/* raise */
%define SWIG_Raise(obj, type, desc)
VALUE exc = SWIG_Ruby_ExceptionType(desc, obj);
rb_exc_raise(exc)%enddef
/* Include the unified typemap library */
%include <typemaps/swigtypemaps.swg>