Lua 5.1 中的 lua_len() 替代方案是什么?



我刚刚为我的项目用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-compat-5.3将lua_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)));
}
}
%}

最新更新