Created
July 18, 2012 15:37
-
-
Save hoelzro/3136989 to your computer and use it in GitHub Desktop.
Coroutine-local storage for Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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