Getting the source of anon func as a string?

Showing 1-7 of 7 messages
Getting the source of anon func as a string? Dmitri Shuralyov 9/2/13 2:57 AM
Say I have a button with a func() var for the press action.

button.action = func() { cmd.Process.Stop() }

I'd like to be able to get the anon func's source as a string so I can have it in a tooltip or something, in case I forget what the button does.

var actionSrc string
actionSrc := GetSourceAsString(button.action)
fmt.Printf("%q", actionSrc) // "func() { cmd.Process.Stop() }"

Any thoughts about how this can be done (other than manually copying the source of the anon func into a string)? From what I'm seeing "reflect" pkg can't help here, can it?

I'm currently thinking of parsing the program's source code and figuring it from there. runtime/debug's Stack() can be used to figure out the source code path. But the fact it's an anon func and not a named func in global scope makes getting its source from the AST somewhat trickier.
Re: [go-nuts] Getting the source of anon func as a string? Kyle Lemons 9/2/13 9:52 AM
There is no easy way to do what you ask.  My suggestion is to redefine action to be a `type ButtonAction struct { Tooltip string; Action func() }`.  I wouldn't copy/paste the source code, I'd just describe it.

button.action = ButtonAction{"Stop the process", cmd.Process.Stop}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: [go-nuts] Getting the source of anon func as a string? Dmitri Shuralyov 9/2/13 6:37 PM
Kyle, thanks, that's a very practical alternative solution, and anyone in their right mind would just do that.

However, for the sake of solving a fun challenge and being able to do something that I am currently unable to do, I'd like to try and solve the original problem anyway.

Some leads I got so far:

1. Parsing the .go file where the func is defined can give me the AST, and the anon func will be one of the *ast.FuncLit types. It might be tricky to figure out which one it is, if there are many of them and the assignment logic isn't trivial.

2. If I were to execute the anon func, it's possible to get its pc (program counter). e.g. func() { pc, _, _, _ := runtime.Caller(0); println(pc); ... } Then you can do file, line := runtime.FuncForPC(pc).FileLine(pc) to get the line where the func is defined. This can help greatly with finding the correct *ast.FuncLit in the AST.

Is there any way to find the pc of a given func entry point without resorting to calling it and doing runtime.Caller(0)?
Re: [go-nuts] Getting the source of anon func as a string? Steven Blenkinsop 9/2/13 11:52 PM
You can use reflect.Value.Pointer, but it won't necessarily work, specifically if your function value comes from a method expression.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Re: [go-nuts] Getting the source of anon func as a string? Dmitri Shuralyov 9/3/13 12:26 AM
Incredible! Thanks Steven, somehow I looked over Pointer() in "reflect" package.

It seems to do exactly what I want! Now I'm very close. I know which file and line the func literal is on. So unless there are more than 1 func declarations on the same line (not possible with gofmt'ed code anyway), it should be fairly straightforward to get it now! :D


Thank you again!
Re: [go-nuts] Getting the source of anon func as a string? Gerard 9/3/13 12:49 AM
This might also solve the problem (link). The goplay doesn't work because of the unsafe package.
Re: [go-nuts] Getting the source of anon func as a string? Dmitri Shuralyov 9/4/13 10:37 PM
Woohoo! I've been able to solve my original question and get the GetSourceAsString() func working! :D

Thanks to people who've replied, it wouldn't be possible without your help. Especially Steven's tip.

It will work only if the source file where the func was declared is present (and readable, which rules out this running on go playground, sadly; ioutil.ReadFile() of the source code file results in no permission error there). That's good enough for now.

Here's the working source:

https://gist.github.com/shurcooL/6418462
(GetLineStartEndIndicies() is defined here; FindFirst() here, SprintAst() here)

If you run the main() there (requires changing package name to "main"), the output is:

func() {
println("Hello from anon func!")
}

Here's a screenshot of it in action in my app... Now I won't forget what that temporary unlabelled button does hehe.


Thanks again!