1

我使用 Lua 进行数组操作;数组是简单的二进制数据:

local ram_ctx = {0,0,0,0,0,0,0,0,0}

我想将它传递给用 C 编写的 GUI。问题是如果我像 func(ram_ctx) 一样直接传递它,Lua 函数似乎在调用后停止执行。不执行相应的 C 函数(可以为空)。但是如果我在 Lua 中创建全局数组并使用 lua_getglobal 访问它 - 一切似乎都很好。我做错了什么或者有可能吗?将数组名称作为参数传递以将其称为全局数组是不可行的

卢阿代码:

function device_init()
--set_callback(1000000000000, 0)
local array = {0xFF,0,0,0,0,0,0}
--create_memory_popup("Test")
set_memory_popup(array)
end

这是我尝试使用的 C 代码:

static int32_t lua_set_popup_memory (lua_State *L) 
{  
  int32_t argnum = lua_gettop(L);
  if (1 != argnum)
  {
    out_error("Function %s expects 1 argument got %d\n", __PRETTY_FUNCTION__, argnum);
    return 0;
  }  
if (0 == lua_istable(L, -1))
{
    out_log("No array found");
    return 0;
}
int32_t a_size = lua_rawlen(L, -1);
uint8_t *buf = calloc(1, a_size);
for (int i=1;;i++)
{    
    lua_rawgeti(L,-1, i);
    if (lua_isnil(L,-1)) 
        break;
    buf[i] = lua_tonumber(L,-1);
    lua_pop(L, 1);
}
set_popup_memory(memory_popup, 0, buf, a_size);
free(buf);
  return 0;  
}
4

1 回答 1

1

我怀疑是否有人可以在没有完整示例的情况下真正诊断出问题,但这是处理 Lua-to-C 调用的惯用方法以及对代码本身的一些评论:

static int // not int32_t
lua_set_popup_memory(lua_State *L)
{
    luaL_checktype(L, 1, LUA_TTABLE);
    // let alone excessive arguments (idiomatic), or do:
    lua_settop(L, 1);

    int a_size = lua_rawlen(L, 1); // absolute indexing for arguments
    uint8_t *buf = malloc((size_t)a_size);

    for (int i = 1; i <= a_size; i++) {
        lua_pushinteger(L, i);
        lua_gettable(L, 1); // always give a chance to metamethods
        // OTOH, metamethods are already broken here with lua_rawlen()
        // if you are on 5.2, use lua_len()

        if (lua_isnil(L, -1)) { // relative indexing for "locals"
            a_size = i-1; // fix actual size (e.g. 4th nil means a_size==3)
            break;
        }

        if (!lua_isnumber(L, -1)) // optional check
            return luaL_error(L, "item %d invalid (number required, got %s)",
                              i, luaL_typename(L, -1));

        lua_Integer b = lua_tointeger(L, -1);

        if (b < 0 || b > UINT8_MAX) // optional
            return luaL_error(L, "item %d out of range", i);

        buf[i-1] = b; // Lua is 1-based, C is 0-based
        lua_pop(L, 1);
    }

    set_popup_memory(memory_popup, 0, buf, a_size);
    free(buf);

    return 0;
}

请注意,它lua_CFunction被定义为int (*)(lua_State *),因此返回类型int32_t可能(并且很可能会)在非 32 位平台上引起问题。此外,原始代码可能溢出buf[i],因为 C 索引从零开始,而不是 1。还有一个更明显的问题:lua_rawlen()可能返回大于循环计数的索引(例如,带有零孔的数组),导致不需要的零被传递给set_popup_memory(假设 first-nil 方法优先于表长度)。

不确定out_error,使用 Lua 错误可能会提供更清晰的诊断,尤其是在使用 lua_pcall 的 traceback 参数调用入口点时。

此代码段实际上并未经过测试。

于 2014-09-22T02:33:31.330 回答