2013-02-13 69 views
1

我终于遇到了一个问题,我在这里找不到解决方案。我正在使用在这里找到的Lua Wrapper类http://lua-users.org/wiki/CppConvenientLuaWrapperClass。我们已经能够公开一个完整的API以及其他更多的功能,如串行通信等等。Lua Wrapper类 - 通过DLL向Lua公开C++静态方法

这个Lua Wrapper背后的概念是,你在编译之前公开每一个方法,所以当你运行你的程序时,所有的方法都会被添加到Lua栈中,并以这种方式执行它们。现在的想法是构建一种Dll来完成这个暴露方法的过程。这样你就不需要发布一个包含所有公开方法的版本,而是通过多个dll文件加载它们。

我试着创建另一个表,并在该表中注册其他方法,但与此,以前暴露的方法停止工作。

另一种方式我可以想到的是创建一个DLL,但在C中包含所有可取的方法,并直接加载到Lua。但我认为另一种方式会更好。

你能够做类似的事情吗?我有一些错误的概念吗?

感谢

赫姆...我真的不希望在这个时候改变我们的包装。我想我可以设法做到这一点。我没有为插件函数添加一个新表格,而是添加了一个新的子表格,其中包含要从Lua调用的函数名称和cClosures。 所以在最后,我们应该有:

application.functionName() 
application.plugin.functionName() 

即使这样工作就会做得很好。 现在我想知道如何在公开要添加到应用程序[plugin] [pluginFunction]而不是应用程序[pluginFunction]的函数时引用lua_settable ?! 这是怎样的正常功能被暴露:

//mState is a pointer to a Lua_State 
lua_pushstring(mState, functionName); 

//methodDesc is a pointer to an object that describes the function arguments/returns 
lua_pushlightuserdata(mState, methodDesc); 

//exposeMethodProxy is the method that is responsible for conneting lua c-calls to the c-functions 
lua_pushcclosure(mState, exposedMethodProxy, 1); 

//mMethodTableIndex is a member variable that contains the index of the table tha hold all exposed functions 
lua_settable(mState, mMethodTableIndex); 

上我如何能实现将所述cclosures不到主表(在mMethodTableIndex)作为mainTable [functionName],但在maintable [插件]任何想法[functionNane] 。?

+0

wxLua做同样的事情 - 你可以看看他们的代码,看看他们是如何做到的。 – finnw 2013-02-13 14:19:40

+0

Humm谢谢,我会看看 – MRodrigues 2013-02-14 14:07:14

回答

1

我不确定,你清楚你想做什么。扩展lua的一个典型方法是用一种使用Lua API注册C++类型和C函数的单一方法编写DLL。为了方便地绑定C++函数和类,您可以使用LuaBridge。的这样的结合的例子是在这里:https://github.com/d-led/xerceslua

为xerceslua模块的DLL首部仅包含一个功能:

#include <lua.hpp> 
void register_xerceslua (lua_State* L); 

实施LuaBridge内部用于结合到C++:

#include "xerceslua_lib.h" 

#include <lua.hpp> 
#include <LuaBridge.h> 

void register_xerceslua (lua_State* L) { 
... 
luabridge::getGlobalNamespace(L) 
    .beginNamespace("xerces") 
    .addVariable("version",&version,false) 
... 

在Lua你就可以访问暴露的C++ API:

assert(require 'xerceslua') 

local parser=xerces.XercesDOMParser() 
parser:loadGrammar("Employee.dtd",xerces.GrammarType.DTDGrammarType) 

您可以使用Lua作为嵌入式脚本语言,您可以在其中执行lua from within your software,或者您可以将其用作可扩展脚本语言,并使用上述方法对其进行扩展。两者都是有效的,但你必须考虑,你到底想要做什么。