首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LuaBridge将LuaJIT绑定到C++会导致“死机:无保护错误”

使用LuaBridge将LuaJIT绑定到C++会导致“死机:无保护错误”
EN

Stack Overflow用户
提问于 2018-11-02 11:31:08
回答 1查看 334关注 0票数 2

Windows 10 x64、MSVC 2017、LuaJIT 2.0.5。

我在网上搜索了一下,但答案没有帮助。

基本上,我正在尝试遵循this manual,除了我必须将#include <LuaBridge.h>放在Lua include之后,否则说LuaBridge应该放在Lua include之后是行不通的。

Hovewher,我得到了以下错误:PANIC: unprotected error in call to Lua API (attempt to call a nil value)

我不知道为什么。如果你需要更多信息--尽管说吧。

代码语言:javascript
复制
#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:

代码语言:javascript
复制
testString = "LuaBridge works!"
number = 42
EN

回答 1

Stack Overflow用户

发布于 2018-11-03 06:06:28

教程中的代码有问题。lua_pcall没有什么可调用的,因为luaL_dofileluaL_openlibs不会将函数推入堆栈,因此它尝试调用nil并返回2 (宏LUA_ERRRUN的值)。

因此,我通过更改教程中的代码并使用g++编译来验证这一点。不管是什么原因,我都没有收到恐慌性错误;可能是因为它使用的是Lua 5.3:

代码语言:javascript
复制
#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;
}

正如您所注意到的,代码也是错误的,因为Lua头必须包含在LuaBridge头之前!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53112327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档