Removing some memory leak problems when handling exceptions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10713 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
96a52c763c
commit
7e23a5a55e
10 changed files with 336 additions and 39 deletions
|
|
@ -1,3 +1,4 @@
|
|||
# see top-level Makefile.in
|
||||
class
|
||||
simple
|
||||
exception
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ TARGET = example
|
|||
INTERFACE = example.i
|
||||
RUNME = runme.c
|
||||
PROXY = example_proxy.c
|
||||
MEMTOOL = valgrind --leak-check=full
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
|
|
@ -15,7 +16,14 @@ run:
|
|||
TARGET='$(TARGET)' c_compile
|
||||
env LD_LIBRARY_PATH=. ./runme
|
||||
|
||||
memchk:
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' CXXFLAGS='-g' c_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
|
||||
TARGET='$(TARGET)' CFLAGS='-g' c_compile
|
||||
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
|
||||
|
||||
clean:
|
||||
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
|
||||
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~ runme
|
||||
|
||||
check: all
|
||||
|
|
|
|||
29
Examples/c/exception/Makefile
Normal file
29
Examples/c/exception/Makefile
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
RUNME = runme.c
|
||||
PROXY = example_proxy.c
|
||||
MEMTOOL = valgrind --leak-check=full
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' c_cpp
|
||||
|
||||
run:
|
||||
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
|
||||
TARGET='$(TARGET)' c_compile
|
||||
env LD_LIBRARY_PATH=. ./runme
|
||||
|
||||
memchk:
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' CXXFLAGS='-g' c_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
|
||||
TARGET='$(TARGET)' CFLAGS='-g' c_compile
|
||||
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
|
||||
|
||||
clean:
|
||||
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~ runme
|
||||
|
||||
check: all
|
||||
0
Examples/c/exception/example.cxx
Normal file
0
Examples/c/exception/example.cxx
Normal file
52
Examples/c/exception/example.h
Normal file
52
Examples/c/exception/example.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* File : example.h */
|
||||
|
||||
#ifndef SWIG
|
||||
struct A {
|
||||
};
|
||||
#endif
|
||||
|
||||
class Exc {
|
||||
public:
|
||||
Exc(int c, const char *m) {
|
||||
code = c;
|
||||
strncpy(msg,m,256);
|
||||
}
|
||||
int code;
|
||||
char msg[256];
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
class Test {
|
||||
public:
|
||||
int simple() throw(int&) {
|
||||
throw(37);
|
||||
return 1;
|
||||
}
|
||||
int message() throw(const char *) {
|
||||
throw("I died.");
|
||||
return 1;
|
||||
}
|
||||
int hosed() throw(Exc) {
|
||||
throw(Exc(42,"Hosed"));
|
||||
return 1;
|
||||
}
|
||||
int unknown() throw(A*) {
|
||||
static A a;
|
||||
throw &a;
|
||||
return 1;
|
||||
}
|
||||
int multi(int x) throw(int, const char *, Exc) {
|
||||
if (x == 1) throw(37);
|
||||
if (x == 2) throw("Bleah!");
|
||||
if (x == 3) throw(Exc(42,"No-go-diggy-die"));
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
10
Examples/c/exception/example.i
Normal file
10
Examples/c/exception/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
55
Examples/c/exception/runme.c
Normal file
55
Examples/c/exception/runme.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "example_proxy.h"
|
||||
|
||||
int main() {
|
||||
Test *t = new_Test();
|
||||
|
||||
SWIG_try {
|
||||
Test_unknown(t);
|
||||
}
|
||||
SWIG_catch(SWIG_AnyException) {
|
||||
printf("incomplete type: %s\n", SWIG_exception.msg);
|
||||
}
|
||||
SWIG_endtry;
|
||||
|
||||
SWIG_try {
|
||||
Test_simple(t);
|
||||
}
|
||||
SWIG_catch(SWIG_AnyException) {
|
||||
printf("%s\n", SWIG_exception.msg);
|
||||
}
|
||||
SWIG_endtry;
|
||||
|
||||
SWIG_try {
|
||||
Test_message(t);
|
||||
}
|
||||
SWIG_catch(SWIG_AnyException) {
|
||||
printf("%s\n", SWIG_exception.msg);
|
||||
}
|
||||
SWIG_endtry;
|
||||
|
||||
SWIG_try {
|
||||
Test_hosed(t);
|
||||
}
|
||||
SWIG_catch(Exc) {
|
||||
printf("%d %s\n", Exc_code_get(SWIG_exception.klass), Exc_msg_get(SWIG_exception.klass));
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
SWIG_try {
|
||||
Test_multi(t, i);
|
||||
}
|
||||
SWIG_catch(Exc) {
|
||||
printf("%d %s\n", Exc_code_get(SWIG_exception.klass), Exc_msg_get(SWIG_exception.klass));
|
||||
}
|
||||
SWIG_catch(SWIG_AnyException) {
|
||||
printf("%s\n", SWIG_exception.msg);
|
||||
}
|
||||
SWIG_endtry;
|
||||
}
|
||||
|
||||
SWIG_exit(0);
|
||||
}
|
||||
|
||||
|
|
@ -5,6 +5,7 @@ TARGET = example
|
|||
INTERFACE = example.i
|
||||
RUNME = runme.c
|
||||
PROXY = example_proxy.c
|
||||
MEMTOOL = valgrind --leak-check=full
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
|
|
@ -15,7 +16,14 @@ run:
|
|||
TARGET='$(TARGET)' c_compile
|
||||
env LD_LIBRARY_PATH=. ./runme
|
||||
|
||||
memchk:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' CFLAGS='-g' c
|
||||
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
|
||||
TARGET='$(TARGET)' CFLAGS='-g' c_compile
|
||||
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
|
||||
|
||||
clean:
|
||||
rm -f *.o *.so *.out *.a *.exe *.dll *_wrap* *_proxy* *~
|
||||
rm -f *.o *.so *.out *.a *.exe *.dll *_wrap* *_proxy* *~ runme
|
||||
|
||||
check: all
|
||||
|
|
|
|||
132
Lib/c/c.swg
132
Lib/c/c.swg
|
|
@ -18,11 +18,6 @@ typedef struct {
|
|||
void *obj;
|
||||
const char **typenames;
|
||||
} SwigObj;
|
||||
|
||||
SWIGINTERN SwigObj *SWIG_registry_base = 0;
|
||||
SWIGINTERN SwigObj *SWIG_registry = 0;
|
||||
|
||||
#define SWIG_STR(x) #x
|
||||
%}
|
||||
|
||||
// typemaps for function parameters
|
||||
|
|
@ -39,6 +34,7 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
%typemap(ctype) const short, const int, const long, const char, const float, const double "$1_type"
|
||||
%typemap(ctype) const unsigned short, const unsigned int, const unsigned long, const unsigned char "$1_type"
|
||||
%typemap(ctype) const void *, const short *, const int *, const long *, const char *, const float *, const double *, const bool * "$1_type"
|
||||
%typemap(ctype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY], bool [ANY] "$1_basetype *"
|
||||
%typemap(ctype) void * [ANY], short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY], bool * [ANY] "$1_basetype **"
|
||||
%typemap(ctype) SWIGTYPE "SwigObj *"
|
||||
%typemap(ctype) SWIGTYPE * "SwigObj *"
|
||||
|
|
@ -62,7 +58,7 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
%typemap(in) const short &, const int &, const long &, const char &, const float &, const double &, const bool & "$1 = ($1_basetype *) $input;"
|
||||
%typemap(in) unsigned short &, unsigned int &, unsigned long &, unsigned char & "$1 = ($1_basetype *) $input;"
|
||||
%typemap(in) const unsigned short &, const unsigned int &, const unsigned long &, const unsigned char & "$1 = ($1_basetype *) $input;"
|
||||
|
||||
%typemap(in) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY], bool [ANY] "$1 = ($1_basetype *) $input;"
|
||||
%typemap(in) void * [ANY], short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY], bool * [ANY] "$1 = ($1_basetype *) $input;"
|
||||
|
||||
%typemap(in, fragment="stdbool_inc") bool, bool *, bool **, const bool, const bool *, bool * [ANY] "$1 = ($1_type) $input;"
|
||||
|
|
@ -108,6 +104,7 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
%typemap(couttype) const void *, const short *, const int *, const long *, const char *, const float *, const double * "$1_type"
|
||||
%typemap(couttype) short &, int &, long &, char &, float &, double &, bool & "$1_basetype *"
|
||||
%typemap(couttype) const short &, const int &, const long &, const char &, const float &, const double & "$1_basetype const *"
|
||||
%typemap(couttype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY], bool [ANY] "$1_basetype *"
|
||||
%typemap(couttype) SWIGTYPE "SwigObj *"
|
||||
%typemap(couttype) SWIGTYPE * "SwigObj *"
|
||||
%typemap(couttype) SWIGTYPE & "SwigObj *"
|
||||
|
|
@ -126,6 +123,7 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
%typemap(out) unsigned short &, unsigned int &, unsigned long &, unsigned char & "$result = $1;"
|
||||
%typemap(out) const short &, const int &, const long &, const char &, const float &, const double & "$result = $1;"
|
||||
%typemap(out) const unsigned short &, const unsigned int &, const unsigned long &, const unsigned char & "$result = $1;"
|
||||
%typemap(out) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY], bool [ANY] "$result = $1;"
|
||||
%typemap(out) void ""
|
||||
|
||||
%typemap(out, fragment="stdbool_inc") bool, bool *, const bool, const bool * "$result = $1;"
|
||||
|
|
@ -148,17 +146,19 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
|
||||
// exception handling
|
||||
|
||||
%typemap(throws) int, long, short, unsigned int, unsigned long, unsigned short {
|
||||
%typemap(throws) BASIC_INT_TYPES {
|
||||
char error_msg[256];
|
||||
sprintf(error_msg, "C++ $1_type exception thrown, value: %d", $1);
|
||||
SWIG_CThrowException(0, error_msg);
|
||||
}
|
||||
|
||||
%apply BASIC_INT_TYPES { int, long, short, unsigned int, unsigned long, unsigned short, int &, long &, short &, unsigned int &, unsigned long &, unsigned short & };
|
||||
|
||||
%typemap(throws) char *, const char * {
|
||||
SWIG_CThrowException(0, $1);
|
||||
}
|
||||
|
||||
// this should match only non-built-in objects
|
||||
// this should match only SwigObj objects
|
||||
%typemap(throws) SWIGTYPE {
|
||||
SwigObj *c_ex;
|
||||
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
|
||||
|
|
@ -166,7 +166,7 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
SWIG_CThrowException(c_ex, "C++ $1_type exception thrown");
|
||||
}
|
||||
|
||||
%typemap(throws) SWIGTYPE *, SWIGTYPE & {
|
||||
%typemap(throws) SWIGTYPE * {
|
||||
SwigObj *c_ex;
|
||||
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
|
||||
c_ex->obj = (void*) $1;
|
||||
|
|
@ -174,16 +174,27 @@ SWIGINTERN SwigObj *SWIG_registry = 0;
|
|||
}
|
||||
|
||||
%insert("runtime") %{
|
||||
#define SWIG_STR(x) #x
|
||||
#define SWIG_MAX_RT_STACK 256
|
||||
#define SWIG_REGISTRY_INIT 256
|
||||
|
||||
SWIGEXPORTC struct {
|
||||
SWIGINTERN SwigObj **SWIG_registry_base = 0;
|
||||
SWIGINTERN SwigObj **SWIG_registry = 0;
|
||||
SWIGINTERN int SWIG_registry_size = SWIG_REGISTRY_INIT;
|
||||
|
||||
SWIGINTERN SwigObj *SWIG_create_object(const char *classname);
|
||||
SWIGINTERN void SWIG_destroy_object(SwigObj *object);
|
||||
|
||||
SWIGEXPORTC struct SWIG_exception_struct {
|
||||
int code;
|
||||
char *msg;
|
||||
SwigObj *klass;
|
||||
} SWIG_exception;
|
||||
int handled;
|
||||
} SWIG_exception = { 0, 0, 0, 0 };
|
||||
|
||||
SWIGEXPORTC jmp_buf SWIG_rt_env;
|
||||
SWIGEXPORTC int SWIG_rt_init = 0;
|
||||
SWIGINTERN jmp_buf SWIG_cpp_back_env;
|
||||
SWIGINTERN jmp_buf *SWIG_rt_stack_base = 0;
|
||||
SWIGINTERN jmp_buf *SWIG_rt_stack_ptr = 0;
|
||||
|
||||
|
|
@ -200,11 +211,56 @@ SWIGINTERN void SWIG_rt_stack_pop() {
|
|||
memcpy(SWIG_rt_env, SWIG_rt_stack_ptr, sizeof(SWIG_rt_env));
|
||||
}
|
||||
|
||||
SWIGINTERN void SWIG_add_registry_entry(SwigObj *entry) {
|
||||
if (SWIG_registry_base == 0) {
|
||||
SWIG_registry_base = SWIG_registry = (SwigObj **) malloc(SWIG_registry_size * sizeof(SwigObj *));
|
||||
memset(SWIG_registry_base, 0, SWIG_registry_size * sizeof(SwigObj *));
|
||||
}
|
||||
*SWIG_registry = entry;
|
||||
SWIG_registry++;
|
||||
if ((SWIG_registry - SWIG_registry_base) == SWIG_registry_size) {
|
||||
SWIG_registry = SWIG_registry_base;
|
||||
SWIG_registry_size += SWIG_REGISTRY_INIT;
|
||||
int new_size = SWIG_registry_size * sizeof(SwigObj *);
|
||||
SWIG_registry_base = (SwigObj **) malloc(new_size);
|
||||
memset(SWIG_registry_base, 0, new_size);
|
||||
memcpy(SWIG_registry_base, SWIG_registry, (SWIG_registry_size - SWIG_REGISTRY_INIT) * sizeof(SwigObj *));
|
||||
free(SWIG_registry);
|
||||
SWIG_registry = SWIG_registry_base + (SWIG_registry_size - SWIG_REGISTRY_INIT);
|
||||
}
|
||||
}
|
||||
|
||||
SWIGINTERN void SWIG_remove_registry_entry(SwigObj *entry) {
|
||||
int i;
|
||||
for (i = 0; i < SWIG_registry_size; ++i) {
|
||||
if (*(SWIG_registry_base + i) == entry) {
|
||||
*(SWIG_registry_base + i) = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SWIGINTERN void SWIG_cleanup() {
|
||||
if (SWIG_rt_stack_base)
|
||||
free(SWIG_rt_stack_base);
|
||||
if (SWIG_exception.msg)
|
||||
free(SWIG_exception.msg);
|
||||
if (SWIG_exception.klass) {
|
||||
if (SWIG_exception.klass->typenames)
|
||||
free(SWIG_exception.klass->typenames);
|
||||
free(SWIG_exception.klass);
|
||||
}
|
||||
int i;
|
||||
if (SWIG_registry_base) {
|
||||
for (i = 0; i < SWIG_registry_size; ++i) {
|
||||
if (*(SWIG_registry_base + i)) {
|
||||
SWIG_destroy_object(*(SWIG_registry_base + i));
|
||||
*(SWIG_registry_base + i) = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(SWIG_registry_base);
|
||||
SWIG_registry_base = 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
@ -229,8 +285,10 @@ SWIGEXPORTC int SWIG_rt_catch(const char *type) {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (result)
|
||||
if (result) {
|
||||
SWIG_rt_stack_pop();
|
||||
SWIG_exception.handled = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -240,20 +298,35 @@ SWIGEXPORTC void SWIG_rt_throw(SwigObj *klass, const char *msg) {
|
|||
SWIG_exception.msg = (char *) 0;
|
||||
}
|
||||
if (msg) {
|
||||
SWIG_exception.msg = (char *) malloc(strlen(msg));
|
||||
SWIG_exception.msg = (char *) malloc(strlen(msg) + 1);
|
||||
strcpy(SWIG_exception.msg, msg);
|
||||
}
|
||||
SWIG_exception.klass = klass;
|
||||
SWIG_exception.handled = 0;
|
||||
longjmp(SWIG_rt_env, 1);
|
||||
}
|
||||
|
||||
SWIGEXPORTC void SWIG_rt_endtry() {
|
||||
if (SWIG_exception.msg)
|
||||
SWIGEXPORTC void SWIG_rt_unhandled() {
|
||||
if (SWIG_exception.msg) {
|
||||
free(SWIG_exception.msg);
|
||||
SWIG_exception.msg = 0;
|
||||
}
|
||||
SWIG_rt_stack_pop();
|
||||
longjmp(SWIG_rt_env, SWIG_exception.code);
|
||||
}
|
||||
|
||||
SWIGEXPORTC void SWIG_rt_endtry() {
|
||||
if (SWIG_exception.handled) {
|
||||
if (setjmp(SWIG_rt_env) == 0) {
|
||||
SWIG_rt_stack_push();
|
||||
longjmp(SWIG_cpp_back_env, 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
SWIG_rt_stack_pop(); // pop the SWIG_try context
|
||||
}
|
||||
}
|
||||
|
||||
SWIGEXPORTC int SWIG_exit(int code) {
|
||||
SWIG_cleanup();
|
||||
exit(code);
|
||||
|
|
@ -264,10 +337,9 @@ SWIGEXPORTC int SWIG_exit(int code) {
|
|||
#endif
|
||||
|
||||
SWIGINTERN void SWIG_terminate() {
|
||||
fprintf(stderr, "Unhandled exception: %s\n%s (error code: %d)\nExitting...\n",
|
||||
fprintf(stderr, "Unhandled exception: %s\n%s\nExitting...\n",
|
||||
SWIG_exception.klass->typenames[0],
|
||||
SWIG_exception.msg,
|
||||
SWIG_exception.code);
|
||||
SWIG_exception.msg ? SWIG_exception.msg : "");
|
||||
SWIG_exit(SWIG_exception.code);
|
||||
}
|
||||
|
||||
|
|
@ -276,17 +348,22 @@ SWIGINTERN void SWIG_Runtime_init() {
|
|||
if (!SWIG_rt_init) {
|
||||
SWIG_rt_init = 1;
|
||||
SWIG_rt_stack_base = SWIG_rt_stack_ptr = (jmp_buf *) malloc(sizeof(jmp_buf) * SWIG_MAX_RT_STACK);
|
||||
if (SWIG_exception.code = setjmp(SWIG_rt_env))
|
||||
if (SWIG_exception.code = setjmp(SWIG_rt_env)) {
|
||||
// deallocate C++ exception
|
||||
if (setjmp(SWIG_rt_env) == 0) {
|
||||
SWIG_rt_stack_push();
|
||||
SWIG_exception.handled = 1;
|
||||
longjmp(SWIG_cpp_back_env, 1);
|
||||
}
|
||||
SWIG_terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SWIGINTERN void SWIG_CThrowException(void *klass, const char *msg) {
|
||||
if (SWIG_rt_init)
|
||||
#define SWIG_CThrowException(klass, msg) \
|
||||
if (setjmp(SWIG_cpp_back_env) == 0) \
|
||||
SWIG_rt_throw((SwigObj *) klass, msg);
|
||||
else
|
||||
SWIG_terminate();
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%insert("proxy_header") %{
|
||||
|
|
@ -302,7 +379,7 @@ typedef struct {
|
|||
const char **typenames;
|
||||
} SwigObj;
|
||||
|
||||
SWIGIMPORT struct {
|
||||
SWIGIMPORT struct SWIG_exception_struct {
|
||||
int code;
|
||||
char *msg;
|
||||
SwigObj *klass;
|
||||
|
|
@ -311,7 +388,8 @@ SWIGIMPORT struct {
|
|||
SWIGIMPORT void SWIG_rt_try();
|
||||
SWIGIMPORT int SWIG_rt_catch(const char *type);
|
||||
SWIGIMPORT void SWIG_rt_throw(SwigObj *klass, const char * msg);
|
||||
SWIGIMPORT int SWIG_rt_endtry();
|
||||
SWIGIMPORT int SWIG_rt_unhandled();
|
||||
SWIGIMPORT void SWIG_rt_endtry();
|
||||
SWIGIMPORT int SWIG_exit(int code);
|
||||
|
||||
#define SWIG_try \
|
||||
|
|
@ -320,6 +398,6 @@ SWIGIMPORT int SWIG_exit(int code);
|
|||
#define SWIG_catch(type) else if (SWIG_rt_catch(#type))
|
||||
#define SWIG_throw(klass) SWIG_rt_throw((SwigObj *) klass, 0);
|
||||
#define SWIG_throw_msg(klass, msg) SWIG_rt_throw((SwigObj *) klass, msg);
|
||||
#define SWIG_endtry else SWIG_rt_endtry();
|
||||
#define SWIG_endtry else SWIG_rt_unhandled(); SWIG_rt_endtry();
|
||||
%}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ class C:public Language {
|
|||
String *int_string;
|
||||
String *enum_values;
|
||||
String *create_object;
|
||||
String *destroy_object;
|
||||
|
||||
bool proxy_flag;
|
||||
bool runtime_flag;
|
||||
|
|
@ -46,6 +47,7 @@ public:
|
|||
int_string(NewString("int")),
|
||||
enum_values(0),
|
||||
create_object(0),
|
||||
destroy_object(0),
|
||||
proxy_flag(true),
|
||||
runtime_flag(true),
|
||||
typecheck_flag(false) {
|
||||
|
|
@ -111,11 +113,35 @@ public:
|
|||
|
||||
String *finish_create_object() {
|
||||
String *s = create_object;
|
||||
//Printf(s, "SWIG_add_registry_entry(result);\n");
|
||||
Printf(s, "return result;\n");
|
||||
Printf(s, "}\n\n");
|
||||
return create_object;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* start_destroy_object()
|
||||
* --------------------------------------------------------------------- */
|
||||
|
||||
void start_destroy_object() {
|
||||
String *s = destroy_object = NewString("");
|
||||
Printf(s, "\nSWIGINTERN void SWIG_destroy_object(SwigObj *object) {\n");
|
||||
Printf(s, "if (object)\nif (object->typenames) {\n");
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* finish_destroy_object()
|
||||
* --------------------------------------------------------------------- */
|
||||
|
||||
String *finish_destroy_object() {
|
||||
String *s = destroy_object;
|
||||
Printf(s, "free(object->typenames);\n");
|
||||
Printf(s, "free(object);\n");
|
||||
Printf(s, "object = (SwigObj *) 0;\n");
|
||||
Printf(s, "}\n}\n");
|
||||
return destroy_object;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* top()
|
||||
* --------------------------------------------------------------------- */
|
||||
|
|
@ -175,11 +201,13 @@ public:
|
|||
Printf(f_wrappers, "#endif\n\n");
|
||||
|
||||
start_create_object();
|
||||
start_destroy_object();
|
||||
|
||||
// emit code for children
|
||||
Language::top(n);
|
||||
|
||||
Append(f_header, finish_create_object());
|
||||
Append(f_header, finish_destroy_object());
|
||||
|
||||
Printf(f_wrappers, "#ifdef __cplusplus\n");
|
||||
Printf(f_wrappers, "}\n");
|
||||
|
|
@ -468,7 +496,9 @@ ready:
|
|||
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
|
||||
if (SwigType_isconst(type))
|
||||
SwigType_del_qualifier(type);
|
||||
SwigType *return_var_type = SwigType_isreference(type) ? SwigType_add_pointer(SwigType_base(type)) : type;
|
||||
SwigType *return_var_type;
|
||||
return_var_type = SwigType_isreference(type) ? SwigType_add_pointer(SwigType_base(type)) : type;
|
||||
return_var_type = SwigType_isarray(type) ? SwigType_add_pointer(SwigType_base(type)) : type;
|
||||
if (SwigType_isenum(type))
|
||||
Wrapper_add_localv(wrapper, "cppresult", "int", "cppresult", NIL);
|
||||
else
|
||||
|
|
@ -568,6 +598,9 @@ ready:
|
|||
|
||||
// emit action code
|
||||
String *action = emit_action(n);
|
||||
if (Getattr(n, "throws")) {
|
||||
Printf(action, "if (SWIG_exception.handled) {\nSWIG_rt_stack_pop();\nlongjmp(SWIG_rt_env, 1);\n}\n");
|
||||
}
|
||||
Replaceall(action, "$cppresult", "cppresult");
|
||||
|
||||
// handle return-by-reference
|
||||
|
|
@ -954,9 +987,19 @@ ready:
|
|||
|
||||
if (!SwigType_isconst(type)) {
|
||||
// create code for 'set' function
|
||||
code = NewString("");
|
||||
if (SwigType_isenum(Getattr(n, "type")) && Getattr(n, "unnamed"))
|
||||
Printf(code, "* (int *) &");
|
||||
if (SwigType_isarray(Getattr(n, "type"))) {
|
||||
code = NewString("");
|
||||
Printf(code, "if (arg2) {\n");
|
||||
Printf(code, "int i;\n");
|
||||
Printv(code, "for (i = 0; i < ", SwigType_array_getdim(Getattr(n, "type"), 0), "; ++i) {\n", NIL);
|
||||
Printv(code, classname, "::", name, "[i] = arg2[i];\n", NIL);
|
||||
Printf(code, "}\n}\n");
|
||||
}
|
||||
else if (SwigType_isenum(Getattr(n, "type")) && Getattr(n, "unnamed")) {
|
||||
code = NewString("* (int *) &");
|
||||
}
|
||||
else
|
||||
code = NewString("");
|
||||
Printv(code, classname, "::", name, " = arg1;\n", NIL);
|
||||
wrap_set_variable(n, classname, newclassname, name, code);
|
||||
}
|
||||
|
|
@ -1006,7 +1049,15 @@ ready:
|
|||
set_nextSibling(p, t);
|
||||
Setattr(n, "parms", p);
|
||||
String *code = 0;
|
||||
if (SwigType_isenum(Getattr(n, "type")) && Getattr(n, "unnamed")) {
|
||||
if (SwigType_isarray(Getattr(n, "type"))) {
|
||||
code = NewString("");
|
||||
Printf(code, "if (arg2) {\n");
|
||||
Printf(code, "int i;\n");
|
||||
Printv(code, "for (i = 0; i < ", SwigType_array_getdim(Getattr(n, "type"), 0), "; ++i) {\n", NIL);
|
||||
Printv(code, "((", classname, "*) arg1->obj)->", name, "[i] = arg2[i];\n", NIL);
|
||||
Printf(code, "}\n}\n");
|
||||
}
|
||||
else if (SwigType_isenum(Getattr(n, "type")) && Getattr(n, "unnamed")) {
|
||||
code = NewString("");
|
||||
Printv(code, "* (int *) &((", classname, "*) arg1->obj)->", name, " = arg2;\n", NIL);
|
||||
}
|
||||
|
|
@ -1044,6 +1095,12 @@ ready:
|
|||
}
|
||||
Printf(s, "result->typenames[%d] = 0;\n", i);
|
||||
Printf(s, "}\n");
|
||||
|
||||
s = destroy_object;
|
||||
|
||||
Printv(s, "if (strcmp(object->typenames[0], \"", classname, "\") == 0) {\n", NIL);
|
||||
Printv(s, "if (object->obj)\ndelete (", classname, " *) (object->obj);\n", NIL);
|
||||
Printf(s, "}\n");
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
|
|
@ -1087,6 +1144,8 @@ ready:
|
|||
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
|
||||
Printv(code, "result->obj = (void*) new ", classname, arg_lnames, ";\n", NIL);
|
||||
|
||||
Printf(code, "SWIG_add_registry_entry(result);\n");
|
||||
|
||||
Setattr(n, "wrap:action", code);
|
||||
|
||||
functionWrapper(n);
|
||||
|
|
@ -1153,7 +1212,6 @@ ready:
|
|||
* --------------------------------------------------------------------- */
|
||||
|
||||
virtual int destructorHandler(Node *n) {
|
||||
String *classname = Getattr(parentNode(n), "classtype");
|
||||
String *newclassname = Getattr(parentNode(n), "sym:name");
|
||||
String *sobj_name = NewString("");
|
||||
String *ctype;
|
||||
|
|
@ -1183,10 +1241,8 @@ ready:
|
|||
if (typecheck_flag)
|
||||
emit_runtime_typecheck(newclassname, destr_name, code);
|
||||
|
||||
Printv(code, "if (arg1) if (arg1->obj) {\n", NIL);
|
||||
Printv(code, " delete (", classname, "*) (arg1->obj);\n", NIL);
|
||||
Printv(code, " free(arg1->typenames);\n free(arg1);\n", NIL);
|
||||
Printv(code, " arg1 = (", sobj_name, "*)0;\n}\n", NIL);
|
||||
Printf(code, "SWIG_remove_registry_entry(arg1);\n");
|
||||
Printf(code, "SWIG_destroy_object(arg1);\n");
|
||||
Setattr(n, "wrap:action", code);
|
||||
|
||||
functionWrapper(n);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue