Crate rhai

Source
Expand description

§Rhai - embedded scripting for Rust

Rhai logo

Rhai is a tiny, simple and fast embedded scripting language for Rust that gives you a safe and easy way to add scripting to your applications.

It provides a familiar syntax based on JavaScript+Rust and a simple Rust interface.

§A Quick Example

§Contents of my_script.rhai

/// Brute force factorial function
fn factorial(x) {
    if x == 1 { return 1; }
    x * factorial(x - 1)
}

// Calling an external function 'compute'
compute(factorial(10))

§The Rust part

use rhai::{Engine, EvalAltResult};

fn main() -> Result<(), Box<EvalAltResult>>
{
    // Define external function
    fn compute_something(x: i64) -> bool {
        (x % 40) == 0
    }

    // Create scripting engine
    let mut engine = Engine::new();

    // Register external function as 'compute'
    engine.register_fn("compute", compute_something);

    // Evaluate the script, expecting a 'bool' result
    let result: bool = engine.eval_file("my_script.rhai".into())?;

    assert_eq!(result, true);

    Ok(())
}

§Features

§On-Line Documentation

See The Rhai Book for details on the Rhai scripting engine and language.

Modules§

config
Configuration for Rhai.
module_resolvers
Module containing all built-in module resolvers.
packages
Module containing all built-in packages available to Rhai, plus facilities to define custom packages.
plugin
Module defining macros for developing plugins.

Macros§

combine_with_exported_module
Macro to combine a plugin module into an existing module.
def_package
Macro that makes it easy to define a package (which is basically a shared module) and register functions into it.
exported_module
Macro to generate a Rhai Module from a plugin module defined via #[export_module].
register_exported_fn
Macro to register a plugin function (defined via #[export_fn]) into an Engine.
set_exported_fn
Macro to register a plugin function into a Rhai Module.
set_exported_global_fn
Macro to register a plugin function into a Rhai Module and expose it globally.

Structs§

AST
Compiled AST (abstract syntax tree) of a Rhai script.
CallFnOptions
Options for calling a script-defined function via Engine::call_fn_with_options.
Dynamic
Dynamic type containing any value.
Engine
Rhai main scripting engine.
EvalContext
Context of a script evaluation process.
Expression
An expression sub-tree in an AST.
FnPtr
A general function pointer, which may carry additional (i.e. curried) argument values to be passed onto a function during a call.
FuncRegistration
Type for fine-tuned module function registration.
ImmutableString
The system immutable string type.
Instant
A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.
Locked
A reader-writer lock
Module
A module which may contain variables, sub-modules, external Rust functions, and/or script-defined functions.
NativeCallContext
Context of a native Rust function call.
ParseError
Error when parsing a script.
Position
A location (line number + character position) in the input script.
Scope
Type containing information about the current scope. Useful for keeping state between Engine evaluation runs.
ScriptFnMetadata
A type containing the metadata of a script-defined function.
Shared
A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
TypeBuilder
Builder to build the API of a custom type for use with an Engine.
VarDefInfo
Information on a variable declaration.

Enums§

EvalAltResult
Evaluation result.
FnAccess
A type representing the access mode of a function.
FnNamespace
A type representing the namespace of a function.
LexError
Error encountered when tokenizing the script text.
OptimizationLevel
Level of optimization performed.
ParseErrorType
Error encountered when parsing a script.

Constants§

FUNC_TO_DEBUG
Standard debug-print function.
FUNC_TO_STRING
Standard pretty-print function.
OP_CONTAINS
Standard containment testing function.
OP_EQUALS
Standard equality comparison operator.

Traits§

CustomType
Trait to build the API of a custom type for use with an Engine (i.e. register the type and its getters, setters, methods, etc.).
Func
Trait to create a Rust closure from a script.
FuncArgs
Trait that parses arguments to a function call.
ModuleResolver
Trait that encapsulates a module resolution service.
RhaiNativeFunc
Trait to register custom Rust functions.

Functions§

eval
Evaluate a string as a script, returning the result value or an error.
eval_file
Evaluate a script file, returning the result value or an error.
format_map_as_json
Return the JSON representation of an object map.
run
Evaluate a string as a script.
run_file
Evaluate a file.

Type Aliases§

Array
Variable-sized array of Dynamic values.
Blob
Variable-sized array of u8 values (byte array).
FLOAT
The system floating-point type. It is defined as f64.
INT
The system integer type. It is defined as i64.
Map
A dictionary of Dynamic values with string keys.

Attribute Macros§

export_fn
Attribute, when put on a Rust function, turns it into a plugin function.
export_module
Attribute, when put on a Rust module, turns it into a plugin module.
expose_under_internals
Macro to automatically expose a Rust function, type-def or use statement as pub when under the internals feature.

Derive Macros§

CustomType
Macro to implement the [CustomType][rhai::CustomType] trait.