单个 Lua 状态下的多个脚本并使用 _ENV

inz*_*iak 5 lua lua-c++-connection lua-api c++11

我目前正在学习如何使用 Lua C API,虽然我在 C/C++ 和 Lua 之间成功绑定了函数,但我有几个问题:

  1. 将多个脚本加载到一个中是个好主意lua_State吗?有没有办法关闭特定的块?如果不再使用脚本,如何lua_State在保留其他所有内容的同时将其清除?

  2. 使用可能对函数/全局变量使用相同名称的脚本的最佳方法是什么?如果我加载所有这些,新的定义会覆盖旧的。

    在线阅读后,我认为我需要将每个加载的块分离到不同的环境中。我设想的这种工作方式是每次加载一个块时,我都会为它分配一个唯一的环境名称,当我需要使用它时,我只需使用该名称从 中获取环境LUA_REGISTRYINDEX并执行操作。到目前为止,我还没有想出如何做到这一点。网上有例子,但他们使用 Lua 5.1。

inz*_*iak 2

经过更多探索后,我发现了我认为是我正在寻找的解决方案。我不确定这是否是正确/最好的方法,但它在我的基本测试用例中有效。@jpjacobs 对这个问题的回答很有帮助。

测试1.lua

x = 1
function hi()
    print("hi1");
    print(x);
end
hi()
Run Code Online (Sandbox Code Playgroud)

测试2.lua

x =2
function hi()
    print("hi2");
    print(x);
end
hi()
Run Code Online (Sandbox Code Playgroud)

主程序

int main(void)
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);

    char* file1 = "Rooms/test1.lua";
    char* file2 = "Rooms/test2.lua";

    //We load the file
    luaL_loadfile(L, file1);
    //Create _ENV tables
    lua_newtable(L);
    //Create metatable
    lua_newtable(L);
    //Get the global table
    lua_getglobal(L, "_G");
    lua_setfield(L, -2, "__index");
    //Set global as the metatable
    lua_setmetatable(L, -2);
    //Push to registry with a unique name.
    //I feel like these 2 steps could be merged or replaced but I'm not sure how
    lua_setfield(L, LUA_REGISTRYINDEX, "test1");
    //Retrieve it. 
    lua_getfield(L, LUA_REGISTRYINDEX, "test1");
    //Set the upvalue (_ENV)
    lua_setupvalue(L, 1, 1);
    //Run chunks
    lua_pcall(L, 0, LUA_MULTRET, 0);

    //Repeat
    luaL_loadfile(L, file2);
    lua_newtable(L);
    lua_newtable(L);
    lua_getglobal(L, "_G");
    lua_setfield(L, -2, "__index");
    lua_setmetatable(L, -2);
    lua_setfield(L, LUA_REGISTRYINDEX, "test2");
    lua_getfield(L, LUA_REGISTRYINDEX, "test2");
    lua_setupvalue(L, 1, 1);
    lua_pcall(L, 0, LUA_MULTRET, 0);

    //Retrieve the table containing the functions of the chunk
    lua_getfield(L, LUA_REGISTRYINDEX, "test1");
    //Get the function we want to call
    lua_getfield(L, -1, "hi");
    //Call it
    lua_call(L, 0, 0);
    //Repeat
    lua_getfield(L, LUA_REGISTRYINDEX, "test2");
    lua_getfield(L, -1, "hi");
    lua_call(L, 0, 0);
    lua_getfield(L, LUA_REGISTRYINDEX, "test2");
    lua_getfield(L, -1, "hi");
    lua_call(L, 0, 0);
    lua_getfield(L, LUA_REGISTRYINDEX, "test1");
    lua_getfield(L, -1, "hi");
    lua_call(L, 0, 0);

    lua_close(L);
}
Run Code Online (Sandbox Code Playgroud)

输出:

hi1
1
hi2
2
hi1
1
hi2
2
hi2
2
hi1
1
Run Code Online (Sandbox Code Playgroud)

我正在使用 Lua 5.3.2 和 Visual Studio 2013(如果这意味着什么的话)。

这个基本测试用例可以根据需要运行。我将继续测试,看看是否出现任何问题/改进。如果有人发现我可以改进这段代码或明显的错误,请发表评论。