gpt4 book ai didi

compilation - 如何使用 Elixir 宏获取模块的完整生成代码

转载 作者:行者123 更新时间:2023-12-04 11:03:15 28 4
gpt4 key购买 nike

以下是 Phoenix 应用程序示例的路由器模块。我想在宏注入(inject)函数后查看模块的完整代码。

我试过像 Macro.to_string (Macro.expand (Code.string_to_quoted! File.read!("web/router.ex")), __ENV__) 这样的东西但它并没有完全扩展宏。我如何递归地扩展每个宏,即 pipeline , plug , scope , pipe_throughget .

谢谢

defmodule HelloPhoenix.Router do
use HelloPhoenix.Web, :router

pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end

pipeline :api do
plug :accepts, ["json"]
end

scope "/", HelloPhoenix do
pipe_through :browser # Use the default browser stack

get "/", PageController, :index
end

end

最佳答案

这段代码会将一个梁文件反编译回erlang。我没有针对基于插件的模块运行它,所以我不知道结果会有多糟糕,但这会为您提供任何模块的最终结果。

  def disassemble(beam_file) do
beam_file = String.to_char_list(beam_file)
{:ok,
{_, [{:abstract_code, {_, ac}}]}} = :beam_lib.chunks(beam_file,
[:abstract_code])
:io.fwrite('~s~n', [:erl_prettypr.format(:erl_syntax.form_list(ac))])
end

原始来源,以及更多信息: http://erlang.org/doc/man/beam_lib.html

请注意,此方法采用 文件路径 [模块].beam

关于compilation - 如何使用 Elixir 宏获取模块的完整生成代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36017266/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com