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@8353 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
17dbf525da
commit
c2399a7aa2
3 changed files with 36 additions and 2 deletions
|
|
@ -35,6 +35,15 @@ VALUE getObjectPreviouslyDeletedError() {
|
||||||
return rb_eObjectPreviouslyDeleted;
|
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
|
SWIGINTERN VALUE
|
||||||
SWIG_Ruby_ErrorType(int SWIG_code) {
|
SWIG_Ruby_ErrorType(int SWIG_code) {
|
||||||
|
|
@ -88,4 +97,19 @@ SWIG_Ruby_ErrorType(int SWIG_code) {
|
||||||
return type;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
@ -42,7 +42,11 @@
|
||||||
/* Simple overload of the output/constant/exception handling */
|
/* Simple overload of the output/constant/exception handling */
|
||||||
#define SWIG_AppendOutput(result,obj) SWIG_Ruby_AppendOutput(result, obj)
|
#define SWIG_AppendOutput(result,obj) SWIG_Ruby_AppendOutput(result, obj)
|
||||||
#define SWIG_SetConstant(name, obj) rb_define_const($module, name, 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 the unified typemap library */
|
||||||
%include <typemaps/swigtypemaps.swg>
|
%include <typemaps/swigtypemaps.swg>
|
||||||
|
|
|
||||||
|
|
@ -1800,7 +1800,13 @@ public:
|
||||||
Printv(s, tab4, "rb_undef_alloc_func(", klass->vname, ");\n", NIL);
|
Printv(s, tab4, "rb_undef_alloc_func(", klass->vname, ");\n", NIL);
|
||||||
Replaceall(klass->init,"$allocator", s);
|
Replaceall(klass->init,"$allocator", s);
|
||||||
Replaceall(klass->init,"$initializer", "");
|
Replaceall(klass->init,"$initializer", "");
|
||||||
Replaceall(klass->init,"$super", "rb_cObject");
|
|
||||||
|
if (GetFlag(n,"feature:exceptionclass")) {
|
||||||
|
Replaceall(klass->init,"$super", "rb_eRuntimeError");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Replaceall(klass->init,"$super", "rb_cObject");
|
||||||
|
}
|
||||||
Delete(s);
|
Delete(s);
|
||||||
|
|
||||||
Printv(f_init,klass->init,NIL);
|
Printv(f_init,klass->init,NIL);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue