2022-01-25 07:32:42 +01:00
|
|
|
#![cfg(feature = "debugging")]
|
2022-01-28 11:59:18 +01:00
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult, INT};
|
2022-01-25 07:32:42 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
use rhai::Array;
|
|
|
|
|
2022-01-28 11:59:18 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
use rhai::Map;
|
|
|
|
|
2022-01-25 07:32:42 +01:00
|
|
|
#[test]
|
|
|
|
fn test_debugging() -> Result<(), Box<EvalAltResult>> {
|
2022-02-02 07:57:30 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_debugger(
|
2022-11-25 06:20:03 +01:00
|
|
|
|_, dbg| dbg,
|
2022-02-02 07:57:30 +01:00
|
|
|
|_, _, _, _, _| Ok(rhai::debugger::DebuggerCommand::Continue),
|
|
|
|
);
|
2022-01-25 07:32:42 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
{
|
|
|
|
let r = engine.eval::<Array>(
|
|
|
|
"
|
|
|
|
fn foo(x) {
|
|
|
|
if x >= 5 {
|
2022-02-02 07:47:35 +01:00
|
|
|
back_trace()
|
2022-01-25 07:32:42 +01:00
|
|
|
} else {
|
|
|
|
foo(x+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(0)
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
assert_eq!(r.len(), 6);
|
|
|
|
|
2022-02-02 07:47:35 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("len(back_trace())")?, 0);
|
2022-01-25 07:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-28 11:59:18 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
fn test_debugger_state() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2022-02-01 07:07:06 +01:00
|
|
|
engine.register_debugger(
|
2022-11-25 06:20:03 +01:00
|
|
|
|_, mut debugger| {
|
2022-01-28 11:59:18 +01:00
|
|
|
// Say, use an object map for the debugger state
|
|
|
|
let mut state = Map::new();
|
|
|
|
// Initialize properties
|
|
|
|
state.insert("hello".into(), (42 as INT).into());
|
|
|
|
state.insert("foo".into(), false.into());
|
2022-11-25 06:20:03 +01:00
|
|
|
debugger.set_state(state);
|
|
|
|
debugger
|
2022-01-28 11:59:18 +01:00
|
|
|
},
|
2022-05-01 18:03:45 +02:00
|
|
|
|mut context, _, _, _, _| {
|
2022-01-28 11:59:18 +01:00
|
|
|
// Print debugger state - which is an object map
|
2022-05-21 15:44:12 +02:00
|
|
|
println!(
|
|
|
|
"Current state = {}",
|
2022-11-25 06:20:03 +01:00
|
|
|
context.global_runtime_state().debugger().state()
|
2022-05-21 15:44:12 +02:00
|
|
|
);
|
2022-01-28 11:59:18 +01:00
|
|
|
|
|
|
|
// Modify state
|
2022-05-21 15:44:12 +02:00
|
|
|
let mut state = context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-05-21 15:44:12 +02:00
|
|
|
.state_mut()
|
|
|
|
.write_lock::<Map>()
|
|
|
|
.unwrap();
|
2022-01-28 11:59:18 +01:00
|
|
|
let hello = state.get("hello").unwrap().as_int().unwrap();
|
|
|
|
state.insert("hello".into(), (hello + 1).into());
|
|
|
|
state.insert("foo".into(), true.into());
|
|
|
|
state.insert("something_new".into(), "hello, world!".into());
|
|
|
|
|
|
|
|
// Continue with debugging
|
|
|
|
Ok(rhai::debugger::DebuggerCommand::StepInto)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
engine.run("let x = 42;")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|