Thread Tools Display Modes
04-20-22, 01:20 PM   #1
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
Question Equivalent of Lua's "debug" Global in WoW?

I've been hacking away for a week and a half while on vacation without testing things along the way. It's very time-consuming to test my addon as it requires you to be in a group and also queue into arena.

What I'm trying to do is make some slash commands that allow me to test the different modules in my addon without needing to fill the aforementioned criteria, instead just spoofing it and testing the actual WoW API calls later.

I wrote a function that uses Lua's built-in "debug" global to get the number of parameters my modules' functions have, but it appears this does not work in WoW's Lua implementation. Is there any other way to dynamically get the number of parameters a function takes without hard-coding the information into the modules? I just want to be able to abort the call if the required parameters aren't passed.
  Reply With Quote
04-21-22, 02:07 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
If you're talking about having a function check its args, you can use select().
Code:
local function PrintArgCount(...)
	print(select("#",...));
end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
04-21-22, 04:53 PM   #3
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
Arrow

I appreciate the reply! I don't think that's exactly what I'm looking for, but it might be close. Let me give an example.

Say I have a core file with some arbitrary function I may want to call from the chat box with a slash command.

Core.lua
Lua Code:
  1. local _, ns = ...
  2.  
  3. local ns.Core = {}
  4.  
  5. function ns.Core.DoSomething(x, y)
  6.     print(x * y)
  7. end

Say I also have a debug file with a function the slash command "/addon test" points to, as well as a function to dynamically call another function by providing the url/arguments for said function.

Debug.lua
Lua Code:
  1. local _, ns = ...
  2.  
  3. local ns.Debug = {}
  4.  
  5. function ns.Debug.Test(...)
  6.     -- url is a string representing the location of the function to call - e.g. "ns.Core.DoSomething"
  7.     -- args is a table containing the function's arguments' in order - e.g. { 2, 3 }
  8.     url, args = ...
  9.     callFunc(url, args)
  10. end
  11.  
  12. local function callFunc(url, args)
  13.     -- Traverse the pairs in each table in the url until you reach the end (the function field)
  14.     local funcVar = getFuncVar(url)
  15.    
  16.     -- Call the function
  17.     funcVar(args[1], args[2], args[3], ...)
  18. end

In game, I would just type "/addon test ns.core.dosomething ..." and depending on what is actually placed where the "..." appears, the function would either be called if it has all the arguments it needs or the call would be skipped and an error message would be printed to chat.

Line 15 in Debug.lua is where I'd like this check to be. In a standard Lua environment, there is a global called debug that lets you do this (calling debug.getinfo(funcVar).nparams would give me exactly what I need), but WoW's custom Lua implementation doesn't include this global.

One last thing, I have all the functionality of this already implemented except the ability to dynamically retrieve the number of arguments a function requires. I can call any of my addon's functions directly from the chat box right now, but if I don't pass the correct number of arguments, it throws an error.

Last edited by cosmic_kid : 04-21-22 at 05:23 PM.
  Reply With Quote
04-22-22, 10:11 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
As you've pointed out, the debug built-in library was disabled. Only a few functions remain from it, namely getfenv(), setfenv(), and debugstack() (formerly debug.traceback()).

There's no other way to externally query the number of parameters, especially since string.dump() was removed too. This would output the function's compiled code as a string (didn't work on C-defined functions).

Honestly, all argument validation should be done on the called function, not the caller. You can still use assert() and error() to throw custom errors and pcall()/xpcall() to catch them in your slash handler or do status/error returns and catch issues that way.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
04-22-22, 10:28 PM   #5
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
Thumbs up

Well it’s a little different than I was looking for, but it seems pcall/xpcall should do just fine. I just wanted some way to wrap the call and display my own dialog describing the error rather than let the in-game Lua error dialog pop. Thank you for the help!
  Reply With Quote
04-23-22, 11:27 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
xpcall() specifically lets you set a specific error handler to run if it encounters an error. Even though the Lua documentation doesn't show this, it also allows you to pass arguments to the function the same way pcall() does.

For example:
Lua Code:
  1. local function SomeErroringFunc(...)
  2.     print(...);
  3.     error("something bad happened");
  4.     print("something else");
  5. end
  6.  
  7. xpcall(SomeErroringFunc,print,1,2,3);
  8. print("returned from call");

Outputs:
1 2 3
something bad happened
returned from call
Note the error handler is called with the same string that was passed to error(). This also happens with assert().
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 04-23-22 at 11:33 AM.
  Reply With Quote
04-23-22, 04:35 PM   #7
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
Smile

Good stuff; this should help a bunch!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Equivalent of Lua's "debug" Global in WoW?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off