diff --git a/CHANGES.current b/CHANGES.current index 6fe039d30..3731b7561 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-09-17: wsfulton + [Javascript, Octave, R] Improve exceptions for %catches and exception + specifications for native types. String exception messages are shown as + the exception message instead of just the type of the exception. + 2022-09-17: wsfulton Add missing typecheck typemaps for std::auto_ptr and std::unique_ptr to fix overloading when using these types. diff --git a/Examples/javascript/exception/runme.js b/Examples/javascript/exception/runme.js index 1001a7111..8c583f81f 100644 --- a/Examples/javascript/exception/runme.js +++ b/Examples/javascript/exception/runme.js @@ -10,7 +10,7 @@ try{ if(error == -1) { console.log("t.unknown() didn't throw"); } else { - console.log("successfully caught throw in Test::unknown()."); + console.log("successfully caught throw in Test::unknown() :" + error); } } @@ -22,7 +22,7 @@ catch(error){ if(error == -1) { console.log("t.simple() did not throw"); } else { - console.log("successfully caught throw in Test::simple()."); + console.log("successfully caught throw in Test::simple() :" + error); } } @@ -33,7 +33,7 @@ try{ if(error == -1) { console.log("t.message() did not throw"); } else { - console.log("successfully caught throw in Test::message()."); + console.log("successfully caught throw in Test::message() :" + error); } } @@ -45,7 +45,7 @@ catch(error){ if(error == -1) { console.log("t.hosed() did not throw"); } else { - console.log("successfully caught throw in Test::hosed()."); + console.log("successfully caught throw in Test::hosed() :" + error + " " + error.code + " " + error.msg); } } @@ -58,7 +58,7 @@ for (var i=1; i<4; i++) { if(error == -1) { console.log("t.multi(" + i + ") did not throw"); } else { - console.log("successfully caught throw in Test::multi()."); + console.log("successfully caught throw in Test::multi() :" + error); } } -} +} diff --git a/Lib/javascript/jsc/javascriptrun.swg b/Lib/javascript/jsc/javascriptrun.swg index 2ffb533f4..4d5a9355b 100644 --- a/Lib/javascript/jsc/javascriptrun.swg +++ b/Lib/javascript/jsc/javascriptrun.swg @@ -7,23 +7,27 @@ #define SWIG_exception(code, msg) do { SWIG_JSC_exception(context, exception, code, msg); SWIG_fail; } while (0) #define SWIG_fail goto fail -SWIGRUNTIME void SWIG_Javascript_Raise(JSContextRef context, JSValueRef *exception, const char* type) { - JSStringRef message = JSStringCreateWithUTF8CString(type); +SWIGRUNTIME void SWIG_Javascript_Raise_ValueRef(JSContextRef context, JSValueRef *exception, JSValueRef valRef) { JSValueRef error_arguments[1]; JSObjectRef exception_object; - JSValueRef exception_value; - exception_value = JSValueMakeString(context, message); /* Converting the result to an object will let JavascriptCore add "sourceURL" (file) and "line" (number) and "message" to the exception, instead of just returning a raw string. This is extremely important for debugging your errors. Using JSObjectMakeError is better than JSValueToObject because the latter only populates "sourceURL" and "line", but not "message" or any others I don't know about. */ - error_arguments[0] = exception_value; + error_arguments[0] = valRef; exception_object = JSObjectMakeError(context, 1, error_arguments, NULL); /* Return the exception_object */ *exception = exception_object; +} + +SWIGRUNTIME void SWIG_Javascript_Raise(JSContextRef context, JSValueRef *exception, const char* msg) { + JSStringRef message = JSStringCreateWithUTF8CString(msg); + JSValueRef exception_value = JSValueMakeString(context, message); + + SWIG_Javascript_Raise_ValueRef(context, exception, exception_value); JSStringRelease(message); } diff --git a/Lib/javascript/jsc/javascripttypemaps.swg b/Lib/javascript/jsc/javascripttypemaps.swg index e8fbbeca8..fd8e7aa2f 100644 --- a/Lib/javascript/jsc/javascripttypemaps.swg +++ b/Lib/javascript/jsc/javascripttypemaps.swg @@ -41,7 +41,7 @@ #define SWIG_SetConstant(name, obj) /* raise */ -#define SWIG_Raise(obj, type, desc) SWIG_Javascript_Raise(context, exception, type) +#define SWIG_Raise(obj, type, desc) SWIG_Javascript_Raise_ValueRef(context, exception, obj) %insert("runtime") %{ #define SWIG_JSC_FROM_DECL_ARGS(arg1) (JSContextRef context, arg1) diff --git a/Lib/javascript/v8/javascriptrun.swg b/Lib/javascript/v8/javascriptrun.swg index 6d3d4e82e..a6ccd1e20 100644 --- a/Lib/javascript/v8/javascriptrun.swg +++ b/Lib/javascript/v8/javascriptrun.swg @@ -96,6 +96,11 @@ SWIGINTERN void SWIG_V8_Raise(const char *msg) { SWIGV8_THROW_EXCEPTION(v8::Exception::Error(SWIGV8_STRING_NEW(msg))); } +SWIGINTERN void SWIG_V8_Raise(SWIGV8_VALUE obj, const char *msg) { + SWIGV8_THROW_EXCEPTION(obj); +} + + /* Note: There are two contexts for handling errors. A static V8ErrorHandler is used in not overloaded methods. diff --git a/Lib/javascript/v8/javascripttypemaps.swg b/Lib/javascript/v8/javascripttypemaps.swg index cb31100c5..c4d341be5 100644 --- a/Lib/javascript/v8/javascripttypemaps.swg +++ b/Lib/javascript/v8/javascripttypemaps.swg @@ -37,7 +37,7 @@ #define SWIG_SetConstant(name, obj) /* raise */ -#define SWIG_Raise(obj, type, desc) SWIG_V8_Raise(type) +#define SWIG_Raise(obj, type, desc) SWIG_V8_Raise(obj, type) /* Include the unified typemap library */ %include diff --git a/Lib/octave/octtypemaps.swg b/Lib/octave/octtypemaps.swg index 4acf8e076..8962d1b71 100644 --- a/Lib/octave/octtypemaps.swg +++ b/Lib/octave/octtypemaps.swg @@ -32,7 +32,7 @@ #define SWIG_SetConstant(name, obj) SWIG_Octave_SetConstant(module_ns,name,obj) // raise -#define SWIG_Octave_Raise(OBJ, TYPE, DESC) error("C++ side threw an exception of type " TYPE) +#define SWIG_Octave_Raise(OBJ, TYPE, DESC) error(OBJ.is_string() ? OBJ.string_value().c_str() : "C++ side threw an exception of type " TYPE) #define SWIG_Raise(obj, type, desc) SWIG_Octave_Raise(obj, type, desc) // Include the unified typemap library diff --git a/Lib/r/r.swg b/Lib/r/r.swg index b1962e5ea..eeabcf4ba 100644 --- a/Lib/r/r.swg +++ b/Lib/r/r.swg @@ -26,9 +26,13 @@ SWIGEXPORT void SWIG_init(void) { assign(name, _obj); %end_block %enddef -%define %raise(obj,type,desc) -return R_NilValue; -%enddef +%runtime %{ +void SWIG_R_Raise(SEXP obj, const char *msg) { + Rf_error(Rf_isString(obj) ? STRING_VALUE(obj) : msg); +} +%} + +#define %raise(OBJ, TYPE, DESC) SWIG_R_Raise(OBJ, "C/C++ exception of type " TYPE); return R_NilValue %insert("sinit") "srun.swg"