Selaa lähdekoodia

rf: beamToSrc函数调整

master
SisMaker 4 vuotta sitten
vanhempi
commit
50baa78a1c
3 muutettua tiedostoa jossa 51 lisäystä ja 43 poistoa
  1. +51
    -0
      src/dynamicCompile/utBeamToSrc.erl
  2. +0
    -29
      src/dynamicCompile/utDecompile.erl
  3. +0
    -14
      src/dynamicCompile/utKvsToBeam.erl

+ 51
- 0
src/dynamicCompile/utBeamToSrc.erl Näytä tiedosto

@ -0,0 +1,51 @@
-module(utBeamToSrc).
-export([
genSrc/2
, genSrcs/2
]).
%% beam生成erl文件beam编译选项必要带debug_info才能反编译生成代码
genSrc(Module, SrcDir) ->
case beam_lib:chunks(code:which(Module), [abstract_code]) of
{ok, {_, [{abstract_code, {_, AC}}]}} ->
Code = erl_prettypr:format(erl_syntax:form_list(AC)),
%% unicode码
%% SrcStr = io_lib:fwrite("~ts~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
SrcBin = unicode:characters_to_binary(Code),
file:write_file(lists:concat([SrcDir, Module, ".erl"]), SrcBin),
io:format("build beam:~p to erl:~p success.~n", [Module, Module]);
{error, beam_lib, Reason} ->
io:format("code_gen_erl_file error, reason:~p~n", [Reason]);
_Err ->
io:format("code_gen_erl_file error, reason:~p~n", [_Err])
end.
%% beam生成erl文件beam编译选项必要带debug_info才能反编译生成代码
genSrcs(BeamDir, SrcDir) ->
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".beam" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
ModName = filename:basename(File, ".beam"),
Module = list_to_atom(ModName),
case beam_lib:chunks(code:which(Module), [abstract_code]) of
{ok, {_, [{abstract_code, {_, AC}}]}} ->
Code = erl_prettypr:format(erl_syntax:form_list(AC)),
%% unicode码
%% SrcStr = io_lib:fwrite("~ts~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
SrcBin = unicode:characters_to_binary(Code),
file:write_file(lists:concat([SrcDir, Module, ".erl"]), SrcBin),
io:format("build beam:~p to erl:~p success.~n", [Module, Module]);
{error, beam_lib, Reason} ->
io:format("code_gen_erl_file error, reason:~p~n", [Reason]);
_Err ->
io:format("code_gen_erl_file error, reason:~p~n", [_Err])
end,
ProAcc;
_ ->
ProAcc
end
end,
filelib:fold_files(BeamDir, "\\.beam$", true, FunRead, []).

+ 0
- 29
src/dynamicCompile/utDecompile.erl Näytä tiedosto

@ -1,29 +0,0 @@
-module(utDecompile).
-export([decCom/2]).
decCom(BeamDir, SrcDir) ->
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".beam" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
ModName = filename:basename(File, ".beam"),
Module = list_to_atom(ModName),
case beam_lib:chunks(code:which(Module), [abstract_code]) of
{ok, {_, [{abstract_code, {_, AC}}]}} ->
%% Code = erl_prettypr:format(erl_syntax:form_list(AC)),
SrcStr = io_lib:fwrite("~ts~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
file:write_file(lists:concat([SrcDir, Module, ".erl"]), SrcStr),
io:format("build beam:~p to erl:~p success.~n", [Module, Module]);
{error, beam_lib, Reason} ->
io:format("code_gen_erl_file error, reason:~p~n", [Reason]);
_Err ->
io:format("code_gen_erl_file error, reason:~p~n", [_Err])
end,
ProAcc;
_ ->
ProAcc
end
end,
filelib:fold_files(BeamDir, "\\.beam$", true, FunRead, []).

+ 0
- 14
src/dynamicCompile/utKvsToBeam.erl Näytä tiedosto

@ -2,7 +2,6 @@
-export([
load/2
, beamToSrc/1
]).
%% map类型的数据不能当做key
@ -41,16 +40,3 @@ lookup_clauses([], Acc) ->
lists:reverse(lists:flatten([lookup_clause_anon() | Acc]));
lookup_clauses([{Key, Value} | T], Acc) ->
lookup_clauses(T, [lookup_clause(Key, Value) | Acc]).
%% beam生成erl文件beam编译选项必要带debug_info才行
beamToSrc(Module) ->
case beam_lib:chunks(code:which(Module), [abstract_code]) of
{ok, {_, [{abstract_code, {_, AC}}]}} ->
Code = erl_prettypr:format(erl_syntax:form_list(AC)),
file:write_file(lists:concat([Module, ".erl"]), list_to_binary(Code)),
io:format("build beam:~p to erl:~p success.~n", [Module, Module]);
{error, beam_lib, Reason} ->
io:format("code_gen_erl_file error, reason:~p~n", [Reason]);
_Err ->
io:format("code_gen_erl_file error, reason:~p~n", [_Err])
end.

Ladataan…
Peruuta
Tallenna