gpt4 book ai didi

c++ - LuaPlus:如何让函数返回一个表?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:42 24 4
gpt4 key购买 nike

我想知道如何从 C++ 端创建和注册一个函数,该函数在从 Lua 端调用时返回一个表。
我尝试了很多东西,但没有任何效果。 :/

(抱歉代码太长)这例如是行不通的,因为 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;
}
}

当我尝试将其设置为 luaCFunction 函数时,它崩溃了 (0x3) 并说:
断言失败:0,文件 C:\......\luafunction.h , 第 41 行

int 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");

tableObj.Push();

return state->GetTop() - top;
}

澄清一下:从 Lua 的角度来看,我希望它可以像这样调用:

myVar = Test("aaa")
Print(myVar) -- output: ABC

编辑:打印功能来自 here .并且基本上是这个不起作用的原因。 Print 只能打印字符串而不是表格...如果您只返回 1,上面的 C++ 代码可以正常工作。

顺便说一句,这是我的 LuaPlus 版本附带的文档:http://luaplus.funpic.de/

我真的希望你能帮助我..我已经开始认为这是不可能的。 :'(

编辑:我完全忘了说使用 PushStack() 会导致错误,因为“该成员不存在”...

最佳答案

经过长时间的评论讨论进行了一番艰苦的探索之后,我发布了这个答案以帮助总结情况并希望提供一些有用的建议。

OP 遇到的主要问题是错误的 print函数在 lua 测试脚本中被调用。与显示的原始代码相反,OP 正在测试的真实代码正在调用 Print(myVar) 这是自定义提供的 lua_CFunction 而不是内置的 print功能。

不知何故,这最终创建了 template <typename RT> class LuaFunction 的一些实例化并调用重载的 operator()() .从检查 luafunction.h来自 luaPlus,此调用中发生的任何 lua 错误都将在没有任何类型的日志记录的情况下被吞没(对 luaPlus 而言,这不是一个好的设计决策):

  if (lua_pcall(L, 0, 1, 0)) {
const char* errorString = lua_tostring(L, -1); (void)errorString;
luaplus_assert(0);
}

为了帮助捕获此类错误,我建议添加一个新的 luaplus_assertlog宏。具体来说,此宏将包括 errorString这样上下文就不会完全丢失,并希望有助于调试。此更改希望不会破坏 luaplua_assert 的现有用途来自 API 的其他部分。但从长远来看,修改 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

LuaFunction.h

@@ -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);
}

在上面的更改中,我选择不使用 std::cerr仅仅是因为 C++ 流往往比普通的 C 风格的 io 函数更重。如果您使用 mingw 作为工具链,则尤其如此 -- ld链接器无法消除未使用的 C++ 流符号,即使您的程序从不使用它也是如此。

有了它,下面是一个对 lua 函数进行不 protected 调用的示例,因此您可以看到 errorString崩溃前打印出来:

// 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.

关于c++ - LuaPlus:如何让函数返回一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com