Generic GetParameters Function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Generic GetParameters Function

08 Nov 2013, 14:17

Is there a reliable way from inside a function I get a list of the parameters passed. In Python I would use their "introspection" features to get access to a hash table that contains the local parameters (the key is the name of the parameter, and the value is the value passed in). Is there any way to do this in AHK? I don't actually think there is, but thought I would ask anyway.

I know I could brute force it by looking at A_LineFile and A_ThisFunc to get access to the script (assuming it's not compiled), then parse out the parameter names and read them using %var%. I may go that route if there isn't an elegant solution, but I'm wondering if there is one that I'm missing.
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Generic GetParameters Function

08 Nov 2013, 16:31

What do you need this for? Reading the script file for parameters and such is only going to work as long as the script isn't compiled...

If you want a set of parameters passed as keys associated with values, why not just use an associative array?
http://l.autohotkey.net/docs/Objects.ht ... ive_Arrays

Code: Select all

foo({greeting:"Hello world", pi:3.1415, path:A_WorkingDir})

foo(params)
{
  for key,value in params
    Msgbox % "params[" key "] = " value
}
Also possibly related are variadic functions. The downside to these is that parameters aren't given "meaningful" names and instead each parameter's key is just a number.
http://l.autohotkey.net/docs/Functions.htm#Variadic

Code: Select all

foo("Hello world", 3.1415, A_WorkingDir)

foo(params*) 
{
  for key,value in params
    Msgbox % "params[" key "] = " value
}
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: Generic GetParameters Function

08 Nov 2013, 17:07

I want it for debugging and crash messages. For example, if I have a generic exception handler that takes any unhandled exception, and wraps it up with a bunch of debugging information. I'd like to be able to have the names and values of the parameters, not just variadics, but all the parameters passed to the function.

One of the things I like about Python (although overall I like AHK a lot better) is that all that kind of stuff is readily available via introspection. You can get the local variables and parameters for every function on the call stack, which comes in handy. Just hoping I can do a light version of that with AHK.
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Generic GetParameters Function

08 Nov 2013, 19:18

Well, AHK does have a debugger..viewing local variables as well as the stack should be possible. SciTE4AutoHotkey comes with support for it built in. The link and more info is in the manual here:
http://l.autohotkey.net/docs/AHKL_DBGPClients.htm
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Generic GetParameters Function

10 Nov 2013, 01:55

DBGP can only be used from a separate script or program.

You can probably do it with AutoHotkey.dll, which includes some functions for accessing low-level structures used by the interpreter. However, you can only access variables of the top-most instance of each function due to the way local variables work. This limitation also applies to DBGP.
I know I could brute force it by looking at A_LineFile and A_ThisFunc to get access to the script (assuming it's not compiled), then parse out the parameter names and read them using %var%.
In that case you would need to copy and paste the debugging code into each function, which sounds totally not worth doing.
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: Generic GetParameters Function

12 Nov 2013, 14:16

That all makes sense, and pretty much what I expected. I've been putting the code in the gives me the information I want with editor macros which are pretty efficient, but not very pretty. Maybe one of these days I'll add a bit of introspection capabilities to AHK, but for the moment I'll just carry on as I've been doing.

Thanks for the help.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Generic GetParameters Function

13 Nov 2013, 03:09

Wade Hatler wrote:Maybe one of these days I'll add a bit of introspection capabilities to AHK, but for the moment I'll just carry on as I've been doing.
That sounds like something I'd say, or might've said back when I wrote LowLevel.
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: Generic GetParameters Function

13 Nov 2013, 11:32

Didn't know about that. That's impressive work. I'll look at it a more closely.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 374 guests