From e90e87c8a0b2b3f663f5a27c1ea2849524e4418e Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 25 Oct 2014 10:11:18 +0200 Subject: some desperate trials to use create on normalizer module loader to create Lua normalizer objects --- include/module/ModuleLoader.hpp | 73 ++++++++++++++++++++++++++++++++++++++++- src/crawl/crawl.cpp | 6 ++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/include/module/ModuleLoader.hpp b/include/module/ModuleLoader.hpp index cf12a67..91fc4e8 100755 --- a/include/module/ModuleLoader.hpp +++ b/include/module/ModuleLoader.hpp @@ -22,6 +22,11 @@ #include "TypeList.hpp" #include "TypeInfo.hpp" +#ifdef WITH_LUA +#include "lua.hpp" +#include "LuaVM.hpp" +#endif + #ifndef _WIN32 typedef void * ModuleHandle; #else @@ -81,6 +86,11 @@ class BaseModuleLoader { mapType m_modules; int m_flags; + +#ifdef WITH_LUA + public: + lua_CFunction luaCreateFunc; +#endif public: @@ -190,7 +200,12 @@ class ModuleLoader< Interface, NullType > : public BaseModuleLoader< Interface, public: ModuleLoader< Interface >( const std::vector files, int flags = DEFAULT_FLAGS, void *user_data = 0 ) - : BaseModuleLoader< Interface >( files, flags, user_data ) { } + : BaseModuleLoader< Interface >( files, flags, user_data ) + { +#ifdef WITH_LUA + BaseModuleLoader< Interface >::luaCreateFunc = &ModuleLoader< Interface, NullType >::lua_create; +#endif + } Interface *create( std::string subclass ) { @@ -209,6 +224,30 @@ class ModuleLoader< Interface, NullType > : public BaseModuleLoader< Interface, return obj; } + +#ifdef WITH_LUA + private: + static int lua_create( lua_State *l ) + { + ModuleLoader< Interface, NullType > *loader = + (ModuleLoader< Interface, NullType > *) + lua_touserdata( l, lua_upvalueindex( 1 ) ); + + size_t nofParams = lua_gettop( l ) - 1; + if( nofParams != 1 ) { + lua_pushnil( l ); + lua_pushstring( l, "expecting 1 class parameter, no ctor parameters" ); + return 2; + } + + LuaVM::dumpStack( l ); + std::string clazz = lua_tostring( l, -1 ); + + loader->create( clazz ); + + return 0; + } +#endif }; // one param @@ -238,6 +277,14 @@ class ModuleLoader< Interface, TYPELIST_1( T1 ) > : public BaseModuleLoader< Int return obj; } + +#ifdef WITH_LUA + private: + static int lua_create( lua_State * ) + { + return 0; + } +#endif }; // two params @@ -267,6 +314,14 @@ class ModuleLoader< Interface, TYPELIST_2( T1, T2 ) > : public BaseModuleLoader< return obj; } + +#ifdef WITH_LUA + private: + static int lua_create( lua_State * ) + { + return 0; + } +#endif }; // three params @@ -296,6 +351,14 @@ class ModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) > : public BaseModuleLoa return obj; } + +#ifdef WITH_LUA + private: + static int lua_create( lua_State * ) + { + return 0; + } +#endif }; // four params @@ -325,6 +388,14 @@ class ModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) > : public BaseModul return obj; } + +#ifdef WITH_LUA + private: + static int lua_create( lua_State * ) + { + return 0; + } +#endif }; #endif diff --git a/src/crawl/crawl.cpp b/src/crawl/crawl.cpp index 24d6427..04f4d1c 100755 --- a/src/crawl/crawl.cpp +++ b/src/crawl/crawl.cpp @@ -214,6 +214,12 @@ int main( int /* argc */, char *argv[] ) modules = luaVm.getStringArray( "modules.urlnormalizers" ); vector normalizerModules = searchModuleFiles( modules, allModuleFiles ); ModuleLoader urlNormalizers( normalizerModules, CLOSE_DEFERRED, (void *)&luaVm ); + lua_newtable( luaVm.handle( ) ); + lua_pushstring( luaVm.handle( ), "create" ); + lua_pushlightuserdata( luaVm.handle( ), &urlNormalizers ); + lua_pushcclosure( luaVm.handle( ), (lua_CFunction)urlNormalizers.luaCreateFunc, 1 ); + lua_settable( luaVm.handle( ), -3 ); + lua_setglobal( luaVm.handle( ), "urlnormalizers" ); modules = luaVm.getStringArray( "modules.urlfilters" ); vector filterModules = searchModuleFiles( modules, allModuleFiles ); -- cgit v1.2.3-54-g00ecf