Add strict command to rhai-repl.
This commit is contained in:
parent
bb34e316e8
commit
0b3250a262
@ -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::<Dynamic>().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::<Dynamic>().unwrap(),
|
||||
)
|
||||
});
|
||||
println!();
|
||||
print_scope(&scope);
|
||||
continue;
|
||||
}
|
||||
"astu" => {
|
||||
|
Loading…
Reference in New Issue
Block a user