Add strict command to rhai-repl.

This commit is contained in:
Stephen Chung 2022-01-19 14:02:55 +08:00
parent bb34e316e8
commit 0b3250a262

View File

@ -1,4 +1,4 @@
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST}; use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT};
use std::{ use std::{
env, env,
@ -46,6 +46,7 @@ fn print_help() {
println!("help => print this help"); println!("help => print this help");
println!("quit, exit => quit"); println!("quit, exit => quit");
println!("scope => print all variables in the scope"); println!("scope => print all variables in the scope");
println!("strict => toggle on/off Strict Variables Mode");
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
println!("functions => print all functions defined"); println!("functions => print all functions defined");
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -56,6 +57,30 @@ fn print_help() {
println!(); 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::<Dynamic>().unwrap(),
)
});
println!();
}
fn main() { fn main() {
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION")); let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
println!("{}", title); println!("{}", title);
@ -156,15 +181,12 @@ fn main() {
} }
engine engine
.register_fn("test", |x: rhai::INT, y: rhai::INT| format!("{} {}", x, y)) .register_fn("test", |x: INT, y: INT| format!("{} {}", x, y))
.register_fn("test", |x: &mut rhai::INT, y: rhai::INT, z: &str| { .register_fn("test", |x: &mut INT, y: INT, z: &str| {
*x += y; *x += y;
println!("{} {} {}", x, y, z); println!("{} {} {}", x, y, z);
}); });
// Make Engine immutable
let engine = engine;
// Create scope // Create scope
let mut scope = Scope::new(); let mut scope = Scope::new();
@ -215,26 +237,18 @@ fn main() {
continue; continue;
} }
"exit" | "quit" => break, // quit "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" => {
scope print_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::<Dynamic>().unwrap(),
)
});
println!();
continue; continue;
} }
"astu" => { "astu" => {