Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created July 18, 2012 15:37
Show Gist options
  • Select an option

  • Save hoelzro/3136989 to your computer and use it in GitHub Desktop.

Select an option

Save hoelzro/3136989 to your computer and use it in GitHub Desktop.
Coroutine-local storage for Lua
local true_storage = {}
tls = setmetatable({}, {
__mode = 'k',
__index = function(self, k)
local current = coroutine.running() or 'main'
local storage = true_storage[current]
if not storage then
storage = {}
true_storage[current] = storage
end
return storage[k]
end,
__newindex = function(self, k, v)
local current = coroutine.running() or 'main'
local storage = true_storage[current]
if not storage then
storage = {}
true_storage[current] = storage
end
storage[k] = v
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment