Is it possible to save the complete "path" to a Lua table field in a C function? -


if have global object in lua this:

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 time global_object.stat_group_1.stat_2 = 2 -- value changes time global_object.stat_group_1.stat_3 = 3 -- value changes time -- ... , same thing other stat_groups 

my question lual_ref, lua_rawgeti, , lua_getfield. can use lual_ref save path each stat avoid explicitly calling of them on stack, this:

int global_object_ref; int stat_group_1_ref; int stat_1_ref;  //assume function has been called before of 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 prefered option. possible int get_stat1_v1 ( lua_state * l ) {     //stat_1 can have different values in lua table @ different moments     lua_rawgeti ( l, lua_registryindex, stat_1_ref );      //is value of field stat_1?     int value_of_stat_1 = lua_tointeger ( l, -1 );      return 1; }  //this alternative, in case v1 doesn't work. work? //again, remember stat_1 can have different values @ 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 value of field stat_1?     int value_of_stat_1 = lua_tointeger ( l, -1 );      return 1; } 

note v2 calls of saved references onto stack. work?

edit: in light of answer @nicol bolas, want propose v3. if table , sub-tables never garbage collected, values updated (imagine whole structure of sub-tables tree, each sub-table branch, each fundamental value leaf. structure of tree remains same during execution, leaves updated).

//this v3, learned how lua registry interacts c, , propose //a direct leaf access, indirect branch access. int get_stat1_v3 ( lua_state * l ) {     //can skip line bellow , go directly next? or have     //to follow whole hierarchy of branches?     lua_rawgeti ( l, lua_registryindex, global_object_ref );//is needed?      //i know i'm repeating myself, want know if call above     //is necessary or not. can directly cut chase calling function?     lua_rawgeti ( l, lua_registryindex, stat_group_1_ref );      //notice work i'd have removed reference stat_1     //from registry of proven flawed implementation, wouldn't     //freeze , pin down value of stat_1, means stat_1_ref gets     //removed code.     lua_getfield ( l, -1, "stat_1" );      //is dynamically date value of field stat_1?     int value_of_stat_1 = lua_tointeger ( l, -1 );      return 1; } 

you this, you'd giving form of garbage collection. registry part of lua state, long tables in registry, must exist. objects referenced them exist until deregister them or close lua state.

you aren't saving "paths". you're saving actual value stored in tables @ locations. what's stored in registry not updated if table's value change. saving value of stat_1 saved value has, not value may change to.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -