erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
13 KiB

4 years ago
  1. %% Copyright (c) 2007
  2. %% Mats Cronqvist <mats.cronqvist@ericsson.com>
  3. %% Chris Newcombe <chris.newcombe@gmail.com>
  4. %% Jacob Vorreuter <jacob.vorreuter@gmail.com>
  5. %%
  6. %% Permission is hereby granted, free of charge, to any person
  7. %% obtaining a copy of this software and associated documentation
  8. %% files (the "Software"), to deal in the Software without
  9. %% restriction, including without limitation the rights to use,
  10. %% copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. %% copies of the Software, and to permit persons to whom the
  12. %% Software is furnished to do so, subject to the following
  13. %% conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be
  16. %% included in all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. %% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. %% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. %% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. %% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. %% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. %% OTHER DEALINGS IN THE SOFTWARE.
  26. %%%-------------------------------------------------------------------
  27. %%% File : dynamic_compile.erl
  28. %%% Description :
  29. %%% Authors : Mats Cronqvist <mats.cronqvist@ericsson.com>
  30. %%% Chris Newcombe <chris.newcombe@gmail.com>
  31. %%% Jacob Vorreuter <jacob.vorreuter@gmail.com>
  32. %%% - add support for limit include-file depth (and prevent circular references)
  33. %%% prevent circular macro expansion set FILE correctly when -module() is found
  34. %%% -include_lib support $ENVVAR in include filenames
  35. %%% substitute-stringize (??MACRO)
  36. %%% -undef/-ifdef/-ifndef/-else/-endif
  37. %%% -file(File, Line)
  38. %%%-------------------------------------------------------------------
  39. -module(dynamic_compile).
  40. %% API
  41. -export([from_string/1, from_string/2]).
  42. -import(lists, [reverse/1, keyreplace/4]).
  43. %%====================================================================
  44. %% API
  45. %%====================================================================
  46. %%--------------------------------------------------------------------
  47. %% Function:
  48. %% Description:
  49. %% Returns a binary that can be used with
  50. %% code:load_binary(Module, ModuleFilenameForInternalRecords, Binary).
  51. %%--------------------------------------------------------------------
  52. from_string(CodeStr) ->
  53. from_string(CodeStr, []).
  54. % takes Options as for compile:forms/2
  55. from_string(CodeStr, CompileFormsOptions) ->
  56. %% Initialise the macro dictionary with the default predefined macros,
  57. %% (adapted from epp.erl:predef_macros/1
  58. Filename = "compiled_from_string",
  59. %%Machine = list_to_atom(erlang:system_info(machine)),
  60. Ms0 = dict:new(),
  61. % Ms1 = dict:store('FILE', {[], "compiled_from_string"}, Ms0),
  62. % Ms2 = dict:store('LINE', {[], 1}, Ms1), % actually we might add special code for this
  63. % Ms3 = dict:store('MODULE', {[], undefined}, Ms2),
  64. % Ms4 = dict:store('MODULE_STRING', {[], undefined}, Ms3),
  65. % Ms5 = dict:store('MACHINE', {[], Machine}, Ms4),
  66. % InitMD = dict:store(Machine, {[], true}, Ms5),
  67. InitMD = Ms0,
  68. %% From the docs for compile:forms:
  69. %% When encountering an -include or -include_dir directive, the compiler searches for header files in the following directories:
  70. %% 1. ".", the current working directory of the file server;
  71. %% 2. the base name of the compiled file;
  72. %% 3. the directories specified using the i option. The directory specified last is searched first.
  73. %% In this case, #2 is meaningless.
  74. IncludeSearchPath = ["." | reverse([Dir || {i, Dir} <- CompileFormsOptions])],
  75. {RevForms, _OutMacroDict} = scan_and_parse(CodeStr, Filename, 1, [], InitMD, IncludeSearchPath),
  76. Forms = reverse(RevForms),
  77. %% note: 'binary' is forced as an implicit option, whether it is provided or not.
  78. case compile:forms(Forms, CompileFormsOptions) of
  79. {ok, ModuleName, CompiledCodeBinary} when is_binary(CompiledCodeBinary) ->
  80. {ModuleName, CompiledCodeBinary};
  81. {ok, ModuleName, CompiledCodeBinary, []} when is_binary(CompiledCodeBinary) -> % empty warnings list
  82. {ModuleName, CompiledCodeBinary};
  83. {ok, _ModuleName, _CompiledCodeBinary, Warnings} ->
  84. throw({?MODULE, warnings, Warnings});
  85. Other ->
  86. throw({?MODULE, compile_forms, Other})
  87. end.
  88. %%====================================================================
  89. %% Internal functions
  90. %%====================================================================
  91. %%% Code from Mats Cronqvist
  92. %%% See http://www.erlang.org/pipermail/erlang-questions/2007-March/025507.html
  93. %%%## 'scan_and_parse'
  94. %%%
  95. %%% basically we call the OTP scanner and parser (erl_scan and
  96. %%% erl_parse) line-by-line, but check each scanned line for (or
  97. %%% definitions of) macros before parsing.
  98. %% returns {ReverseForms, FinalMacroDict}
  99. scan_and_parse([], _CurrFilename, _CurrLine, RevForms, MacroDict, _IncludeSearchPath) ->
  100. {RevForms, MacroDict};
  101. scan_and_parse(RemainingText, CurrFilename, CurrLine, RevForms, MacroDict, IncludeSearchPath) ->
  102. case scanner(RemainingText, CurrLine, MacroDict) of
  103. {tokens, NLine, NRemainingText, Toks} ->
  104. {ok, Form} = erl_parse:parse_form(Toks),
  105. scan_and_parse(NRemainingText, CurrFilename, NLine, [Form | RevForms], MacroDict, IncludeSearchPath);
  106. {macro, NLine, NRemainingText, NMacroDict} ->
  107. scan_and_parse(NRemainingText, CurrFilename, NLine, RevForms, NMacroDict, IncludeSearchPath);
  108. {include, NLine, NRemainingText, IncludeFilename} ->
  109. IncludeFileRemainingTextents = read_include_file(IncludeFilename, IncludeSearchPath),
  110. %%io:format("include file ~p contents: ~n~p~nRemainingText = ~p~n", [IncludeFilename,IncludeFileRemainingTextents, RemainingText]),
  111. %% Modify the FILE macro to reflect the filename
  112. %%IncludeMacroDict = dict:store('FILE', {[],IncludeFilename}, MacroDict),
  113. IncludeMacroDict = MacroDict,
  114. %% Process the header file (inc. any nested header files)
  115. {RevIncludeForms, IncludedMacroDict} = scan_and_parse(IncludeFileRemainingTextents, IncludeFilename, 1, [], IncludeMacroDict, IncludeSearchPath),
  116. %io:format("include file results = ~p~n", [R]),
  117. %% Restore the FILE macro in the NEW MacroDict (so we keep any macros defined in the header file)
  118. %%NMacroDict = dict:store('FILE', {[],CurrFilename}, IncludedMacroDict),
  119. NMacroDict = IncludedMacroDict,
  120. %% Continue with the original file
  121. scan_and_parse(NRemainingText, CurrFilename, NLine, RevIncludeForms ++ RevForms, NMacroDict, IncludeSearchPath);
  122. done ->
  123. scan_and_parse([], CurrFilename, CurrLine, RevForms, MacroDict, IncludeSearchPath)
  124. end.
  125. scanner(Text, Line, MacroDict) ->
  126. case erl_scan:tokens([], Text, Line) of
  127. {done, {ok, Toks, NLine}, LeftOverChars} ->
  128. case pre_proc(Toks, MacroDict) of
  129. {tokens, NToks} -> {tokens, NLine, LeftOverChars, NToks};
  130. {macro, NMacroDict} -> {macro, NLine, LeftOverChars, NMacroDict};
  131. {include, Filename} -> {include, NLine, LeftOverChars, Filename}
  132. end;
  133. {more, _Continuation} ->
  134. %% This is supposed to mean "term is not yet complete" (i.e. a '.' has
  135. %% not been reached yet).
  136. %% However, for some bizarre reason we also get this if there is a comment after the final '.' in a file.
  137. %% So we check to see if Text only consists of comments.
  138. case is_only_comments(Text) of
  139. true ->
  140. done;
  141. false ->
  142. throw({incomplete_term, Text, Line})
  143. end
  144. end.
  145. is_only_comments(Text) -> is_only_comments(Text, not_in_comment).
  146. is_only_comments([], _) -> true;
  147. is_only_comments([$ | T], not_in_comment) ->
  148. is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
  149. is_only_comments([$\t | T], not_in_comment) ->
  150. is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
  151. is_only_comments([$\n | T], not_in_comment) ->
  152. is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
  153. is_only_comments([$% | T], not_in_comment) -> is_only_comments(T, in_comment); % found start of a comment
  154. is_only_comments(_, not_in_comment) -> false;
  155. % found any significant char NOT in a comment
  156. is_only_comments([$\n | T], in_comment) -> is_only_comments(T, not_in_comment); % found end of a comment
  157. is_only_comments([_ | T], in_comment) -> is_only_comments(T, in_comment). % skipping over in-comment chars
  158. %%%## 'pre-proc'
  159. %%%
  160. %%% have to implement a subset of the pre-processor, since epp insists
  161. %%% on running on a file.
  162. %%% only handles 2 cases;
  163. %% -define(MACRO, something).
  164. %% -define(MACRO(VAR1,VARN),{stuff,VAR1,more,stuff,VARN,extra,stuff}).
  165. pre_proc([{'-', _}, {atom, _, define}, {'(', _}, {_, _, Name} | DefToks], MacroDict) ->
  166. false = dict:is_key(Name, MacroDict),
  167. case DefToks of
  168. [{',', _} | Macro] ->
  169. {macro, dict:store(Name, {[], macro_body_def(Macro, [])}, MacroDict)};
  170. [{'(', _} | Macro] ->
  171. {macro, dict:store(Name, macro_params_body_def(Macro, []), MacroDict)}
  172. end;
  173. pre_proc([{'-', _}, {atom, _, include}, {'(', _}, {string, _, Filename}, {')', _}, {dot, _}], _MacroDict) ->
  174. {include, Filename};
  175. pre_proc(Toks, MacroDict) ->
  176. {tokens, subst_macros(Toks, MacroDict)}.
  177. macro_params_body_def([{')', _}, {',', _} | Toks], RevParams) ->
  178. {reverse(RevParams), macro_body_def(Toks, [])};
  179. macro_params_body_def([{var, _, Param} | Toks], RevParams) ->
  180. macro_params_body_def(Toks, [Param | RevParams]);
  181. macro_params_body_def([{',', _}, {var, _, Param} | Toks], RevParams) ->
  182. macro_params_body_def(Toks, [Param | RevParams]).
  183. macro_body_def([{')', _}, {dot, _}], RevMacroBodyToks) ->
  184. reverse(RevMacroBodyToks);
  185. macro_body_def([Tok | Toks], RevMacroBodyToks) ->
  186. macro_body_def(Toks, [Tok | RevMacroBodyToks]).
  187. subst_macros(Toks, MacroDict) ->
  188. reverse(subst_macros_rev(Toks, MacroDict, [])).
  189. %% returns a reversed list of tokes
  190. subst_macros_rev([{'?', _}, {_, LineNum, 'LINE'} | Toks], MacroDict, RevOutToks) ->
  191. %% special-case for ?LINE, to avoid creating a new MacroDict for every line in the source file
  192. subst_macros_rev(Toks, MacroDict, [{integer, LineNum, LineNum}] ++ RevOutToks);
  193. subst_macros_rev([{'?', _}, {_, _, Name}, {'(', _} = Paren | Toks], MacroDict, RevOutToks) ->
  194. case dict:fetch(Name, MacroDict) of
  195. {[], MacroValue} ->
  196. %% This macro does not have any vars, so ignore the fact that the invocation is followed by "(...stuff"
  197. %% Recursively expand any macro calls inside this macro's value
  198. %% TODO: avoid infinite expansion due to circular references (even indirect ones)
  199. RevExpandedOtherMacrosToks = subst_macros_rev(MacroValue, MacroDict, []),
  200. subst_macros_rev([Paren | Toks], MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks);
  201. ParamsAndBody ->
  202. %% This macro does have vars.
  203. %% Collect all of the passe arguments, in an ordered list
  204. {NToks, Arguments} = subst_macros_get_args(Toks, []),
  205. %% Expand the varibles
  206. ExpandedParamsToks = subst_macros_subst_args_for_vars(ParamsAndBody, Arguments),
  207. %% Recursively expand any macro calls inside this macro's value
  208. %% TODO: avoid infinite expansion due to circular references (even indirect ones)
  209. RevExpandedOtherMacrosToks = subst_macros_rev(ExpandedParamsToks, MacroDict, []),
  210. subst_macros_rev(NToks, MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks)
  211. end;
  212. subst_macros_rev([{'?', _}, {_, _, Name} | Toks], MacroDict, RevOutToks) ->
  213. %% This macro invocation does not have arguments.
  214. %% Therefore the definition should not have parameters
  215. {[], MacroValue} = dict:fetch(Name, MacroDict),
  216. %% Recursively expand any macro calls inside this macro's value
  217. %% TODO: avoid infinite expansion due to circular references (even indirect ones)
  218. RevExpandedOtherMacrosToks = subst_macros_rev(MacroValue, MacroDict, []),
  219. subst_macros_rev(Toks, MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks);
  220. subst_macros_rev([Tok | Toks], MacroDict, RevOutToks) ->
  221. subst_macros_rev(Toks, MacroDict, [Tok | RevOutToks]);
  222. subst_macros_rev([], _MacroDict, RevOutToks) -> RevOutToks.
  223. subst_macros_get_args([{')', _} | Toks], RevArgs) ->
  224. {Toks, reverse(RevArgs)};
  225. subst_macros_get_args([{',', _}, {var, _, ArgName} | Toks], RevArgs) ->
  226. subst_macros_get_args(Toks, [ArgName | RevArgs]);
  227. subst_macros_get_args([{var, _, ArgName} | Toks], RevArgs) ->
  228. subst_macros_get_args(Toks, [ArgName | RevArgs]).
  229. subst_macros_subst_args_for_vars({[], BodyToks}, []) ->
  230. BodyToks;
  231. subst_macros_subst_args_for_vars({[Param | Params], BodyToks}, [Arg | Args]) ->
  232. NBodyToks = keyreplace(Param, 3, BodyToks, {var, 1, Arg}),
  233. subst_macros_subst_args_for_vars({Params, NBodyToks}, Args).
  234. read_include_file(Filename, IncludeSearchPath) ->
  235. case file:path_open(IncludeSearchPath, Filename, [read, raw, binary]) of
  236. {ok, IoDevice, FullName} ->
  237. {ok, Data} = file:read(IoDevice, filelib:file_size(FullName)),
  238. file:close(IoDevice),
  239. binary_to_list(Data);
  240. {error, Reason} ->
  241. throw({failed_to_read_include_file, Reason, Filename, IncludeSearchPath})
  242. end.