Created
December 20, 2018 18:16
-
-
Save scfmod/e9acd995584edf55b38d2c376d54f959 to your computer and use it in GitHub Desktop.
Observe function calls
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
---@param fSrc function | |
---@param fDst function | |
---@param fResultCallback function | |
function hook(fSrc, fDst, fResultCallback) | |
return function(...) | |
--DebugUtil.printCallingFunctionLocation() | |
if type(fResultCallback) == 'function' then | |
if type(fDst) == 'function' then | |
fDst(...) | |
end | |
return fResultCallback(fSrc(...)) | |
else | |
if type(fDst) == 'function' then | |
fDst(...) | |
end | |
return fSrc(...) | |
end | |
end | |
end | |
local original_function = ConfigurationManager.addConfigurationType | |
-- Only observe function arguments | |
ConfigurationManager.addConfigurationType = hook(original_function, function(...) | |
print('Arguments:') | |
print_r(...) | |
end) | |
-- Observe function arguments and return value from original function | |
ConfigurationManager.addConfigurationType = hook(original_function, function(...) | |
print('Arguments:') | |
print_r(...) | |
end, function(...) | |
print('Function result:') | |
print_r(...) | |
return ... -- IMPORTANT! pass return value(s) back to calling function | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment