Expand description
Rhai - embedded scripting for Rust
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
-
default— Default features:std, uses runtime random numbers for hashing. -
std(enabled by default) — Standard features: uses compile-time random number for hashing.
Enable Special Functionalities
-
sync— Require that all data types implementSend + Sync(for multi-threaded usage). -
decimal— Add support for theDecimaldata type (acts as the system floating-point type underno_float). -
serde— Enable serialization/deserialization of Rhai data types viaserde. -
unicode-xid-ident— Allow Unicode Standard Annex #31 for identifiers. -
metadata— Enable functions metadata (including doc-comments); impliesserde. -
internals— Expose internal data structures (e.g.ASTnodes). -
debugging— Enable the debugging interface (impliesinternals). -
bin-features— Features and dependencies required bybintools:decimal,metadata,serde,debuggingandrustyline.
System Configuration Features
-
f32_float— Usef32instead off64as the system floating-point number type. -
only_i32— Usei32instead ofi64for the system integer number type (useful for 32-bit architectures). All other integer types (e.g.u8) are disabled. -
only_i64— Disable all integer types (e.g.u8) other thani64.
Disable Language Features
-
no_float— Remove support for floating-point numbers. -
no_index— Remove support for arrays and indexing. -
no_object— Remove support for custom types, properties, method-style calls and object maps. -
no_time— Remove support for time-stamps. -
no_function— Remove support for script-defined functions (impliesno_closure). -
no_closure— Remove support for capturing external variables in anonymous functions (i.e. closures). -
no_module— Remove support for loading external modules. -
no_custom_syntax— Remove support for custom syntax.
Performance-Related Features
-
unchecked— Disable all safety checks. -
no_position— Do not track position when parsing. -
no_optimize— Disable the script optimizer.
Compiling for no-std
no_std— Turn onno-stdcompilation (nightly only).
JavaScript Interface for WASM
-
wasm-bindgen— Usewasm-bindgenas JavaScript interface. -
stdweb— Usestdwebas JavaScript interface.
Features used in testing environments only
testing-environ— Running under a testing environment.
On-Line Documentation
See The Rhai Book for details on the Rhai scripting engine and language.
Modules
- Configuration for Rhai.
- (debugging) Module containing types for debugging. Exported under the
debuggingfeature only. - Module containing all built-in module resolvers available to Rhai. Module containing all built-in module resolvers.
- Module containing all built-in packages available to Rhai, plus facilities to define custom packages.
- Module defining macros for developing plugins.
- (serde) Serialization and deserialization support for
serde. Exported under theserdefeature only.
Macros
- Macro to combine a plugin module into an existing module.
- Macro that makes it easy to define a package (which is basically a shared module) and register functions into it.
- Macro to generate a Rhai
Modulefrom a plugin module defined via#[export_module]. - Macro to register a plugin function (defined via
#[export_fn]) into anEngine. - Macro to register a plugin function into a Rhai
Module. - Macro to register a plugin function into a Rhai
Moduleand expose it globally.
Structs
- Compiled AST (abstract syntax tree) of a Rhai script.
- (internals) Bit-flags containing
ASTnode configuration options. Exported under theinternalsfeature only. - (internals) A binary expression. Exported under the
internalsfeature only. - (internals) A type containing system-wide caches. Exported under the
internalsfeature only. - Options for calling a script-defined function via
Engine::call_fn_with_options. - An expression with a condition.
- (internals) A custom syntax expression. Exported under the
internalsfeature only. - (internals) Information for a registered custom type. Exported under the
internalsfeature only. - (metadata, internals) Definitions helper type to generate definition files based on the contents of an
Engine. Exported under theinternalsandmetadatafeature only. - Dynamic type containing any value.
- (internals) Lock guard for reading a
Dynamic. Exported under theinternalsfeature only. - (internals) Lock guard for writing a
Dynamic. Exported under theinternalsfeature only. - (internals) Encapsulated AST environment. Exported under the
internalsfeature only. - Rhai main scripting engine.
- Context of a script evaluation process.
- An expression sub-tree in an
AST. - A type that wraps a floating-point number and implements
Hash. - (internals) A flow control block containing:
- (internals) A function call. Exported under the
internalsfeature only. - (internals) A set of function call hashes. Exported under the
internalsfeature only. - A general function pointer, which may carry additional (i.e. curried) argument values to be passed onto a function during a call.
- (internals) A function resolution cache with a bloom filter. Exported under the
internalsfeature only. - (internals) An entry in a function resolution cache. Exported under the
internalsfeature only. - (internals) Global runtime states. Exported under the
internalsfeature only. - (internals) An identifier containing a name and a position. Exported under the
internalsfeature only. - The system immutable string type.
- A measurement of a monotonically nondecreasing clock. Opaque and useful only with
Duration. - A module which may contain variables, sub-modules, external Rust functions, and/or script-defined functions.
- (internals) A type that implements the
InputStreamtrait. Exported under theinternalsfeature only. - (internals) A chain of module names to namespace-qualify a variable or function call. Exported under the
internalsfeature only. - Context of a native Rust function call.
- NativeCallContextStoreDeprecated(internals) Context of a native Rust function call. Exported under the
internalsfeature only. - (internals) An op-assignment operator. Exported under the
internalsfeature only. - Error when parsing a script.
- (internals) A type that encapsulates the current state of the parser. Exported under the
internalsfeature only. - A location (line number + character position) in the input script.
- Type containing information about the current scope. Useful for keeping state between
Engineevaluation runs. - (internals) A type containing information on a script-defined function. Exported under the
internalsfeature only. - A type containing the metadata of a script-defined function.
- (internals) A span consisting of a starting and an ending positions. Exported under the
internalsfeature only. - (internals) A scoped block of statements. Exported under the
internalsfeature only. - (internals) A cache for interned strings. Exported under the
internalsfeature only. - (internals) A type containing all cases for a
switchstatement. Exported under theinternalsfeature only. - (internals) An iterator on a
Tokenstream. Exported under theinternalsfeature only. - (internals) State of the tokenizer. Exported under the
internalsfeature only. - (internals) A type containing commands to control the tokenizer.
- Builder to build the API of a custom type for use with an
Engine. - Information on a variable definition.
Enums
- (internals) Modes of access. Exported under the
internalsfeature only. - (internals) A type encapsulating a function callable by Rhai. Exported under the
internalsfeature only. - Evaluation result.
- (internals) An expression sub-tree. Exported under the
internalsfeature only. - A type representing the access mode of a function.
- A type representing the namespace of a function.
- Error encountered when tokenizing the script text.
- Level of optimization performed.
- Error encountered when parsing a script.
- (internals) A type containing a range case for a
switchstatement. Exported under theinternalsfeature only. - (internals) A statement. Exported under the
internalsfeature only. - (internals) A Rhai language token. Exported under the
internalsfeature only.
Constants
- Standard containment testing function.
- Standard equality comparison operator.
Traits
- 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.). - Trait to create a Rust closure from a script.
- Trait that parses arguments to a function call.
- (internals) Trait that encapsulates a peekable character input stream. Exported under the
internalsfeature only. - Trait that encapsulates a module resolution service.
- Trait to register custom Rust functions.
- (internals) Trait to represent any type. Exported under the
internalsfeature only.
Functions
- Evaluate a string as a script, returning the result value or an error.
- Evaluate a script file, returning the result value or an error.
- Return the JSON representation of an object map.
- (internals) Get the next token from the input stream. Exported under the
internalsfeature only. - (internals) Is a text string a valid script-defined function name? Exported under the
internalsfeature only. - (internals) Is a text string a valid identifier? Exported under the
internalsfeature only. - (internals) Lock a
Lockedresource for mutable access. Exported under theinternalsfeature only. - (internals) Lock a
Lockedresource for mutable access. Exported under theinternalsfeature only. - (internals) Parse a string literal ended by a specified termination character. Exported under the
internalsfeature only. - Evaluate a string as a script.
- Evaluate a file.
Type Aliases
- Variable-sized array of
Dynamicvalues. - Variable-sized array of
u8values (byte array). - The system floating-point type. It is defined as
f64. - The system integer type. It is defined as
i64. - An identifier in Rhai.
SmartStringis used because most identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline. - A dictionary of
Dynamicvalues with string keys. - (internals) Alias to
smallvec::SmallVec<[T; 3]>, which is aVecbacked by a small, inline, fixed-size array when there are ≤ 3 items stored. Exported under theinternalsfeature only. - (internals) A shared object that allows control of the tokenizer from outside.
Attribute Macros
- Attribute, when put on a Rust function, turns it into a plugin function.
- Attribute, when put on a Rust module, turns it into a plugin module.