luaL_ref
和luaL_unref
函數在lauxlib.c
中定義。他們通過跟蹤他們存儲在他們正在操作的表中的免費參考列表來工作。這些功能相對較短,所以我將把它們包括在這裏。
LUALIB_API int luaL_ref (lua_State *L, int t) {
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 */
ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
lua_pop(L, 1); /* remove it from stack */
if (ref != 0) { /* any free element? */
lua_rawgeti(L, t, ref); /* remove it from list */
lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
}
else { /* no free elements */
ref = (int)lua_objlen(L, t);
ref++; /* create new reference */
}
lua_rawseti(L, t, ref);
return ref;
}
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
if (ref >= 0) {
t = abs_index(L, t);
lua_rawgeti(L, t, FREELIST_REF);
lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
lua_pushinteger(L, ref);
lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
}
}
這些功能非常聰明,因爲它們不需要額外的存儲空間(除了它們正在運行的表格之外)。但是,將自由引用列表存儲在與引用值相同的表中,例如在需要迭代引用的值時,偶爾會不受歡迎。
我寫了luaX_ref
和luaX_unref
來解決這個問題。他們的工作幾乎與luaL_ref
和luaL_unref
相同,只是他們將免費參考列表存儲在單獨的表中,l
對於列表。 (我的項目,我在一個單獨的源文件把這些並在必要時已包括他們,我不建議修改lauxlib.c
。)
static int abs_index(lua_State * L, int i){
return i > 0 || i <= LUA_REGISTRYINDEX ? i : lua_gettop(L) + i + 1;
}
LUALIB_API int luaX_ref(lua_State *L, int t, int l){
int ref;
t = abs_index(L, t);
l = abs_index(L, l);
if(lua_isnil(L, -1)){
lua_pop(L, 1); /* remove from stack */
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
}
lua_rawgeti(L, l, FREELIST_REF); /* get first free element */
ref = (int) lua_tointeger(L, -1); /* ref = l[FREELIST_REF] */
lua_pop(L, 1); /* remove it from stack */
if(ref != 0){ /* any free element? */
lua_rawgeti(L, l, ref); /* remove it from list */
lua_rawseti(L, l, FREELIST_REF); /* (l[FREELIST_REF] = l[ref]) */
}else{ /* no free elements */
ref = (int)lua_objlen(L, l);
ref++; /* create new reference */
}
lua_pushboolean(L, 1);
lua_rawseti(L, l, ref); /* l[ref] = true */
lua_rawseti(L, t, ref); /* t[ref] = value */
return ref;
}
LUALIB_API void luaX_unref(lua_State *L, int t, int l, int ref){
if(ref >= 0){
t = abs_index(L, t);
l = abs_index(L, l);
lua_rawgeti(L, l, FREELIST_REF);
lua_rawseti(L, l, ref); /* l[ref] = l[FREELIST_REF] */
lua_pushinteger(L, ref);
lua_rawseti(L, l, FREELIST_REF); /* l[FREELIST_REF] = ref */
lua_pushnil(L);
lua_rawseti(L, t, ref); /* t[ref] = nil */
}
}
現在看到的用法:
lua_newtable(L); /* 1 */
lua_newtable(L); /* 2 */
lua_pushboolean(L, 0);
int ref1 = luaX_ref(L, 1, 2);
lua_pushinteger(L, 7);
int ref2 = luaX_ref(L, 1, 2);
lua_pushstring(L, "test");
int ref3 = luaX_ref(L, 1, 2);
tableDump(L, 1);
tableDump(L, 2);
luaX_unref(L, 1, 2, ref1);
tableDump(L, 1);
tableDump(L, 2);
luaX_unref(L, 1, 2, ref3);
tableDump(L, 1);
tableDump(L, 2);
luaX_unref(L, 1, 2, ref2);
tableDump(L, 1);
tableDump(L, 2);
printf("done.\n");
輸出:
1: false, 2: 7, 3: `test',
1: true, 2: true, 3: true,
2: 7, 3: `test',
3: true, 2: true, 0: 1,
2: 7,
3: 1, 2: true, 0: 3,
3: 1, 2: 3, 0: 2,
done.
我使用的是Lua 5.1。 – tprk77
停止濫用API。 'luaL_ref'和'luaL_unref'的目的是*不*填充表格。這是創建一個簡單的參考系統,可以通過引用存儲值並稍後再次獲取它們。你不應該在表格上迭代;你應該使用返回的引用來訪問表條目。 –