IElixir.Sandbox (IElixir v0.9.20) View Source
This is module responsible for running user's code.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Clean Sandbox's binding, environment and scope so there's loaded only Kernel, Kernel.Typespec and IEx.Helpers modules.
Execute passed request
Get code completion propositions suggested by IEx.Autocomplete.expand/1 function.
Get value of execution counter saved in state of Sandbox.
Callback implementation for GenServer.init/1
.
Checks if provided code is complete or client should wait for more, eg. there is unclosed parenthesis.
Link to this section Types
Specs
execution_response() :: {status :: :ok, result :: :"this is raw html" | :"do not show this result in output" | :"this is an inline image" | {:"this is an inline image", Keyword.t()} | String.t(), stdout :: String.t(), stderr :: String.t(), execution_count :: integer()} | {status :: :error, exception_name :: String.t(), traceback :: [String.t()], execution_count :: integer()}
Execution response
Specs
yes_or_no() :: :yes | :no
Yes or no atoms
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Specs
clean() :: :ok
Clean Sandbox's binding, environment and scope so there's loaded only Kernel, Kernel.Typespec and IEx.Helpers modules.
Examples
iex> IElixir.Sandbox.clean()
:ok
Specs
execute_code(map()) :: execution_response()
Execute passed request
Examples
iex> IElixir.Sandbox.execute_code(%{"code" => "a=10"})
{:ok, "10", "", "", 1}
iex> IElixir.Sandbox.execute_code(%{"code" => "b=25"})
{:ok, "25", "", "", 2}
iex> IElixir.Sandbox.execute_code(%{"code" => "IO.puts(a+b)"})
{:ok, ":ok", "35\n", "", 3}
iex> IElixir.Sandbox.execute_code(%{"code" => ~S[IO.puts(:stderr, "boom"); a+b]})
{:ok, "35", "", "boom\n", 4}
iex> IElixir.Sandbox.execute_code(%{"code" => ~S[IO.puts("one"); IO.puts(:stderr, "boom"); IO.puts(a+b)]})
{:ok, ":ok", "one\n35\n", "boom\n", 5}
iex> IElixir.Sandbox.execute_code(%{"code" => ":sampleatom"})
{:ok, ":sampleatom", "", "", 1}
iex> IElixir.Sandbox.execute_code(%{"code" => "asdf"})
{:error, "CompileError", ["** (CompileError) console:1 \"undefined function asdf/0\""], 1}
iex> IElixir.Sandbox.execute_code(%{"code" => "hd []"})
{:error, "ArgumentError", ["** (ArgumentError) \"argument error\""], 1}
iex> abc = IElixir.Sandbox.execute_code(%{"code" => "\"a\" + 5"})
iex> elem(abc, 0)
:error
iex> elem(abc, 1)
"ArithmeticError"
Specs
Get code completion propositions suggested by IEx.Autocomplete.expand/1 function.
Examples
iex> IElixir.Sandbox.get_code_completion("En")
{:yes, "um", []}
iex> IElixir.Sandbox.get_code_completion("asdf")
{:no, "", []}
iex> IElixir.Sandbox.get_code_completion("q")
{:yes, "uote", []}
iex> IElixir.Sandbox.get_code_completion("Enum")
{:yes, "", ["Enum", "Enumerable"]}
Specs
get_execution_count() :: integer()
Get value of execution counter saved in state of Sandbox.
Examples
iex> IElixir.Sandbox.get_execution_count()
1
iex> IElixir.Sandbox.execute_code(%{"code" => "a=10"})
{:ok, "10", "", "", 1}
iex> IElixir.Sandbox.get_execution_count()
2
Callback implementation for GenServer.init/1
.
Specs
is_complete_code(String.t()) :: :complete | :invalid | :incomplete
Checks if provided code is complete or client should wait for more, eg. there is unclosed parenthesis.
Examples
iex> IElixir.Sandbox.is_complete_code("a = 10")
:complete
iex> IElixir.Sandbox.is_complete_code("a + b")
:invalid
iex> IElixir.Sandbox.is_complete_code("case x do")
:incomplete