[英]Binding LuaJIT to C++ with LuaBridge results in “PANIC: unprotected error”
Windows 10 x64, MSVC 2017, LuaJIT 2.0.5. Windows 10 x64,MSVC 2017,LuaJIT 2.0.5。
I searched the web, but answers didn't help. 我在网上搜索,但答案无济于事。
Basically I'm trying to follow this manual , except that I had to place #include <LuaBridge.h>
after Lua includes, because otherwise it doesn't work saying that the LuaBridge should go after Lua includes. 基本上,我试图遵循本手册 ,除了我必须在Lua包含之后放置
#include <LuaBridge.h>
,因为否则,说LuaBridge应该在Lua包含之后就行不通了。
Hovewher, I get following error: PANIC: unprotected error in call to Lua API (attempt to call a nil value)
. 但是,我收到以下错误消息:
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
。
I have no idea why. 我不知道为什么。 If you need more info - just say what.
如果您需要更多信息,请说什么。
#include "stdafx.h"
#include <iostream>
#include <lua.hpp>
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
using namespace std;
int main()
{
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
string luaString = s.cast<string>();
int answer = n.cast<int>();
cout << luaString << endl;
cout << "And here's our number:" << answer << endl;
system("pause");
return 0;
}
script.lua: script.lua:
testString = "LuaBridge works!"
number = 42
The code in the tutorial is faulty. 本教程中的代码错误。
lua_pcall
has nothing to call because luaL_dofile
and luaL_openlibs
don't push a function onto the stack, so it tries to call nil
and returns 2
(the value of the macro LUA_ERRRUN
). lua_pcall
没什么可调用的,因为luaL_dofile
和luaL_openlibs
不会将函数推入堆栈,因此它尝试调用nil
并返回2
(宏LUA_ERRRUN
的值)。
I verified this by changing the code from the tutorial thus and compiling with g++. 我通过更改本教程中的代码并使用g ++进行编译来验证了这一点。 I didn't get a PANIC error, for whatever reason;
出于某种原因,我没有收到PANIC错误; maybe because it was using Lua 5.3:
也许是因为它使用的是Lua 5.3:
#include <iostream>
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
int main() {
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
luaL_openlibs(L);
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
std::cout << "result of pcall: " << lua_pcall(L, 0, 0, 0) << std::endl; // Print return value of lua_pcall. This prints 2.
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
std::string luaString = s.cast<std::string>();
int answer = n.cast<int>();
std::cout << luaString << std::endl;
std::cout << "And here's our number: " << answer << std::endl;
}
As you noticed, the code is also faulty because the Lua headers have to be included before the LuaBridge header! 正如您所注意到的,该代码也是错误的,因为必须在LuaBridge头之前包含Lua头!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.