Skip to content

Commit

Permalink
Refactored opts parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustaf Sjöberg committed Apr 23, 2014
1 parent 6e53c74 commit 4a36f99
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/erlang_v8_vm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,18 @@ priv_dir() ->

%% @doc Parse proplists/opts and populate a state record.
parse_opts(Opts) ->
parse_opts(Opts, #state{initial_source = []}).
lists:foldl(fun parse_opt/2, #state{initial_source = []}, Opts).

parse_opts([], State) ->
State;
%% @doc Append source specified in source option.
parse_opt({source, S}, #state{initial_source = InitialSource} = State) ->
State#state{initial_source = [S|InitialSource]};

parse_opts([{source, S}|T], #state{initial_source = InitialSource} = State) ->
parse_opts(T, State#state{initial_source = [S|InitialSource]});

parse_opts([{file, F}|T], #state{initial_source = InitialSource} = State) ->
%% @doc Read contents of file option and append to state.
parse_opt({file, F}, #state{initial_source = InitialSource} = State) ->
%% Files should probably be read in the OS process instead to prevent
%% keeping multiple copies of the JS source code in memory.
{ok, S} = file:read_file(F),
parse_opts(T, State#state{initial_source = [S|InitialSource]});
State#state{initial_source = [S|InitialSource]};

parse_opts([_|T], State) ->
parse_opts(T, State).
%% @doc Ignore unknown options.
parse_opt(_, State) -> State.

0 comments on commit 4a36f99

Please sign in to comment.