From f28ac67b9c2b07a1a0eb10041a7eee20827fbf1d Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 3 Feb 2010 23:20:25 +0200 Subject: erts/preloaded: Add types and specs --- erts/preloaded/src/erl_prim_loader.erl | 76 +++++++++++++++++++--------------- erts/preloaded/src/erlang.erl | 51 ++++++++++++++--------- erts/preloaded/src/init.erl | 4 +- erts/preloaded/src/otp_ring0.erl | 1 + erts/preloaded/src/zlib.erl | 1 - 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/erts/preloaded/src/erl_prim_loader.erl b/erts/preloaded/src/erl_prim_loader.erl index c0785083de..a13292d5ab 100644 --- a/erts/preloaded/src/erl_prim_loader.erl +++ b/erts/preloaded/src/erl_prim_loader.erl @@ -51,22 +51,25 @@ %% Used by escript and code -export([set_primary_archive/3, release_archives/0]). -%% Internal function. Exported to avoid dialyzer warnings --export([concat/1]). - -include_lib("kernel/include/file.hrl"). -type host() :: atom(). +-record(prim_state, {debug :: boolean(), + cache, + primary_archive}). +-type prim_state() :: #prim_state{}. + -record(state, - {loader :: 'efile' | 'inet', - hosts = [] :: [host()], % hosts list (to boot from) + {loader :: 'efile' | 'inet', + hosts = [] :: [host()], % hosts list (to boot from) id, % not used any more? - data, % data port etc - timeout, % idle timeout - n_timeouts, % Number of timeouts before archives are released - multi_get = false :: boolean(), - prim_state}). % state for efile code loader + data :: 'noport' | port(), % data port etc + timeout :: timeout(), % idle timeout + %% Number of timeouts before archives are released + n_timeouts :: non_neg_integer(), + multi_get = false :: boolean(), + prim_state :: prim_state()}). % state for efile code loader -define(IDLE_TIMEOUT, 60000). %% tear inet connection after 1 minutes -define(N_TIMEOUTS, 6). %% release efile archive after 6 minutes @@ -89,8 +92,6 @@ end end()). --record(prim_state, {debug, cache, primary_archive}). - debug(#prim_state{debug = Deb}, Term) -> case Deb of false -> ok; @@ -124,7 +125,7 @@ start(Id, Pgm0, Hosts) -> start_it("ose_inet"=Cmd, Id, Pid, Hosts) -> %% Setup reserved port for ose_inet driver (only OSE) - case catch erlang:open_port({spawn,Cmd},[binary]) of + case catch erlang:open_port({spawn,Cmd}, [binary]) of {'EXIT',Why} -> ?dbg(ose_inet_port_open_fail, Why), Why; @@ -317,7 +318,7 @@ loop(State, Parent, Paths) -> {Res,State1} = handle_get_cwd(State, Args), {Res,State1,Paths}; {set_primary_archive,File,Bin,FileInfo} -> - {Res,State1} = handle_set_primary_archive(State, File, Bin,FileInfo), + {Res,State1} = handle_set_primary_archive(State, File, Bin, FileInfo), {Res,State1,Paths}; release_archives -> {Res,State1} = handle_release_archives(State), @@ -326,7 +327,7 @@ loop(State, Parent, Paths) -> {ignore,State,Paths} end, if Resp =:= ignore -> ok; - true -> Pid ! {self(),Resp} + true -> Pid ! {self(),Resp}, ok end, if is_record(State2, state) -> @@ -335,7 +336,7 @@ loop(State, Parent, Paths) -> exit({bad_state, Req, State2}) end; {'EXIT',Parent,W} -> - handle_stop(State), + _State1 = handle_stop(State), exit(W); {'EXIT',P,W} -> State1 = handle_exit(State, P, W), @@ -605,7 +606,6 @@ find_collect(U,Retry,AL,Delay,Acc) -> _Garbage -> ?dbg(collect_garbage, _Garbage), find_collect(U, Retry, AL, Delay, Acc) - after Delay -> ?dbg(collected, Acc), case keymember(0, 1, Acc) of %% got high priority server? @@ -619,7 +619,7 @@ sleep(Time) -> receive after Time -> ok end. inet_exit_port(State, Port, _Reason) when State#state.data =:= Port -> - State#state { data = noport, timeout = infinity }; + State#state{data = noport, timeout = infinity}; inet_exit_port(State, _, _) -> State. @@ -629,7 +629,7 @@ inet_timeout_handler(State, _Parent) -> if is_port(Tcp) -> ll_close(Tcp); true -> ok end, - State#state { timeout = infinity, data = noport }. + State#state{timeout = infinity, data = noport}. %% -> {{ok,BinFile,Tag},State} | {{error,Reason},State} inet_get_file_from_port(State, File, Paths) -> @@ -659,9 +659,9 @@ inet_get_file_from_port1(_File, [], State) -> inet_send_and_rcv(Msg, Tag, State) when State#state.data =:= noport -> {ok,Tcp} = find_master(State#state.hosts), %% reconnect - inet_send_and_rcv(Msg, Tag, State#state { data = Tcp, - timeout = ?IDLE_TIMEOUT }); -inet_send_and_rcv(Msg, Tag, #state{data=Tcp,timeout=Timeout}=State) -> + inet_send_and_rcv(Msg, Tag, State#state{data = Tcp, + timeout = ?IDLE_TIMEOUT}); +inet_send_and_rcv(Msg, Tag, #state{data = Tcp, timeout = Timeout} = State) -> prim_inet:send(Tcp, term_to_binary(Msg)), receive {tcp,Tcp,BinMsg} -> @@ -679,13 +679,13 @@ inet_send_and_rcv(Msg, Tag, #state{data=Tcp,timeout=Timeout}=State) -> end; {tcp_closed,Tcp} -> %% Ok we must reconnect - inet_send_and_rcv(Msg, Tag, State#state { data = noport }); + inet_send_and_rcv(Msg, Tag, State#state{data = noport}); {tcp_error,Tcp,_Reason} -> %% Ok we must reconnect inet_send_and_rcv(Msg, Tag, inet_stop_port(State)); {'EXIT', Tcp, _} -> %% Ok we must reconnect - inet_send_and_rcv(Msg, Tag, State#state { data = noport }) + inet_send_and_rcv(Msg, Tag, State#state{data = noport}) after Timeout -> %% Ok we must reconnect inet_send_and_rcv(Msg, Tag, inet_stop_port(State)) @@ -772,6 +772,7 @@ port_error(S, Error) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec prim_init() -> prim_state(). prim_init() -> Deb = case init:get_argument(loader_debug) of @@ -779,10 +780,10 @@ prim_init() -> error -> false end, cache_new(#prim_state{debug = Deb}). - + prim_release_archives(PS) -> debug(PS, release_archives), - {Res, PS2}= prim_do_release_archives(PS, get(), []), + {Res, PS2} = prim_do_release_archives(PS, get(), []), debug(PS2, {return, Res}), {Res, PS2}. @@ -822,8 +823,8 @@ prim_set_primary_archive(PS, undefined, undefined, undefined) -> debug(PS2, {return, Res}), {Res, PS2} end; -prim_set_primary_archive(PS, ArchiveFile, ArchiveBin, FileInfo) - when is_list(ArchiveFile), is_binary(ArchiveBin), is_record(FileInfo, file_info) -> +prim_set_primary_archive(PS, ArchiveFile, ArchiveBin, #file_info{} = FileInfo) + when is_list(ArchiveFile), is_binary(ArchiveBin) -> %% Try the archive file debug(PS, {set_primary_archive, ArchiveFile, byte_size(ArchiveBin)}), {Res3, PS3} = @@ -861,6 +862,7 @@ prim_set_primary_archive(PS, ArchiveFile, ArchiveBin, FileInfo) debug(PS3, {return, Res3}), {Res3, PS3}. +-spec prim_get_file(prim_state(), file:filename()) -> {_, prim_state()}. prim_get_file(PS, File) -> debug(PS, {get_file, File}), {Res2, PS2} = @@ -885,7 +887,9 @@ prim_get_file(PS, File) -> debug(PS, {return, Res2}), {Res2, PS2}. -%% -> {{ok,List},State} | {{error,Reason},State} +-spec prim_list_dir(prim_state(), file:filename()) -> + {{'ok', [file:filename()]}, prim_state()} + | {{'error', term()}, prim_state()}. prim_list_dir(PS, Dir) -> debug(PS, {list_dir, Dir}), {Res2, PS3} = @@ -936,7 +940,9 @@ prim_list_dir(PS, Dir) -> debug(PS, {return, Res2}), {Res2, PS3}. -%% -> {{ok,Info},State} | {{error,Reason},State} +-spec prim_read_file_info(prim_state(), file:filename()) -> + {{'ok', #file_info{}}, prim_state()} + | {{'error', term()}, prim_state()}. prim_read_file_info(PS, File) -> debug(PS, {read_file_info, File}), {Res2, PS2} = @@ -976,6 +982,8 @@ prim_read_file_info(PS, File) -> debug(PS2, {return, Res2}), {Res2, PS2}. +-spec prim_get_cwd(prim_state(), [file:filename()]) -> + {{'error', term()} | {'ok', _}, prim_state()}. prim_get_cwd(PS, []) -> debug(PS, {get_cwd, []}), Res = prim_file:get_cwd(), @@ -1029,7 +1037,7 @@ apply_archive(PS, Fun, Acc, Archive) -> {Acc2, PS}; Error -> debug(PS, {cache, {clear, Error}}), - clear_cache(Archive, Cache), + ok = clear_cache(Archive, Cache), debug(PS, {cache, Error}), erase(Archive), %% put(Archive, {Error, FI}), @@ -1041,7 +1049,7 @@ apply_archive(PS, Fun, Acc, Archive) -> end; Error -> debug(PS, {cache, {clear, Error}}), - clear_cache(Archive, Cache), + ok = clear_cache(Archive, Cache), apply_archive(PS, Fun, Acc, Archive) end end. @@ -1143,8 +1151,8 @@ send_all(U, [IP | AL], Cmd) -> send_all(U, AL, Cmd); send_all(_U, [], _) -> ok. -concat([A|T]) when is_atom(A) -> %Atom - atom_to_list(A) ++ concat(T); +%%concat([A|T]) when is_atom(A) -> %Atom +%% atom_to_list(A) ++ concat(T); concat([C|T]) when C >= 0, C =< 255 -> [C|concat(T)]; concat([S|T]) -> %String diff --git a/erts/preloaded/src/erlang.erl b/erts/preloaded/src/erlang.erl index 552121d485..b88a316205 100644 --- a/erts/preloaded/src/erlang.erl +++ b/erts/preloaded/src/erlang.erl @@ -29,25 +29,16 @@ -export([send_nosuspend/2, send_nosuspend/3]). -export([localtime_to_universaltime/1]). -export([suspend_process/1]). --export([min/2,max/2]). - +-export([min/2, max/2]). -export([dlink/1, dunlink/1, dsend/2, dsend/3, dgroup_leader/2, dexit/2, dmonitor_node/3, dmonitor_p/2]). - -export([delay_trap/2]). - -export([set_cookie/2, get_cookie/0]). - -export([nodes/0]). - -export([concat_binary/1]). - -export([list_to_integer/2,integer_to_list/2]). - -export([flush_monitor_message/2]). - -export([set_cpu_topology/1, format_cpu_topology/1]). - -export([await_proc_exit/3]). -deprecated([hash/2]). @@ -55,14 +46,22 @@ -compile(nowarn_bif_clash). +%%-------------------------------------------------------------------------- + +-type date() :: {pos_integer(), pos_integer(), pos_integer()}. +-type time() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}. +-type date_time() :: {date(), time()}. + +%%-------------------------------------------------------------------------- + apply(Fun, Args) -> apply(Fun, Args). apply(Mod, Name, Args) -> apply(Mod, Name, Args). +%% Spawns with a fun -% Spawns with a fun spawn(F) when is_function(F) -> spawn(erlang, apply, [F, []]); spawn({M,F}=MF) when is_atom(M), is_atom(F) -> @@ -125,7 +124,7 @@ spawn_opt(N, {M,F}=MF, O) when is_atom(M), is_atom(F) -> spawn_opt(N, F, O) -> erlang:error(badarg, [N, F, O]). -% Spawns with MFA +%% Spawns with MFA spawn(N,M,F,A) when N =:= node(), is_atom(M), is_atom(F), is_list(A) -> spawn(M,F,A); @@ -254,12 +253,17 @@ crasher(Node,Mod,Fun,Args,Opts,Reason) -> [Mod,Fun,Args,Opts,Node]), exit(Reason). +-spec yield() -> 'true'. yield() -> erlang:yield(). -nodes() -> erlang:nodes(visible). +-spec nodes() -> [node()]. +nodes() -> + erlang:nodes(visible). -disconnect_node(Node) -> net_kernel:disconnect(Node). +-spec disconnect_node(node()) -> boolean(). +disconnect_node(Node) -> + net_kernel:disconnect(Node). fun_info(Fun) when is_function(Fun) -> Keys = [type,env,arity,name,uniq,index,new_uniq,new_index,module,pid], @@ -285,9 +289,11 @@ send_nosuspend(Pid, Msg, Opts) -> _ -> false end. +-spec localtime_to_universaltime(date_time()) -> date_time(). localtime_to_universaltime(Localtime) -> erlang:localtime_to_universaltime(Localtime, undefined). +-spec suspend_process(pid()) -> 'true'. suspend_process(P) -> case catch erlang:suspend_process(P, []) of {'EXIT', {Reason, _}} -> erlang:error(Reason, [P]); @@ -298,10 +304,11 @@ suspend_process(P) -> %% %% If the emulator wants to perform a distributed command and %% a connection is not established to the actual node the following -%% functions is called in order to set up the connection and then +%% functions are called in order to set up the connection and then %% reactivate the command. %% +-spec dlink(pid() | port()) -> 'true'. dlink(Pid) -> case net_kernel:connect(node(Pid)) of true -> link(Pid); @@ -309,6 +316,7 @@ dlink(Pid) -> end. %% Can this ever happen? +-spec dunlink(identifier()) -> 'true'. dunlink(Pid) -> case net_kernel:connect(node(Pid)) of true -> unlink(Pid); @@ -322,7 +330,7 @@ dmonitor_node(Node, Flag, []) -> end; dmonitor_node(Node, Flag, Opts) -> - case lists:member(allow_passive_connect,Opts) of + case lists:member(allow_passive_connect, Opts) of true -> case net_kernel:passive_cnct(Node) of true -> erlang:monitor_node(Node, Flag, Opts); @@ -378,11 +386,11 @@ dsend({Name, Node}, Msg, Opts) -> ignored -> ok % Not distributed. end. +-spec dmonitor_p('process', pid() | {atom(),atom()}) -> reference(). dmonitor_p(process, ProcSpec) -> %% ProcSpec = pid() | {atom(),atom()} %% ProcSpec CANNOT be an atom because a locally registered process %% is never handled here. - Node = case ProcSpec of {S,N} when is_atom(S), is_atom(N), N =/= node() -> N; _ when is_pid(ProcSpec) -> node(ProcSpec) @@ -400,6 +408,7 @@ dmonitor_p(process, ProcSpec) -> %% Trap function used when modified timing has been enabled. %% +-spec delay_trap(Result, timeout()) -> Result. delay_trap(Result, 0) -> erlang:yield(), Result; delay_trap(Result, Timeout) -> receive after Timeout -> Result end. @@ -423,13 +432,15 @@ set_cookie(Node, C) when Node =/= nonode@nohost, is_atom(Node) -> error -> exit(badarg); Other -> Other end. - + +-spec get_cookie() -> atom(). get_cookie() -> auth:get_cookie(). concat_binary(List) -> list_to_binary(List). +-spec integer_to_list(integer(), 1..255) -> string(). integer_to_list(I, 10) -> erlang:integer_to_list(I); integer_to_list(I, Base) @@ -457,7 +468,6 @@ integer_to_list(I0, Base, R0) -> end. - list_to_integer(L, 10) -> erlang:list_to_integer(L); list_to_integer(L, Base) @@ -662,6 +672,7 @@ rvrs([X|Xs],Ys) -> rvrs(Xs, [X|Ys]). %% functions in bif.c. Do not make %% any changes to it without reading %% the comment about them in bif.c! +-spec await_proc_exit(dst(), 'apply' | 'data' | 'reason', term()) -> term(). await_proc_exit(Proc, Op, Data) -> Mon = erlang:monitor(process, Proc), receive @@ -677,8 +688,10 @@ await_proc_exit(Proc, Op, Data) -> end end. +-spec min(term(), term()) -> term(). min(A, B) when A > B -> B; min(A, _) -> A. +-spec max(term(), term()) -> term(). max(A, B) when A < B -> B; max(A, _) -> A. diff --git a/erts/preloaded/src/init.erl b/erts/preloaded/src/init.erl index c0b3d286e8..4fda69e22f 100644 --- a/erts/preloaded/src/init.erl +++ b/erts/preloaded/src/init.erl @@ -51,7 +51,7 @@ get_status/0,boot/1,get_arguments/0,get_plain_arguments/0, get_argument/1,script_id/0]). -% internal exports +%% internal exports -export([fetch_loaded/0,ensure_loaded/1,make_permanent/2, notify_when_started/1,wait_until_started/0, objfile_extension/0, archive_extension/0,code_path_choice/0]). @@ -115,6 +115,8 @@ fetch_loaded() -> ensure_loaded(Module) -> request({ensure_loaded, Module}). +-spec make_permanent(file:filename(), 'false' | file:filename()) -> + 'ok' | {'error', term()}. make_permanent(Boot,Config) -> request({make_permanent,Boot,Config}). diff --git a/erts/preloaded/src/otp_ring0.erl b/erts/preloaded/src/otp_ring0.erl index 3b0d562d1f..d630d80351 100644 --- a/erts/preloaded/src/otp_ring0.erl +++ b/erts/preloaded/src/otp_ring0.erl @@ -22,6 +22,7 @@ -export([start/2]). +-spec start(_, term()) -> term(). start(_Env, Argv) -> run(init, boot, Argv). diff --git a/erts/preloaded/src/zlib.erl b/erts/preloaded/src/zlib.erl index 21971a75cf..a6422e0119 100644 --- a/erts/preloaded/src/zlib.erl +++ b/erts/preloaded/src/zlib.erl @@ -115,7 +115,6 @@ %%------------------------------------------------------------------------ %% Main data types of the file --type(iodata() :: iolist() | binary()). %XXX To be removed in R13B. -type zstream() :: port(). %% Auxiliary data types of the file -- cgit v1.2.3