Support for throwing C# exceptions from C/C++
Fix for pinvoke methods calling convention on Windows (__stdcall). The default calling conventions in the C# 'extern' method now matches the C function calling convention declaration. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5017 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
cae5ca7a36
commit
4a8e40a3ae
1 changed files with 97 additions and 41 deletions
|
|
@ -9,61 +9,117 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#ifdef WIN32
|
||||
#if defined(_WIN32) || defined(__CYGWIN32__)
|
||||
# define DllExport __declspec( dllexport )
|
||||
# define SWIGSTDCALL __stdcall
|
||||
#else
|
||||
# define DllExport
|
||||
# define SWIGSTDCALL
|
||||
#endif
|
||||
%}
|
||||
|
||||
#if 0
|
||||
%insert(runtime) %{
|
||||
/* Support for throwing CSharp exceptions */
|
||||
/* Support for throwing C# exceptions from C/C++ */
|
||||
typedef enum {
|
||||
SWIG_JavaOutOfMemoryError = 1,
|
||||
SWIG_JavaIOException,
|
||||
SWIG_JavaRuntimeException,
|
||||
SWIG_JavaIndexOutOfBoundsException,
|
||||
SWIG_JavaArithmeticException,
|
||||
SWIG_JavaIllegalArgumentException,
|
||||
SWIG_JavaNullPointerException,
|
||||
SWIG_JavaUnknownError
|
||||
} SWIG_JavaExceptionCodes;
|
||||
SWIG_CSharpException,
|
||||
SWIG_CSharpOutOfMemoryException,
|
||||
SWIG_CSharpIndexOutOfRangeException,
|
||||
SWIG_CSharpDivideByZeroException,
|
||||
SWIG_CSharpArgumentOutOfRangeException,
|
||||
SWIG_CSharpNullReferenceException
|
||||
} SWIG_CSharpExceptionCodes;
|
||||
|
||||
typedef void (SWIGSTDCALL* SWIG_CSharpExceptionCallback_t)(const char *);
|
||||
|
||||
typedef struct {
|
||||
SWIG_JavaExceptionCodes code;
|
||||
const char *java_exception;
|
||||
} SWIG_JavaExceptions_t;
|
||||
SWIG_CSharpExceptionCodes code;
|
||||
SWIG_CSharpExceptionCallback_t callback;
|
||||
} SWIG_CSharpExceptions_t;
|
||||
|
||||
static SWIG_CSharpExceptions_t csharp_exceptions[] = {
|
||||
{ SWIG_CSharpException, NULL },
|
||||
{ SWIG_CSharpOutOfMemoryException, NULL },
|
||||
{ SWIG_CSharpIndexOutOfRangeException, NULL },
|
||||
{ SWIG_CSharpDivideByZeroException, NULL },
|
||||
{ SWIG_CSharpArgumentOutOfRangeException, NULL },
|
||||
{ SWIG_CSharpNullReferenceException, NULL } };
|
||||
|
||||
#if defined(SWIG_NOINCLUDE)
|
||||
void SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg);
|
||||
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg);
|
||||
#else
|
||||
%}
|
||||
%insert(runtime) {
|
||||
void SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) {
|
||||
jclass excep;
|
||||
static const SWIG_JavaExceptions_t java_exceptions[] = {
|
||||
{ SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" },
|
||||
{ SWIG_JavaIOException, "java/io/IOException" },
|
||||
{ SWIG_JavaRuntimeException, "java/lang/RuntimeException" },
|
||||
{ SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" },
|
||||
{ SWIG_JavaArithmeticException, "java/lang/ArithmeticException" },
|
||||
{ SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" },
|
||||
{ SWIG_JavaNullPointerException, "java/lang/NullPointerException" },
|
||||
{ SWIG_JavaUnknownError, "java/lang/UnknownError" },
|
||||
{ (SWIG_JavaExceptionCodes)0, "java/lang/UnknownError" } };
|
||||
const SWIG_JavaExceptions_t *except_ptr = java_exceptions;
|
||||
|
||||
while (except_ptr->code != code && except_ptr->code)
|
||||
except_ptr++;
|
||||
|
||||
JCALL0(ExceptionClear, jenv);
|
||||
excep = JCALL1(FindClass, jenv, except_ptr->java_exception);
|
||||
if (excep)
|
||||
JCALL2(ThrowNew, jenv, excep, msg);
|
||||
void SWIG_CSharpThrowException(SWIG_CSharpExceptionCodes code, const char *msg) {
|
||||
SWIG_CSharpExceptionCallback_t callback = csharp_exceptions[SWIG_CSharpException].callback;
|
||||
if (code >=0 && code < sizeof(csharp_exceptions)/sizeof(SWIG_CSharpExceptionCodes)) {
|
||||
callback = csharp_exceptions[code].callback;
|
||||
}
|
||||
callback(msg);
|
||||
}
|
||||
}
|
||||
%insert(runtime) %{
|
||||
#endif
|
||||
%}
|
||||
|
||||
%{
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
#endif
|
||||
DllExport void SWIGSTDCALL SWIGRegisterExceptionCallbacks(SWIG_CSharpExceptionCallback_t systemException,
|
||||
SWIG_CSharpExceptionCallback_t outOfMemory,
|
||||
SWIG_CSharpExceptionCallback_t indexOutOfRange,
|
||||
SWIG_CSharpExceptionCallback_t divideByZero,
|
||||
SWIG_CSharpExceptionCallback_t argumentOutOfRange,
|
||||
SWIG_CSharpExceptionCallback_t nullReference) {
|
||||
csharp_exceptions[SWIG_CSharpException].callback = systemException;
|
||||
csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemory;
|
||||
csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRange;
|
||||
csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZero;
|
||||
csharp_exceptions[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRange;
|
||||
csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReference;
|
||||
}
|
||||
%}
|
||||
|
||||
%pragma(csharp) imclasscode=%{
|
||||
public delegate void SWIGExceptionDelegate(string message);
|
||||
|
||||
static SWIGExceptionDelegate systemDelegate = new SWIGExceptionDelegate(ThrowSystemException);
|
||||
static SWIGExceptionDelegate outOfMemoryDelegate = new SWIGExceptionDelegate(ThrowOutOfMemoryException);
|
||||
static SWIGExceptionDelegate indexOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowIndexOutOfRangeException);
|
||||
static SWIGExceptionDelegate divideByZeroDelegate = new SWIGExceptionDelegate(ThrowDivideByZeroException);
|
||||
static SWIGExceptionDelegate argumentOutOfRangeDelegate = new SWIGExceptionDelegate(ThrowArgumentOutOfRangeException);
|
||||
static SWIGExceptionDelegate nullReferenceDelegate = new SWIGExceptionDelegate(ThrowNullReferenceException);
|
||||
|
||||
[DllImport("$module", EntryPoint="SWIGRegisterExceptionCallbacks")]
|
||||
public static extern void SWIGRegisterExceptionCallbacks(
|
||||
SWIGExceptionDelegate systemExceptionDelegate,
|
||||
SWIGExceptionDelegate outOfMemoryDelegate,
|
||||
SWIGExceptionDelegate indexOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate divideByZeroDelegate,
|
||||
SWIGExceptionDelegate argumentOutOfRangeDelegate,
|
||||
SWIGExceptionDelegate nullReferenceDelegate);
|
||||
|
||||
static void ThrowSystemException(string message) {
|
||||
throw new System.SystemException(message);
|
||||
}
|
||||
|
||||
static void ThrowOutOfMemoryException(string message) {
|
||||
throw new System.OutOfMemoryException(message);
|
||||
}
|
||||
|
||||
static void ThrowIndexOutOfRangeException(string message) {
|
||||
throw new System.IndexOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowDivideByZeroException(string message) {
|
||||
throw new System.DivideByZeroException(message);
|
||||
}
|
||||
|
||||
static void ThrowArgumentOutOfRangeException(string message) {
|
||||
throw new System.ArgumentOutOfRangeException(message);
|
||||
}
|
||||
|
||||
static void ThrowNullReferenceException(string message) {
|
||||
throw new System.NullReferenceException(message);
|
||||
}
|
||||
|
||||
static $modulePINVOKE() {
|
||||
SWIGRegisterExceptionCallbacks(systemDelegate, outOfMemoryDelegate, indexOutOfRangeDelegate, divideByZeroDelegate, argumentOutOfRangeDelegate, nullReferenceDelegate);
|
||||
}
|
||||
%}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue