32 lines
728 B
Elixir
32 lines
728 B
Elixir
defmodule BeamJs.Module do
|
|
import BeamJs.Utils
|
|
|
|
alias BeamJs.Function
|
|
|
|
def to_ast(module, opts \\ []) do
|
|
function_map =
|
|
module
|
|
|> BeamFile.byte_code()
|
|
|> ok(fn {_, _, _, _, _, sections} ->
|
|
{:ok, Enum.filter(sections, fn
|
|
{:function, _fun_name, _arity, _, _code} ->
|
|
true
|
|
_other ->
|
|
false
|
|
end)}
|
|
end)
|
|
|> ok(fn functions ->
|
|
Enum.map(functions, fn {:function, name, arity, _, code} ->
|
|
{"#{name}$#{arity}", Function.to_ast(code)}
|
|
end)
|
|
|> Map.new()
|
|
end)
|
|
|
|
case Keyword.get(opts, :only) do
|
|
nil ->
|
|
function_map
|
|
only ->
|
|
Map.take(function_map, only)
|
|
end
|
|
end
|
|
end
|