2013-02-13 73 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,或者您可以將其用作可擴展腳本語言,並使用上述方法對其進行擴展。兩者都是有效的,但你必須考慮,你到底想要做什麼。