[erlang-questions] Getting code from closures

Thomas Lindgren <>
Tue Dec 3 20:10:26 CET 2013


OK, Ulf, I'll have a look at that. It looks like there probably will be a parse transform involved somewhere.

Best,
Thomas



On Tuesday, December 3, 2013 4:28 PM, Ulf Wiger <> wrote:
 

>The ct_expand transform has a variation of this, where it also ‘funnifies’ local functions, in order to be able to evaluate them at compile-time.
>
>The locks_watcher also has a simple inlining transform that transforms a ‘pseudo-function’ into an abstract expression list that can be evaluated without the need of compiled code. It chases down calls to local functions and transforms those functions into funs inside the expression list.
>
>https://github.com/uwiger/locks/blob/master/src/locks_watcher.erl#L109
>
>BR,
>Ulf W
>
>
>On 3 Dec 2013, at 14:02, Richard Carlsson <> wrote:
>
>> On 2013-12-03 13:14 , Thomas Lindgren wrote:
>>> Hi guys,
>>> 
>>> Before I start hacking on something, I thought I'd ask whether there
>>> already is a standard way to get the original code from a compiled
>>> fun/closure. (I know you can get it for interpreted funs by using fun_info.)
>>> 
>>> E.g., for code like this:
>>> 
>>> -module(a).
>>> -compile(export_all).
>>> adder(N) -> fun(X) X+N end.
>>> 
>>> I'd like to be able to write something like this:
>>> 
>>> > c(a), F =
 a:adder(3), magic:get_code(F).
>>> {fun, 0, [{clause, 0, [{var, 0, 'X'}], [], [{op, '+', {var,
>>> 0,'X'},{var,0,'N'}}]}], [{'N', 3}]}  %% AST of fun F with free var env
>>> 
>>> Or the equivalent.
>> 
>> Use fun_info to get the name/arity of the generated function and dig it out from the debug_info AST (or for bonus points, reverse-compile the beam code)?
>> 
>> Keep in mind that if the code in the fun contains a local call to a function in the same module, you'll need to include the code for all reachable functions as well. Hence, the whole AST or the .beam file as a binary might be more suitable for whatever it is you're doing.
>> 
>>   /Richard
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
>http://feuerlabs.com
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131203/dada528f/attachment.html>


More information about the erlang-questions mailing list