是否可以将完整的“路径”保存到C函数中的Lua表字段?

时间:2017-04-08 00:18:33

标签: c lua

如果我在Lua中有一个全局对象:

global_object = { }
global_object.stat_group_1 = { }
global_object.stat_group_2 = { }
global_object.stat_group_3 = { }
global_object.stat_group_1.stat_1 = 1 -- value changes with time
global_object.stat_group_1.stat_2 = 2 -- value changes with time
global_object.stat_group_1.stat_3 = 3 -- value changes with time
-- ... and same thing for other stat_groups

我的问题是关于luaL_reflua_rawgetilua_getfield。我可以使用luaL_ref来保存每个stat的路径,以避免在堆栈上显式调用它们,如下所示:

int global_object_ref;
int stat_group_1_ref;
int stat_1_ref;

//assume this function has been called before any of the get_* functions
int start ( lua_State * L )
{
    lua_getfield ( L, LUA_RIDX_GLOBALS, "global_object" );
    lua_pushvalue ( L, -1 );
    global_object_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_group_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    return 0;
}

//this is the prefered option. I would like this to be possible
int get_stat1_v1 ( lua_State * L )
{
    //stat_1 can have different values in the Lua table at different moments
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}

//this is an alternative, in case v1 doesn't work. Would this work?
//again, remember stat_1 can have different values at different moments.
int get_stat1_v2 ( lua_State * L )
{
    lua_rawgeti ( L, LUA_REGISTRYINDEX, global_object_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_group_1_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}

请注意v2将所有已保存的引用调用到堆栈中。这有用吗?

编辑:根据@Nicol Bolas的回答,我想提出一个v3。如果表和子表永远不会被垃圾收集,但它们的值会不断更新(想象一下子表的整个结构为树,每个子表都有一个分支,每个基本值都是一个叶子。树的结构仍然存在在执行期间相同,但叶子得到更新)。

//this is the v3, where I learned how the Lua registry interacts with C, and propose
//a direct leaf access, but indirect branch access.
int get_stat1_v3 ( lua_State * L )
{
    //can I skip the line bellow and go directly to the next? Or do I have
    //to follow the whole hierarchy of branches?
    lua_rawgeti ( L, LUA_REGISTRYINDEX, global_object_ref );//is this needed?

    //I know I'm repeating myself, but I want to know if the call above
    //is necessary or not. Can I directly cut to the chase by calling this function?
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_group_1_ref );

    //Notice that for this to work I'd have to removed the reference to stat_1
    //from the registry of my proven flawed implementation, so I wouldn't
    //freeze and pin down the value of stat_1, which means stat_1_ref gets
    //removed from the code.
    lua_getfield ( L, -1, "stat_1" );

    //is this the dynamically up to date value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}

1 个答案:

答案 0 :(得分:2)

你可以这样做,但你基本上放弃了任何形式的垃圾收集。注册表是Lua状态的一部分,因此只要这些表在注册表中,它们就必须存在。因此,在您取消注册它们或关闭Lua状态之前,它们引用的任何对象都将存在。

你并没有真正拯救"路径"。您将保存存储在这些位置的表中的实际值。因此,如果表的值发生变化,则不会更新存储在注册表中的内容。保存stat_1已保存的值将是当前的值,而不是它可能更改的值。