【回転】lua tableのC APIについて
6268 ワード
//Lua - lua table C API
// yuliying CSDN .
//Lua 5.2
/* API:
====lua_createtable
: void lua_createtable (lua_State *L, int narr, int nrec);
: table .narr table ,nrec table hash .
table , ,lua , .
lua_newtable table.
====lua_newtable
: void lua_newtable (lua_State *L);
: table . lua_createtable(L, 0, 0).
====lua_getfield
: void lua_getfield (lua_State *L, int index, const char *k);
: t[k] push . t index table.
index .
====lua_setfield
: void lua_setfield (lua_State *L, int index, const char *k);
: table key . t[k] = v . t index table , v .
newindex .
(value).
====lua_gettable
: void lua_gettable (lua_State *L, int index);
: t[k] push . t index table,k .
index .
(key).
====lua_settable
: void lua_settable (lua_State *L, int index);
: table key . t[k] = v . t index table , v . k -2 .
newindex .
(key , value)
====lua_rawget
: void lua_rawget (lua_State *L, int index);
: lua_gettable , index .
====lua_rawset
: void lua_rawset (lua_State *L, int index);
: lua_settable , newindex .
====lua_rawgeti
: void lua_rawgeti (lua_State *L, int index, int n);
: t[n] push . t index table.
index .
====lua_rawseti
: void lua_rawseti (lua_State *L, int index, int n);
: table key . t[n] = v . t index table , v .
newindex .
.
====lua_rawgetp
: void lua_rawgetp (lua_State *L, int index, const void *p);
: t[p] push . t index table. p lightuserdata.
index .
====lua_rawsetp
: void lua_rawsetp (lua_State *L, int index, const void *p);
: table key . t[p] = v . t index table , p lightuserdata , v .
newindex .
.
====lua_getmetatable
: int lua_getmetatable (lua_State *L, int index);
: index push . , 0 , .
====lua_setmetatable
: void lua_setmetatable (lua_State *L, int index);
: index .
.
====lua_istable
: int lua_istable (lua_State *L, int index);
: index table , 1, 0.
====lua_pushglobaltable
: void lua_pushglobaltable (lua_State *L);
: lua .
====luaL_newmetatable
: int luaL_newmetatable (lua_State *L, const char *tname);
: tname key, 0.
table userdata . , tname key. 1.
.
====luaL_getmetatable
: void luaL_getmetatable (lua_State *L, const char *tname);
: tname key push .
====luaL_setmetatable
: void luaL_setmetatable (lua_State *L, const char *tname);
: , key tname.
====luaL_getsubtable
: int luaL_getsubtable (lua_State *L, int idx, const char *fname);
: t[fname] push , t index table , t[fname] table.
t[fname] , true , false, t[fname] .
====lua_getglobal
: void lua_getglobal (lua_State *L, const char *name);
: t[name] push , t .
====lua_setglobal
: void lua_setglobal (lua_State *L, const char *name);
: table key . t[name] = v . t . v .
(v).
====luaL_newlibtable
: void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);
: , lua .
luaL_setfuncs .
====luaL_setfuncs
: void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
: luaL_Reg table .
upvalue 0 , upvalue. -2 -(nup+1) upvalue.
( : upvalue c upvalue, lua upvalue, c lua_upvalueindex(n) .)
upvalue.
====luaL_newlib
: void luaL_newlib (lua_State *L, const luaL_Reg *l);
: table , luaL_Reg .
(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
====lua_next
: int lua_next (lua_State *L, int index);
: table.
key , push key-value ( key ) , .
table , 0.
nil , .
:
lua_pushnil(L); // first key
while (lua_next(L, t) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
printf("%s - %s
",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
: table , key , key lua_tolstring ,
key , lua_next .
====lua_rawlen
: size_t lua_rawlen (lua_State *L, int index);
: index .
, .
table , # . .
userdata , .
0.
====lua_len
: void lua_len (lua_State *L, int index);
: index # , .
*/
/* :
1. :
Lua , -1, . , 1, .
, , .
, lua_CFunction upvalue.
2. :
Lua table, c api .
LUA_REGISTRYINDEX .
lua_getfield "hello" key :
lua_getfield( L , LUA_REGISTRYINDEX , "hello");
3. upvalue:
lua_pushcfunction luaL_setfuncs lua_CFunction Lua ,
upvalue .
lua_CFunction lua_upvalueindex(n) upvalue.
*/