Rhai.Engine (rhai_rustler v1.0.2)

Rhai main scripting engine.

Summary

Functions

Is anonymous function allowed? Default is true.

Is if-expression allowed? Default is true.

Are loop-expression allowed? Default is true.

Is looping allowed? Default is true.

Is shadowing allowed? Default is true.

Is statement_expression allowed? Default is true.

Is switch expression allowed? Default is true.

Call a script function defined in an AST with multiple arguments.

Compact a script to eliminate insignificant whitespaces and comments. This is useful to prepare a script for further compressing. The output script is semantically identical to the input script, except smaller in size. Unlike other uglifiers and minifiers, this method does not rename variables nor perform any optimization on the input script.

Compile a string into an AST, which can be used later for evaluation.

Compile a string containing an expression into an AST, which can be used later for evaluation.

Compile a string containing an expression into an AST using own scope, which can be used later for evaluation.

Compile a script file into an AST, which can be used later for evaluation.

Compile a script file into an AST using own scope, which can be used later for evaluation.

Compile a string into an AST using own scope, which can be used later for evaluation, embedding all imported modules. Modules referred by import statements containing literal string paths are eagerly resolved via the current module resolver and embedded into the resultant AST. When it is evaluated later, import statement directly recall pre-resolved modules and the resolution process is not performed again.

When passed a list of strings, first join the strings into one large script, and then compile them into an AST using own scope, which can be used later for evaluation.

Compile a string into an AST using own scope, which can be used later for evaluation.

Disable a particular keyword or operator in the language.

Return an error if the size of a Dynamic is out of limits (if any).

Evaluate a string as a script, returning the result value or an error.

Evaluate an AST, returning the result value or an error.

Evaluate an AST with own scope, returning the result value or an error.

Evaluate a string containing an expression, returning the result value or an error.

Evaluate a string containing an expression with own scope, returning the result value or an error.

Evaluate a script file, returning the result value or an error.

Evaluate a script file with own scope, returning the result value or an error.

Evaluate a string as a script with own scope, returning the result value or an error.

Set whether to raise error if an object map property does not exist.

Is fast operators mode enabled? Default is false.

The maximum length of arrays (0 for unlimited).

Is fast operators mode enabled? Default is false.

The depth limit for expressions (0 for unlimited).

The depth limit for expressions in functions (0 for unlimited).

The maximum size of object maps (0 for unlimited).

The maximum number of imported modules allowed for a script.

The maximum number of operations allowed for a script to run (0 for unlimited).

The maximum length, in bytes, of strings (0 for unlimited).

Create a new Engine

Create a new Engine with minimal built-in functions.

The current optimization level. It controls whether and how the Engine will optimize an AST after compilation.

Optimize the AST with constants defined in an external Scope. An optimized copy of the AST is returned while the original AST is consumed.

Register a custom operator with a precedence into the language.

Register a custom operator with a precedence into the language.

Register a shared dylib Module into the global namespace of Engine.

Register a shared dylib Module into the global namespace of Engine.

Register the package with an Engine.

Register a shared Module into the namespace of Engine.

Register a shared Module into the namespace of Engine.

Evaluate a string as script.

Evaluate an AST.

Evaluate an AST with own scope.

Evaluate a file.

Evaluate a file with own scope.

Evaluate a string as script with own scope.

Set whether anonymous function is allowed.

Set whether if-expression is allowed.

Set whether loop expressions are allowed.

Set whether looping is allowed.

Set whether shadowing is allowed.

Set whether statement_expression is allowed.

Set whether switch expression is allowed.

Set whether to raise error if an object map property does not exist.

Set whether fast operators mode is enabled.

Set the maximum length of arrays (0 for unlimited).

Set the maximum levels of function calls allowed for a script in order to avoid infinite recursion and stack overflows.

Set the depth limits for expressions (0 for unlimited).

Set the maximum size of object maps (0 for unlimited).

Set the maximum number of imported modules allowed for a script.

Set the maximum number of operations allowed for a script to run to avoid consuming too much resources (0 for unlimited).

Set the maximum length, in bytes, of strings (0 for unlimited).

Set the module resolution services used by the Engine.

Control whether and how the Engine will optimize an AST after compilation.

Set whether strict variables mode is enabled.

Is strict variables mode enabled? Default is false.

Types

@type t() :: %Rhai.Engine{reference: term(), resource: term()}

Functions

Link to this function

allow_anonymous_fn?(engine)

@spec allow_anonymous_fn?(t()) :: boolean()

Is anonymous function allowed? Default is true.

Not available under no_function.

Link to this function

allow_if_expression?(engine)

@spec allow_if_expression?(t()) :: boolean()

Is if-expression allowed? Default is true.

Link to this function

allow_loop_expressions?(engine)

@spec allow_loop_expressions?(t()) :: boolean()

Are loop-expression allowed? Default is true.

Link to this function

allow_looping?(engine)

@spec allow_looping?(t()) :: boolean()

Is looping allowed? Default is true.

Link to this function

allow_shadowing?(engine)

@spec allow_shadowing?(t()) :: boolean()

Is shadowing allowed? Default is true.

Link to this function

allow_statement_expression?(engine)

@spec allow_statement_expression?(t()) :: boolean()

Is statement_expression allowed? Default is true.

Link to this function

allow_switch_expression?(engine)

@spec allow_switch_expression?(t()) :: boolean()

Is switch expression allowed? Default is true.

Link to this function

call_fn(engine, scope, ast, name, args)

@spec call_fn(t(), Rhai.Scope.t(), Rhai.AST.t(), String.t(), list()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Call a script function defined in an AST with multiple arguments.

Link to this function

compact_script(engine, script)

@spec compact_script(t(), String.t()) :: String.t()

Compact a script to eliminate insignificant whitespaces and comments. This is useful to prepare a script for further compressing. The output script is semantically identical to the input script, except smaller in size. Unlike other uglifiers and minifiers, this method does not rename variables nor perform any optimization on the input script.

Link to this function

compile(engine, script)

@spec compile(t(), String.t()) :: {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a string into an AST, which can be used later for evaluation.

Link to this function

compile_expression(engine, script)

@spec compile_expression(t(), String.t()) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a string containing an expression into an AST, which can be used later for evaluation.

Link to this function

compile_expression_with_scope(engine, scope, script)

@spec compile_expression_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a string containing an expression into an AST using own scope, which can be used later for evaluation.

Link to this function

compile_file(engine, path)

@spec compile_file(t(), String.t()) :: {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a script file into an AST, which can be used later for evaluation.

Link to this function

compile_file_with_scope(engine, scope, script)

@spec compile_file_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a script file into an AST using own scope, which can be used later for evaluation.

Link to this function

compile_into_self_contained(engine, scope, script)

@spec compile_into_self_contained(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a string into an AST using own scope, which can be used later for evaluation, embedding all imported modules. Modules referred by import statements containing literal string paths are eagerly resolved via the current module resolver and embedded into the resultant AST. When it is evaluated later, import statement directly recall pre-resolved modules and the resolution process is not performed again.

Link to this function

compile_scripts_with_scope(engine, scope, script)

@spec compile_scripts_with_scope(t(), Rhai.Scope.t(), [String.t()]) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

When passed a list of strings, first join the strings into one large script, and then compile them into an AST using own scope, which can be used later for evaluation.

The scope is useful for passing constants into the script for optimization when using :full optimization level.

Link to this function

compile_with_scope(engine, scope, script)

@spec compile_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.AST.t()} | {:error, Rhai.Error.t()}

Compile a string into an AST using own scope, which can be used later for evaluation.

Constants Propagation: If not optimization_level = :none, constants defined within the scope are propagated throughout the script including functions. This allows functions to be optimized based on dynamic global constants.

Link to this function

disable_symbol(engine, symbol)

@spec disable_symbol(t(), String.t()) :: t()

Disable a particular keyword or operator in the language.

Link to this function

ensure_data_size_within_limits(engine, value)

@spec ensure_data_size_within_limits(t(), Rhai.Any.t()) :: :ok | Rhai.Error.t()

Return an error if the size of a Dynamic is out of limits (if any).

Link to this function

eval(engine, script)

@spec eval(t(), String.t()) :: {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a string as a script, returning the result value or an error.

Link to this function

eval_ast(engine, ast)

@spec eval_ast(t(), Rhai.AST.t()) :: {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate an AST, returning the result value or an error.

Link to this function

eval_ast_with_scope(engine, scope, ast)

@spec eval_ast_with_scope(t(), Rhai.Scope.t(), Rhai.AST.t()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate an AST with own scope, returning the result value or an error.

Link to this function

eval_expression(engine, script)

@spec eval_expression(t(), String.t()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a string containing an expression, returning the result value or an error.

Link to this function

eval_expression_with_scope(engine, scope, script)

@spec eval_expression_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a string containing an expression with own scope, returning the result value or an error.

Link to this function

eval_file(engine, path)

@spec eval_file(t(), String.t()) :: {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a script file, returning the result value or an error.

Link to this function

eval_file_with_scope(engine, scope, path)

@spec eval_file_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a script file with own scope, returning the result value or an error.

Link to this function

eval_with_scope(engine, scope, script)

@spec eval_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  {:ok, Rhai.Any.t()} | {:error, Rhai.Error.t()}

Evaluate a string as a script with own scope, returning the result value or an error.

Link to this function

fail_on_invalid_map_property?(engine)

@spec fail_on_invalid_map_property?(t()) :: boolean()

Set whether to raise error if an object map property does not exist.

Link to this function

fast_operators?(engine)

@spec fast_operators?(t()) :: boolean()

Is fast operators mode enabled? Default is false.

Link to this function

max_array_size(engine)

@spec max_array_size(t()) :: integer()

The maximum length of arrays (0 for unlimited).

Zero under no_index.

Link to this function

max_call_levels(engine)

@spec max_call_levels(t()) :: non_neg_integer()

Is fast operators mode enabled? Default is false.

Link to this function

max_expr_depth(engine)

@spec max_expr_depth(t()) :: non_neg_integer()

The depth limit for expressions (0 for unlimited).

Link to this function

max_function_expr_depth(engine)

@spec max_function_expr_depth(t()) :: non_neg_integer()

The depth limit for expressions in functions (0 for unlimited).

Zero under no_function.

Link to this function

max_map_size(engine)

@spec max_map_size(t()) :: non_neg_integer()

The maximum size of object maps (0 for unlimited).

Zero under no_object.

Link to this function

max_modules(engine)

@spec max_modules(t()) :: non_neg_integer()

The maximum number of imported modules allowed for a script.

Zero under no_module.

Link to this function

max_operations(engine)

@spec max_operations(t()) :: non_neg_integer()

The maximum number of operations allowed for a script to run (0 for unlimited).

Not available under unchecked.

Link to this function

max_string_size(engine)

@spec max_string_size(t()) :: non_neg_integer()

The maximum length, in bytes, of strings (0 for unlimited).

@spec new() :: t()

Create a new Engine

@spec new_raw() :: t()

Create a new Engine with minimal built-in functions.

Link to this function

optimization_level(engine)

@spec optimization_level(t()) :: :none | :simple | :full

The current optimization level. It controls whether and how the Engine will optimize an AST after compilation.

Link to this function

optimize_ast(engine, scope, ast, optimization_level)

@spec optimize_ast(t(), Rhai.Scope.t(), Rhai.AST.t(), :none | :simple | :full) ::
  Rhai.AST.t()

Optimize the AST with constants defined in an external Scope. An optimized copy of the AST is returned while the original AST is consumed.

Although optimization is performed by default during compilation, sometimes it is necessary to re-optimize an AST. For example, when working with constants that are passed in via an external scope, it will be more efficient to optimize the AST once again to take advantage of the new constants. With this method, it is no longer necessary to recompile a large script. The script AST can be compiled just once. Before evaluation, constants are passed into the Engine via an external scope (i.e. with Rhai.Scope.push_constant/2). Then, the AST is cloned and the copy re-optimized before running.

Link to this function

register_custom_operator(engine, operator, precedence)

@spec register_custom_operator(t(), String.t(), non_neg_integer()) ::
  {:ok, t()} | {:error, {:custom_operator, String.t()}}

Register a custom operator with a precedence into the language.

The operator can be a valid identifier, a reserved symbol, a disabled operator or a disabled keyword. The precedence cannot be zero.

Link to this function

register_custom_operator!(engine, operator, precedence)

@spec register_custom_operator!(t(), String.t(), non_neg_integer()) :: t()

Register a custom operator with a precedence into the language.

The operator can be a valid identifier, a reserved symbol, a disabled operator or a disabled keyword. The precedence cannot be zero.

Raises if the operator cannot be registered.

Link to this function

register_global_module(engine, path)

@spec register_global_module(t(), String.t()) ::
  {:ok, t()} | {:error, {:runtime, String.t()}}

Register a shared dylib Module into the global namespace of Engine.

All functions and type iterators are automatically available to scripts without namespace qualifications. Sub-modules and variables are ignored. When searching for functions, modules loaded later are preferred. In other words, loaded modules are searched in reverse order.

Returns an error if the module cannot be loaded.

Link to this function

register_global_module!(engine, path)

@spec register_global_module!(t(), String.t()) :: t()

Register a shared dylib Module into the global namespace of Engine.

All functions and type iterators are automatically available to scripts without namespace qualifications. Sub-modules and variables are ignored. When searching for functions, modules loaded later are preferred. In other words, loaded modules are searched in reverse order.

Raises an error if the module cannot be loaded.

Link to this function

register_package(engine, package)

@spec register_package(t(), Rhai.Package.t()) :: t()

Register the package with an Engine.

Link to this function

register_static_module(engine, namespace, path)

@spec register_static_module(t(), String.t(), String.t()) ::
  {:ok, t()} | {:error, {:runtime, String.t()}}

Register a shared Module into the namespace of Engine.

Returns an error if the module cannot be loaded.

Link to this function

register_static_module!(engine, namespace, path)

@spec register_static_module!(t(), String.t(), String.t()) :: t()

Register a shared Module into the namespace of Engine.

Raises an error if the module cannot be loaded.

Link to this function

run(engine, script)

@spec run(t(), String.t()) :: :ok | {:error, Rhai.Error.t()}

Evaluate a string as script.

Link to this function

run_ast(engine, ast)

@spec run_ast(t(), Rhai.AST.t()) :: :ok | {:error, Rhai.Error.t()}

Evaluate an AST.

Link to this function

run_ast_with_scope(engine, scope, ast)

@spec run_ast_with_scope(t(), Rhai.Scope.t(), Rhai.AST.t()) ::
  :ok | {:error, Rhai.Error.t()}

Evaluate an AST with own scope.

Link to this function

run_file(engine, path)

@spec run_file(t(), String.t()) :: :ok | {:error, Rhai.Error.t()}

Evaluate a file.

Link to this function

run_file_with_scope(engine, scope, path)

@spec run_file_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  :ok | {:error, Rhai.Error.t()}

Evaluate a file with own scope.

Constants Propagation If the optimization_level is not :none constants defined within the scope are propagated throughout the script including functions. This allows functions to be optimized based on dynamic global constants.

Link to this function

run_with_scope(engine, scope, script)

@spec run_with_scope(t(), Rhai.Scope.t(), String.t()) ::
  :ok | {:error, Rhai.Error.t()}

Evaluate a string as script with own scope.

Constants Propagation If the optimization_level is not :none constants defined within the scope are propagated throughout the script including functions. This allows functions to be optimized based on dynamic global constants.

Link to this function

set_allow_anonymous_fn(engine, enable)

@spec set_allow_anonymous_fn(t(), boolean()) :: t()

Set whether anonymous function is allowed.

Not available under no_function.

Link to this function

set_allow_if_expression(engine, enable)

@spec set_allow_if_expression(t(), boolean()) :: t()

Set whether if-expression is allowed.

Link to this function

set_allow_loop_expressions(engine, enable)

@spec set_allow_loop_expressions(t(), boolean()) :: t()

Set whether loop expressions are allowed.

Link to this function

set_allow_looping(engine, enable)

@spec set_allow_looping(t(), boolean()) :: t()

Set whether looping is allowed.

Link to this function

set_allow_shadowing(engine, enable)

@spec set_allow_shadowing(t(), boolean()) :: t()

Set whether shadowing is allowed.

Link to this function

set_allow_statement_expression(engine, enable)

@spec set_allow_statement_expression(t(), boolean()) :: t()

Set whether statement_expression is allowed.

Link to this function

set_allow_switch_expression(engine, enable)

@spec set_allow_switch_expression(t(), boolean()) :: t()

Set whether switch expression is allowed.

Link to this function

set_fail_on_invalid_map_property(engine, enable)

@spec set_fail_on_invalid_map_property(t(), boolean()) :: t()

Set whether to raise error if an object map property does not exist.

Link to this function

set_fast_operators(engine, enable)

@spec set_fast_operators(t(), boolean()) :: t()

Set whether fast operators mode is enabled.

Link to this function

set_max_array_size(engine, max_size)

@spec set_max_array_size(t(), integer()) :: t()

Set the maximum length of arrays (0 for unlimited).

Not available under unchecked or no_index.

Link to this function

set_max_call_levels(engine, levels)

@spec set_max_call_levels(t(), non_neg_integer()) :: t()

Set the maximum levels of function calls allowed for a script in order to avoid infinite recursion and stack overflows.

Not available under unchecked or no_function.

Link to this function

set_max_expr_depths(engine, max_expr_depth, max_function_expr_depth)

@spec set_max_expr_depths(t(), non_neg_integer(), non_neg_integer()) :: t()

Set the depth limits for expressions (0 for unlimited).

Not available under unchecked.

Link to this function

set_max_map_size(engine, size)

@spec set_max_map_size(t(), non_neg_integer()) :: t()

Set the maximum size of object maps (0 for unlimited).

Not available under unchecked or no_object.

Link to this function

set_max_modules(engine, modules)

@spec set_max_modules(t(), non_neg_integer()) :: t()

Set the maximum number of imported modules allowed for a script.

Not available under unchecked or no_module.

Link to this function

set_max_operations(engine, operations)

@spec set_max_operations(t(), non_neg_integer()) :: t()

Set the maximum number of operations allowed for a script to run to avoid consuming too much resources (0 for unlimited).

Not available under unchecked.

Link to this function

set_max_string_size(engine, string_size)

@spec set_max_string_size(t(), non_neg_integer()) :: t()

Set the maximum length, in bytes, of strings (0 for unlimited).

Not available under unchecked.

Link to this function

set_module_resolvers(engine, module_resolvers)

@spec set_module_resolvers(t(), [:file | :dylib]) :: t()

Set the module resolution services used by the Engine.

This library supports :file and :dylib module resolvers.

Link to this function

set_optimization_level(engine, optimization_level)

@spec set_optimization_level(t(), :none | :simple | :full) :: t()

Control whether and how the Engine will optimize an AST after compilation.

Link to this function

set_strict_variables(engine, enable)

@spec set_strict_variables(t(), boolean()) :: t()

Set whether strict variables mode is enabled.

Link to this function

strict_variables?(engine)

@spec strict_variables?(t()) :: boolean()

Is strict variables mode enabled? Default is false.