|
| 1 | +defmodule IEx.History do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + def init do |
| 5 | + Process.put(:iex_history_start_counter, 1) |
| 6 | + Process.put(:iex_history_counter, 1) |
| 7 | + end |
| 8 | + |
| 9 | + ### append ### |
| 10 | + |
| 11 | + def append(entry, counter) do |
| 12 | + Process.put({:iex_history, counter}, entry) |
| 13 | + Process.put(:iex_history_counter, counter+1) |
| 14 | + |
| 15 | + limit = IEx.Options.get(:history_size) |
| 16 | + start_counter = Process.get(:iex_history_start_counter) |
| 17 | + should_collect = limit_history(start_counter, counter, limit, false) |
| 18 | + if should_collect do |
| 19 | + collect_garbage() |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + defp limit_history(_, _, limit, _) when limit < 0 do |
| 24 | + false |
| 25 | + end |
| 26 | + |
| 27 | + defp limit_history(counter, max_counter, limit, should_collect) when max_counter - counter < limit do |
| 28 | + Process.put(:iex_history_start_counter, counter) |
| 29 | + should_collect |
| 30 | + end |
| 31 | + |
| 32 | + defp limit_history(counter, max_counter, limit, should_collect) do |
| 33 | + if not should_collect do |
| 34 | + entry = Process.delete({:iex_history, counter}) |
| 35 | + should_collect = has_binary(entry.result) |
| 36 | + else |
| 37 | + Process.delete({:iex_history, counter}) |
| 38 | + end |
| 39 | + limit_history(counter+1, max_counter, limit, should_collect) |
| 40 | + end |
| 41 | + |
| 42 | + # Checks val and each of its elements (if it is a list or a tuple) |
| 43 | + # recursively to see if it has any binaries |
| 44 | + defp has_binary(val) do |
| 45 | + try do |
| 46 | + has_bin(val) |
| 47 | + catch |
| 48 | + :throw, true -> true |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + # Worker function used by has_binary. Throws when the first binary of the |
| 53 | + # minimum specified size is found |
| 54 | + defp has_bin(val) when is_tuple(val) do |
| 55 | + has_bin(val, tuple_size(val)-1) |
| 56 | + end |
| 57 | + |
| 58 | + defp has_bin([h|t]) do |
| 59 | + has_bin(h) |
| 60 | + has_bin(t) |
| 61 | + end |
| 62 | + |
| 63 | + defp has_bin(val) when byte_size(val) > 64 do |
| 64 | + throw true |
| 65 | + end |
| 66 | + |
| 67 | + defp has_bin(_) do |
| 68 | + false |
| 69 | + end |
| 70 | + |
| 71 | + defp has_bin(_, -1) do |
| 72 | + false |
| 73 | + end |
| 74 | + |
| 75 | + defp has_bin(tuple, index) do |
| 76 | + has_bin(elem(tuple, index)) |
| 77 | + has_bin(tuple, index-1) |
| 78 | + end |
| 79 | + |
| 80 | + # Based on https://github.com/erlang/otp/blob/7dcccee4371477e983f026db9e243cb66900b1ef/lib/stdlib/src/shell.erl#L1401 |
| 81 | + defp collect_garbage do |
| 82 | + :erlang.garbage_collect(self()) |
| 83 | + try do |
| 84 | + :erlang.garbage_collect(Process.whereis(:user)) |
| 85 | + catch |
| 86 | + _, _ -> nil |
| 87 | + end |
| 88 | + try do |
| 89 | + :erlang.garbage_collect(Process.group_leader()) |
| 90 | + catch |
| 91 | + _, _ -> nil |
| 92 | + end |
| 93 | + :erlang.garbage_collect() |
| 94 | + end |
| 95 | + |
| 96 | + ### each ### |
| 97 | + |
| 98 | + def each(fun) do |
| 99 | + each(Process.get(:iex_history_start_counter), |
| 100 | + Process.get(:iex_history_counter), |
| 101 | + fun) |
| 102 | + end |
| 103 | + |
| 104 | + defp each(counter, max_counter, fun) when counter < max_counter do |
| 105 | + entry = Process.get({:iex_history, counter}) |
| 106 | + fun.(entry) |
| 107 | + each(counter+1, max_counter, fun) |
| 108 | + end |
| 109 | + |
| 110 | + defp each(_, _, _) do |
| 111 | + :ok |
| 112 | + end |
| 113 | + |
| 114 | + ### nth ### |
| 115 | + |
| 116 | + def nth(n) do |
| 117 | + entry = case n do |
| 118 | + n when n >= 0 -> |
| 119 | + Process.get({:iex_history, n}) |
| 120 | + n when n < 0 -> |
| 121 | + counter = Process.get(:iex_history_counter) |
| 122 | + Process.get({:iex_history, counter + n}) |
| 123 | + end |
| 124 | + if nil?(entry) do |
| 125 | + raise "Out of bounds" |
| 126 | + end |
| 127 | + entry |
| 128 | + end |
| 129 | +end |
0 commit comments