Skip to content

Instantly share code, notes, and snippets.

@moteus moteus/registry.lua
Created Oct 19, 2017

Embed
What would you like to do?
Implementation registry in lua
local Registry = {} do
Registry.__index = Registry
function Registry:new()
return setmetatable({_r = {0}}, {__index = self})
end
function Registry:ref(value)
local registry = self._r
local ref = self._r[1]
if ref == 0 then
self._r[#self._r + 1] = value
return #self._r
end
self._r[1] = self._r[ref]
self._r[ref] = value
return ref
end
function Registry:unref(ref)
self._r[ref] = self._r[1]
self._r[1] = ref
end
function Registry:get(ref)
return self._r[ref]
end
function Registry:get_unref(ref)
local value = self._r[ref]
self:unref(ref)
return value
end
end
return Registry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.