rhai/src/api/run.rs

70 lines
2.5 KiB
Rust
Raw Normal View History

2021-11-20 14:57:21 +08:00
//! Module that defines the public evaluation API of [`Engine`].
2022-04-16 16:36:53 +08:00
use crate::eval::{Caches, GlobalRuntimeState};
2021-11-20 14:57:21 +08:00
use crate::parser::ParseState;
2021-12-25 23:49:14 +08:00
use crate::{Engine, Module, RhaiResultOf, Scope, AST};
2021-11-20 14:57:21 +08:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
impl Engine {
2021-11-20 15:43:55 +08:00
/// Evaluate a script, returning any error (if any).
#[inline(always)]
2021-12-25 23:49:14 +08:00
pub fn run(&self, script: &str) -> RhaiResultOf<()> {
2021-11-20 15:43:55 +08:00
self.run_with_scope(&mut Scope::new(), script)
}
2021-11-20 14:57:21 +08:00
/// Evaluate a script with own scope, returning any error (if any).
///
/// ## Constants Propagation
///
2021-11-20 21:29:36 +08:00
/// If not [`OptimizationLevel::None`][crate::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.
2021-11-20 14:57:21 +08:00
#[inline]
2021-12-25 23:49:14 +08:00
pub fn run_with_scope(&self, scope: &mut Scope, script: &str) -> RhaiResultOf<()> {
2021-11-20 14:57:21 +08:00
let scripts = [script];
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(Box::as_ref));
let mut state = ParseState::new(self, scope, tokenizer_control);
2021-11-20 14:57:21 +08:00
let ast = self.parse(
&mut stream.peekable(),
&mut state,
2022-03-29 08:26:42 +08:00
self.options.optimization_level,
2021-11-20 14:57:21 +08:00
)?;
self.run_ast_with_scope(scope, &ast)
}
2021-11-20 21:29:36 +08:00
/// Evaluate an [`AST`], returning any error (if any).
2021-11-20 14:57:21 +08:00
#[inline(always)]
2021-12-25 23:49:14 +08:00
pub fn run_ast(&self, ast: &AST) -> RhaiResultOf<()> {
2021-11-20 14:57:21 +08:00
self.run_ast_with_scope(&mut Scope::new(), ast)
}
/// Evaluate an [`AST`] with own scope, returning any error (if any).
#[inline]
2021-12-25 23:49:14 +08:00
pub fn run_ast_with_scope(&self, scope: &mut Scope, ast: &AST) -> RhaiResultOf<()> {
2022-04-16 16:36:53 +08:00
let caches = &mut Caches::new();
2022-01-28 18:59:18 +08:00
let global = &mut GlobalRuntimeState::new(self);
global.source = ast.source_raw().clone();
2021-11-20 14:57:21 +08:00
#[cfg(not(feature = "no_module"))]
{
2021-12-27 23:03:30 +08:00
global.embedded_module_resolver = ast.resolver().cloned();
2021-11-20 14:57:21 +08:00
}
let statements = ast.statements();
if !statements.is_empty() {
let lib = [
#[cfg(not(feature = "no_function"))]
ast.as_ref(),
];
let lib = if lib.first().map(|m: &&Module| m.is_empty()).unwrap_or(true) {
&lib[0..0]
} else {
&lib
};
2022-04-16 16:36:53 +08:00
self.eval_global_statements(scope, global, caches, statements, lib, 0)?;
2021-11-20 14:57:21 +08:00
}
Ok(())
}
}