rhai/src/scope.rs

438 lines
12 KiB
Rust
Raw Normal View History

2020-03-08 12:54:02 +01:00
//! Module that defines the `Scope` type representing a function call-stack scope.
use crate::any::{Any, Dynamic};
use crate::parser::{map_dynamic_to_expr, Expr, Position};
2020-03-11 04:03:18 +01:00
2020-03-17 19:26:11 +01:00
use crate::stdlib::{
borrow::Cow,
iter,
string::{String, ToString},
vec::Vec,
};
2020-03-03 08:20:20 +01:00
2020-03-25 04:27:18 +01:00
/// Type of an entry in the Scope.
2020-03-13 11:12:41 +01:00
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
2020-03-25 04:27:18 +01:00
pub enum EntryType {
/// Normal value.
2020-03-13 11:12:41 +01:00
Normal,
/// Immutable constant value.
2020-03-13 11:12:41 +01:00
Constant,
}
/// An entry in the Scope.
2020-03-25 04:27:18 +01:00
#[derive(Debug, Clone)]
pub struct Entry<'a> {
/// Name of the entry.
pub name: Cow<'a, str>,
2020-03-25 04:27:18 +01:00
/// Type of the entry.
pub typ: EntryType,
/// Current value of the entry.
pub value: Dynamic,
/// A constant expression if the initial value matches one of the recognized types.
pub expr: Option<Expr>,
}
/// Information about a particular entry in the Scope.
2020-03-25 04:27:18 +01:00
#[derive(Debug, Hash, Copy, Clone)]
pub(crate) struct EntryRef<'a> {
pub name: &'a str,
2020-03-25 04:27:18 +01:00
pub index: usize,
pub typ: EntryType,
}
/// A type containing information about the current scope.
/// Useful for keeping state between `Engine` evaluation runs.
2020-03-03 08:20:20 +01:00
///
2020-03-04 15:00:01 +01:00
/// # Example
///
2020-03-19 06:52:10 +01:00
/// ```
2020-03-09 14:09:53 +01:00
/// # fn main() -> Result<(), rhai::EvalAltResult> {
2020-03-03 08:20:20 +01:00
/// use rhai::{Engine, Scope};
///
/// let mut engine = Engine::new();
/// let mut my_scope = Scope::new();
///
2020-04-05 17:43:40 +02:00
/// my_scope.push("z", 40_i64);
2020-03-09 14:09:53 +01:00
///
2020-04-05 17:43:40 +02:00
/// engine.eval_with_scope::<()>(&mut my_scope, "let x = z + 1; z = 0;")?;
///
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1")?, 42);
///
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 41);
/// assert_eq!(my_scope.get_value::<i64>("z").unwrap(), 0);
2020-03-09 14:09:53 +01:00
/// # Ok(())
/// # }
2020-03-03 08:20:20 +01:00
/// ```
///
2020-03-25 04:27:18 +01:00
/// When searching for entries, newly-added entries are found before similarly-named but older entries,
/// allowing for automatic _shadowing_.
2020-04-03 13:42:01 +02:00
///
/// Currently, `Scope` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`.
#[derive(Debug, Clone)]
2020-03-25 04:27:18 +01:00
pub struct Scope<'a>(Vec<Entry<'a>>);
2020-03-03 08:20:20 +01:00
2020-03-07 03:15:42 +01:00
impl<'a> Scope<'a> {
2020-03-03 08:20:20 +01:00
/// Create a new Scope.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-03-03 08:20:20 +01:00
pub fn new() -> Self {
Self(Vec::new())
}
/// Empty the Scope.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert!(my_scope.contains("x"));
/// assert_eq!(my_scope.len(), 1);
/// assert!(!my_scope.is_empty());
///
/// my_scope.clear();
/// assert!(!my_scope.contains("x"));
/// assert_eq!(my_scope.len(), 0);
/// assert!(my_scope.is_empty());
/// ```
2020-03-03 08:20:20 +01:00
pub fn clear(&mut self) {
self.0.clear();
}
2020-03-25 04:27:18 +01:00
/// Get the number of entries inside the Scope.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
/// assert_eq!(my_scope.len(), 0);
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.len(), 1);
/// ```
2020-03-03 08:20:20 +01:00
pub fn len(&self) -> usize {
self.0.len()
}
2020-03-24 09:57:35 +01:00
/// Is the Scope empty?
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
/// assert!(my_scope.is_empty());
///
/// my_scope.push("x", 42_i64);
/// assert!(!my_scope.is_empty());
/// ```
2020-03-24 09:57:35 +01:00
pub fn is_empty(&self) -> bool {
self.0.len() == 0
}
2020-03-25 04:27:18 +01:00
/// Add (push) a new entry to the Scope.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-03-19 12:53:42 +01:00
pub fn push<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) {
2020-03-25 04:27:18 +01:00
self.push_dynamic_value(name, EntryType::Normal, value.into_dynamic(), false);
}
2020-03-25 04:27:18 +01:00
/// Add (push) a new `Dynamic` entry to the Scope.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::{Any, Scope};
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push_dynamic("x", (42_i64).into_dynamic());
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-03-25 04:27:18 +01:00
pub fn push_dynamic<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Dynamic) {
self.push_dynamic_value(name, EntryType::Normal, value, false);
2020-03-03 08:20:20 +01:00
}
2020-03-13 11:12:41 +01:00
/// Add (push) a new constant to the Scope.
///
/// Constants are immutable and cannot be assigned to. Their values never change.
/// Constants propagation is a technique used to optimize an AST.
2020-04-05 17:43:40 +02:00
///
/// However, in order to be used for optimization, constants must be in one of the recognized types:
/// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push_constant("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-03-19 12:53:42 +01:00
pub fn push_constant<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) {
2020-03-25 04:27:18 +01:00
self.push_dynamic_value(name, EntryType::Constant, value.into_dynamic(), true);
}
2020-03-25 04:27:18 +01:00
/// Add (push) a new constant with a `Dynamic` value to the Scope.
///
/// Constants are immutable and cannot be assigned to. Their values never change.
/// Constants propagation is a technique used to optimize an AST.
2020-04-05 17:43:40 +02:00
///
2020-03-25 04:27:18 +01:00
/// However, in order to be used for optimization, the `Dynamic` value must be in one of the
/// recognized types:
/// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::{Any, Scope};
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push_constant_dynamic("x", (42_i64).into_dynamic());
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-03-25 04:27:18 +01:00
pub fn push_constant_dynamic<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Dynamic) {
self.push_dynamic_value(name, EntryType::Constant, value, true);
2020-03-13 11:12:41 +01:00
}
2020-03-25 04:27:18 +01:00
/// Add (push) a new entry with a `Dynamic` value to the Scope.
pub(crate) fn push_dynamic_value<K: Into<Cow<'a, str>>>(
2020-03-13 11:12:41 +01:00
&mut self,
name: K,
2020-03-25 04:27:18 +01:00
entry_type: EntryType,
2020-03-13 11:12:41 +01:00
value: Dynamic,
2020-03-25 04:27:18 +01:00
map_expr: bool,
2020-03-13 11:12:41 +01:00
) {
2020-03-25 04:27:18 +01:00
self.0.push(Entry {
name: name.into(),
2020-03-25 04:27:18 +01:00
typ: entry_type,
2020-04-05 11:44:48 +02:00
value: value.clone(),
expr: if map_expr {
map_dynamic_to_expr(value, Position::none())
} else {
None
},
});
2020-03-03 08:20:20 +01:00
}
/// Truncate (rewind) the Scope to a previous size.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// my_scope.push("y", 123_i64);
/// assert!(my_scope.contains("x"));
/// assert!(my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 2);
///
/// my_scope.rewind(1);
/// assert!(my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 1);
///
/// my_scope.rewind(0);
/// assert!(!my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// assert_eq!(my_scope.len(), 0);
/// assert!(my_scope.is_empty());
/// ```
2020-03-03 08:20:20 +01:00
pub fn rewind(&mut self, size: usize) {
self.0.truncate(size);
}
2020-03-25 04:27:18 +01:00
/// Does the scope contain the entry?
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert!(my_scope.contains("x"));
/// assert!(!my_scope.contains("y"));
/// ```
pub fn contains(&self, name: &str) -> bool {
2020-03-19 13:55:49 +01:00
self.0
.iter()
.rev() // Always search a Scope in reverse order
2020-04-05 17:43:40 +02:00
.any(|Entry { name: key, .. }| name == key)
2020-03-19 13:55:49 +01:00
}
2020-03-25 04:27:18 +01:00
/// Find an entry in the Scope, starting from the last.
2020-04-05 17:43:40 +02:00
pub(crate) fn get(&self, name: &str) -> Option<(EntryRef, Dynamic)> {
2020-03-03 08:20:20 +01:00
self.0
.iter()
.enumerate()
2020-03-04 15:00:01 +01:00
.rev() // Always search a Scope in reverse order
2020-04-05 17:43:40 +02:00
.find_map(
|(
2020-04-05 11:44:48 +02:00
index,
2020-03-25 04:27:18 +01:00
Entry {
2020-04-05 17:43:40 +02:00
name: key,
typ,
value,
..
},
)| {
2020-04-05 17:43:40 +02:00
if name == key {
Some((
EntryRef {
name: key,
index,
typ: *typ,
},
value.clone(),
))
} else {
None
}
},
)
2020-03-03 08:20:20 +01:00
}
2020-03-25 04:27:18 +01:00
/// Get the value of an entry in the Scope, starting from the last.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
2020-04-05 13:17:48 +02:00
pub fn get_value<T: Any + Clone>(&self, name: &str) -> Option<T> {
2020-03-03 08:20:20 +01:00
self.0
.iter()
2020-04-05 17:43:40 +02:00
.rev()
.find(|Entry { name: key, .. }| name == key)
.and_then(|Entry { value, .. }| value.downcast_ref::<T>())
.map(T::clone)
2020-03-03 08:20:20 +01:00
}
2020-04-05 17:43:40 +02:00
/// Update the value of the named entry.
/// Search starts backwards from the last, and only the first entry matching the specified name is updated.
/// If no entry matching the specified name is found, a new one is added.
2020-04-05 13:17:48 +02:00
///
/// # Panics
///
/// Panics when trying to update the value of a constant.
2020-04-05 17:43:40 +02:00
///
/// # Examples
///
/// ```
/// use rhai::Scope;
///
/// let mut my_scope = Scope::new();
///
/// my_scope.push("x", 42_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
///
/// my_scope.set_value("x", 0_i64);
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 0);
/// ```
2020-04-05 13:17:48 +02:00
pub fn set_value<T: Any + Clone>(&mut self, name: &'a str, value: T) {
match self.get(name) {
Some((
EntryRef {
typ: EntryType::Constant,
..
},
_,
)) => panic!("variable {} is constant", name),
Some((
EntryRef {
index,
typ: EntryType::Normal,
..
},
_,
)) => self.0.get_mut(index).unwrap().value = value.into_dynamic(),
None => self.push(name, value.into_dynamic()),
}
}
2020-03-25 04:27:18 +01:00
/// Get a mutable reference to an entry in the Scope.
pub(crate) fn get_mut(&mut self, key: EntryRef) -> &mut Dynamic {
let entry = self.0.get_mut(key.index).expect("invalid index in Scope");
assert_eq!(entry.typ, key.typ, "entry type not matched");
2020-03-03 08:20:20 +01:00
// assert_ne!(
2020-03-25 04:27:18 +01:00
// entry.typ,
// EntryType::Constant,
// "get mut of constant entry"
// );
2020-03-25 04:27:18 +01:00
assert_eq!(entry.name, key.name, "incorrect key at Scope entry");
2020-03-03 08:20:20 +01:00
&mut entry.value
2020-03-03 08:20:20 +01:00
}
2020-03-25 04:27:18 +01:00
/// Get a mutable reference to an entry in the Scope and downcast it to a specific type
pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, key: EntryRef) -> &mut T {
self.get_mut(key)
.downcast_mut::<T>()
.expect("wrong type cast")
}
2020-03-25 04:27:18 +01:00
/// Get an iterator to entries in the Scope.
pub fn iter(&self) -> impl Iterator<Item = &Entry> {
self.0.iter().rev() // Always search a Scope in reverse order
2020-03-03 08:20:20 +01:00
}
}
2020-03-24 09:57:35 +01:00
impl Default for Scope<'_> {
fn default() -> Self {
Scope::new()
}
}
2020-03-25 04:27:18 +01:00
impl<'a, K> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a>
2020-03-07 03:15:42 +01:00
where
K: Into<Cow<'a, str>>,
{
2020-03-25 04:27:18 +01:00
fn extend<T: IntoIterator<Item = (K, EntryType, Dynamic)>>(&mut self, iter: T) {
self.0
2020-03-25 04:27:18 +01:00
.extend(iter.into_iter().map(|(name, typ, value)| Entry {
name: name.into(),
2020-03-25 04:27:18 +01:00
typ,
value,
expr: None,
}));
}
}