Skip to content

Instantly share code, notes, and snippets.

@scfmod
Created December 20, 2018 18:16
Show Gist options
  • Save scfmod/e9acd995584edf55b38d2c376d54f959 to your computer and use it in GitHub Desktop.
Save scfmod/e9acd995584edf55b38d2c376d54f959 to your computer and use it in GitHub Desktop.
Observe function calls
---@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