C++ LuaPlus:如何使函数返回表?
我想知道如何从C++端创建和注册一个函数,从Lua端调用该函数时返回一个表。C++ LuaPlus:如何使函数返回表?,c++,function,lua,lua-table,luaplus,C++,Function,Lua,Lua Table,Luaplus,我想知道如何从C++端创建和注册一个函数,从Lua端调用该函数时返回一个表。 我尝试了很多东西,但都没有真正起作用。:/ (抱歉代码太长) 例如,此示例不起作用,因为Register()需要一个“luaCFunction”样式的函数: LuaPlus::LuaObject Test( LuaPlus::LuaState* state ) { int top = state->GetTop(); std::string var( state->ToString(1) )
我尝试了很多东西,但都没有真正起作用。:/ (抱歉代码太长) 例如,此示例不起作用,因为Register()需要一个“luaCFunction”样式的函数:
LuaPlus::LuaObject Test( LuaPlus::LuaState* state ) {
int top = state->GetTop();
std::string var( state->ToString(1) );
LuaPlus::LuaObject tableObj(state);
tableObj.AssignNewTable(state);
if (var == "aaa")
tableObj.SetString("x", "ABC");
else if (var == "bbb")
tableObj.SetString("x", "DEF");
tableObj.SetString("y", "XYZ");
return tableObj;
}
int main()
{
LuaPlus::LuaState* L = LuaPlus::LuaState::Create(true);
//without true I can't access the standard libraries like "math.","string."...
//with true, GetLastError returns 2 though (ERROR_FILE_NOT_FOUND)
//no side effects noticed though
LuaPlus::LuaObject globals = L->GetGlobals();
globals.Register("Test",Test);
char pPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH,pPath);
strcat_s(pPath,MAX_PATH,"\\test.lua");
if(L->DoFile(pPath)) {
if( L->GetTop() == 1 ) // An error occured
std::cout << "An error occured: " << L->CheckString(1) << std::endl;
}
}
澄清一下:从Lua方面来说,我希望它可以调用,比如:
myVar = Test("aaa")
Print(myVar) -- output: ABC
编辑:打印功能来自。这基本上是导致这一切不起作用的原因。打印只能打印字符串而不能打印表。。。如果你只返回1,上面的C++代码就可以工作了。< /强>
这是我的LuaPlus版本附带的文档,顺便说一句:
我真的希望你能帮助我。。我已经开始认为这是不可能的(
编辑:
我完全忘了说使用PushStack()会导致错误,因为“成员不存在”…首先,您可以尝试使用RegisterDirect()注册函数,这可能会避免lua_CFunction的问题,请查看luaplus手册
LuaPlus::LuaObject globals = L->GetGlobals();
globals.RegisterDirect("Test",Test);
第二,如果我记得创建一个表,有两个解决方案,如下所示
//first
LuaObject globalsObj = state->GetGlobals();
LuaObject myArrayOfStuffTableObj = globalsObj.CreateTable("MyArrayOfStuff");
//second
LuaObject aStandaloneTableObj;
aStandaloneTableObj.AssignNewTable(state);
检查您是否使用了正确的功能
第三,我记得lua堆栈对象不是luaobject,它们有一个转换,可能你可以试试这个
LuaStackObject stack1Obj(state, 1);
LuaObject nonStack1Obj = stack1Obj;
第四,就像你上面给出的函数Test()一样,你推到lua堆栈上的tableObj表,你必须记得清除这个对象。在经过长时间的评论讨论之后,我发布这个答案来帮助总结情况,并希望提供一些有用的建议 OP遇到的主要问题是在lua测试脚本中调用了错误的
print
函数。与原始代码显示的相反,OP测试的真实代码调用的是print(myVar)
,这是一个自定义提供的lua\u CFunction,而不是内置的print
函数
不知何故,这最终创建了模板类luaffunction
的一些实例化,并调用了重载的操作符()
。通过检查luaPlus的luaffunction.h
,此调用中发生的任何lua错误都将在没有任何日志记录的情况下消失(就luaPlus而言,这不是一个好的设计决策):
为了帮助捕获将来的此类错误,我建议添加一个新的luaplus\u assertlog
宏。具体地说,该宏将包含errorString
,这样上下文就不会完全丢失,并有助于调试。希望此更改不会破坏当前使用的luaplua\u assert
但从长远来看,修改luaplus_assert可能更好,这样它实际上包含了一些有意义的东西
无论如何,这里有一些不同的变化:
LuaPlusInternal.h
@@ -81,5 +81,6 @@
} // namespace LuaPlus
#if !LUAPLUS_EXCEPTIONS
+#include <stdio.h>
#include <assert.h>
#define luaplus_assert(e) if (!(e)) assert(0)
@@ -84,5 +85,6 @@
#include <assert.h>
#define luaplus_assert(e) if (!(e)) assert(0)
+#define luaplus_assertlog(e, msg) if (!(e)) { fprintf(stderr, msg); assert(0); }
//(void)0
#define luaplus_throw(e) assert(0)
//(void)0
@@ -21,7 +21,7 @@
class LuaFunction
{
public:
- LuaFunction(LuaObject& _functionObj)
+ LuaFunction(const LuaObject& _functionObj)
: functionObj(_functionObj) {
}
@@ -36,7 +36,7 @@
if (lua_pcall(L, 0, 1, 0)) {
const char* errorString = lua_tostring(L, -1); (void)errorString;
- luaplus_assert(0);
+ luaplus_assertlog(0, errorString);
}
return LPCD::Type<RT>::Get(L, -1);
}
运行上述命令将触发崩溃,但会包含一条半有用的错误消息:
g++ -Wall -pedantic -O0 -g -I ./Src -I ./Src/LuaPlus/lua51-luaplus/src plustest.cpp -o plustest.exe lua51-luaplus.dll
plustest.exe plustest.lua
plustest.lua:2: bad argument #1 to 'Print' (string expected, got table)Assertion failed!
Program: G:\OSS\luaplus51-all\plustest.exe
File: ./Src/LuaPlus/LuaFunction.h, Line 39
Expression: 0
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
如果将
返回状态->GetTop()-top;
替换为返回1;
,会发生什么情况?(在测试的最后一行中)“代码”> ErrRoScist< /Cube >包含什么?是的,我得到了同样的错误。你如何调用C++中的测试脚本?LuAPLUS是从LuaAuPCALL中获取错误,然后对它完全不做任何操作。(除非它被用在我看不到的地方)很可能包含有用的信息。不幸的是,所有这些混乱的最终结果是,我认为这个问题,无论是书面的还是被问到的,都是完全不正确的,最终对其他任何人都没有帮助。我不知道这方面的指导方针是什么,但我可能会建议收回这个问题,或者,至少,将其编辑为包括正在运行的实际lua脚本和涉及的打印函数。
@@ -21,7 +21,7 @@
class LuaFunction
{
public:
- LuaFunction(LuaObject& _functionObj)
+ LuaFunction(const LuaObject& _functionObj)
: functionObj(_functionObj) {
}
@@ -36,7 +36,7 @@
if (lua_pcall(L, 0, 1, 0)) {
const char* errorString = lua_tostring(L, -1); (void)errorString;
- luaplus_assert(0);
+ luaplus_assertlog(0, errorString);
}
return LPCD::Type<RT>::Get(L, -1);
}
// snip...
int main(int argc, const char *argv[])
{
LuaStateAuto L ( LuaState::Create(true) );
LuaObject globals = L->GetGlobals();
globals.Register("Test", Test);
globals.Register("Print", Print);
if(argc > 1)
{
/*
if (L->DoFile(argv[argc - 1]))
std::cout << L->CheckString(1) << '\n';
/*/
L->LoadFile( argv[argc - 1] );
LuaFunction<int> f ( LuaObject (L, -1) );
f();
//*/
}
}
g++ -Wall -pedantic -O0 -g -I ./Src -I ./Src/LuaPlus/lua51-luaplus/src plustest.cpp -o plustest.exe lua51-luaplus.dll
plustest.exe plustest.lua
plustest.lua:2: bad argument #1 to 'Print' (string expected, got table)Assertion failed!
Program: G:\OSS\luaplus51-all\plustest.exe
File: ./Src/LuaPlus/LuaFunction.h, Line 39
Expression: 0
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.