From 0b3250a262a8fb77d4138ceb93427a5fde6d48a3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 19 Jan 2022 14:02:55 +0800 Subject: [PATCH] Add strict command to rhai-repl. --- src/bin/rhai-repl.rs | 64 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 5e06f511..1b77f562 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -1,4 +1,4 @@ -use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST}; +use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT}; use std::{ env, @@ -46,6 +46,7 @@ fn print_help() { println!("help => print this help"); println!("quit, exit => quit"); println!("scope => print all variables in the scope"); + println!("strict => toggle on/off Strict Variables Mode"); #[cfg(feature = "metadata")] println!("functions => print all functions defined"); #[cfg(feature = "metadata")] @@ -56,6 +57,30 @@ fn print_help() { println!(); } +/// Display the scope. +fn print_scope(scope: &Scope) { + scope + .iter_raw() + .enumerate() + .for_each(|(i, (name, constant, value))| { + #[cfg(not(feature = "no_closure"))] + let value_is_shared = if value.is_shared() { " (shared)" } else { "" }; + #[cfg(feature = "no_closure")] + let value_is_shared = ""; + + println!( + "[{}] {}{}{} = {:?}", + i + 1, + if constant { "const " } else { "" }, + name, + value_is_shared, + *value.read_lock::().unwrap(), + ) + }); + + println!(); +} + fn main() { let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION")); println!("{}", title); @@ -156,15 +181,12 @@ fn main() { } engine - .register_fn("test", |x: rhai::INT, y: rhai::INT| format!("{} {}", x, y)) - .register_fn("test", |x: &mut rhai::INT, y: rhai::INT, z: &str| { + .register_fn("test", |x: INT, y: INT| format!("{} {}", x, y)) + .register_fn("test", |x: &mut INT, y: INT, z: &str| { *x += y; println!("{} {} {}", x, y, z); }); - // Make Engine immutable - let engine = engine; - // Create scope let mut scope = Scope::new(); @@ -215,26 +237,18 @@ fn main() { continue; } "exit" | "quit" => break, // quit + "strict" if engine.strict_variables() => { + engine.set_strict_variables(false); + println!("Strict Variables Mode turned OFF."); + continue; + } + "strict" => { + engine.set_strict_variables(true); + println!("Strict Variables Mode turned ON."); + continue; + } "scope" => { - scope - .iter_raw() - .enumerate() - .for_each(|(i, (name, constant, value))| { - #[cfg(not(feature = "no_closure"))] - let value_is_shared = if value.is_shared() { " (shared)" } else { "" }; - #[cfg(feature = "no_closure")] - let value_is_shared = ""; - - println!( - "[{}] {}{}{} = {:?}", - i + 1, - if constant { "const " } else { "" }, - name, - value_is_shared, - *value.read_lock::().unwrap(), - ) - }); - println!(); + print_scope(&scope); continue; } "astu" => {