IElixir v0.9.18 IElixir.Sandbox View Source

This is module responsible for running user's code.

Link to this section Summary

Types

Execution response

Yes or no atoms

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.

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

Checks if provided code is complete or client should wait for more, eg. there is unclosed parenthesis.

Link to this section Types

Link to this type

execution_response() View Source
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

Link to this type

yes_or_no() View Source
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.

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
Link to this function

execute_code(request) View Source
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"
Link to this function

get_code_completion(code) View Source
get_code_completion(String.t()) :: {yes_or_no(), String.t(), [String.t()]}

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"]}
Link to this function

get_execution_count() View Source
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

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

init_arg is the argument term (second argument) passed to start_link/3.

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See c:handle_call/3 for more information on hibernation.

Returning {:ok, state, {:continue, continue}} is similar to {:ok, state} except that immediately after entering the loop the c:handle_continue/2 callback will be invoked with the value continue as first argument.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling c:terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

  • The GenServer is disabled by configuration but might be enabled later.
  • An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.

Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling c:terminate/2.

Callback implementation for GenServer.init/1.

Link to this function

is_complete_code(code) View Source
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