Operate v0.1.0-beta.15 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.

Parses the given map decoded from the VM, and recursively transforms it into a keyword list with atom keys.

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

Specs

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

Operate return value

Specs

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

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

Specs

t() :: {:luerl, tuple()}

Operate VM state

Link to this section Functions

Link to this function

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

View Source

Specs

call(t(), lua_path(), list()) :: {:ok, 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}
Link to this function

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

View Source

Specs

call!(t(), lua_path(), list()) :: lua_output()

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"

Specs

decode(binary() | number() | list()) :: lua_output()

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}

Specs

eval(t(), String.t()) :: {:ok, lua_output()} | {:error, String.t()}

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}

Specs

eval!(t(), String.t()) :: lua_output()

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

Examples

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

Specs

exec(t(), String.t()) :: {:ok, t()} | {:error, String.t()}

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

Specs

exec!(t(), String.t()) :: t()

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

Link to this function

exec_function(function, args \\ [])

View Source

Specs

exec_function(function(), list()) :: {:ok, 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

Specs

exec_function!(function(), list()) :: 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"

Specs

extend(t(), list() | module()) :: t()

Extends the VM state with the given module or modules.

Examples

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

Specs

get(t(), lua_path()) :: {:ok, lua_output()} | {:error, String.t()}

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}

Specs

get!(t(), lua_path()) :: lua_output()

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

Specs

init(keyword()) :: t()

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

Specs

parse_opts(map()) :: keyword()

Parses the given map decoded from the VM, and recursively transforms it into a keyword list with atom keys.

Examples

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

iex> [{"foo", 1}, {"bar", 2}]
...> |> Operate.VM.decode
...> |> Operate.VM.parse_opts
[bar: 2, foo: 1]
Link to this function

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

View Source

Specs

set(t(), lua_path(), any(), keyword()) :: {:ok, 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

Specs

set!(t(), lua_path(), any(), keyword()) :: t()

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

Specs

set_function(t(), lua_path(), function(), keyword()) ::
  {:ok, 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

Specs

set_function!(t(), lua_path(), function(), keyword()) :: t()

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.