luby/crates/luaffi/src/lib.lua
2025-06-23 09:27:15 +10:00

25 lines
526 B
Lua

local LUA_REFNIL = -1 -- lib_aux.c
local FREELIST_REF = 0
local function __ref(value, t)
if value == nil then return LUA_REFNIL end
if t == nil then t = __registry end
local ref = t[FREELIST_REF]
if ref ~= nil and ref ~= 0 then
t[FREELIST_REF] = t[ref]
else
ref = #t + 1
end
t[ref] = value
return ref
end
local function __unref(ref, t)
if ref < 0 then return nil end
if t == nil then t = __registry end
local value = t[ref]
t[ref] = t[FREELIST_REF]
t[FREELIST_REF] = ref
return value
end