Better encapsulate EvalContext.
This commit is contained in:
parent
591f7d7362
commit
7c8c6659ae
@ -1,7 +1,6 @@
|
|||||||
//! Module implementing custom syntax for [`Engine`].
|
//! Module implementing custom syntax for [`Engine`].
|
||||||
|
|
||||||
use crate::ast::Expr;
|
use crate::ast::Expr;
|
||||||
use crate::eval::Caches;
|
|
||||||
use crate::func::native::SendSync;
|
use crate::func::native::SendSync;
|
||||||
use crate::parser::ParseResult;
|
use crate::parser::ParseResult;
|
||||||
use crate::tokenizer::{is_valid_identifier, Token};
|
use crate::tokenizer::{is_valid_identifier, Token};
|
||||||
@ -140,34 +139,6 @@ impl Deref for Expression<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> {
|
|
||||||
/// Evaluate an [expression tree][Expression] within this [evaluation context][`EvalContext`].
|
|
||||||
///
|
|
||||||
/// # WARNING - Low Level API
|
|
||||||
///
|
|
||||||
/// This function is very low level. It evaluates an expression from an [`AST`][crate::AST].
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn eval_expression_tree(&mut self, expr: &Expression) -> RhaiResult {
|
|
||||||
let mut caches;
|
|
||||||
|
|
||||||
self.engine.eval_expr(
|
|
||||||
self.scope,
|
|
||||||
self.global,
|
|
||||||
match self.caches.as_mut() {
|
|
||||||
Some(c) => c,
|
|
||||||
None => {
|
|
||||||
caches = Caches::new();
|
|
||||||
&mut caches
|
|
||||||
}
|
|
||||||
},
|
|
||||||
self.lib,
|
|
||||||
self.this_ptr,
|
|
||||||
expr,
|
|
||||||
self.level,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Definition of a custom syntax definition.
|
/// Definition of a custom syntax definition.
|
||||||
pub struct CustomSyntax {
|
pub struct CustomSyntax {
|
||||||
/// A parsing function to return the next token in a custom syntax based on the
|
/// A parsing function to return the next token in a custom syntax based on the
|
||||||
|
@ -496,15 +496,7 @@ impl Engine {
|
|||||||
Some(source.as_str())
|
Some(source.as_str())
|
||||||
};
|
};
|
||||||
|
|
||||||
let context = crate::EvalContext {
|
let context = crate::EvalContext::new(self, scope, global, None, lib, this_ptr, level);
|
||||||
engine: self,
|
|
||||||
scope,
|
|
||||||
global,
|
|
||||||
caches: None,
|
|
||||||
lib,
|
|
||||||
this_ptr,
|
|
||||||
level,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((.., ref on_debugger)) = self.debugger {
|
if let Some((.., ref on_debugger)) = self.debugger {
|
||||||
let command = on_debugger(context, event, node, source, node.position())?;
|
let command = on_debugger(context, event, node, source, node.position())?;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Evaluation context.
|
//! Evaluation context.
|
||||||
|
|
||||||
use super::{Caches, GlobalRuntimeState};
|
use super::{Caches, GlobalRuntimeState};
|
||||||
use crate::{Dynamic, Engine, Module, Scope};
|
use crate::{Dynamic, Engine, Expression, Module, RhaiResult, Scope};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
@ -9,26 +9,48 @@ use std::prelude::v1::*;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EvalContext<'a, 's, 'ps, 'g, 'pg, 'c, 't, 'pt> {
|
pub struct EvalContext<'a, 's, 'ps, 'g, 'pg, 'c, 't, 'pt> {
|
||||||
/// The current [`Engine`].
|
/// The current [`Engine`].
|
||||||
pub(crate) engine: &'a Engine,
|
engine: &'a Engine,
|
||||||
/// The current [`Scope`].
|
/// The current [`Scope`].
|
||||||
pub(crate) scope: &'s mut Scope<'ps>,
|
scope: &'s mut Scope<'ps>,
|
||||||
/// The current [`GlobalRuntimeState`].
|
/// The current [`GlobalRuntimeState`].
|
||||||
pub(crate) global: &'g mut GlobalRuntimeState<'pg>,
|
global: &'g mut GlobalRuntimeState<'pg>,
|
||||||
/// The current [caches][Caches], if available.
|
/// The current [caches][Caches], if available.
|
||||||
pub(crate) caches: Option<&'c mut Caches>,
|
caches: Option<&'c mut Caches>,
|
||||||
/// The current stack of imported [modules][Module].
|
/// The current stack of imported [modules][Module].
|
||||||
pub(crate) lib: &'a [&'a Module],
|
lib: &'a [&'a Module],
|
||||||
/// The current bound `this` pointer, if any.
|
/// The current bound `this` pointer, if any.
|
||||||
pub(crate) this_ptr: &'t mut Option<&'pt mut Dynamic>,
|
this_ptr: &'t mut Option<&'pt mut Dynamic>,
|
||||||
/// The current nesting level of function calls.
|
/// The current nesting level of function calls.
|
||||||
pub(crate) level: usize,
|
level: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'ps, 'g, 'pg, 'pt> EvalContext<'_, 's, 'ps, 'g, 'pg, '_, '_, 'pt> {
|
impl<'a, 's, 'ps, 'g, 'pg, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'pg, 'c, 't, 'pt> {
|
||||||
|
/// Create a new [`EvalContext`].
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(
|
||||||
|
engine: &'a Engine,
|
||||||
|
scope: &'s mut Scope<'ps>,
|
||||||
|
global: &'g mut GlobalRuntimeState<'pg>,
|
||||||
|
caches: Option<&'c mut Caches>,
|
||||||
|
lib: &'a [&'a Module],
|
||||||
|
this_ptr: &'t mut Option<&'pt mut Dynamic>,
|
||||||
|
level: usize,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
engine,
|
||||||
|
scope,
|
||||||
|
global,
|
||||||
|
caches,
|
||||||
|
lib,
|
||||||
|
this_ptr,
|
||||||
|
level,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// The current [`Engine`].
|
/// The current [`Engine`].
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn engine(&self) -> &Engine {
|
pub const fn engine(&self) -> &'a Engine {
|
||||||
self.engine
|
self.engine
|
||||||
}
|
}
|
||||||
/// The current source.
|
/// The current source.
|
||||||
@ -110,8 +132,8 @@ impl<'s, 'ps, 'g, 'pg, 'pt> EvalContext<'_, 's, 'ps, 'g, 'pg, '_, '_, 'pt> {
|
|||||||
/// Mutable reference to the current bound `this` pointer, if any.
|
/// Mutable reference to the current bound `this` pointer, if any.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn this_ptr_mut(&mut self) -> Option<&mut &'pt mut Dynamic> {
|
pub fn this_ptr_mut(&mut self) -> &mut Option<&'pt mut Dynamic> {
|
||||||
self.this_ptr.as_mut()
|
&mut self.this_ptr
|
||||||
}
|
}
|
||||||
/// The current nesting level of function calls.
|
/// The current nesting level of function calls.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@ -119,4 +141,29 @@ impl<'s, 'ps, 'g, 'pg, 'pt> EvalContext<'_, 's, 'ps, 'g, 'pg, '_, '_, 'pt> {
|
|||||||
pub const fn call_level(&self) -> usize {
|
pub const fn call_level(&self) -> usize {
|
||||||
self.level
|
self.level
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate an [expression tree][Expression] within this [evaluation context][`EvalContext`].
|
||||||
|
///
|
||||||
|
/// # WARNING - Low Level API
|
||||||
|
///
|
||||||
|
/// This function is very low level. It evaluates an expression from an [`AST`][crate::AST].
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn eval_expression_tree(&mut self, expr: &Expression) -> RhaiResult {
|
||||||
|
let mut new_caches = Caches::new();
|
||||||
|
|
||||||
|
let caches = match self.caches.as_mut() {
|
||||||
|
Some(c) => c,
|
||||||
|
None => &mut new_caches,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.engine.eval_expr(
|
||||||
|
self.scope,
|
||||||
|
self.global,
|
||||||
|
caches,
|
||||||
|
self.lib,
|
||||||
|
self.this_ptr,
|
||||||
|
expr,
|
||||||
|
self.level,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,15 +151,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Check the variable resolver, if any
|
// Check the variable resolver, if any
|
||||||
if let Some(ref resolve_var) = self.resolve_var {
|
if let Some(ref resolve_var) = self.resolve_var {
|
||||||
let context = EvalContext {
|
let context = EvalContext::new(self, scope, global, None, lib, this_ptr, level);
|
||||||
engine: self,
|
|
||||||
scope,
|
|
||||||
global,
|
|
||||||
caches: None,
|
|
||||||
lib,
|
|
||||||
this_ptr,
|
|
||||||
level,
|
|
||||||
};
|
|
||||||
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
|
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
|
||||||
match resolve_var(var_name, index, context) {
|
match resolve_var(var_name, index, context) {
|
||||||
Ok(Some(mut result)) => {
|
Ok(Some(mut result)) => {
|
||||||
@ -480,15 +472,8 @@ impl Engine {
|
|||||||
*pos,
|
*pos,
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
let mut context = EvalContext {
|
let mut context =
|
||||||
engine: self,
|
EvalContext::new(self, scope, global, Some(caches), lib, this_ptr, level);
|
||||||
scope,
|
|
||||||
global,
|
|
||||||
caches: Some(caches),
|
|
||||||
lib,
|
|
||||||
this_ptr,
|
|
||||||
level,
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = (custom_def.func)(&mut context, &expressions);
|
let result = (custom_def.func)(&mut context, &expressions);
|
||||||
|
|
||||||
|
@ -831,15 +831,7 @@ impl Engine {
|
|||||||
nesting_level,
|
nesting_level,
|
||||||
will_shadow,
|
will_shadow,
|
||||||
};
|
};
|
||||||
let context = EvalContext {
|
let context = EvalContext::new(self, scope, global, None, lib, this_ptr, level);
|
||||||
engine: self,
|
|
||||||
scope,
|
|
||||||
global,
|
|
||||||
caches: None,
|
|
||||||
lib,
|
|
||||||
this_ptr,
|
|
||||||
level,
|
|
||||||
};
|
|
||||||
|
|
||||||
match filter(true, info, context) {
|
match filter(true, info, context) {
|
||||||
Ok(true) => None,
|
Ok(true) => None,
|
||||||
|
Loading…
Reference in New Issue
Block a user