【问题标题】:Lua 5.4 replacement for luaL_openlib gives nil value errorLua 5.4 替换 luaL_openlib 给出 nil 值错误
【发布时间】:2021-07-11 21:55:15
【问题描述】:

我目前正在尝试更新 Dungeon Crawl: Stone Soup 中使用的 Lua 版本,但由于 luaL_openlib 函数被大量使用并且已被弃用,因此我遇到了问题。目前,我已将其替换为以下代码(根据位置使用不同的参数):

//luaL_openlib(ls, nullptr, lr, 0); //OLD CALL

//New call
lua_getglobal(ls, "NULL");
if (lua_isnil(ls, -1)) {
   lua_pop(ls, 1);
   lua_newtable(ls);
}
luaL_setfuncs(ls, lr, 0);
lua_setglobal(ls, "NULL");

代码全部编译,但是当我尝试运行游戏时,我收到以下错误:

./crawl
/mnt/d/Google Drive/Jon/UK/Spring 2021/CS 498/Crawl/crawl/crawl-ref/source/dat/des/00init.des:18: ...CS 498/Crawl/crawl/crawl-ref/source/dat/dlua/dungeon.lua:255: global 'setfenv' is not callable (a nil value)

任何人都可以就为什么会发生这种情况提供任何建议,或者任何人都可以就替换所有对 luaL_openlib 的调用的更好方法提供建议吗?我正在处理的代码可以在 here 找到,并且在提交中它显示了我为更新对 luaL_openlib 的引用所做的所有最新更改。

编辑: 正如评论中提到的,这里是 dungeon.lua 中实际引发错误的代码:

-- Given a list of map chunk functions, runs each one in order in that
-- map's environment (wrapped with setfenv) and returns the return
-- value of the last chunk. If the caller is interested in the return
-- values of all the chunks, this function may be called multiple
-- times, once for each chunk.
function dgn_run_map(...)
  local map_chunk_functions = { ... }
  if #map_chunk_functions > 0 then
    local ret
    if not g_dgn_curr_map then
      error("No current map?")
    end
    local env = dgn_map_meta_wrap(g_dgn_curr_map, dgn)
    for _, map_chunk_function in ipairs(map_chunk_functions) do
      if map_chunk_function then
        ret = setfenv(map_chunk_function, env)()
      end
    end
    return ret
  end
end

【问题讨论】:

  • 您发布的 C++ 代码不是问题所在。问题是您在未发布的dungeon.lua 代码中使用了setfenv(已在Lua 5.2 中删除)。请发布该代码。

标签: c++ lua lua-c++-connection


【解决方案1】:

如 cmets 中所述,setfenv 在 Lua 5.2 及更高版本中不可用。函数环境现在的工作方式不同了。在 Lua 5.1 中,它们是可变的,当您以某种方式在不同环境中进行并发函数调用(递归调用或使用协程)时,这可能会导致问题。在 Lua 5.2 及更高版本中,环境是不可变的,并且函数保留了定义它的环境。但是,一段代码(例如一个函数)可以通过使用局部变量_ENV在执行过程中自愿临时切换到不同的环境。

因此,我建议对您的代码进行以下更改,以使其也能在 Lua 5.2+ 上运行。

当你遍历你的 map 块函数时,你应该有条件地调用 setfenv(仅当它可用时,即 Lua 5.1)并将环境作为额外参数传递给函数调用。

代替

if map_chunk_function then
  ret = setfenv(map_chunk_function, env)()
end

使用

if map_chunk_function then
  if setfenv then setfenv(map_chunk_function, env) end
  ret = map_chunk_function(env)
end

(顺便说一句,除了最后一次调用的返回值之外,您将丢失所有返回值——不确定这是有意的。)

现在每个单独的地图块函数可以决定是否以及何时使用参数作为环境。如果整个函数应该使用传递的环境,定义如下:

local function a_map_chunk_function(_ENV)
  -- code uses value passed in _ENV as environment and ignores original
  -- environment
end

如果只有部分映射块函数应该使用传递的环境,请使用以下模式:

local function another_map_chunk_function(env)
  -- code using original function environment it was defined with
  do
    local _ENV = env
    -- code uses env as environment
  end
  -- code uses original function environment again
end

当然,map chunk 函数也可以完全忽略传入的环境,只使用它定义时得到的原始环境:

local function yet_another_map_chunk_function()
  -- code ignores passed value completely and uses environment is was
  -- defined with
end

显然,最后两个选项与 Lua 5.1 的处理方式不兼容。如果您仍想修改原始函数环境而不是传入它们,则必须为此使用debug 模块。您可能可以使用您喜欢的搜索引擎找到一些针对 Lua 5.2+ 的 setfenv 的重新实现。

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2018-05-30
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多