Struct rhai::Engine

source ·
pub struct Engine { /* private fields */ }
Expand description

Rhai main scripting engine.

Thread Safety

Engine is re-entrant.

Currently, Engine is neither Send nor Sync. Use the sync feature to make it Send + Sync.

Example

use rhai::Engine;

let engine = Engine::new();

let result = engine.eval::<i64>("40 + 2")?;

println!("Answer: {result}");   // prints 42

Implementations

Pretty-print a type name.

If a type is registered via register_type_with_name, the type name provided for the registration will be used.

Panics

Panics if the type name is &mut.

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

Example
use rhai::Engine;

let engine = Engine::new();

assert_eq!(engine.eval::<i64>("40 + 2")?, 42);

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

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions.

This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 40_i64);

assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x += 2; x")?, 42);
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x += 2; x")?, 44);

// The variable in the scope is modified
assert_eq!(scope.get_value::<i64>("x").expect("variable x should exist"), 44);

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

Example
use rhai::Engine;

let engine = Engine::new();

assert_eq!(engine.eval_expression::<i64>("40 + 2")?, 42);

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

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 40_i64);

assert_eq!(engine.eval_expression_with_scope::<i64>(&mut scope, "x + 2")?, 42);

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

Example
use rhai::Engine;

let engine = Engine::new();

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile("40 + 2")?;

// Evaluate it
assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);

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

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 40_i64);

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile("x += 2; x")?;

// Evaluate it
assert_eq!(engine.eval_ast_with_scope::<i64>(&mut scope, &ast)?, 42);
assert_eq!(engine.eval_ast_with_scope::<i64>(&mut scope, &ast)?, 44);

// The variable in the scope is modified
assert_eq!(scope.get_value::<i64>("x").expect("variable x should exist"), 44);

(internals) Evaluate a list of statements with no this pointer. Exported under the internals feature only.

This is commonly used to evaluate a list of statements in an AST or a script function body.

WARNING - Low Level API

This function is very low level.

Evaluate a string as a script.

Example
use rhai::Engine;

let engine = Engine::new();

engine.run("print(40 + 2);")?;

Evaluate a string as a script with own scope.

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions.

This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 40_i64);

engine.run_with_scope(&mut scope, "x += 2; print(x);")?;

// The variable in the scope is modified
assert_eq!(scope.get_value::<i64>("x").expect("variable x should exist"), 42);

Evaluate an AST.

Example
use rhai::Engine;

let engine = Engine::new();

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile("print(40 + 2);")?;

// Evaluate it
engine.run_ast(&ast)?;

Evaluate an AST with own scope.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 40_i64);

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile("x += 2; x")?;

// Evaluate it
engine.run_ast_with_scope(&mut scope, &ast)?;

// The variable in the scope is modified
assert_eq!(scope.get_value::<i64>("x").expect("variable x should exist"), 42);

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

Example
use rhai::Engine;

let engine = Engine::new();

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile("40 + 2")?;

for _ in 0..42 {
    assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);
}

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

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions. This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope, OptimizationLevel};

let mut engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push_constant("x", 42_i64);   // 'x' is a constant

// Compile a script to an AST and store it for later evaluation.
// Notice that `Full` optimization is on, so constants are folded
// into function calls and operators.
let ast = engine.compile_with_scope(&mut scope,
            "if x > 40 { x } else { 0 }"    // all 'x' are replaced with 42
)?;

// Normally this would have failed because no scope is passed into the 'eval_ast'
// call and so the variable 'x' does not exist.  Here, it passes because the script
// has been optimized and all references to 'x' are already gone.
assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);

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

Not available under no_module.

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.

The scope is useful for passing constants into the script for optimization when using OptimizationLevel::Full.

Note

All strings are simply parsed one after another with nothing inserted in between, not even a newline or space.

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions. This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope, OptimizationLevel};

let mut engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push_constant("x", 42_i64);   // 'x' is a constant

// Compile a script made up of script segments to an AST and store it for later evaluation.
// Notice that `Full` optimization is on, so constants are folded
// into function calls and operators.
let ast = engine.compile_scripts_with_scope(&mut scope, &[
            "if x > 40",            // all 'x' are replaced with 42
            "{ x } el",
            "se { 0 }"              // segments do not need to be valid scripts!
])?;

// Normally this would have failed because no scope is passed into the 'eval_ast'
// call and so the variable 'x' does not exist.  Here, it passes because the script
// has been optimized and all references to 'x' are already gone.
assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);

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

Example
use rhai::Engine;

let engine = Engine::new();

// Compile a script to an AST and store it for later evaluation
let ast = engine.compile_expression("40 + 2")?;

for _ in 0..42 {
    assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);
}

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

Example
use rhai::{Engine, Scope, OptimizationLevel};

let mut engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push_constant("x", 10_i64);   // 'x' is a constant

// Compile a script to an AST and store it for later evaluation.
// Notice that `Full` optimization is on, so constants are folded
// into function calls and operators.
let ast = engine.compile_expression_with_scope(&mut scope,
            "2 + (x + x) * 2"    // all 'x' are replaced with 10
)?;

// Normally this would have failed because no scope is passed into the 'eval_ast'
// call and so the variable 'x' does not exist.  Here, it passes because the script
// has been optimized and all references to 'x' are already gone.
assert_eq!(engine.eval_ast::<i64>(&ast)?, 42);

Parse a JSON string into an object map.

This is a light-weight alternative to using, say, serde_json to deserialize the JSON.

Not available under no_object.

The JSON string must be an object hash. It cannot be a simple primitive value.

Set has_null to true in order to map null values to (). Setting it to false causes a syntax error for any null value.

JSON sub-objects are handled transparently.

This function can be used together with format_map_as_json to work with JSON texts without using the serde_json crate (which is heavy).

Example
use rhai::{Engine, Map};

let engine = Engine::new();

let map = engine.parse_json(r#"
{
    "a": 123,
    "b": 42,
    "c": {
        "x": false,
        "y": true,
        "z": '$'
    },
    "d": null
}"#, true)?;

assert_eq!(map.len(), 4);
assert_eq!(map["a"].as_int().expect("a should exist"), 123);
assert_eq!(map["b"].as_int().expect("b should exist"), 42);
assert_eq!(map["d"].as_unit().expect("d should exist"), ());

let c = map["c"].read_lock::<Map>().expect("c should exist");
assert_eq!(c["x"].as_bool().expect("x should be bool"), false);
assert_eq!(c["y"].as_bool().expect("y should be bool"), true);
assert_eq!(c["z"].as_char().expect("z should be char"), '$');

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

Not available under no_std or WASM.

Example
use rhai::Engine;

let engine = Engine::new();

// Compile a script file to an AST and store it for later evaluation.
// Notice that a PathBuf is required which can easily be constructed from a string.
let ast = engine.compile_file("script.rhai".into())?;

for _ in 0..42 {
    engine.eval_ast::<i64>(&ast)?;
}

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

Not available under no_std or WASM.

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions.

This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope, OptimizationLevel};

let mut engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push_constant("x", 42_i64);   // 'x' is a constant

// Compile a script to an AST and store it for later evaluation.
// Notice that a PathBuf is required which can easily be constructed from a string.
let ast = engine.compile_file_with_scope(&mut scope, "script.rhai".into())?;

let result = engine.eval_ast::<i64>(&ast)?;

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

Not available under no_std or WASM.

Example
use rhai::Engine;

let engine = Engine::new();

// Notice that a PathBuf is required which can easily be constructed from a string.
let result = engine.eval_file::<i64>("script.rhai".into())?;

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

Not available under no_std or WASM.

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions.

This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 42_i64);

// Notice that a PathBuf is required which can easily be constructed from a string.
let result = engine.eval_file_with_scope::<i64>(&mut scope, "script.rhai".into())?;

Evaluate a file.

Not available under no_std or WASM.

Example
use rhai::Engine;

let engine = Engine::new();

// Notice that a PathBuf is required which can easily be constructed from a string.
engine.run_file("script.rhai".into())?;

Evaluate a file with own scope.

Not available under no_std or WASM.

Constants Propagation

If not OptimizationLevel::None, constants defined within the scope are propagated throughout the script including functions.

This allows functions to be optimized based on dynamic global constants.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

// Create initialized scope
let mut scope = Scope::new();
scope.push("x", 42_i64);

// Notice that a PathBuf is required which can easily be constructed from a string.
engine.run_file_with_scope(&mut scope, "script.rhai".into())?;

Register a custom function with the Engine.

Example
use rhai::Engine;

// Normal function
fn add(x: i64, y: i64) -> i64 {
    x + y
}

let mut engine = Engine::new();

engine.register_fn("add", add);

assert_eq!(engine.eval::<i64>("add(40, 2)")?, 42);

// You can also register a closure.
engine.register_fn("sub", |x: i64, y: i64| x - y );

assert_eq!(engine.eval::<i64>("sub(44, 2)")?, 42);

Register a function of the Engine.

WARNING - Low Level API

This function is very low level. It takes a list of TypeId’s indicating the actual types of the parameters.

Arguments

Arguments are simply passed in as a mutable array of &mut Dynamic. The arguments are guaranteed to be of the correct types matching the TypeId’s.

To access a primary argument value (i.e. cloning is cheap), use: args[n].as_xxx().unwrap()

To access an argument value and avoid cloning, use std::mem::take(args[n]).cast::<T>(). Notice that this will consume the argument, replacing it with ().

To access the first mutable parameter, use args.get_mut(0).unwrap()

Register a custom type for use with the Engine. The type must implement Clone.

Example
#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
    fn update(&mut self, offset: i64) {
        self.field += offset;
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine
    .register_type::<TestStruct>()
    .register_fn("new_ts", TestStruct::new)
    // Use `register_fn` to register methods on the type.
    .register_fn("update", TestStruct::update);

assert_eq!(
    engine.eval::<TestStruct>("let x = new_ts(); x.update(41); x")?,
    TestStruct { field: 42 }
);

Register a custom type for use with the Engine, with a pretty-print name for the type_of function. The type must implement Clone.

Example
#[derive(Clone)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine
    .register_type::<TestStruct>()
    .register_fn("new_ts", TestStruct::new);

assert_eq!(
    engine.eval::<String>("let x = new_ts(); type_of(x)")?,
    "rust_out::TestStruct"
);

// Re-register the custom type with a name.
engine.register_type_with_name::<TestStruct>("Hello");

assert_eq!(
    engine.eval::<String>("let x = new_ts(); type_of(x)")?,
    "Hello"
);

Register a custom type for use with the Engine, with a pretty-print name for the type_of function. The type must implement Clone.

WARNING - Low Level API

This function is low level.

Register a type iterator for an iterable type with the Engine. This is an advanced API.

Register a fallible type iterator for an iterable type with the Engine. This is an advanced API.

Register a getter function for a member of a registered type with the Engine.

The function signature must start with &mut self and not &self.

Not available under no_object.

Example
#[derive(Clone)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
    // Even a getter must start with `&mut self` and not `&self`.
    fn get_field(&mut self) -> i64  {
        self.field
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine
    .register_type::<TestStruct>()
    .register_fn("new_ts", TestStruct::new)
    // Register a getter on a property (notice it doesn't have to be the same name).
    .register_get("xyz", TestStruct::get_field);

assert_eq!(engine.eval::<i64>("let a = new_ts(); a.xyz")?, 1);

Register a setter function for a member of a registered type with the Engine.

Not available under no_object.

Example
#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
    fn set_field(&mut self, new_val: i64) {
        self.field = new_val;
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine
    .register_type::<TestStruct>()
    .register_fn("new_ts", TestStruct::new)
    // Register a setter on a property (notice it doesn't have to be the same name)
    .register_set("xyz", TestStruct::set_field);

// Notice that, with a getter, there is no way to get the property value
assert_eq!(
    engine.eval::<TestStruct>("let a = new_ts(); a.xyz = 42; a")?,
    TestStruct { field: 42 }
);

Short-hand for registering both getter and setter functions of a registered type with the Engine.

All function signatures must start with &mut self and not &self.

Not available under no_object.

Example
#[derive(Clone)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
    // Even a getter must start with `&mut self` and not `&self`.
    fn get_field(&mut self) -> i64 {
        self.field
    }
    fn set_field(&mut self, new_val: i64) {
        self.field = new_val;
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine
    .register_type::<TestStruct>()
    .register_fn("new_ts", TestStruct::new)
    // Register both a getter and a setter on a property
    // (notice it doesn't have to be the same name)
    .register_get_set("xyz", TestStruct::get_field, TestStruct::set_field);

assert_eq!(engine.eval::<i64>("let a = new_ts(); a.xyz = 42; a.xyz")?, 42);

Register an index getter for a custom type with the Engine.

The function signature must start with &mut self and not &self.

Not available under both no_index and no_object.

Panics

Panics if the type is Array, Map, String, ImmutableString, &str or INT. Indexers for arrays, object maps, strings and integers cannot be registered.

Example
#[derive(Clone)]
struct TestStruct {
    fields: Vec<i64>
}

impl TestStruct {
    fn new() -> Self {
        Self { fields: vec![1, 2, 3, 4, 5] }
    }
    // Even a getter must start with `&mut self` and not `&self`.
    fn get_field(&mut self, index: i64) -> i64 {
        self.fields[index as usize]
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine.register_type::<TestStruct>();

engine
    .register_fn("new_ts", TestStruct::new)
    // Register an indexer.
    .register_indexer_get(TestStruct::get_field);

assert_eq!(engine.eval::<i64>("let a = new_ts(); a[2]")?, 3);

Register an index setter for a custom type with the Engine.

Not available under both no_index and no_object.

Panics

Panics if the type is Array, Map, String, ImmutableString, &str or INT. Indexers for arrays, object maps, strings and integers cannot be registered.

Example
#[derive(Clone)]
struct TestStruct {
    fields: Vec<i64>
}

impl TestStruct {
    fn new() -> Self {
        Self { fields: vec![1, 2, 3, 4, 5] }
    }
    fn set_field(&mut self, index: i64, value: i64) {
        self.fields[index as usize] = value;
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine.register_type::<TestStruct>();

engine
    .register_fn("new_ts", TestStruct::new)
    // Register an indexer.
    .register_indexer_set(TestStruct::set_field);

let result = engine.eval::<TestStruct>("let a = new_ts(); a[2] = 42; a")?;

assert_eq!(result.fields[2], 42);

Short-hand for registering both index getter and setter functions for a custom type with the Engine.

Not available under both no_index and no_object.

Panics

Panics if the type is Array, Map, String, ImmutableString, &str or INT. Indexers for arrays, object maps, strings and integers cannot be registered.

Example
#[derive(Clone)]
struct TestStruct {
    fields: Vec<i64>
}

impl TestStruct {
    fn new() -> Self {
        Self { fields: vec![1, 2, 3, 4, 5] }
    }
    // Even a getter must start with `&mut self` and not `&self`.
    fn get_field(&mut self, index: i64) -> i64 {
        self.fields[index as usize]
    }
    fn set_field(&mut self, index: i64, value: i64) {
        self.fields[index as usize] = value;
    }
}

use rhai::Engine;

let mut engine = Engine::new();

// Register API for the custom type.
engine.register_type::<TestStruct>();

engine
    .register_fn("new_ts", TestStruct::new)
    // Register an indexer.
    .register_indexer_get_set(TestStruct::get_field, TestStruct::set_field);

assert_eq!(engine.eval::<i64>("let a = new_ts(); a[2] = 42; a[2]")?, 42);

Register a shared 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.

Register a shared Module as a static module namespace with the Engine.

Functions marked FnNamespace::Global and type iterators are exposed to scripts without namespace qualifications.

Not available under no_module.

Example
use rhai::{Engine, Shared, Module};

let mut engine = Engine::new();

// Create the module
let mut module = Module::new();
module.set_native_fn("calc", |x: i64| Ok(x + 1));

let module: Shared<Module> = module.into();

engine
    // Register the module as a fixed sub-module
    .register_static_module("foo::bar::baz", module.clone())
    // Multiple registrations to the same partial path is also OK!
    .register_static_module("foo::bar::hello", module.clone())
    .register_static_module("CalcService", module);

assert_eq!(engine.eval::<i64>("foo::bar::baz::calc(41)")?, 42);
assert_eq!(engine.eval::<i64>("foo::bar::hello::calc(41)")?, 42);
assert_eq!(engine.eval::<i64>("CalcService::calc(41)")?, 42);

(metadata) Generate a list of all registered functions. Exported under the metadata feature only.

Functions from the following sources are included, in order:

  1. Functions registered into the global namespace
  2. Functions in registered sub-modules
  3. Functions in registered packages
  4. Functions in standard packages (optional)

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

Not available under no_function.

The AST is evaluated before calling the function. This allows a script to load the necessary modules. This is usually desired. If not, a specialized AST can be prepared that contains only function definitions without any body script via AST::clear_statements.

Example
use rhai::{Engine, Scope};

let engine = Engine::new();

let ast = engine.compile("
    fn add(x, y) { len(x) + y + foo }
    fn add1(x)   { len(x) + 1 + foo }
    fn bar()     { foo/2 }
")?;

let mut scope = Scope::new();
scope.push("foo", 42_i64);

// Call the script-defined function
let result = engine.call_fn::<i64>(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?;
assert_eq!(result, 168);

let result = engine.call_fn::<i64>(&mut scope, &ast, "add1", ( "abc", ) )?;
//                                                           ^^^^^^^^^^ tuple of one
assert_eq!(result, 46);

let result = engine.call_fn::<i64>(&mut scope, &ast, "bar", () )?;
assert_eq!(result, 21);

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

The following options are available:

  • whether to evaluate the AST to load necessary modules before calling the function
  • whether to rewind the Scope after the function call
  • a value for binding to the this pointer (if any)

Not available under no_function.

WARNING - Low Level API

This function is very low level.

Arguments

All the arguments are consumed, meaning that they’re replaced by (). This is to avoid unnecessarily cloning the arguments.

Do not use the arguments after this call. If they are needed afterwards, clone them before calling this function.

Example
use rhai::{Engine, Scope, Dynamic};

let engine = Engine::new();

let ast = engine.compile("
    fn add(x, y) { len(x) + y + foo }
    fn add1(x)   { len(x) + 1 + foo }
    fn bar()     { foo/2 }
    fn action(x) { this += x; }         // function using 'this' pointer
    fn decl(x)   { let hello = x; }     // declaring variables
")?;

let mut scope = Scope::new();
scope.push("foo", 42_i64);

// Call the script-defined function
let result = engine.call_fn_raw(&mut scope, &ast, true, true, "add", None, [ "abc".into(), 123_i64.into() ])?;
//                                                                   ^^^^ no 'this' pointer
assert_eq!(result.cast::<i64>(), 168);

let result = engine.call_fn_raw(&mut scope, &ast, true, true, "add1", None, [ "abc".into() ])?;
assert_eq!(result.cast::<i64>(), 46);

let result = engine.call_fn_raw(&mut scope, &ast, true, true, "bar", None, [])?;
assert_eq!(result.cast::<i64>(), 21);

let mut value = 1_i64.into();
let result = engine.call_fn_raw(&mut scope, &ast, true, true, "action", Some(&mut value), [ 41_i64.into() ])?;
//                                                                      ^^^^^^^^^^^^^^^^ binding the 'this' pointer
assert_eq!(value.as_int().unwrap(), 42);

engine.call_fn_raw(&mut scope, &ast, true, false, "decl", None, [ 42_i64.into() ])?;
//                                         ^^^^^ do not rewind scope
assert_eq!(scope.get_value::<i64>("hello").unwrap(), 42);
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.

(internals) Call a script function defined in an AST with multiple Dynamic arguments. Exported under the internals feature only.

The following options are available:

  • whether to evaluate the AST to load necessary modules before calling the function
  • whether to rewind the Scope after the function call
  • a value for binding to the this pointer (if any)

Not available under no_function.

WARNING - Unstable API

This API is volatile and may change in the future.

WARNING - Low Level API

This function is extremely low level.

A GlobalRuntimeState and Caches need to be passed into the function, which can be created via GlobalRuntimeState::new and Caches::new.

This makes repeatedly calling particular functions more efficient as the functions resolution cache is kept intact.

Arguments

All the arguments are consumed, meaning that they’re replaced by (). This is to avoid unnecessarily cloning the arguments.

Do not use the arguments after this call. If they are needed afterwards, clone them before calling this function.

Is if-expression allowed? Default is true.

Set whether if-expression is allowed.

Is switch expression allowed? Default is true.

Set whether switch expression is allowed.

Are loop expressions allowed? Default is true.

Set whether loop expressions are allowed.

Is statement-expression allowed? Default is true.

Set whether statement-expression is allowed.

Is anonymous function allowed? Default is true.

Not available under no_function.

Set whether anonymous function is allowed.

Not available under no_function.

Is looping allowed? Default is true.

Set whether looping is allowed.

Is variables shadowing allowed? Default is true.

Set whether variables shadowing is allowed.

Is strict variables mode enabled? Default is false.

Set whether strict variables mode is enabled.

Raise error if an object map property does not exist? Default is false.

Not available under no_object.

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

Not available under no_object.

Is fast operators mode enabled? Default is false.

Set whether fast operators mode is enabled.

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

Not available under no_optimize.

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

Not available under no_optimize.

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

Not available under no_optimize.

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 Scope::push_constant).

Then, the AST is cloned and the copy re-optimized before running.

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.

The maximum levels of function calls allowed for a script.

Zero under no_function.

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.

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

Not available under unchecked.

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

Not available under unchecked or no_module.

The maximum number of imported modules allowed for a script.

Zero under no_module.

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

Not available under unchecked.

The depth limit for expressions (0 for unlimited).

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

Zero under no_function.

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

Not available under unchecked.

The maximum length of strings (0 for unlimited).

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

Not available under unchecked or no_index.

The maximum length of arrays (0 for unlimited).

Zero under no_index.

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

Not available under unchecked or no_object.

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

Zero under no_object.

👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.

Provide a callback that will be invoked before each variable access.

WARNING - Unstable API

This API is volatile and may change in the future.

Callback Function Signature

Fn(name: &str, index: usize, context: EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>

where:

  • name: name of the variable.
  • index: an offset from the bottom of the current Scope that the variable is supposed to reside. Offsets start from 1, with 1 meaning the last variable in the current Scope. Essentially the correct variable is at position scope.len() - index. If index is zero, then there is no pre-calculated offset position and a search through the current Scope must be performed.
  • context: the current evaluation context.
Return value
  • Ok(None): continue with normal variable access.
  • Ok(Some(Dynamic)): the variable’s value.
Raising errors

Return Err(...) if there is an error.

Example
use rhai::Engine;

let mut engine = Engine::new();

// Register a variable resolver.
engine.on_var(|name, _, _| {
    match name {
        "MYSTIC_NUMBER" => Ok(Some(42_i64.into())),
        _ => Ok(None)
    }
});

engine.eval::<i64>("MYSTIC_NUMBER")?;
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.

Provide a callback that will be invoked before the definition of each variable .

WARNING - Unstable API

This API is volatile and may change in the future.

Callback Function Signature

Fn(is_runtime: bool, info: VarInfo, context: EvalContext) -> Result<bool, Box<EvalAltResult>>

where:

  • is_runtime: true if the variable definition event happens during runtime, false if during compilation.
  • info: information on the variable.
  • context: the current evaluation context.
Return value
  • Ok(true): continue with normal variable definition.
  • Ok(false): deny the variable definition with an runtime error.
Raising errors

Return Err(...) if there is an error.

Example
use rhai::Engine;

let mut engine = Engine::new();

// Register a variable definition filter.
engine.on_def_var(|_, info, _| {
    // Disallow defining MYSTIC_NUMBER as a constant
    if info.name == "MYSTIC_NUMBER" && info.is_const {
        Ok(false)
    } else {
        Ok(true)
    }
});

// The following runs fine:
engine.eval::<i64>("let MYSTIC_NUMBER = 42;")?;

// The following will cause an error:
engine.eval::<i64>("const MYSTIC_NUMBER = 42;")?;
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.

(internals) Register a callback that will be invoked during parsing to remap certain tokens. Exported under the internals feature only.

WARNING - Unstable API

This API is volatile and may change in the future.

Callback Function Signature

Fn(token: Token, pos: Position, state: &TokenizeState) -> Token

where:

  • token: current token parsed
  • pos: location of the token
  • state: current state of the tokenizer
Raising errors

It is possible to raise a parsing error by returning Token::LexError as the mapped token.

Example
use rhai::{Engine, Token};

let mut engine = Engine::new();

// Register a token mapper.
engine.on_parse_token(|token, _, _| {
    match token {
        // Convert all integer literals to strings
        Token::IntegerConstant(n) => Token::StringConstant(Box::new(n.to_string().into())),
        // Convert 'begin' .. 'end' to '{' .. '}'
        Token::Identifier(s) if &*s == "begin" => Token::LeftBrace,
        Token::Identifier(s) if &*s == "end" => Token::RightBrace,
        // Pass through all other tokens unchanged
        _ => token
    }
});

assert_eq!(engine.eval::<String>("42")?, "42");
assert_eq!(engine.eval::<bool>("true")?, true);
assert_eq!(engine.eval::<String>("let x = 42; begin let x = 0; end; x")?, "42");

Register a callback for script evaluation progress.

Not available under unchecked.

Callback Function Signature

Fn(counter: u64) -> Option<Dynamic>

Return value
  • None: continue running the script.
  • Some(Dynamic): terminate the script with the specified exception value.
Example
use rhai::Engine;

let result = Arc::new(RwLock::new(0_u64));
let logger = result.clone();

let mut engine = Engine::new();

engine.on_progress(move |ops| {
    if ops > 1000 {
        Some("Over 1,000 operations!".into())
    } else if ops % 123 == 0 {
        *logger.write().unwrap() = ops;
        None
    } else {
        None
    }
});

engine.run("for x in 0..5000 { print(x); }")
      .expect_err("should error");

assert_eq!(*result.read().unwrap(), 984);

Override default action of print (print to stdout using println!)

Example
use rhai::Engine;

let result = Arc::new(RwLock::new(String::new()));

let mut engine = Engine::new();

// Override action of 'print' function
let logger = result.clone();
engine.on_print(move |s| logger.write().unwrap().push_str(s));

engine.run("print(40 + 2);")?;

assert_eq!(*result.read().unwrap(), "42");

Override default action of debug (print to stdout using println!)

Callback Function Signature

The callback function signature passed takes the following form:

Fn(text: &str, source: Option<&str>, pos: Position)

where:

  • text: the text to display
  • source: current source, if any
  • pos: location of the debug call
Example
use rhai::Engine;

let result = Arc::new(RwLock::new(String::new()));

let mut engine = Engine::new();

// Override action of 'print' function
let logger = result.clone();
engine.on_debug(move |s, src, pos| logger.write().unwrap().push_str(
                    &format!("{} @ {:?} > {}", src.unwrap_or("unknown"), pos, s)
               ));

let mut ast = engine.compile(r#"let x = "hello"; debug(x);"#)?;
ast.set_source("world");
engine.run_ast(&ast)?;

#[cfg(not(feature = "no_position"))]
assert_eq!(*result.read().unwrap(), r#"world @ 1:18 > "hello""#);
#[cfg(feature = "no_position")]
assert_eq!(*result.read().unwrap(), r#"world @ none > "hello""#);
👎Deprecated: This API is NOT deprecated, but it is considered volatile and may change in the future.

(debugging) Register a callback for debugging. Exported under the debugging feature only.

WARNING - Unstable API

This API is volatile and may change in the future.

Register a custom syntax with the Engine.

Not available under no_custom_syntax.

  • symbols holds a slice of strings that define the custom syntax.
  • scope_may_be_changed specifies variables may be added/removed by this custom syntax.
  • func is the implementation function.
Note on symbols
  • Whitespaces around symbols are stripped.
  • Symbols that are all-whitespace or empty are ignored.
  • If symbols does not contain at least one valid token, then the custom syntax registration is simply ignored.
Note on scope_may_be_changed

If scope_may_be_changed is true, then size of the current Scope may be modified by this custom syntax.

Adding new variables and/or removing variables count.

Simply modifying the values of existing variables does NOT count, as the size of the current Scope is unchanged, so false should be passed.

Replacing one variable with another (i.e. adding a new variable and removing one variable at the same time so that the total size of the Scope is unchanged) also does NOT count, so false should be passed.

Register a custom syntax with the Engine with custom user-defined state.

Not available under no_custom_syntax.

WARNING - Low Level API

This function is very low level.

  • scope_may_be_changed specifies variables have been added/removed by this custom syntax.
  • parse is the parsing function.
  • func is the implementation function.

All custom keywords used as symbols must be manually registered via Engine::register_custom_operator. Otherwise, they won’t be recognized.

Parsing Function Signature

The parsing function has the following signature:

Fn(symbols: &[ImmutableString], look_ahead: &str, state: &mut Dynamic) -> Result<Option<ImmutableString>, ParseError>

where:

  • symbols: a slice of symbols that have been parsed so far, possibly containing $expr$ and/or $block$; $ident$ and other literal markers are replaced by the actual text
  • look_ahead: a string slice containing the next symbol that is about to be read
  • state: a Dynamic value that contains a user-defined state
Return value
  • Ok(None): parsing complete and there are no more symbols to match.
  • Ok(Some(symbol)): the next symbol to match, which can also be $expr$, $ident$ or $block$.
  • Err(ParseError): error that is reflected back to the Engine, normally ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE) to indicate a syntax error, but it can be any ParseError.
👎Deprecated since 1.1.0: use run_file instead

Evaluate a file, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Not available under no_std or WASM.

Deprecated

This method is deprecated. Use run_file instead.

This method will be removed in the next majocd cr version.

👎Deprecated since 1.1.0: use run_file_with_scope instead

Evaluate a file with own scope, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Not available under no_std or WASM.

Deprecated

This method is deprecated. Use run_file_with_scope instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0: use run instead

Evaluate a string, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Deprecated

This method is deprecated. Use run instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0: use run_with_scope instead

Evaluate a string with own scope, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Deprecated

This method is deprecated. Use run_with_scope instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0: use run_ast instead

Evaluate an AST, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Deprecated

This method is deprecated. Use run_ast instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0: use run_ast_with_scope instead

Evaluate an AST with own scope, but throw away the result and only return error (if any). Useful for when you don’t need the result, but still need to keep track of possible errors.

Deprecated

This method is deprecated. Use run_ast_with_scope instead.

This method will be removed in the next major version.

👎Deprecated since 1.1.0: use call_fn_raw instead

Call a script function defined in an AST with multiple Dynamic arguments and optionally a value for binding to the this pointer.

Not available under no_function.

There is an option to evaluate the AST to load necessary modules before calling the function.

Deprecated

This method is deprecated. Use run_ast_with_scope instead.

This method will be removed in the next major version.

👎Deprecated since 1.9.1: use register_fn instead

Register a custom fallible function with the Engine.

Deprecated

This method is deprecated. Use register_fn instead.

This method will be removed in the next major version.

👎Deprecated since 1.9.1: use register_get instead

Register a getter function for a member of a registered type with the Engine.

The function signature must start with &mut self and not &self.

Not available under no_object.

Deprecated

This method is deprecated. Use register_get instead.

This method will be removed in the next major version.

👎Deprecated since 1.9.1: use register_set instead

Register a setter function for a member of a registered type with the Engine.

Not available under no_object.

Deprecated

This method is deprecated. Use register_set instead.

This method will be removed in the next major version.

👎Deprecated since 1.9.1: use register_indexer_get instead

Register an index getter for a custom type with the Engine.

The function signature must start with &mut self and not &self.

Not available under both no_index and no_object.

Deprecated

This method is deprecated. Use register_indexer_get instead.

This method will be removed in the next major version.

Panics

Panics if the type is Array, Map, String, ImmutableString, &str or INT. Indexers for arrays, object maps, strings and integers cannot be registered.

👎Deprecated since 1.9.1: use register_indexer_set instead

Register an index setter for a custom type with the Engine.

Not available under both no_index and no_object.

Deprecated

This method is deprecated. Use register_indexer_set instead.

This method will be removed in the next major version.

Panics

Panics if the type is Array, Map, String, ImmutableString, &str or INT. Indexers for arrays, object maps, strings and integers cannot be registered.

👎Deprecated since 1.11.0: use register_custom_syntax_with_state_raw instead

Register a custom syntax with the Engine.

Not available under no_custom_syntax.

Deprecated

This method is deprecated. Use register_custom_syntax_with_state_raw instead.

This method will be removed in the next major version.

WARNING - Low Level API

This function is very low level.

  • scope_may_be_changed specifies variables have been added/removed by this custom syntax.
  • parse is the parsing function.
  • func is the implementation function.

All custom keywords used as symbols must be manually registered via Engine::register_custom_operator. Otherwise, they won’t be recognized.

Parsing Function Signature

The parsing function has the following signature:

Fn(symbols: &[ImmutableString], look_ahead: &str) -> Result<Option<ImmutableString>, ParseError>

where:

  • symbols: a slice of symbols that have been parsed so far, possibly containing $expr$ and/or $block$; $ident$ and other literal markers are replaced by the actual text
  • look_ahead: a string slice containing the next symbol that is about to be read
Return value
  • Ok(None): parsing complete and there are no more symbols to match.
  • Ok(Some(symbol)): the next symbol to match, which can also be $expr$, $ident$ or $block$.
  • Err(ParseError): error that is reflected back to the Engine, normally ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE) to indicate a syntax error, but it can be any ParseError.

Build the API of a custom type for use with the Engine.

The custom type must implement CustomType.

WARNING - Unstable API

This API is volatile and may change in the future.

(metadata, internals) Return Definitions that can be used to generate definition files for the Engine. Exported under the internals and metadata feature only.

Example
let engine = Engine::new();

engine
    .definitions()
    .write_to_dir(".rhai/definitions")?;

(metadata, internals) Return Definitions that can be used to generate definition files for the Engine and the given Scope. Exported under the internals and metadata feature only.

Example
let engine = Engine::new();
let scope = Scope::new();
engine
    .definitions_with_scope(&scope)
    .write_to_dir(".rhai/definitions")?;

The module resolution service used by the Engine.

Not available under no_module.

Set the module resolution service used by the Engine.

Not available under no_module.

Disable a particular keyword or operator in the language.

Examples

The following will raise an error during parsing because the if keyword is disabled and is recognized as a reserved symbol!

use rhai::Engine;

let mut engine = Engine::new();

engine.disable_symbol("if");    // disable the 'if' keyword

engine.compile("let x = if true { 42 } else { 0 };")?;
//                      ^ 'if' is rejected as a reserved symbol

The following will raise an error during parsing because the += operator is disabled.

use rhai::Engine;

let mut engine = Engine::new();

engine.disable_symbol("+=");    // disable the '+=' operator

engine.compile("let x = 42; x += 1;")?;
//                            ^ unknown operator

Register a custom operator with a precedence into the language.

Not available under no_custom_syntax.

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

The precedence cannot be zero.

Example
use rhai::Engine;

let mut engine = Engine::new();

// Register a custom operator called '#' and give it
// a precedence of 160 (i.e. between +|- and *|/).
engine.register_custom_operator("#", 160).expect("should succeed");

// Register a binary function named '#'
engine.register_fn("#", |x: i64, y: i64| (x * y) - (x + y));

assert_eq!(
    engine.eval_expression::<i64>("1 + 2 * 3 # 4 - 5 / 6")?,
    15
);

Get the default value of the custom state for each evaluation run.

Get a mutable reference to the default value of the custom state for each evaluation run.

Set the default value of the custom state for each evaluation run.

Create a new Engine.

Create a new Engine with minimal built-in functions.

Use register_global_module to add packages of functions.

(internals) Get an interned string. Exported under the internals feature only.

Engine keeps a cache of ImmutableString instances and tries to avoid new allocations when an existing instance is found.

Get an empty ImmutableString which refers to a shared instance.

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

Not available under unchecked.

(internals) Tokenize an input text stream. Exported under the internals feature only.

(internals) Tokenize an input text stream with a mapping function. Exported under the internals feature only.

(metadata) Generate a list of all functions (including those defined in an AST) in JSON format. Exported under the metadata feature only.

Functions from the following sources are included:

  1. Functions defined in an AST
  2. Functions registered into the global namespace
  3. Functions in static modules
  4. Functions in registered global packages
  5. Functions in standard packages (optional)

Generate a list of all functions in JSON format. Exported under the metadata feature only.

Functions from the following sources are included:

  1. Functions registered into the global namespace
  2. Functions in static modules
  3. Functions in registered global packages
  4. Functions in standard packages (optional)

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more
The closure’s output type.
Create a Rust closure from an AST. Read more
Create a Rust closure from a script. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.