rhai/src/api/eval.rs

291 lines
8.9 KiB
Rust
Raw Normal View History

2021-11-20 07:57:21 +01:00
//! Module that defines the public evaluation API of [`Engine`].
2022-04-16 10:36:53 +02:00
use crate::eval::{Caches, GlobalRuntimeState};
2021-11-20 07:57:21 +01:00
use crate::parser::ParseState;
use crate::types::dynamic::Variant;
2022-02-07 14:03:39 +01:00
use crate::{
2022-04-26 12:32:43 +02:00
Dynamic, Engine, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR,
2022-02-07 14:03:39 +01:00
};
2022-06-09 12:22:53 +02:00
use std::any::type_name;
2021-11-20 07:57:21 +01:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
impl Engine {
2022-08-08 03:10:15 +02:00
/// Evaluate a string as a script, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
///
/// assert_eq!(engine.eval::<i64>("40 + 2")?, 42);
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2021-12-25 16:49:14 +01:00
pub fn eval<T: Variant + Clone>(&self, script: &str) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
self.eval_with_scope(&mut Scope::new(), script)
}
2022-08-08 03:10:15 +02:00
/// Evaluate a string as a script with own scope, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// ## Constants Propagation
///
2021-11-20 14:29:36 +01:00
/// If not [`OptimizationLevel::None`][crate::OptimizationLevel::None], constants defined within
2022-02-24 03:36:20 +01:00
/// the scope are propagated throughout the script _including_ functions.
///
/// This allows functions to be optimized based on dynamic global constants.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// 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);
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
script: &str,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
let ast = self.compile_with_scope_and_optimization_level(
scope,
&[script],
2022-05-21 16:13:02 +02:00
self.optimization_level,
2021-11-20 07:57:21 +01:00
)?;
self.eval_ast_with_scope(scope, &ast)
}
2022-08-08 03:10:15 +02:00
/// Evaluate a string containing an expression, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
///
/// assert_eq!(engine.eval_expression::<i64>("40 + 2")?, 42);
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2021-12-25 16:49:14 +01:00
pub fn eval_expression<T: Variant + Clone>(&self, script: &str) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
self.eval_expression_with_scope(&mut Scope::new(), script)
}
2022-08-08 03:10:15 +02:00
/// Evaluate a string containing an expression with own scope, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// 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);
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_expression_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
script: &str,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
let scripts = [script];
let (stream, tokenizer_control) =
2022-07-05 10:26:38 +02:00
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
2022-08-12 10:34:57 +02:00
let mut state = ParseState::new(self, scope, Default::default(), tokenizer_control);
2021-11-20 07:57:21 +01:00
// No need to optimize a lone expression
let ast = self.parse_global_expr(
&mut stream.peekable(),
&mut state,
#[cfg(not(feature = "no_optimize"))]
2022-02-07 14:03:39 +01:00
OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
OptimizationLevel::default(),
2021-11-20 07:57:21 +01:00
)?;
self.eval_ast_with_scope(scope, &ast)
}
2022-08-08 03:10:15 +02:00
/// Evaluate an [`AST`], returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// 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);
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2021-12-25 16:49:14 +01:00
pub fn eval_ast<T: Variant + Clone>(&self, ast: &AST) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
self.eval_ast_with_scope(&mut Scope::new(), ast)
}
2022-08-08 03:10:15 +02:00
/// Evaluate an [`AST`] with own scope, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// 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);
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_ast_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
ast: &AST,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<T> {
2022-01-28 11:59:18 +01:00
let global = &mut GlobalRuntimeState::new(self);
2022-01-24 10:04:40 +01:00
2021-12-27 16:03:30 +01:00
let result = self.eval_ast_with_scope_raw(scope, global, ast, 0)?;
2021-11-20 07:57:21 +01:00
2022-04-26 10:36:24 +02:00
#[cfg(feature = "debugging")]
if self.debugger.is_some() {
global.debugger.status = crate::eval::DebuggerStatus::Terminate;
2022-04-26 12:32:43 +02:00
let lib = &[
#[cfg(not(feature = "no_function"))]
ast.as_ref(),
];
2022-04-26 10:36:24 +02:00
let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(scope, global, lib, &mut None, node, 0)?;
}
2021-11-20 07:57:21 +01:00
let typ = self.map_type_name(result.type_name());
result.try_cast::<T>().ok_or_else(|| {
2022-03-14 01:50:17 +01:00
let t = self.map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
2021-11-20 07:57:21 +01:00
})
}
2022-08-08 03:10:15 +02:00
/// Evaluate an [`AST`] with own scope, returning the result value or an error.
2021-11-20 07:57:21 +01:00
#[inline]
pub(crate) fn eval_ast_with_scope_raw<'a>(
&self,
scope: &mut Scope,
2021-12-27 16:03:30 +01:00
global: &mut GlobalRuntimeState,
2021-11-20 07:57:21 +01:00
ast: &'a AST,
level: usize,
) -> RhaiResult {
2022-04-16 10:36:53 +02:00
let mut caches = Caches::new();
global.source = ast.source_raw().clone();
2021-11-20 07:57:21 +01:00
#[cfg(not(feature = "no_module"))]
2022-06-09 12:22:53 +02:00
let orig_embedded_module_resolver = std::mem::replace(
2022-04-23 07:37:08 +02:00
&mut global.embedded_module_resolver,
ast.resolver().cloned(),
);
2021-11-20 07:57:21 +01:00
let statements = ast.statements();
if statements.is_empty() {
return Ok(Dynamic::UNIT);
}
2022-04-26 12:32:43 +02:00
let mut _lib = &[
#[cfg(not(feature = "no_function"))]
ast.as_ref(),
2022-04-26 12:32:43 +02:00
][..];
#[cfg(not(feature = "no_function"))]
if !ast.has_functions() {
_lib = &[];
}
2022-04-23 07:37:08 +02:00
let result =
2022-04-26 12:32:43 +02:00
self.eval_global_statements(scope, global, &mut caches, statements, _lib, level);
2022-04-23 07:37:08 +02:00
#[cfg(not(feature = "no_module"))]
{
global.embedded_module_resolver = orig_embedded_module_resolver;
}
result
}
/// _(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.
#[cfg(feature = "internals")]
#[inline(always)]
pub fn eval_statements_raw(
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
statements: &[crate::ast::Stmt],
2022-04-26 12:32:43 +02:00
lib: &[&crate::Module],
2022-04-23 07:37:08 +02:00
level: usize,
) -> RhaiResult {
self.eval_global_statements(scope, global, caches, statements, lib, level)
2021-11-20 07:57:21 +01:00
}
}
2022-08-08 03:10:15 +02:00
/// Evaluate a string as a script, returning the result value or an error.
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let result: i64 = rhai::eval("40 + 2")?;
///
/// assert_eq!(result, 42);
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub fn eval<T: Variant + Clone>(script: &str) -> RhaiResultOf<T> {
Engine::new().eval(script)
}