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));
|
2022-04-21 13:21:53 +08:00
|
|
|
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);
|
2022-01-01 17:20:00 +08:00
|
|
|
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() {
|
2021-11-29 09:43:35 +08:00
|
|
|
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
|
|
|
}
|
2022-04-26 16:36:24 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "debugging")]
|
|
|
|
if self.debugger.is_some() {
|
|
|
|
global.debugger.status = crate::eval::DebuggerStatus::Terminate;
|
|
|
|
let lib = &[ast.as_ref()];
|
|
|
|
let node = &crate::ast::Stmt::Noop(crate::Position::NONE);
|
|
|
|
self.run_debugger(scope, global, lib, &mut None, node, 0)?;
|
|
|
|
}
|
|
|
|
|
2021-11-20 14:57:21 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|