c++ - luaL_dofile在已知有效的字节码上失败,可用于未编译版本

标签 c++ lua

我已经组装了一个非常简单的Lua引擎,但是它似乎拒绝了在lua控制台中工作的字节码。未编译的版本可在引擎中使用。我是否以某种方式使用luac错误?
我使用给定的命令进行编译并以'./a.out'运行。
res / default.lua:

print("Setting up world structure.")
luac命令:
luac -o res/default.lux res/default.lua
MWE:
#define SCRIPTDIR "res/"

#define THROW_IF_NONZERO(x,m) if((x)!=0) throw std::runtime_error(m);
#define THROW_IF_ZERO(x,m) if((x)==0) throw std::runtime_error(m);

extern "C" {
        #include "lua.h"
        #include "lauxlib.h"
        #include "lualib.h"
}

#include "sys/stat.h"

#include <string>
#include <system_error>
#include <iostream>

using std::string;

class Entity {
        private:
                lua_State *m_lua;
        public:
                Entity() : Entity(nullptr) { }
                Entity(lua_State *lua) : m_lua{lua} { }
                virtual ~Entity() { }
                void load_and_run(string);
};

class WorldEntity : public Entity {
        public:
                WorldEntity(lua_State *lua) : Entity(lua) {
                        luaL_openlibs(lua);
                }
                ~WorldEntity() { }
};

int main() {
        lua_State *lua{nullptr};
        try {
                lua = luaL_newstate();
                WorldEntity eWorld{lua};
                eWorld.load_and_run("default"); // load default.lua/lux
        } catch(std::exception &e) {
                if (lua != nullptr) {
                        lua_close(lua);
                }
                std::cout << "Error: " << e.what() << std::endl;
        }
        return 0;
}

void Entity::load_and_run(string filename) {
        THROW_IF_ZERO(m_lua, "Lua not started.");
        filename = SCRIPTDIR + filename + ".lux";
        struct stat sb;
        int rc = stat(filename.c_str(), &sb);
        if (rc == -1) {
                filename.pop_back();
                filename += "a";
                rc = stat(filename.c_str(), &sb);
                THROW_IF_NONZERO(rc, "File not found!");
        }
        std::cout << "File: " << filename << std::endl;
        // Currently won't run compiled Lua scripts, not sure why.
        rc = luaL_dofile(m_lua, filename.c_str());
        THROW_IF_NONZERO(rc, "Could not load lua file.");
}
编译命令:
gcc src/bug001mwe.cpp -std=c++14 -llua -lstdc++
脚本的正确输出:
File: res/default.lua
Setting up world structure.
字节码输出错误:
File: res/default.lux
Error: Could not load lua file.
两个文件,从lua控制台输出:
Setting up world structure.

最佳答案

让我感到困惑的是,它在lua控制台中有效,但在我的程序中却无效。我在调用luaL_dofile之后添加了对lua_tostring的调用,如下所示:

rc = luaL_dofile(m_lua, filename.c_str());
std::ostringstream ostr;
ostr << "Could not load lua file. ";
ostr << lua_tostring(m_lua, -1);
THROW_IF_NONZERO(rc, ostr.str());
错误字符串变为:
Error: Could not load lua file. res/default.lux: version mismatch in precompiled chunk
有没有搞错?
长话短说,由于某些无关内容中软件包的依赖关系过旧,因此我安装了Lua的先前版本。较早的luac拦截了luac命令并编译为有效但不兼容的字节码。卸载了我不需要的无关软件包,现在一切正常。
故事的寓意:始终在Lua堆栈上检查错误字符串,它将(可能)告诉您哪里出了问题。

关于c++ - luaL_dofile在已知有效的字节码上失败,可用于未编译版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62792963/

相关文章:

c++ - 诸如 bits/vector.tcc 之类的头文件名是否符合标准?

C++ 在基础对象 std::swap 之后是否保证派生对象数据有效?

machine-learning - 神经网络学习悬停。时间延误?

c - lua_tostring 在 C 代码中的 SQL 函数中不起作用

c++ - 如何使用沙漏模式在库/模块边界之间传递 vector <T>?

c++ - 使用动态多态时如何避免指针?

c++ - 在雷达图像上绘制区域而不是点

machine-learning - 以张量作为输入的 MLP 构造

loops - 如何在 Lua 中的二维表上创建迭代器?

android - 为 Android 编译 Lua lib - 成功,但奇怪的段错误