This probably should be more of a comment to @Dogbert’s answer above, but I would post it as a separate answer for the sake of formatting.
One does not need to create .ex files and invoke the compiler on them to produce beams:
{:module, _, bytecode, _} =
defmodule Elixir.Test do
def t1(a), do: a
def t1(a, b \\ 2), do: a + b
end
# File.write!("Elixir.Test.beam", bytecode)
now you might have had a beam file written (we have it stored in the bytecode variable by the way.)
NB: beam_lib:chunks/2 works if and only the beam contains unencrypted debug information (elixir beams by default do.)
Also, you don’t need to write decompiled erlang code, you might simply pass a binary there, directly in Elixir:
:beam_lib.chunks(bytecode, [:abstract_code])
To extract the code itself:
{:ok,{_,[abstract_code: {_, code}]}} =
bytecode |> :beam_lib.chunks([:abstract_code])
Now code contains the code, it should be enough to examine it, but you still are free to use erlang build-ins:
code |> :erl_syntax.form_list
or:
code |> :erl_syntax.form_list |> :erl_prettypr.format
The latter will give you the binary charlist, containing erlang code, exactly as in @Dogbert’s answer. Use IO.puts to output it.
aonly, you'll have to pass a0as the second parameter. If you don't pass a second parameter, you'll geta + 2- Onorio CatenacciTest.t1(0)the output is0, but if you callTest.t1(0, 4)then the outcome is4. Basically everytime you call the function with one argument you end up invoking the first one. Btw, I'm on elixir 1.3.4 - ipinak