-
-
Save creationix/94d8bd5e815dcbfa98fe to your computer and use it in GitHub Desktop.
This file contains 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
function utils.bind(fn, self, ...) | |
local argsLength = select("#", ...) | |
if argsLength == 0 then | |
return function (...) | |
return fn(self, ...) | |
end | |
else | |
-- This table will get mutated on every call, but it's async safe and the | |
-- unpack fence will only use the fresh data so it's ok. | |
local args = {...} | |
return function (...) | |
local extraLength = select("#", ...) | |
local extra = {...} | |
for i = 1, extraLength do | |
args[i + argsLength] = extra[i] | |
end | |
return fn(self, unpack(args, 1, argsLength + extraLength)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment