Lua包装类-通过DLL向Lua公开c++静态方法
Lua Wrapper Class - Exposing c++ static methods to Lua through a DLL
我终于遇到了一个在这里找不到解决方案的问题。我使用的是在这里找到的Lua包装类http://lua-users.org/wiki/CppConvenientLuaWrapperClass.我们已经能够公开一个完整的API和更多的其他功能,如串行通信等
这个LuaWrapper背后的概念是,你在编译之前公开每个方法,所以当你运行程序时,所有方法都会被添加到LuaStack中,这样你就可以执行它们。现在的想法是构建一种Dll,以完成公开方法的过程。这样你就不需要发布一个包含所有公开方法的版本,而是通过多个dll文件加载它们。
我曾尝试创建另一个表并在该表中注册其他方法,但这样一来,以前公开的方法就无法工作了。
我能想到的另一种方法是创建一个dll,但在C中,它包含所有想要的方法,并将其直接加载到Lua。但我认为换一种方式会更好。
你能做类似的事情吗?我是不是有什么错误的想法?
感谢
哼。。。我真的不想在这个时候改变我们的包装。我想我可以做到的。我没有为插件函数添加一个新表,而是添加了一个新的子表,它将包含要从Lua调用的函数名称和cClose。所以最后我们应该有:
application.functionName()
application.plugin.functionName()
即使这样工作,它也会做得很好。现在我想知道,当我们将要添加到application[plugin][pluginFunction]而不是application[pluginFunction]的函数公开时,如何引用lua_stable?!这就是正常功能的暴露方式:
//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 );
关于如何实现不将cclosure添加到主表(在mMethodTableIndex)中作为mainTable[functionName],而是添加到mainTable[plugin][functionNane],有什么想法吗。?
我不确定,您是否清楚自己想要做什么。扩展lua的典型方法是用一个方法编写一个DLL,该方法使用lua API注册C++类型和C函数。要方便地绑定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用作嵌入式脚本语言,也可以将其用作可扩展脚本语言,使用上面显示的方法进行扩展。两者都是有效的,但你必须考虑,你到底想做什么。
- C++Lua用户数据扰乱了Mathfu浮点值
- 使用 LuaBridge 或 Lua 将对象传递给 Lua 函数C++
- 将 Lua 函数块转换为 C 字符串
- 我可以从 lua 5.0.2 加载用 c++ 编写的 lua 5.3.5 dll 吗?
- 使用 lua 协程 API 和lua_close时出现分段错误
- 使用 C++ 自动 Lua 绑定
- 调用 lua 函数的地址为 C/C++?
- lua 5.0.2 模块和 5.3.5 有什么区别?
- 如何将 Lua 添加到C++项目
- 使用 Lua 中的C++库对象
- 如何在C++中将用户数据从一个 Lua 块传递到另一个块
- 如何使用重新定义的打印函数打印Lua表?
- Lua table in C++ dll
- 在运行时环境中使用 C++ w/ Lua
- 将shared_ptr传递给Lua
- luaL_len is missing in Lua 5.1
- 将向量传递给<struct> Lua 表
- 将 Lua 与 C++ DLL 结合使用
- Lua C API - 从C++分配和使用类的对象成员
- C++中最接近 Lua 表操作的东西是什么?