Struct rhai::plugin::Engine[][src]

pub struct Engine { /* fields omitted */ }
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

Register a custom syntax with the Engine.

  • keywords holds a slice of strings that define the custom syntax.
  • scope_changed specifies variables have been added/removed by this custom syntax.
  • func is the implementation function.

Caveat - Do not change beyond block scope

If scope_changed is true, then the current Scope is assumed to be modified by this custom syntax.

Adding new variables and/or removing variables are allowed. Simply modifying the values of variables does NOT count, so false should be passed in this case.

However, only variables declared within the current block scope should be touched, since they all go away at the end of the block.

Variables in parent blocks should be left untouched as they persist beyond the current block.

Register a custom syntax with the Engine.

WARNING - Low Level API

This function is very low level.

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

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

Create a new Engine

Create a new Engine with minimal built-in functions.

Use register_global_module to add packages of functions.

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.

Engine public API

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 custom fallible function with the Engine.

Example

use rhai::{Engine, EvalAltResult};

// Normal function
fn div(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> {
    if y == 0 {
        // '.into()' automatically converts to 'Box<EvalAltResult::ErrorRuntime>'
        Err("division by zero!".into())
    } else {
        Ok(x / y)
    }
}

let mut engine = Engine::new();

engine.register_result_fn("div", div);

engine.eval::<i64>("div(42, 0)")
      .expect_err("expecting division by zero error!");
👎 Deprecated:

this function is volatile and may change

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 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.

Not available under no_object.

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.

Not available under no_object.

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 an type iterator for an iterable type with the Engine. This is an advanced feature.

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 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

use rhai::{Engine, Dynamic, EvalAltResult};

#[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) -> Result<i64, Box<EvalAltResult>> {
        Ok(self.field)
    }
}

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_result("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 }
);

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

Not available under no_object.

Example

use rhai::{Engine, Dynamic, EvalAltResult};

#[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) -> Result<(), Box<EvalAltResult>> {
        self.field = new_val;
        Ok(())
    }
}

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_result("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 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

use rhai::{Engine, Dynamic, EvalAltResult};

#[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) -> Result<i64, Box<EvalAltResult>> {
        Ok(self.fields[index as usize])
    }
}

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_result(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);

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

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

use rhai::{Engine, Dynamic, EvalAltResult};

#[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) -> Result<(), Box<EvalAltResult>> {
        self.fields[index as usize] = value;
        Ok(())
    }
}

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_result(TestStruct::set_field);

assert_eq!(
    engine.eval::<TestStruct>("let a = new_ts(); a[2] = 42; a")?.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.

👎 Deprecated since 0.19.9:

use register_global_module instead

Register a shared Module into the global namespace of Engine. This function is deprecated and will be removed in the future. Use register_global_module instead.

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);
👎 Deprecated since 0.19.9:

use register_static_module instead

Register a shared Module as a static module namespace with the Engine. This function is deprecated and will be removed in the future. Use register_static_module instead.

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.

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

Example

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

let mut engine = Engine::new();

// Set optimization level to 'Full' so the Engine can fold constants
// into function calls and operators.
engine.set_optimization_level(OptimizationLevel::Full);

// 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.

Example

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

let mut engine = Engine::new();

// Set optimization level to 'Full' so the Engine can fold constants
// into function calls and operators.
engine.set_optimization_level(OptimizationLevel::Full);

// 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 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.

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

Example

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

let mut engine = Engine::new();

// Set optimization level to 'Full' so the Engine can fold constants.
engine.set_optimization_level(OptimizationLevel::Full);

// 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)?;

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 scalar value.

Set has_null to true in order to map null values to (). Setting it to false will cause an ErrorVariableNotFound error during parsing.

JSON With Sub-Objects

This method assumes no sub-objects in the JSON string. That is because the syntax of a JSON sub-object (or object hash), { .. }, is different from Rhai’s syntax, #{ .. }. Parsing a JSON string with sub-objects will cause a syntax error.

If it is certain that the character { never appears in any text string within the JSON object, which is a valid assumption for many use cases, then globally replace { with #{ before calling this method.

Example

use rhai::{Engine, Map};

let engine = Engine::new();

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

assert_eq!(map.len(), 4);
assert_eq!(map["a"].as_int().unwrap(), 123);
assert_eq!(map["b"].as_int().unwrap(), 42);
assert!(map["d"].is::<()>());

let c = map["c"].read_lock::<Map>().unwrap();
assert_eq!(c["x"].as_bool().unwrap(), false);

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.

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

Example

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

let mut engine = Engine::new();

// Set optimization level to 'Full' so the Engine can fold constants
// into function calls and operators.
engine.set_optimization_level(OptimizationLevel::Full);

// 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);

Evaluate a script 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.
let result = engine.eval_file::<i64>("script.rhai".into())?;

Evaluate a script file with own scope.

Not available under no_std or WASM.

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 string.

Example

use rhai::Engine;

let engine = Engine::new();

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

Evaluate a string 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);

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.

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.

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.

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.

Example

use rhai::{Engine, Scope};

let engine = Engine::new();

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

// 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);

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.

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.

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.

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.

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.

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.

Call a script function defined in an AST with multiple arguments. Arguments are passed as a tuple.

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: i64 = engine.call_fn(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?;
assert_eq!(result, 168);

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

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

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.

WARNING

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
")?;

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

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

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

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

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

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.

(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 packages (optional)

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

Return Value of Callback

Return Ok(None) to continue with normal variable access.
Return Ok(Some(Dynamic)) as the variable’s value.

Errors in Callback

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")?;

Register a callback for script evaluation progress.

Not available under unchecked.

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 > 10000 {
        Some("Over 10,000 operations!".into())
    } else if ops % 800 == 0 {
        *logger.write().unwrap() = ops;
        None
    } else {
        None
    }
});

engine.consume("for x in range(0, 50000) {}")
    .expect_err("should error");

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

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.consume("print(40 + 2);")?;

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

Override default action of debug (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_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.consume_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""#);

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.

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.

Not available under unchecked or 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.

Not available under unchecked or no_module.

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

Not available under unchecked.

The depth limit for expressions (0 for unlimited).

Not available under unchecked.

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

Not available under unchecked or no_function.

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

Not available under unchecked.

The maximum length of strings (0 for unlimited).

Not available under unchecked.

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

Not available under unchecked or no_index.

The maximum length of arrays (0 for unlimited).

Not available under unchecked or 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).

Not available under unchecked or no_object.

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.

The operator must be a valid identifier (i.e. it cannot be a symbol).

The precedence cannot be zero.

Example

use rhai::Engine;

let mut engine = Engine::new();

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

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

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

(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 global modules (optional)

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

Functions from the following sources are included:

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

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

Create a Rust closure from an AST. Read more

Create a Rust closure from a script. Read more

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

Performs the conversion.

Performs the conversion.

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.