Lustex v0.2.0 Lustex.Script

lua script execution

This module provides support for executing custom scripts written in the Lua language. These scripts can take on one of the following forms.

  • expression: a simple Lua expression that returns a result from the context
  • chunk: a script block that ends with a return statement
  • function: a script block that defines one or more exported functions

Expressions can be evaluated using eval, chunks can be executed using exec, and functions can be called using call. Eval/exec accept a context object that can be used to expose global variables to the script. Call accepts a positional parameter list that is passed to the function.

Scripts can be pre-compiled using the compile function. All of the execution methods support pre-compiled scripts

Link to this section Summary

Functions

calls a Lua function and returns an error tuple on failure

calls a Lua function and throws on failure

generates a Lua-compatible callback for a function

generates a Lua-compatible callback for a module/function

compiles a Lua script block

compiles a Lua script block, throwing on error

evaluates a Lua expression (i.e. “x > 0”) within a context and returns an error tuple on failure

evaluates a Lua expression (i.e. “x > 0”) within a context and raises on failure

executes a Lua script within a context and returns an error tuple on failure

executes a Lua script within a context and raises on failure

Link to this section Types

Link to this type compiled()
compiled() :: {chunk :: any(), state :: any()}

Link to this section Functions

Link to this function call(script, function, args)
call(script :: String.t() | compiled(), function :: String.t(), [args :: any()]) ::
  {:ok, any()} |
  {:error, any()}

calls a Lua function and returns an error tuple on failure

Link to this function call!(script, function, args)
call!(script :: String.t() | compiled(), function :: String.t(), [args :: any()]) :: any()

calls a Lua function and throws on failure

Link to this function callback(function)
callback(function :: (... -> any())) :: ([any()] -> [any()])

generates a Lua-compatible callback for a function

Link to this function callback(module, function)
callback(module :: module(), function :: (... -> any())) :: ([any()] -> [any()])

generates a Lua-compatible callback for a module/function

Link to this function compile(script, options \\ [])
compile(script :: String.t(), options :: keyword()) ::
  {:ok, compiled()} |
  {:error, any()}

compiles a Lua script block

optiondescriptiondefault
globalsmap of global variables to assign%{}
eval?evaluate the script to initialze globals?false
Link to this function compile!(script, options \\ [])
compile!(script :: String.t(), options :: keyword()) :: compiled()

compiles a Lua script block, throwing on error

Link to this function eval(expr, context)
eval(script :: String.t(), context :: map()) ::
  {:ok, any()} |
  {:error, any()}

evaluates a Lua expression (i.e. “x > 0”) within a context and returns an error tuple on failure

Link to this function eval!(expr, context)
eval!(script :: String.t(), context :: map()) :: any()

evaluates a Lua expression (i.e. “x > 0”) within a context and raises on failure

Link to this function exec(script, context)
exec(script :: String.t() | compiled(), context :: map()) ::
  {:ok, any()} |
  {:error, any()}

executes a Lua script within a context and returns an error tuple on failure

Link to this function exec!(script, context)
exec!(script :: String.t() | compiled(), context :: map()) :: any()

executes a Lua script within a context and raises on failure