【问题标题】:What is lua_len() alternative in Lua 5.1?Lua 5.1 中的 lua_len() 替代方案是什么?
【发布时间】:2018-06-14 02:48:38
【问题描述】:

我刚刚在我的项目中用 LuaJIT 替换了 Lua,我得到了错误提示

Use of undeclared identifier 'lua_len'

如何更改 lua_len 使其与 Lua 5.1 和 LuaJIT 兼容?

这是我的代码,它使用来自 SWIG 绑定的lua_len。 (如果有帮助的话)

%typemap(in) (int argc, t_atom *argv)
{
    if (!lua_istable(L, $input)) {
      SWIG_exception(SWIG_RuntimeError, "argument mismatch: table expected");
    }
    lua_len(L, $input);
    $1 = lua_tointeger(L, -1);

    if (!$1) {
        SWIG_exception(SWIG_RuntimeError, "table is empty");
    }
    $2 = (t_atom *)getbytes($1 * sizeof(t_atom));

    for (int i=0; i<$1; ++i) {

        lua_pushinteger(L, i+1);
        lua_gettable(L, $input);

        if (lua_isnumber(L, -1)) {
            $2[i].a_type = A_FLOAT;
            $2[i].a_w.w_float = lua_tonumber(L, -1);
        }          
        else if (lua_isstring(L, -1)) {
            $2[i].a_type = A_SYMBOL;
            $2[i].a_w.w_symbol = gensym(lua_tostring(L, -1));
        }
        else {
            SWIG_exception(SWIG_RuntimeError, "unhandled argument type");
        }
    }
}

【问题讨论】:

标签: lua swig luajit


【解决方案1】:

您可以使用lua-compat-5.3lua_len 反向移植到Lua 5.1。如果你不想要所有这些,你可以通过将它内联到你的接口文件中来使用它的一部分。如果是lua_len,您需要

%{
static void lua_len (lua_State *L, int i) {
  switch (lua_type(L, i)) {
    case LUA_TSTRING:
      lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
      break;
    case LUA_TTABLE:
      if (!luaL_callmeta(L, i, "__len"))
        lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
      break;
    case LUA_TUSERDATA:
      if (luaL_callmeta(L, i, "__len"))
        break;
      /* FALLTHROUGH */
    default:
      luaL_error(L, "attempt to get length of a %s value",
                 lua_typename(L, lua_type(L, i)));
  }
}
%}

【讨论】:

  • 哇,这太棒了!非常感谢!
猜你喜欢
  • 2023-03-24
  • 2021-11-13
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
相关资源
最近更新 更多