From 62901873f5b7e37beb81037caae5808396528270 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 4 Oct 2014 22:43:38 +0200 Subject: first experiments with tolua and Lua glue code in a module and how to make it usable in a Lua script --- .gitignore | 1 + tests/tolua/Base.hpp | 11 +++++++++ tests/tolua/GNUmakefile | 40 +++++++++++++++++++++++++++++++++ tests/tolua/libtest1/GNUmakefile | 48 ++++++++++++++++++++++++++++++++++++++++ tests/tolua/libtest1/TestMod.cpp | 23 +++++++++++++++++++ tests/tolua/libtest1/TestMod.hpp | 20 +++++++++++++++++ tests/tolua/libtest1/TestMod.pkg | 10 +++++++++ tests/tolua/test1.cpp | 43 +++++++++++++++++++++++++++++++++++ tests/tolua/test1.lua | 1 + 9 files changed, 197 insertions(+) create mode 100755 tests/tolua/Base.hpp create mode 100644 tests/tolua/GNUmakefile create mode 100755 tests/tolua/libtest1/GNUmakefile create mode 100755 tests/tolua/libtest1/TestMod.cpp create mode 100755 tests/tolua/libtest1/TestMod.hpp create mode 100644 tests/tolua/libtest1/TestMod.pkg create mode 100755 tests/tolua/test1.cpp create mode 100644 tests/tolua/test1.lua diff --git a/.gitignore b/.gitignore index 0a78e34..4dd6b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ tests/*/test1 tests/*/test2 tests/*/test3 +tests/*/test4 src/crawl/crawl makefiles/gmake/platform.mk.vars makefiles/gmake/platform.vars diff --git a/tests/tolua/Base.hpp b/tests/tolua/Base.hpp new file mode 100755 index 0000000..db6a8ad --- /dev/null +++ b/tests/tolua/Base.hpp @@ -0,0 +1,11 @@ +#ifndef __BASE_H +#define __BASE_H + +class Base +{ + public: + virtual ~Base( ) { } + virtual void hello( ) = 0; +}; + +#endif diff --git a/tests/tolua/GNUmakefile b/tests/tolua/GNUmakefile new file mode 100644 index 0000000..124c959 --- /dev/null +++ b/tests/tolua/GNUmakefile @@ -0,0 +1,40 @@ +TOPDIR = ../.. + +SUBDIRS = libtest1 + +#INCLUDE_CXXFLAGS = \ +# -DUSE_MODULELOADER + +INCLUDE_DIRS = \ + -I. \ + -I$(TOPDIR)/include/luaglue \ + -I$(TOPDIR)/include/logger \ + -I$(TOPDIR)/include/util \ + -I$(TOPDIR)/include/module + +INCLUDE_LDFLAGS = \ + -L$(TOPDIR)/src/libluaglue \ + -L$(TOPDIR)/src/liblogger + +INCLUDE_LIBS = \ + -lluaglue \ + -llua \ + -llogger + +TEST_CPP_BINS = \ + test1$(EXE) + +OBJS = + +-include $(TOPDIR)/makefiles/gmake/sub.mk + +local_all: + +local_clean: + +local_distclean: + +LD_LIBRARY_PATH=$(TOPDIR)/src/libutil:$(TOPDIR)/src/liblogger:$(TOPDIR)/src/libcrawler:$(TOPDIR)/src/libluaglue + +local_test: + LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) ./test1 test1.lua diff --git a/tests/tolua/libtest1/GNUmakefile b/tests/tolua/libtest1/GNUmakefile new file mode 100755 index 0000000..9393a93 --- /dev/null +++ b/tests/tolua/libtest1/GNUmakefile @@ -0,0 +1,48 @@ +TOPDIR = ../../.. + +SUBDIRS = + +-include $(TOPDIR)/makefiles/gmake/platform.mk + +INCLUDE_DIRS = \ + -I. -I$(TOPDIR)/src -I.. \ + -I$(TOPDIR)/include/module \ + -I$(TOPDIR)/include/util + +INCLUDE_CXXFLAGS = \ + +INCLUDE_LDFLAGS = \ + -L$(TOPDIR)/src/libcrawler + +INCLUDE_LIBS = \ + -lcrawler \ + -ltolua + +DYNAMIC_MODULE = \ + mod_test.so + +STATIC_LIB = \ + libtest.a + +CPP_OBJS = \ + TestMod.o \ + TestModLua.o + +-include $(TOPDIR)/makefiles/gmake/sub.mk + +TestModLua.cpp: TestMod.pkg + tolua -H TestModLua.hpp -o TestModLua.cpp TestMod.pkg + +local_all: + +local_clean: + @-rm TestModLua.cpp TestModLua.hpp + +local_distclean: + +local_install: + +local_uninstall: + +local_test: + diff --git a/tests/tolua/libtest1/TestMod.cpp b/tests/tolua/libtest1/TestMod.cpp new file mode 100755 index 0000000..491001c --- /dev/null +++ b/tests/tolua/libtest1/TestMod.cpp @@ -0,0 +1,23 @@ +#include "TestMod.hpp" +#include "tolua.h" +#include "TestModLua.hpp" + +#include + +using namespace std; + +void Derived::hello( ) +{ + cout << "hello" << endl; +} + +static void initModule( ) +{ +// tolua_TestMod_open( 0 /* m_lua, where from? */ ); +} + +static void destroyModule( ) +{ +} + +REGISTER_MODULE( "testmod", &initModule, &destroyModule, Base, Derived ) diff --git a/tests/tolua/libtest1/TestMod.hpp b/tests/tolua/libtest1/TestMod.hpp new file mode 100755 index 0000000..f4886cd --- /dev/null +++ b/tests/tolua/libtest1/TestMod.hpp @@ -0,0 +1,20 @@ +#ifndef __TESTMOD_H +#define __TESTMOD_H + +#include "Base.hpp" + +#include "ModuleRegistry.hpp" + +class Derived : public Base +{ + public: + Derived( ) { } + + virtual ~Derived( ) { } + + virtual void hello( ); +}; + +DECLARE_MODULE( Base ) + +#endif diff --git a/tests/tolua/libtest1/TestMod.pkg b/tests/tolua/libtest1/TestMod.pkg new file mode 100644 index 0000000..4fed1d3 --- /dev/null +++ b/tests/tolua/libtest1/TestMod.pkg @@ -0,0 +1,10 @@ +$#include "TestMod.hpp" + +class Derived : public Base +{ + Derived( ) { } + + virtual ~Derived( ) { } + + virtual void hello( ); +}; diff --git a/tests/tolua/test1.cpp b/tests/tolua/test1.cpp new file mode 100755 index 0000000..a2e91cb --- /dev/null +++ b/tests/tolua/test1.cpp @@ -0,0 +1,43 @@ +#include "Logger.hpp" +#include "LuaVM.hpp" +#include "ModuleLoader.hpp" +#include "Base.hpp" + +#include +#include + +using namespace std; + +int main( int /* argc */, char *argv[] ) +{ + try { + Logger::instance( ).openConsoleLog( logDEBUG ); + + LuaVM luaVm; + + vector modules; + modules.push_back( "./libtest1/mod_test.so" ); + ModuleLoader loader( modules ); + + Logger::instance( ).openConsoleLog( logDEBUG ); + + luaVm.loadSource( argv[1] ); + luaVm.executeMain( ); + + //luaVm.dumpState( ); + + //Base *obj = loader.create( "testmod" ); + //obj->hello( ); + //loader.destroy( obj ); + + return 0; + } catch( exception &e ) { + LOG( logFATAL ) << "ERROR: " << e.what( ); + return 1; + } catch( ... ) { + LOG( logFATAL ) << "ERROR: unknown exception!"; + return 1; + } + + return 0; +} diff --git a/tests/tolua/test1.lua b/tests/tolua/test1.lua new file mode 100644 index 0000000..20d97cf --- /dev/null +++ b/tests/tolua/test1.lua @@ -0,0 +1 @@ +local b = Derived:new( ) -- cgit v1.2.3-54-g00ecf