Parcourir la source

动态编译相关添加

master
lijie il y a 5 ans
committed by AICells
Parent
révision
3e1db79e46
5 fichiers modifiés avec 209 ajouts et 7 suppressions
  1. +1
    -1
      src/dynamicCompile/dynamic_compile.erl
  2. +3
    -3
      src/dynamicCompile/dynamic_kvs.erl
  3. +3
    -3
      src/dynamicCompile/meta.erl
  4. +57
    -0
      src/dynamicCompile/utKvsToBeam.erl
  5. +145
    -0
      src/testCase/utTestAtomics.erl

+ 1
- 1
src/dynamicCompile/dynamic_compile.erl Voir le fichier

@ -54,7 +54,7 @@
-import(lists, [reverse/1, keyreplace/4]).
-export([module_src/1, module_lt/1, modules/0]).
-export([module_src/1, module_lt/1, modules/0, module_new/2]).
-record(module, {name = 0, loadtime = 0, src}).

+ 3
- 3
src/dynamicCompile/dynamic_kvs.erl Voir le fichier

@ -10,7 +10,7 @@
-export([start/1, start/0, get_name/1, init/1, handle_call/3, new/1, new/2]).
%% API
-export([set/3]).
-export([set/3, gen/2]).
-record(dynamic_kvs, {
modules = [] :: [{Mod :: module(), [{Key :: atom(), Val :: any()}]}]
@ -53,7 +53,7 @@ handle_call({set, ModuleName, Key, Val}, _, DynamicKvs) ->
false ->
{reply, {error, not_find_module}, DynamicKvs};
{value, {_, Kvs}, RemainMods} ->
NewKvs = com_lists:keyreplace(Key, 1, Kvs, {Key, Val}),
NewKvs = lists:keyreplace(Key, 1, Kvs, {Key, Val}),
gen(ModuleName, NewKvs),
{reply, ok, DynamicKvs#dynamic_kvs{modules = [{ModuleName, NewKvs} | RemainMods]}}
end.
@ -66,7 +66,7 @@ gen(ModuleName, Kvs) ->
Meta = meta:new(ModuleName),
Fun =
fun({K, V}, MetaTemp) ->
FunStr = concat([K, "() ->\n\t", com_util:term_to_string(V), ".\n"]),
FunStr = concat([K, "() ->\n\t", utTypeCast:term_to_string(V), ".\n"]),
case meta:add_func(MetaTemp, FunStr) of
{ok, MetaTemp2} -> MetaTemp2;
Error ->

+ 3
- 3
src/dynamicCompile/meta.erl Voir le fichier

@ -652,12 +652,12 @@ compile(#meta_mod{} = MetaMod, Options) ->
Bin);
false -> ok
end,
io:format("Module ~p Res ~p ~n", [Module, Res]),
%io:format("Module ~p Res ~p ~n", [Module, Res]),
case Res of
ok ->
io:format("Module2 ~p ~n", [Module]),
%io:format("Module2 ~p ~n", [Module]),
catch code:purge(Module),
io:format("Module3 ~p ~n", [Module]),
%io:format("Module3 ~p ~n", [Module]),
%%case code:load_binary(Module, atom_to_list(Module) ++ ".erl", Bin) of %% Fix Xref bug
case catch code:load_binary(Module, atom_to_list(Module), Bin) of
{module, _Module} ->

+ 57
- 0
src/dynamicCompile/utKvsToBeam.erl Voir le fichier

@ -0,0 +1,57 @@
-module(utKvsToBeam).
-export([
load/2
]).
-spec load(atom(), [{term(), term()}]) -> ok.
load(Module, KVs) ->
Forms = forms(Module, KVs),
{ok, Module, Bin} = compile:forms(Forms, [debug_info]),
code:soft_purge(Module),
Filename = atom_to_list(Module) ++ ".erl",
{module, Module} = code:load_binary(Module, Filename, Bin),
ok.
forms(Module, KVs) ->
Mod = erl_syntax:attribute(erl_syntax:atom(module),
[erl_syntax:atom(Module)]),
ExportList = [erl_syntax:arity_qualifier(erl_syntax:atom(get),
erl_syntax:integer(1))],
Export = erl_syntax:attribute(erl_syntax:atom(export),
[erl_syntax:list(ExportList)]),
Function = erl_syntax:function(erl_syntax:atom(get),
lookup_clauses(KVs)),
[erl_syntax:revert(X) || X <- [Mod, Export, Function]].
lookup_clause(Key, Value) ->
Var = to_syntax(Key),
Body = to_syntax(Value),
erl_syntax:clause([Var], [], [Body]).
lookup_clause_anon() ->
Var = erl_syntax:variable("_"),
Body = erl_syntax:atom(undefined),
erl_syntax:clause([Var], [], [Body]).
lookup_clauses(KVs) ->
lookup_clauses(KVs, []).
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]).
to_syntax(Atom) when is_atom(Atom) ->
erl_syntax:atom(Atom);
to_syntax(Binary) when is_binary(Binary) ->
String = erl_syntax:string(binary_to_list(Binary)),
erl_syntax:binary([erl_syntax:binary_field(String)]);
to_syntax(Float) when is_float(Float) ->
erl_syntax:float(Float);
to_syntax(Integer) when is_integer(Integer) ->
erl_syntax:integer(Integer);
to_syntax(List) when is_list(List) ->
erl_syntax:list([to_syntax(X) || X <- List]);
to_syntax(Tuple) when is_tuple(Tuple) ->
erl_syntax:tuple([to_syntax(X) || X <- tuple_to_list(Tuple)]).

+ 145
- 0
src/testCase/utTestAtomics.erl Voir le fichier

@ -0,0 +1,145 @@
-module(utTestAtomics).
-compile([export_all, nowarn_function, nowarn_unused_vars, nowarn_export_all]).
tt1(N) ->
Ref = ets:new(test, [public, set]),
ets:insert(Ref, {rate, 1}),
tt1(N, Ref).
tt1(0, Ref) ->
io:format("IMY************ets ~p~n",[ets:tab2list(Ref)]),
ok;
tt1(N, Ref) ->
ets:update_counter(Ref, rate, {2, 1}),
tt1(N - 1, Ref).
tt2(N) ->
Ref = atomics:new(1, []),
atomics:put(Ref, 1, 1),
tt2(N, Ref).
tt2(0, Ref) ->
io:format("IMY************atomics ~p~n",[atomics:get(Ref, 1)]),
ok;
tt2(N, Ref) ->
atomics:add(Ref, 1, 1),
tt2(N - 1, Ref).
tt3(N) ->
put(rate, 1),
Ref = make_ref(),
tt3(N, Ref).
tt3(0, _Ref) ->
io:format("IMY************dic ~p~n",[get(rate)]),
ok;
tt3(N, Ref) ->
put(rate, get(rate) + 1),
tt3(N - 1, Ref).
tt4(N) ->
Ref = atomics:new(1, []),
atomics:put(Ref, 1, 1),
persistent_term:put(ref, Ref),
ttt4(N).
ttt4(0) ->
io:format("IMY************dic ~p~n",[atomics:get(persistent_term:get(ref), 1)]),
ok;
ttt4(N) ->
atomics:add(persistent_term:get(ref) , 1, 1),
ttt4(N - 1).
tt5(N) ->
put(ref, make_ref()),
ttt5(N, 0).
ttt5(0, _Ref) ->
io:format("IMY************dic ~p~n",[get(ref)]),
ok;
ttt5(N, _Ref) ->
Ref = get(ref),
ttt5(N - 1, Ref).
tt6(N) ->
Ref = atomics:new(1, []),
atomics:put(Ref, 1, 1),
tt6(N, Ref).
tt6(0, Ref) ->
io:format("IMY************atomics ~p~n",[atomics:get(Ref, 1)]),
ok;
tt6(N, Ref) ->
atomics:get(Ref, 1),
tt6(N - 1, Ref).
tt7(0) ->
io:format("IMY*************** ~p~n", [erlang:system_time(milli_seconds)]);
tt7(N) ->
erlang:system_time(milli_seconds),
tt7(N - 1).
tt8(0) ->
{M, S, Ms} = os:timestamp(),
A = M * 1000000000 + S * 1000 + Ms div 1000,
io:format("IMY************ets ~p~n",[A]);
tt8(N) ->
{M, S, Ms} = os:timestamp(),
tt8(N - 1).
t9(N) ->
put(test, []),
tt9(N).
tt9(0) ->
ok;
tt9(N) ->
put(test, [N | erase(a)]),
tt9(N - 1).
t10(N) ->
put(test, []),
tt10(N).
tt10(0) ->
ok;
tt10(N) ->
put(test, [N | get(a)]),
tt10(N - 1).
a1(N) ->
Ref = atomics:new(1, []),
persistent_term:put(ref, Ref),
a1(N, 0).
a1(0, _Ref) ->
io:format("IMY************a1 ~p~n",[persistent_term:get(ref)]);
a1(N, _Ref) ->
Ref = persistent_term:get(ref),
a1(N - 1, Ref).
a2(N) ->
Ref = atomics:new(1, []),
utKvsToBeam:load(utTestRef, [{ref, erlang:ref_to_list(Ref)}]),
a2(N, 0).
a2(0, _Ref) ->
io:format("IMY************a2 ~p~n",[utTestRef:get(ref)]);
a2(N, _Ref) ->
Ref = erlang:list_to_ref(utTestRef:get(ref)),
a2(N - 1, Ref).

Chargement…
Annuler
Enregistrer