【问题标题】:luaL_len is missing in Lua 5.1Lua 5.1 中缺少 luaL_len
【发布时间】:2019-01-31 08:48:11
【问题描述】:

我为我的 Lua 项目编写 C++ dll。 Stackoverflow 程序员 Josh Parnell 非常乐于帮助我。他给了我一个代码,里面有luaL_len()
但是我使用了一个在其中实现了 Lua 5.1.5 的程序。此版本中缺少luaL_len()
这是一个代码:

static int forLua_SumArray (lua_State* L) {
// Get the length of the table (same as # operator in Lua)
    int n = luaL_len(L, 1);
    double sum = 0.0;

    // For each index from 1 to n, get the table
    // value as a number and add to sum
    for (int i = 1; i <= n; ++i) {
      lua_rawgeti(L, 1, i);
      sum += lua_tonumber(L, -1);
      lua_pop(L, 1);
    }

    lua_pushnumber(L, sum);
    return 1; 
}

请帮我做其中一件事或两件事

  1. 使用一些东西而不是 luaL_len 来获取从 Lua 到 dll 的表的大小
  2. 让 luaL_len 在我的 Lua 5.1.5 中工作

【问题讨论】:

    标签: c++ dll lua stack


    【解决方案1】:

    最接近luaL_len 的Lua 5.1 C API 函数是lua_objlen。此功能应该适用于您的情况。

    无论如何,Compat5.3 project 为较旧的 Lua 版本(5.1 和 5.2)实现了许多较新的 C API 函数,并且它包括用于 Lua 5.1 的 luaL_len 的实现。您可以使用或窃取该代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-13
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多