diff options
Diffstat (limited to 'src/lua51/lauxlib.c')
-rw-r--r-- | src/lua51/lauxlib.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/lua51/lauxlib.c b/src/lua51/lauxlib.c index c6ec9b0..83fc2ac 100644 --- a/src/lua51/lauxlib.c +++ b/src/lua51/lauxlib.c @@ -24,7 +24,7 @@ #include "lauxlib.h" - +//c t[FREELIST_REF] 保存可以用luaL_ref引用的索引号 #define FREELIST_REF 0 /* free list of references */ @@ -240,7 +240,7 @@ static int libsize (const luaL_Reg *l) { LUALIB_API void luaI_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { + const luaL_Reg *l, int nup) { // nup number of upvalues if (libname) { int size = libsize(l); /* check whether lib already exists */ @@ -478,14 +478,18 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { /* }====================================================== */ +//c t是目标表的Idx,数据在-1 LUALIB_API int luaL_ref (lua_State *L, int t) { +// 数据在栈顶 + + // 1. 拿到ref号 int ref; t = abs_index(L, t); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* remove from stack */ return LUA_REFNIL; /* `nil' has a unique fixed reference */ } - lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ // table的[FREELIST_REF]位置会保存可用的引用索引号,FREELIST_REF = 0 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ lua_pop(L, 1); /* remove it from stack */ if (ref != 0) { /* any free element? */ @@ -496,7 +500,10 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { ref = (int)lua_objlen(L, t); ref++; /* create new reference */ } + + // 2. t[ref] = o 引用 lua_rawseti(L, t, ref); + return ref; } |