** The previously freed references form a linked list: ** t[freelist] is the index of a first free index, or zero if list is ** empty; t[t[freelist]] is the index of the second element; etc. */
| 662 | ** empty; t[t[freelist]] is the index of the second element; etc. |
| 663 | */ |
| 664 | LUALIB_API int luaL_ref (lua_State *L, int t) { |
| 665 | int ref; |
| 666 | if (lua_isnil(L, -1)) { |
| 667 | lua_pop(L, 1); /* remove from stack */ |
| 668 | return LUA_REFNIL; /* 'nil' has a unique fixed reference */ |
| 669 | } |
| 670 | t = lua_absindex(L, t); |
| 671 | if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ |
| 672 | ref = 0; /* list is empty */ |
| 673 | lua_pushinteger(L, 0); /* initialize as an empty list */ |
| 674 | lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ |
| 675 | } |
| 676 | else { /* already initialized */ |
| 677 | lua_assert(lua_isinteger(L, -1)); |
| 678 | ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ |
| 679 | } |
| 680 | lua_pop(L, 1); /* remove element from stack */ |
| 681 | if (ref != 0) { /* any free element? */ |
| 682 | lua_rawgeti(L, t, ref); /* remove it from list */ |
| 683 | lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ |
| 684 | } |
| 685 | else /* no free elements */ |
| 686 | ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ |
| 687 | lua_rawseti(L, t, ref); |
| 688 | return ref; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { |
no test coverage detected