Skip to content

Commit d6d6303

Browse files
committed
Move load_dot_iex to IEx.Server, after require IEx.Helpers
1 parent 4714b54 commit d6d6303

File tree

3 files changed

+56
-65
lines changed

3 files changed

+56
-65
lines changed

lib/iex/lib/iex.ex

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ defmodule IEx do
121121
122122
"""
123123

124-
alias IEx.Util
125-
126124
@doc """
127125
Registers a function to be invoked after IEx process is spawned.
128126
"""
@@ -183,7 +181,6 @@ defmodule IEx do
183181
184182
set_expand_fun()
185183
run_after_spawn()
186-
config = load_dot_iex(config)
187184
IEx.Server.start(config)
188185
end
189186
end
@@ -235,28 +232,4 @@ defmodule IEx do
235232
defp run_after_spawn do
236233
lc fun inlist Enum.reverse(after_spawn), do: fun.()
237234
end
238-
239-
# Locates and loads an .iex file from one of predefined locations
240-
defp load_dot_iex(config) do
241-
path = Enum.find [".iex", "~/.iex"], fn path -> File.regular?(Path.expand(path)) end
242-
if nil?(path) do
243-
config
244-
else
245-
try do
246-
code = File.read!(path)
247-
248-
# Evaluate the contents in the same environment IEx.Server will run in
249-
{ _result, binding, scope } = :elixir.eval(:unicode.characters_to_list(code), config.binding, 0, config.scope)
250-
config.binding(binding).scope(scope)
251-
rescue
252-
exception ->
253-
Util.print_exception(exception, System.stacktrace)
254-
System.halt(1)
255-
catch
256-
kind, error ->
257-
Util.print_error(kind, error, System.stacktrace)
258-
System.halt(1)
259-
end
260-
end
261-
end
262235
end

lib/iex/lib/iex/server.ex

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule IEx.Server do
22
@moduledoc false
33

4-
alias IEx.Util
5-
64
@doc """
75
Eval loop for an IEx session. Its responsibilities include:
86
@@ -15,7 +13,9 @@ defmodule IEx.Server do
1513
IO.puts "Interactive Elixir (#{System.version}) - press Ctrl+C to exit (type h() ENTER for help)"
1614
Process.put :iex_history, []
1715
{ _, _, scope } = :elixir.eval('require IEx.Helpers', [], 0, config.scope)
18-
do_loop(config.scope(scope))
16+
config = config.scope(scope)
17+
config = load_dot_iex(config)
18+
do_loop(config)
1919
end
2020

2121
defp do_loop(config) do
@@ -28,11 +28,11 @@ defmodule IEx.Server do
2828
eval(code, line, counter, config)
2929
rescue
3030
exception ->
31-
Util.print_exception(exception, System.stacktrace)
31+
print_exception(exception, System.stacktrace)
3232
config.cache('')
3333
catch
3434
kind, error ->
35-
Util.print_error(kind, error, System.stacktrace)
35+
print_error(kind, error, System.stacktrace)
3636
config.cache('')
3737
end
3838

@@ -88,6 +88,31 @@ defmodule IEx.Server do
8888
end
8989
end
9090

91+
# Locates and loads an .iex file from one of predefined locations. Returns
92+
# new config.
93+
defp load_dot_iex(config) do
94+
path = Enum.find [".iex", "~/.iex"], fn path -> File.regular?(Path.expand(path)) end
95+
if nil?(path) do
96+
config
97+
else
98+
try do
99+
code = File.read!(path)
100+
101+
# Evaluate the contents in the same environment do_loop will run in
102+
{ _result, binding, scope } = :elixir.eval(:unicode.characters_to_list(code), config.binding, 0, config.scope)
103+
config.binding(binding).scope(scope)
104+
rescue
105+
exception ->
106+
print_exception(exception, System.stacktrace)
107+
System.halt(1)
108+
catch
109+
kind, error ->
110+
print_error(kind, error, System.stacktrace)
111+
System.halt(1)
112+
end
113+
end
114+
end
115+
91116
defp update_history(config) do
92117
current = Process.get :iex_history
93118
Process.put :iex_history, [config|current]
@@ -113,7 +138,33 @@ defmodule IEx.Server do
113138
IO.puts :stdio, IO.ANSI.escape("%{yellow}#{inspect(result, IEx.inspect_opts)}")
114139
end
115140

141+
defp io_error(result) do
142+
IO.puts :stdio, result
143+
end
144+
116145
defp remote_prefix do
117146
if node == node(:erlang.group_leader), do: "iex", else: "rem"
118147
end
148+
149+
defp print_exception(exception, stacktrace) do
150+
print_stacktrace stacktrace, fn ->
151+
"** (#{inspect exception.__record__(:name)}) #{exception.message}"
152+
end
153+
end
154+
155+
defp print_error(kind, reason, stacktrace) do
156+
print_stacktrace stacktrace, fn ->
157+
"** (#{kind}) #{inspect(reason)}"
158+
end
159+
end
160+
161+
defp print_stacktrace(trace, callback) do
162+
try do
163+
io_error callback.()
164+
io_error Exception.format_stacktrace(trace)
165+
catch
166+
_, _ ->
167+
io_error "** (IEx.Error) error when printing exception message and stacktrace"
168+
end
169+
end
119170
end

lib/iex/lib/iex/util.ex

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)