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. |
If you're talking about having a function check its args, you can use select().
Code:
local function PrintArgCount(...) |
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:
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:
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. |
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. |
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!
|
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:
Outputs: Quote:
|
Good stuff; this should help a bunch!
|
All times are GMT -6. The time now is 07:55 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI