Operate v0.1.0-beta.1 Operate.VM View Source

Operate VM module. Responsible for initalizing the VM state and evaluating and executing Lua code in the VM.

Link to this section Summary

Types

Operate return value

Lua table path. Either a dot-delimited string or list of strings or atoms.

t()

Operate VM state

Functions

Calls a function within the VM state at the given lua path and returns the result.

As call/3, but returns the result or raises an exception.

Decodes a value returned from the VM state into an Elixir type.

Evaluates the given script within the VM state and returns the result.

As eval/2, but returns the result or raises an exception.

Evaluates the given script within the VM state and returns the modified state.

As exec/2, but returns the modified state or raises an exception.

Executes the given function with the given arguments.

As exec_function/2, but returns the result or raises an exception.

Extends the VM state with the given module or modules.

Returns the value from the specified path on the VM state.

As get/2, but returns the result or raises an exception.

Initliazes a new VM state.

Sets the value at the specified path on the given VM state and returns a modified VM state.

As set/4, but returns the VM state or raises an exception.

Sets an Elixir function at the specified path on the given VM state and returns the modified VM state.

As set_function/4, but returns the VM state or raises an exception.

Link to this section Types

Link to this type

lua_output()

View Source
lua_output() :: binary() | number() | list() | map()

Operate return value

Link to this type

lua_path()

View Source
lua_path() :: atom() | String.t() | list()

Lua table path. Either a dot-delimited string or list of strings or atoms.

Operate VM state

Link to this section Functions

Link to this function

call(vm, path, args \\ [])

View Source
call(Operate.VM.t(), Operate.VM.lua_path(), list()) ::
  {:ok, Operate.VM.lua_output()} | {:error, String.t()}

Calls a function within the VM state at the given lua path and returns the result.

Examples

iex> Operate.VM.init
...> |> Operate.VM.exec!("function main() return 'hello world' end")
...> |> Operate.VM.call(:main)
{:ok, "hello world"}

iex> Operate.VM.init
...> |> Operate.VM.exec!("function sum(a, b) return a + b end")
...> |> Operate.VM.call(:sum, [2, 3])
{:ok, 5}

As call/3, but returns the result or raises an exception.

Examples

iex> Operate.VM.init
...> |> Operate.VM.exec!("function main() return 'hello world' end")
...> |> Operate.VM.call!(:main)
"hello world"

Decodes a value returned from the VM state into an Elixir type.

Automatically detects when 64 bit long numbers can be converted to integers, and handles converting Lua tables into either Elixir lists or maps.

Examples

iex> Operate.VM.decode(23.23)
23.23

iex> Operate.VM.decode(23.0)
23

iex> Operate.VM.decode([{"foo", 1}, {"bar", 2}])
%{"foo" => 1, "bar" => 2}

Evaluates the given script within the VM state and returns the result.

Examples

iex> Operate.VM.init
...> |> Operate.VM.eval("return 'hello world'")
{:ok, "hello world"}

iex> Operate.VM.init
...> |> Operate.VM.eval("return 2 / 3")
{:ok, 0.6666666666666666}

As eval/2, but returns the result or raises an exception.

Examples

iex> Operate.VM.init
...> |> Operate.VM.eval!("return 'hello world'")
"hello world"
Link to this function

exec(vm, code)

View Source
exec(Operate.VM.t(), String.t()) :: {:ok, Operate.VM.t()} | {:error, String.t()}

Evaluates the given script within the VM state and returns the modified state.

As exec/2, but returns the modified state or raises an exception.

Link to this function

exec_function(function, args \\ [])

View Source
exec_function(function(), list()) ::
  {:ok, Operate.VM.lua_output()} | {:error, String.t()}

Executes the given function with the given arguments.

Examples

iex> Operate.VM.init
...> |> Operate.VM.eval!("return function(a,b) return a * b end")
...> |> Operate.VM.exec_function([3,4])
{:ok, 12}
Link to this function

exec_function!(function, args \\ [])

View Source
exec_function!(function(), list()) :: Operate.VM.lua_output()

As exec_function/2, but returns the result or raises an exception.

Examples

iex> Operate.VM.init
...> |> Operate.VM.eval!("return function(a,b) return a .. ' ' .. b end")
...> |> Operate.VM.exec_function!(["hello", "world"])
"hello world"

Extends the VM state with the given module or modules.

Examples

Operate.VM.extend(vm, [MyLuaExtension, OtherExtension])

Returns the value from the specified path on the VM state.

Examples

iex> Operate.VM.init
...> |> Operate.VM.set!("foo.bar", 42, force: true)
...> |> Operate.VM.get("foo")
{:ok, %{"bar" => 42}}

iex> Operate.VM.init
...> |> Operate.VM.set!("foo.bar", 42, force: true)
...> |> Operate.VM.get("foo.bar")
{:ok, 42}

As get/2, but returns the result or raises an exception.

Examples

iex> Operate.VM.init
...> |> Operate.VM.set!("foo.bar", 42, force: true)
...> |> Operate.VM.get!("foo.bar")
42

Initliazes a new VM state.

Options

The accepted options are:

  • :extensions - Provide a list of modules with which to extend the VM state.

Examples

iex> vm = Operate.VM.init
...> elem(vm, 0) == :luerl
true
Link to this function

set(vm, path, value, options \\ [])

View Source
set(Operate.VM.t(), Operate.VM.lua_path(), any(), keyword()) ::
  {:ok, Operate.VM.t()} | {:error, String.t()}

Sets the value at the specified path on the given VM state and returns a modified VM state.

Options

The accepted options are:

  • :force - Recusively set the value at a deep path that doesn't already exist.

Examples

iex> {:ok, vm} = Operate.VM.init
...> |> Operate.VM.set("foo.bar", 42, force: true)
...> elem(vm, 0)
:luerl
Link to this function

set!(vm, path, value, options \\ [])

View Source

As set/4, but returns the VM state or raises an exception.

Options

The accepted options are:

  • :force - Recusively set the value at a deep path that doesn't already exist.

Examples

iex> vm = Operate.VM.init
...> |> Operate.VM.set!("foo.bar", 42, force: true)
...> elem(vm, 0)
:luerl
Link to this function

set_function(vm, path, callback, options \\ [])

View Source
set_function(Operate.VM.t(), Operate.VM.lua_path(), function(), keyword()) ::
  {:ok, Operate.VM.t()} | {:error, String.t()}

Sets an Elixir function at the specified path on the given VM state and returns the modified VM state.

Options

The accepted options are:

  • :force - Recusively set the value at a deep path that doesn't already exist.
Link to this function

set_function!(vm, path, callback, options \\ [])

View Source

As set_function/4, but returns the VM state or raises an exception.

Options

The accepted options are:

  • :force - Recusively set the value at a deep path that doesn't already exist.