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
解决了这个问题。它们的工作方式几乎与 and 相同luaL_ref
,luaL_unref
只是它们将免费参考列表存储在单独的表中,l
用于list。(对于我的项目,我将它们放在单独的源文件中,并根据需要包含它们。我不建议修改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.