Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple eval support #339

Merged
merged 6 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions apps/elixir_ls_debugger/lib/debugger/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule ElixirLS.Debugger.Server do
end

alias ElixirLS.Debugger.{Output, Stacktrace, Protocol, Variables}
alias ElixirLS.Debugger.Stacktrace.Frame
use GenServer
use Protocol

Expand Down Expand Up @@ -284,9 +285,13 @@ defmodule ElixirLS.Debugger.Server do
{%{"variables" => vars_json}, state}
end

defp handle_request(request(_, "evaluate"), state) do
msg = "(Debugger) Expression evaluation in Elixir debugger is not supported (yet)."
{%{"result" => msg, "variablesReference" => 0}, state}
defp handle_request(request(_cmd, "evaluate", %{"expression" => expr} = _args), state) do
timeout = 1_000
bindings = all_variables(state.paused_processes)

result = evaluate_code_expression(expr, bindings, timeout)

{%{"result" => inspect(result), "variablesReference" => 0}, state}
end

defp handle_request(request(_, "disconnect"), state) do
Expand Down Expand Up @@ -376,6 +381,49 @@ defmodule ElixirLS.Debugger.Server do
end)
end

defp evaluate_code_expression(expr, bindings, timeout) do
task =
Task.async(fn ->
try do
{term, _bindings} = Code.eval_string(expr, bindings)
term
catch
error -> error
end
end)

Process.unlink(task.pid)

result = Task.yield(task, timeout) || Task.shutdown(task)

case result do
{:ok, data} -> data
nil -> :elixir_ls_expression_timeout
_otherwise -> result
end
end

defp all_variables(paused_processes) do
paused_processes
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
|> Enum.flat_map(fn {_pid, paused_process} -> paused_process.frames |> Map.values() end)
|> Enum.filter(&match?(%Frame{bindings: bindings} when is_map(bindings), &1))
|> Enum.flat_map(fn %Frame{bindings: bindings} ->
bindings |> Enum.map(&rename_binding_to_classic_variable/1)
end)
end

defp rename_binding_to_classic_variable({key, value}) do
# binding is present with prefix _ and postfix @
# for example _key@1 and _value@1 are representations of current function variables
new_key =
key
|> Atom.to_string()
|> String.replace(~r/_(.*)@\d/, "\\1")
|> String.to_atom()

{new_key, value}
end

defp find_var(paused_processes, var_id) do
Enum.find_value(paused_processes, fn {pid, paused_process} ->
if Map.has_key?(paused_process.vars, var_id) do
Expand Down
78 changes: 78 additions & 0 deletions apps/elixir_ls_debugger/test/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,82 @@ defmodule ElixirLS.Debugger.ServerTest do
assert(:hello in :int.interpreted())
end)
end

describe "Watch section" do
defp gen_packet(expr) do
%{
"arguments" => %{
"context" => "watch",
"expression" => expr,
"frameId" => 123
},
"command" => "evaluate",
"seq" => 1,
"type" => "request"
}
end

test "Evaluate expression with OK result", %{server: server} do
Server.receive_packet(
server,
gen_packet("1 + 2 + 3 + 4")
)

assert_receive(%{"body" => %{"result" => "10"}}, 1000)

assert Process.alive?(server)
end

test "Evaluate expression with ERROR result", %{server: server} do
Server.receive_packet(
server,
gen_packet("1 = 2")
)

assert_receive(%{"body" => %{"result" => result}}, 1000)

assert result =~ ~r/badmatch/

assert Process.alive?(server)
end

test "Evaluate expression with attempt to exit debugger process", %{server: server} do
Server.receive_packet(
server,
gen_packet("Process.exit(self)")
)

assert_receive(%{"body" => %{"result" => result}}, 1000)

assert result =~ ~r/:exit/

assert Process.alive?(server)
end

test "Evaluate expression with attempt to throw debugger process", %{server: server} do
Server.receive_packet(
server,
gen_packet("throw(:goodmorning_bug)")
)

assert_receive(%{"body" => %{"result" => result}}, 1000)

assert result =~ ~r/:goodmorning_bug/

assert Process.alive?(server)
end

test "Evaluate expression which has long execution", %{server: server} do
Server.receive_packet(
server,
gen_packet(":timer.sleep(10_000)")
)

assert_receive(%{"body" => %{"result" => result}}, 1100)

assert result =~ ~r/:elixir_ls_expression_timeout/

assert Process.alive?(server)
end
end
end