Lua 5.1中的lua_len()替代品是什么? [英] What is lua_len() alternative in Lua 5.1?

查看:755
本文介绍了Lua 5.1中的lua_len()替代品是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我刚用LuaJIT替换了Lua,但出现了错误提示

I just replaced Lua with LuaJIT for my project and I got the error saying

Use of undeclared identifier 'lua_len'

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

How can I change lua_len so it is compatible with Lua 5.1 and LuaJIT?

这是我的代码,该代码使用SWIG绑定中的lua_len. (以防万一)

Here's my code that uses lua_len from the SWIG binding. (In case it helps)

%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_len反向移植到Lua 5.1. ="nofollow noreferrer"> lua-compat-5.3 .如果您不想要所有这些,则可以通过将其内联到接口文件中来使用它的一部分.如果是lua_len,则需要

You can backport lua_len to Lua 5.1 by using lua-compat-5.3. If you don't want all of that, you can just use part of it by inlining it into your interface file. In case of lua_len you need

%{
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)));
  }
}
%}

这篇关于Lua 5.1中的lua_len()替代品是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆