我使用 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;
}