@@ -14,11 +14,11 @@ defmodule IEx.Evaluator do
1414 old_leader = Process . group_leader
1515 Process . group_leader ( self ( ) , leader )
1616
17- state = loop_state ( opts )
17+ state = loop_state ( server , IEx.History . init , opts )
1818 command == :ack && :proc_lib . init_ack ( self ( ) )
1919
2020 try do
21- loop ( server , IEx.History . init , state )
21+ loop ( state )
2222 after
2323 Process . group_leader ( self ( ) , old_leader )
2424 end
@@ -71,23 +71,23 @@ defmodule IEx.Evaluator do
7171 end
7272 end
7373
74- defp loop ( server , history , state ) do
74+ defp loop ( % { server: server } = state ) do
7575 receive do
7676 { :eval , ^ server , code , iex_state } ->
77- { result , history , state } = eval ( code , iex_state , history , state )
77+ { result , state } = eval ( code , iex_state , state )
7878 send server , { :evaled , self ( ) , result }
79- loop ( server , history , state )
79+ loop ( state )
8080 { :fields_from_env , ref , receiver , fields } ->
8181 send receiver , { ref , Map . take ( state . env , fields ) }
82- loop ( server , history , state )
82+ loop ( state )
8383 { :value_from_binding , ref , receiver , var_name , map_key_path } ->
8484 value = traverse_binding ( state . binding , var_name , map_key_path )
8585 send receiver , { ref , value }
86- loop ( server , history , state )
86+ loop ( state )
8787 { :variables_from_binding , ref , receiver , var_prefix } ->
8888 value = find_matched_variables ( state . binding , var_prefix )
8989 send receiver , { ref , value }
90- loop ( server , history , state )
90+ loop ( state )
9191 { :done , ^ server } ->
9292 :ok
9393 end
@@ -110,7 +110,7 @@ defmodule IEx.Evaluator do
110110 do: var_name
111111 end
112112
113- defp loop_state ( opts ) do
113+ defp loop_state ( server , history , opts ) do
114114 env =
115115 if env = opts [ :env ] do
116116 :elixir . env_for_eval ( env , [ ] )
@@ -121,7 +121,7 @@ defmodule IEx.Evaluator do
121121 { _ , _ , env , scope } = :elixir . eval ( 'import IEx.Helpers' , [ ] , env )
122122
123123 binding = Keyword . get ( opts , :binding , [ ] )
124- state = % { binding: binding , scope: scope , env: env }
124+ state = % { binding: binding , scope: scope , env: env , server: server , history: history }
125125
126126 case opts [ :dot_iex_path ] do
127127 "" -> state
@@ -178,34 +178,38 @@ defmodule IEx.Evaluator do
178178 # https://github.com/elixir-lang/elixir/issues/1089 for discussion.
179179 @ break_trigger '#iex:break\n '
180180
181- defp eval ( code , iex_state , history , state ) do
181+ defp eval ( code , iex_state , state ) do
182182 try do
183- do_eval ( String . to_charlist ( code ) , iex_state , history , state )
183+ do_eval ( String . to_charlist ( code ) , iex_state , state )
184184 catch
185185 kind , error ->
186186 print_error ( kind , error , System . stacktrace )
187- { % { iex_state | cache: '' } , history , state }
187+ { % { iex_state | cache: '' } , state }
188188 end
189189 end
190190
191- defp do_eval ( @ break_trigger , % IEx.State { cache: '' } = iex_state , history , state ) do
192- { iex_state , history , state }
191+ defp do_eval ( @ break_trigger , % IEx.State { cache: '' } = iex_state , state ) do
192+ { iex_state , state }
193193 end
194194
195- defp do_eval ( @ break_trigger , iex_state , _history , _state ) do
195+ defp do_eval ( @ break_trigger , iex_state , _state ) do
196196 :elixir_errors . parse_error ( iex_state . counter , "iex" , "incomplete expression" , "" )
197197 end
198198
199- defp do_eval ( latest_input , iex_state , history , state ) do
199+ defp do_eval ( latest_input , iex_state , state ) do
200200 code = iex_state . cache ++ latest_input
201201 line = iex_state . counter
202- Process . put ( :iex_history , history )
203- handle_eval ( Code . string_to_quoted ( code , [ line: line , file: "iex" ] ) , code , line , iex_state , history , state )
202+ put_history ( state )
203+ handle_eval ( Code . string_to_quoted ( code , [ line: line , file: "iex" ] ) , code , line , iex_state , state )
204204 after
205205 Process . delete ( :iex_history )
206206 end
207207
208- defp handle_eval ( { :ok , forms } , code , line , iex_state , history , state ) do
208+ defp put_history ( % { history: history } ) do
209+ Process . put ( :iex_history , history )
210+ end
211+
212+ defp handle_eval ( { :ok , forms } , code , line , iex_state , state ) do
209213 { result , binding , env , scope } =
210214 :elixir . eval_forms ( forms , state . binding , state . env , state . scope )
211215 unless result == IEx . dont_display_result , do: io_inspect ( result )
@@ -218,22 +222,23 @@ defmodule IEx.Evaluator do
218222 scope: scope ,
219223 binding: binding }
220224
221- { iex_state , update_history ( history , line , code , result ) , state }
225+ { iex_state , update_history ( state , line , code , result ) }
222226 end
223227
224- defp handle_eval ( { :error , { _ , _ , "" } } , code , _line , iex_state , history , state ) do
228+ defp handle_eval ( { :error , { _ , _ , "" } } , code , _line , iex_state , state ) do
225229 # Update iex_state.cache so that IEx continues to add new input to
226230 # the unfinished expression in "code"
227- { % { iex_state | cache: code } , history , state }
231+ { % { iex_state | cache: code } , state }
228232 end
229233
230- defp handle_eval ( { :error , { line , error , token } } , _code , _line , _iex_state , _ , _state ) do
234+ defp handle_eval ( { :error , { line , error , token } } , _code , _line , _iex_state , _state ) do
231235 # Encountered malformed expression
232236 :elixir_errors . parse_error ( line , "iex" , error , token )
233237 end
234238
235- defp update_history ( history , counter , cache , result ) do
236- IEx.History . append ( history , { counter , cache , result } , IEx.Config . history_size )
239+ defp update_history ( state , counter , cache , result ) do
240+ history_size = IEx.Config . history_size
241+ update_in ( state . history , & IEx.History . append ( & 1 , { counter , cache , result } , history_size ) )
237242 end
238243
239244 defp io_inspect ( result ) do
0 commit comments