diff options
author | Mark Gossage <mark@gossage.cjb.net> | 2007-05-02 02:20:29 +0000 |
---|---|---|
committer | Mark Gossage <mark@gossage.cjb.net> | 2007-05-02 02:20:29 +0000 |
commit | 61fdde65cca32b69b419a8a4cf8a0faebaf408ee (patch) | |
tree | 6d7da60f40cf463da8f890c35f18dfe938f722fe /Examples/lua | |
parent | cea5ba04fa4fddfd664bb9290ae5ae43b14e7285 (diff) | |
download | swig-61fdde65cca32b69b419a8a4cf8a0faebaf408ee.tar.gz |
Fixed issues with C++ classes and hierachies across multiple source files.
Fixed imports test case & added run test.
Added Examples/imports.
Added typename for raw lua_State*
Added documentation on native functions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9748 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/lua')
-rw-r--r-- | Examples/lua/check.list | 1 | ||||
-rw-r--r-- | Examples/lua/import/Makefile | 19 | ||||
-rw-r--r-- | Examples/lua/import/README | 36 | ||||
-rw-r--r-- | Examples/lua/import/bar.h | 21 | ||||
-rw-r--r-- | Examples/lua/import/bar.i | 9 | ||||
-rw-r--r-- | Examples/lua/import/base.h | 16 | ||||
-rw-r--r-- | Examples/lua/import/base.i | 6 | ||||
-rw-r--r-- | Examples/lua/import/foo.h | 21 | ||||
-rw-r--r-- | Examples/lua/import/foo.i | 8 | ||||
-rw-r--r-- | Examples/lua/import/runme.lua | 103 | ||||
-rw-r--r-- | Examples/lua/import/spam.h | 24 | ||||
-rw-r--r-- | Examples/lua/import/spam.i | 9 |
12 files changed, 273 insertions, 0 deletions
diff --git a/Examples/lua/check.list b/Examples/lua/check.list index 72102d124..b5343ce16 100644 --- a/Examples/lua/check.list +++ b/Examples/lua/check.list @@ -4,6 +4,7 @@ constants funcptr3 functest functor +import pointer simple variables diff --git a/Examples/lua/import/Makefile b/Examples/lua/import/Makefile new file mode 100644 index 000000000..8f692d175 --- /dev/null +++ b/Examples/lua/import/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SWIGOPT = +LIBS = + +all:: + $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \ + LIBS='$(LIBS)' TARGET='base' INTERFACE='base.i' lua_cpp + $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \ + LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' lua_cpp + $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \ + LIBS='$(LIBS)' TARGET='bar' INTERFACE='bar.i' lua_cpp + $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \ + LIBS='$(LIBS)' TARGET='spam' INTERFACE='spam.i' lua_cpp + +clean:: + $(MAKE) -f $(TOP)/Makefile lua_clean + +check: all diff --git a/Examples/lua/import/README b/Examples/lua/import/README new file mode 100644 index 000000000..1a52e3c1e --- /dev/null +++ b/Examples/lua/import/README @@ -0,0 +1,36 @@ +This example tests the %import directive and working with multiple modules.
+
+Use 'lua runme.lua' to run a test.
+
+Overview:
+---------
+
+The example defines 4 different extension modules--each wrapping
+a separate C++ class.
+
+ base.i - Base class
+ foo.i - Foo class derived from Base
+ bar.i - Bar class derived from Base
+ spam.i - Spam class derived from Bar
+
+Each module uses %import to refer to another module. For
+example, the 'foo.i' module uses '%import base.i' to get
+definitions for its base class.
+
+If everything is okay, all of the modules will load properly and
+type checking will work correctly. Caveat: Some compilers, for example
+gcc-3.2.x, generate broken vtables with the inline methods in this test.
+This is not a SWIG problem and can usually be solved with non-inlined
+destructors compiled into separate shared objects/DLLs.
+
+Unix:
+-----
+- Run make
+- Run the test as described above
+
+Windows:
+-------- +Sorry, no files here. +If you know how, you could copy the python or ruby example dsw & dsp and try editing that
+ +
diff --git a/Examples/lua/import/bar.h b/Examples/lua/import/bar.h new file mode 100644 index 000000000..1c99f28e6 --- /dev/null +++ b/Examples/lua/import/bar.h @@ -0,0 +1,21 @@ +#include "base.h" + +class Bar : public Base { + public: + Bar() { } + ~Bar() { } + virtual const char * A() const { + return "Bar::A"; + } + const char * B() const { + return "Bar::B"; + } + virtual Base *toBase() { + return static_cast<Base *>(this); + } + static Bar *fromBase(Base *b) { + return dynamic_cast<Bar *>(b); + } +}; + + diff --git a/Examples/lua/import/bar.i b/Examples/lua/import/bar.i new file mode 100644 index 000000000..5816cbe17 --- /dev/null +++ b/Examples/lua/import/bar.i @@ -0,0 +1,9 @@ +%module bar +%{ +#include "bar.h" +%} + +%import base.i +%include "bar.h" + + diff --git a/Examples/lua/import/base.h b/Examples/lua/import/base.h new file mode 100644 index 000000000..fec0f329c --- /dev/null +++ b/Examples/lua/import/base.h @@ -0,0 +1,16 @@ +class Base { + public: + Base() { }; + virtual ~Base() { }; + virtual const char * A() const { + return "Base::A"; + } + const char * B() const { + return "Base::B"; + } + virtual Base *toBase() { + return static_cast<Base *>(this); + } +}; + + diff --git a/Examples/lua/import/base.i b/Examples/lua/import/base.i new file mode 100644 index 000000000..f6e19efd8 --- /dev/null +++ b/Examples/lua/import/base.i @@ -0,0 +1,6 @@ +%module base +%{ +#include "base.h" +%} + +%include base.h diff --git a/Examples/lua/import/foo.h b/Examples/lua/import/foo.h new file mode 100644 index 000000000..1abe2c0d8 --- /dev/null +++ b/Examples/lua/import/foo.h @@ -0,0 +1,21 @@ +#include "base.h" + +class Foo : public Base { + public: + Foo() { } + ~Foo() { } + virtual const char * A() const { + return "Foo::A"; + } + const char * B() const { + return "Foo::B"; + } + virtual Base *toBase() { + return static_cast<Base *>(this); + } + static Foo *fromBase(Base *b) { + return dynamic_cast<Foo *>(b); + } +}; + + diff --git a/Examples/lua/import/foo.i b/Examples/lua/import/foo.i new file mode 100644 index 000000000..27feb2e6a --- /dev/null +++ b/Examples/lua/import/foo.i @@ -0,0 +1,8 @@ +%module foo +%{ +#include "foo.h" +%} + +%import base.i +%include "foo.h" + diff --git a/Examples/lua/import/runme.lua b/Examples/lua/import/runme.lua new file mode 100644 index 000000000..2409d436b --- /dev/null +++ b/Examples/lua/import/runme.lua @@ -0,0 +1,103 @@ +# Test various properties of classes defined in separate modules + +print("Testing the %import directive") + +if string.sub(_VERSION,1,7)=='Lua 5.0' then + -- lua5.0 doesnt have a nice way to do this + function loadit(a,b) + lib=loadlib(a..':dll',b) or loadlib(a..':so',b) + assert(lib)() + end + loadit('base','Base_Init') + loadit('foo','Foo_Init') + loadit('bar','Bar_Init') + loadit('spam','Spam_Init') +else + -- lua 5.1 does + require 'base' + require 'foo' + require 'bar' + require 'spam' +end + +-- Create some objects + +print("Creating some objects") + +a = base.Base() +b = foo.Foo() +c = bar.Bar() +d = spam.Spam() + +-- Try calling some methods +print("Testing some methods") +print("Should see 'Base::A' ---> ",a:A()) +print("Should see 'Base::B' ---> ",a:B()) + +print("Should see 'Foo::A' ---> ",b:A()) +print("Should see 'Foo::B' ---> ",b:B()) + +print("Should see 'Bar::A' ---> ",c:A()) +print("Should see 'Bar::B' ---> ",c:B()) + +print("Should see 'Spam::A' ---> ",d:A()) +print("Should see 'Spam::B' ---> ",d:B()) + +-- Try some casts + +print("\nTesting some casts") + +x = a:toBase() +print("Should see 'Base::A' ---> ",x:A()) +print("Should see 'Base::B' ---> ",x:B()) + +x = b:toBase() +print("Should see 'Foo::A' ---> ",x:A()) +print("Should see 'Base::B' ---> ",x:B()) + +x = c:toBase() +print("Should see 'Bar::A' ---> ",x:A()) +print("Should see 'Base::B' ---> ",x:B()) + +x = d:toBase() +print("Should see 'Spam::A' ---> ",x:A()) +print("Should see 'Base::B' ---> ",x:B()) + +x = d:toBar() +print("Should see 'Bar::B' ---> ",x:B()) + + +print "\nTesting some dynamic casts\n" +x = d:toBase() + +print " Spam -> Base -> Foo : " +y = foo.Foo_fromBase(x) +if y then + print "bad swig" +else + print "good swig" +end + +print " Spam -> Base -> Bar : " +y = bar.Bar_fromBase(x) +if y then + print "good swig" +else + print "bad swig" +end + +print " Spam -> Base -> Spam : " +y = spam.Spam_fromBase(x) +if y then + print "good swig" +else + print "bad swig" +end + +print " Foo -> Spam : " +y = spam.Spam_fromBase(b) +if y then + print "bad swig" +else + print "good swig" +end diff --git a/Examples/lua/import/spam.h b/Examples/lua/import/spam.h new file mode 100644 index 000000000..57db84563 --- /dev/null +++ b/Examples/lua/import/spam.h @@ -0,0 +1,24 @@ +#include "bar.h" + +class Spam : public Bar { + public: + Spam() { } + ~Spam() { } + virtual const char * A() const { + return "Spam::A"; + } + const char * B() const { + return "Spam::B"; + } + virtual Base *toBase() { + return static_cast<Base *>(this); + } + virtual Bar *toBar() { + return static_cast<Bar *>(this); + } + static Spam *fromBase(Base *b) { + return dynamic_cast<Spam *>(b); + } +}; + + diff --git a/Examples/lua/import/spam.i b/Examples/lua/import/spam.i new file mode 100644 index 000000000..d3d9121db --- /dev/null +++ b/Examples/lua/import/spam.i @@ -0,0 +1,9 @@ +%module spam +%{ +#include "spam.h" +%} + +%import bar.i +%include "spam.h" + + |