使用LuaBridge将LuaJIT绑定到C++中会导致“PANIC:无保护错误”
2019-4-27 14:32:47
收藏:0
阅读:114
评论:1
Windows 10 x64,MSVC 2017,LuaJIT 2.0.5。
我搜了一下网络,但没有找到解决方案。
基本上我正在尝试按照这个手册的步骤进行操作,除了我必须在Lua包含之后放置#include<LuaBridge.h>,否则会出现错误,说LuaBridge应该在Lua包含之后使用。
但是,我遇到了以下错误:“PANIC:调用Lua API时的无保护错误(尝试调用一个nil值)”。
我不知道为什么。如果您需要更多信息,请告诉我。
#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 << "这里是我们的数字:" << answer << endl;
system("pause");
return 0;
}
script.lua:
testString = "LuaBridge工作了!"
number = 42
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

这个教程中的代码是有问题的。因为
luaL_dofile和luaL_openlibs没有将一个函数推到堆栈中, 所以lua_pcall没有函数可以调用,尝试调用nil并返回错误码 2 (宏值为LUA_ERRRUN)。我通过改变这个不正确的代码并使用 g++ 进行编译进行了验证。出于某种原因(也许是因为它正在使用 Lua 5.3),我没有得到PANIC错误:
#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; // 打印 lua_pcall 的返回值。这将打印 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; }正如您所注意到的,该代码还有错误,因为必须在包含 LuaBridge 头文件之前先包含 Lua 头文件!