Jump to content
Toggle menu
  • 17.3K articles
  • 8.2K files
  • 5 users
  • 280.7K edits
WoW Classic Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

HOWTO: Hook functions in a safer way

From WoW Classic Wiki

Template:Band-red


This HOWTO discusses parameter and return value handling in a safer-than-usual way.

For more information on the actual hooking of functions, see HOWTO: Hook a Function.


How you usually do it

Meet Joe Average Hook:

 local Orig_TheFunction = TheFunction;

 function TheFunction(param1, param2)

   -- some code that looks at param1
   local retval = Orig_TheFunction(param1, param2)
   -- some more code that looks at retval

   return retval
 end

Now, there's three things that could happen:

  • This could work just fine
  • You think it works just fine, but something you yourself don't normally use just stopped working. (Ever installed HoB_DKP and have ctrl+click names in the chat window stop working, for instance?)
  • It works just fine now, but two patches from now, it doesn't.


The problem

If you miss out on passing an argument, or miss returning a value (remember that Lua functions can return multiple values!), things break. Luckily, there's ways to guarantee that it'll work, both today, and in the future!

Red exclamation mark iconBlizzard's API:s and FrameXML calls do change from time to time!


The safer way to do it

 local Orig_TheFunction = TheFunction;

 function TheFunction(...)

   -- some code that looks at arg[1]
   local rettab = { Orig_TheFunction(unpack(arg)) }
   -- some code that looks at rettab[1]

   return unpack(rettab);
 end

What you're doing is passing in all parameters as a table, arg. All returned values end up in the table rettab. We can access both the parameters and returned value(s) just fine in the hook, but since we no longer assume what parameters and returned values will look like, they can grow in the future without the hook breaking them!


Won't there be a huge performance impact?

This creates quite a bit more garbage than managing arguments yourself, yes. If it's a seldom-used hook, it won't matter much. But you certainly do not want to e.g. hook ChatFrame_OnEvent() with this kind of table-based parameter management.

An alternative is to simply append a few extra variables after the ones you know of, but note that some WoW APIs actually count the number of arguments passed (even if they're nil) and behave differently based on the count.


Mixing the styles

This of course works too; now you just have to worry about not typoing the parameter names that you do keep, just like you do with the regular hooking style:

 function TheFunction(param1, ...)

   -- some code that looks at param1
   local rettab = { Orig_TheFunction(param1, unpack(arg)) }
   -- some code that looks at rettab[1]

   return unpack(rettab);
 end

And, no, you can't mix return vales, really. You could copy the first few out of the rettab and tremove() them, and then recombine them when you're returning out of the function, but that's just more work. If you need to modify return values, you can do it directly in e.g. rettab[1].