2022-02-01 15:30:05 +01:00
|
|
|
use rhai::debugger::{BreakPoint, DebuggerCommand, DebuggerEvent};
|
2022-02-08 12:02:40 +01:00
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, Position, Scope, INT};
|
2022-01-24 10:04:40 +01:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
fs::File,
|
|
|
|
io::{stdin, stdout, Read, Write},
|
|
|
|
path::Path,
|
|
|
|
process::exit,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Pretty-print source line.
|
2022-02-11 15:43:09 +01:00
|
|
|
fn print_source(lines: &[String], pos: Position, offset: usize, window: (usize, usize)) {
|
2022-01-24 10:04:40 +01:00
|
|
|
if pos.is_none() {
|
|
|
|
// No position
|
|
|
|
println!();
|
2022-02-11 15:43:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
let line = pos.line().unwrap() - 1;
|
|
|
|
let start = if line >= window.0 { line - window.0 } else { 0 };
|
|
|
|
let end = usize::min(line + window.1, lines.len() - 1);
|
2022-08-11 13:01:23 +02:00
|
|
|
let line_no_len = end.to_string().len();
|
2022-02-11 15:43:09 +01:00
|
|
|
|
|
|
|
// Print error position
|
|
|
|
if start >= end {
|
|
|
|
println!("{}: {}", start + 1, lines[start]);
|
2022-01-25 11:21:05 +01:00
|
|
|
if let Some(pos) = pos.position() {
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("{0:>1$}", "^", pos + offset + line_no_len + 2);
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-27 10:04:24 +02:00
|
|
|
for (n, s) in lines.iter().enumerate().take(end + 1).skip(start) {
|
2022-02-11 15:43:09 +01:00
|
|
|
let marker = if n == line { "> " } else { " " };
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{0}{1}{2:>3$}{5}│ {0}{4}{5}",
|
|
|
|
if n == line { "\x1b[33m" } else { "" },
|
|
|
|
marker,
|
|
|
|
n + 1,
|
|
|
|
line_no_len,
|
2022-07-27 10:04:24 +02:00
|
|
|
s,
|
2022-02-11 15:43:09 +01:00
|
|
|
if n == line { "\x1b[39m" } else { "" },
|
|
|
|
);
|
|
|
|
|
|
|
|
if n == line {
|
|
|
|
if let Some(pos) = pos.position() {
|
|
|
|
let shift = offset + line_no_len + marker.len() + 2;
|
|
|
|
println!("{0:>1$}{2:>3$}", "│ ", shift, "\x1b[36m^\x1b[39m", pos + 10);
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 11:21:05 +01:00
|
|
|
}
|
2022-01-24 10:04:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 15:30:05 +01:00
|
|
|
fn print_current_source(
|
|
|
|
context: &mut rhai::EvalContext,
|
|
|
|
source: Option<&str>,
|
|
|
|
pos: Position,
|
2022-02-11 15:43:09 +01:00
|
|
|
lines: &[String],
|
|
|
|
window: (usize, usize),
|
2022-02-01 15:30:05 +01:00
|
|
|
) {
|
2022-05-21 15:44:12 +02:00
|
|
|
let current_source = &mut *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::<ImmutableString>()
|
|
|
|
.unwrap();
|
2022-02-01 15:30:05 +01:00
|
|
|
let src = source.unwrap_or("");
|
|
|
|
if src != current_source {
|
2022-04-26 10:36:24 +02:00
|
|
|
println!(
|
|
|
|
"\x1b[34m>>> Source => {}\x1b[39m",
|
|
|
|
source.unwrap_or("main script")
|
|
|
|
);
|
2022-02-01 15:30:05 +01:00
|
|
|
*current_source = src.into();
|
|
|
|
}
|
|
|
|
if !src.is_empty() {
|
|
|
|
// Print just a line number for imported modules
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("{src} @ {pos:?}");
|
2022-02-01 15:30:05 +01:00
|
|
|
} else {
|
|
|
|
// Print the current source line
|
2022-02-11 15:43:09 +01:00
|
|
|
print_source(lines, pos, 0, window);
|
2022-02-01 15:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:04:40 +01:00
|
|
|
/// Pretty-print error.
|
|
|
|
fn print_error(input: &str, mut err: EvalAltResult) {
|
|
|
|
let lines: Vec<_> = input.trim().split('\n').collect();
|
|
|
|
let pos = err.take_position();
|
|
|
|
|
|
|
|
let line_no = if lines.len() > 1 {
|
|
|
|
if pos.is_none() {
|
2022-08-21 11:35:44 +02:00
|
|
|
String::new()
|
2022-01-24 10:04:40 +01:00
|
|
|
} else {
|
|
|
|
format!("{}: ", pos.line().unwrap())
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-21 11:35:44 +02:00
|
|
|
String::new()
|
2022-01-24 10:04:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Print error position
|
|
|
|
if pos.is_none() {
|
|
|
|
// No position
|
2022-11-27 11:00:02 +01:00
|
|
|
println!("\x1b[31m{err}\x1b[39m");
|
2022-01-24 10:04:40 +01:00
|
|
|
} else {
|
|
|
|
// Specific position - print line text
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("{line_no}{}", lines[pos.line().unwrap() - 1]);
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-11-27 11:00:02 +01:00
|
|
|
for (i, err_line) in err.to_string().split('\n').enumerate() {
|
|
|
|
// Display position marker
|
|
|
|
println!(
|
|
|
|
"\x1b[31m{0:>1$}{err_line}\x1b[39m",
|
2022-11-27 15:35:17 +01:00
|
|
|
if i > 0 { "| " } else { "^ " },
|
2022-11-27 11:00:02 +01:00
|
|
|
line_no.len() + pos.position().unwrap() + 1,
|
|
|
|
);
|
|
|
|
}
|
2022-01-24 10:04:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print debug help.
|
|
|
|
fn print_debug_help() {
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("help, h => print this help");
|
|
|
|
println!("quit, q, exit, kill => quit");
|
|
|
|
println!("scope => print the scope");
|
2022-04-24 10:06:11 +02:00
|
|
|
println!("operations => print the total operations performed");
|
|
|
|
println!("source => print the current source");
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("print, p => print all variables de-duplicated");
|
2022-04-24 10:06:11 +02:00
|
|
|
println!("print/p this => print the `this` pointer");
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("print/p <variable> => print the current value of a variable");
|
2022-01-29 06:37:58 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("imports => print all imported modules");
|
|
|
|
println!("node => print the current AST node");
|
|
|
|
println!("list, l => print the current source line");
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("list/l <line#> => print a source line");
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("backtrace, bt => print the current call-stack");
|
|
|
|
println!("info break, i b => print all break-points");
|
|
|
|
println!("enable/en <bp#> => enable a break-point");
|
|
|
|
println!("disable/dis <bp#> => disable a break-point");
|
|
|
|
println!("delete, d => delete all break-points");
|
|
|
|
println!("delete/d <bp#> => delete a break-point");
|
2022-01-25 16:59:35 +01:00
|
|
|
#[cfg(not(feature = "no_position"))]
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("break, b => set a new break-point at the current position");
|
2022-01-25 16:59:35 +01:00
|
|
|
#[cfg(not(feature = "no_position"))]
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("break/b <line#> => set a new break-point at a line number");
|
2022-01-25 16:59:35 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("break/b .<prop> => set a new break-point for a property access");
|
|
|
|
println!("break/b <func> => set a new break-point for a function call");
|
2022-01-25 05:24:30 +01:00
|
|
|
println!(
|
2022-02-01 15:30:05 +01:00
|
|
|
"break/b <func> <#args> => set a new break-point for a function call with #args arguments"
|
2022-01-25 05:24:30 +01:00
|
|
|
);
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("throw => throw a runtime exception");
|
|
|
|
println!("throw <message...> => throw an exception with string data");
|
|
|
|
println!("throw <#> => throw an exception with numeric data");
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("run, r => restart the script evaluation from beginning");
|
|
|
|
println!("step, s => go to the next expression, diving into functions");
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("over, o => go to the next expression, skipping oer functions");
|
2022-02-01 15:30:05 +01:00
|
|
|
println!("next, n, <Enter> => go to the next statement, skipping over functions");
|
|
|
|
println!("finish, f => continue until the end of the current function call");
|
|
|
|
println!("continue, c => continue normal execution");
|
2022-01-24 10:04:40 +01:00
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
// Load script to debug.
|
|
|
|
fn load_script(engine: &Engine) -> (rhai::AST, String) {
|
2022-07-27 10:04:24 +02:00
|
|
|
if let Some(filename) = env::args().nth(1) {
|
2022-02-11 15:43:09 +01:00
|
|
|
let mut contents = String::new();
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
let filename = match Path::new(&filename).canonicalize() {
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!(
|
|
|
|
"\x1b[31mError script file path: {}\n{}\x1b[39m",
|
|
|
|
filename, err
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Ok(f) => {
|
|
|
|
match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap()) {
|
|
|
|
Ok(f) => f.into(),
|
|
|
|
_ => f,
|
2022-01-24 10:04:40 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
};
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
let mut f = match File::open(&filename) {
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!(
|
|
|
|
"\x1b[31mError reading script file: {}\n{}\x1b[39m",
|
2022-01-24 10:04:40 +01:00
|
|
|
filename.to_string_lossy(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
Ok(f) => f,
|
|
|
|
};
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
if let Err(err) = f.read_to_string(&mut contents) {
|
|
|
|
println!(
|
|
|
|
"Error reading script file: {}\n{}",
|
|
|
|
filename.to_string_lossy(),
|
|
|
|
err
|
|
|
|
);
|
2022-01-24 10:04:40 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
let script = if contents.starts_with("#!") {
|
|
|
|
// Skip shebang
|
|
|
|
&contents[contents.find('\n').unwrap_or(0)..]
|
|
|
|
} else {
|
|
|
|
&contents[..]
|
|
|
|
};
|
|
|
|
|
|
|
|
let ast = match engine
|
|
|
|
.compile(script)
|
|
|
|
.map_err(Into::<Box<EvalAltResult>>::into)
|
|
|
|
{
|
|
|
|
Err(err) => {
|
|
|
|
print_error(script, *err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Ok(ast) => ast,
|
|
|
|
};
|
2022-01-24 10:04:40 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("Script '{}' loaded.", filename.to_string_lossy());
|
|
|
|
|
|
|
|
(ast, contents)
|
|
|
|
} else {
|
|
|
|
eprintln!("\x1b[31mNo script file specified.\x1b[39m");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main callback for debugging.
|
|
|
|
fn debug_callback(
|
2022-05-01 18:03:45 +02:00
|
|
|
mut context: rhai::EvalContext,
|
2022-02-11 15:43:09 +01:00
|
|
|
event: DebuggerEvent,
|
|
|
|
node: rhai::ASTNode,
|
|
|
|
source: Option<&str>,
|
|
|
|
pos: Position,
|
|
|
|
lines: &[String],
|
|
|
|
) -> Result<DebuggerCommand, Box<EvalAltResult>> {
|
|
|
|
// Check event
|
|
|
|
match event {
|
2022-11-27 11:00:02 +01:00
|
|
|
DebuggerEvent::Start if source.is_some() => {
|
|
|
|
println!("\x1b[32m! Script '{}' start\x1b[39m", source.unwrap())
|
|
|
|
}
|
2022-04-26 10:36:24 +02:00
|
|
|
DebuggerEvent::Start => println!("\x1b[32m! Script start\x1b[39m"),
|
2022-11-27 11:00:02 +01:00
|
|
|
DebuggerEvent::End if source.is_some() => {
|
|
|
|
println!("\x1b[31m! Script '{}' end\x1b[39m", source.unwrap())
|
|
|
|
}
|
2022-04-26 10:36:24 +02:00
|
|
|
DebuggerEvent::End => println!("\x1b[31m! Script end\x1b[39m"),
|
2022-02-11 15:43:09 +01:00
|
|
|
DebuggerEvent::Step => (),
|
|
|
|
DebuggerEvent::BreakPoint(n) => {
|
2022-11-24 13:35:56 +01:00
|
|
|
match context.global_runtime_state().debugger().break_points()[n] {
|
2022-02-11 15:43:09 +01:00
|
|
|
#[cfg(not(feature = "no_position"))]
|
|
|
|
BreakPoint::AtPosition { .. } => (),
|
|
|
|
BreakPoint::AtFunctionName { ref name, .. }
|
|
|
|
| BreakPoint::AtFunctionCall { ref name, .. } => {
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("! Call to function {name}.")
|
2022-02-01 15:30:05 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
BreakPoint::AtProperty { ref name, .. } => {
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("! Property {name} accessed.")
|
2022-01-28 11:59:18 +01:00
|
|
|
}
|
2022-04-26 10:36:24 +02:00
|
|
|
_ => unreachable!(),
|
2022-01-26 15:16:45 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
DebuggerEvent::FunctionExitWithValue(r) => {
|
|
|
|
println!(
|
|
|
|
"! Return from function call '{}' => {:?}",
|
|
|
|
context
|
|
|
|
.global_runtime_state()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.call_stack()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.fn_name,
|
|
|
|
r
|
|
|
|
)
|
|
|
|
}
|
|
|
|
DebuggerEvent::FunctionExitWithError(err) => {
|
|
|
|
println!(
|
|
|
|
"! Return from function call '{}' with error: {}",
|
|
|
|
context
|
|
|
|
.global_runtime_state()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.call_stack()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.fn_name,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
2022-04-26 10:36:24 +02:00
|
|
|
_ => unreachable!(),
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print current source line
|
2022-05-01 18:03:45 +02:00
|
|
|
print_current_source(&mut context, source, pos, lines, (0, 0));
|
2022-01-28 11:59:18 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
// Read stdin for commands
|
|
|
|
let mut input = String::new();
|
2022-02-01 15:30:05 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
loop {
|
2022-04-11 10:29:16 +02:00
|
|
|
print!("dbg> ");
|
2022-01-28 11:59:18 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
stdout().flush().expect("couldn't flush stdout");
|
2022-01-28 11:59:18 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
input.clear();
|
2022-01-28 11:59:18 +01:00
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
match stdin().read_line(&mut input) {
|
|
|
|
Ok(0) => break Ok(DebuggerCommand::Continue),
|
2022-07-27 10:04:24 +02:00
|
|
|
Ok(_) => match input.split_whitespace().collect::<Vec<_>>().as_slice() {
|
2022-02-11 15:43:09 +01:00
|
|
|
["help" | "h"] => print_debug_help(),
|
|
|
|
["exit" | "quit" | "q" | "kill", ..] => {
|
|
|
|
println!("Script terminated. Bye!");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
["node"] => {
|
|
|
|
if pos.is_none() {
|
|
|
|
println!("{:?}", node);
|
|
|
|
} else {
|
2022-10-10 10:46:35 +02:00
|
|
|
match source {
|
2022-10-27 07:38:21 +02:00
|
|
|
Some(source) => println!("{node:?} {source} @ {pos:?}"),
|
|
|
|
None => println!("{node:?} @ {pos:?}"),
|
2022-10-10 10:46:35 +02:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
2022-04-24 10:06:11 +02:00
|
|
|
["operations"] => {
|
|
|
|
println!("{}", context.global_runtime_state().num_operations)
|
|
|
|
}
|
|
|
|
["source"] => {
|
|
|
|
println!("{}", context.global_runtime_state().source().unwrap_or(""))
|
|
|
|
}
|
2022-07-27 10:04:24 +02:00
|
|
|
["list" | "l"] => print_current_source(&mut context, source, pos, lines, (3, 6)),
|
2022-02-11 15:43:09 +01:00
|
|
|
["list" | "l", n] if n.parse::<usize>().is_ok() => {
|
|
|
|
let num = n.parse::<usize>().unwrap();
|
2022-07-27 10:04:24 +02:00
|
|
|
if num == 0 || num > lines.len() {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid line: {num}\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
|
|
|
let pos = Position::new(num as u16, 0);
|
2022-07-27 10:04:24 +02:00
|
|
|
print_current_source(&mut context, source, pos, lines, (3, 6));
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
["continue" | "c"] => break Ok(DebuggerCommand::Continue),
|
|
|
|
["finish" | "f"] => break Ok(DebuggerCommand::FunctionExit),
|
|
|
|
[] | ["step" | "s"] => break Ok(DebuggerCommand::StepInto),
|
|
|
|
["over" | "o"] => break Ok(DebuggerCommand::StepOver),
|
|
|
|
["next" | "n"] => break Ok(DebuggerCommand::Next),
|
2022-04-23 05:53:34 +02:00
|
|
|
["scope"] => println!("{}", context.scope()),
|
2022-10-10 10:46:35 +02:00
|
|
|
["print" | "p", "this"] => match context.this_ptr() {
|
2022-10-27 07:38:21 +02:00
|
|
|
Some(value) => println!("=> {value:?}"),
|
2022-10-10 10:46:35 +02:00
|
|
|
None => println!("`this` pointer is unbound."),
|
|
|
|
},
|
|
|
|
["print" | "p", var_name] => match context.scope().get_value::<Dynamic>(var_name) {
|
2022-10-27 07:38:21 +02:00
|
|
|
Some(value) => println!("=> {value:?}"),
|
|
|
|
None => eprintln!("Variable not found: {var_name}"),
|
2022-10-10 10:46:35 +02:00
|
|
|
},
|
2022-02-11 15:43:09 +01:00
|
|
|
["print" | "p"] => {
|
2022-04-23 05:53:34 +02:00
|
|
|
println!("{}", context.scope().clone_visible());
|
2022-02-11 15:43:09 +01:00
|
|
|
if let Some(value) = context.this_ptr() {
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("this = {value:?}");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
["imports"] => {
|
|
|
|
for (i, (name, module)) in context
|
|
|
|
.global_runtime_state()
|
|
|
|
.scan_imports_raw()
|
|
|
|
.enumerate()
|
2022-01-28 11:59:18 +01:00
|
|
|
{
|
2022-02-11 15:43:09 +01:00
|
|
|
println!(
|
|
|
|
"[{}] {} = {}",
|
|
|
|
i + 1,
|
|
|
|
name,
|
|
|
|
module.id().unwrap_or("<unknown>")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
["backtrace" | "bt"] => {
|
|
|
|
for frame in context
|
|
|
|
.global_runtime_state()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.call_stack()
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
{
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("{frame}")
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
["info" | "i", "break" | "b"] => Iterator::for_each(
|
|
|
|
context
|
|
|
|
.global_runtime_state()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points()
|
|
|
|
.iter()
|
|
|
|
.enumerate(),
|
|
|
|
|(i, bp)| match bp {
|
|
|
|
#[cfg(not(feature = "no_position"))]
|
|
|
|
rhai::debugger::BreakPoint::AtPosition { pos, .. } => {
|
|
|
|
let line_num = format!("[{}] line ", i + 1);
|
|
|
|
print!("{}", line_num);
|
2022-07-27 10:04:24 +02:00
|
|
|
print_source(lines, *pos, line_num.len(), (0, 0));
|
2022-01-25 10:29:34 +01:00
|
|
|
}
|
2022-10-27 07:38:21 +02:00
|
|
|
_ => println!("[{}] {bp}", i + 1),
|
2022-02-11 15:43:09 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
["enable" | "en", n] => {
|
|
|
|
if let Ok(n) = n.parse::<usize>() {
|
|
|
|
let range = 1..=context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points()
|
|
|
|
.len();
|
|
|
|
if range.contains(&n) {
|
2022-01-28 11:59:18 +01:00
|
|
|
context
|
2022-01-25 05:24:30 +01:00
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-01-28 11:59:18 +01:00
|
|
|
.break_points_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.get_mut(n - 1)
|
|
|
|
.unwrap()
|
|
|
|
.enable(true);
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point #{n} enabled.")
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
|
2022-01-28 11:59:18 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
["disable" | "dis", n] => {
|
|
|
|
if let Ok(n) = n.parse::<usize>() {
|
|
|
|
let range = 1..=context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points()
|
|
|
|
.len();
|
|
|
|
if range.contains(&n) {
|
2022-01-25 05:24:30 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-01-25 05:24:30 +01:00
|
|
|
.break_points_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.get_mut(n - 1)
|
|
|
|
.unwrap()
|
|
|
|
.enable(false);
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point #{n} disabled.")
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
|
2022-01-25 05:24:30 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
["delete" | "d", n] => {
|
|
|
|
if let Ok(n) = n.parse::<usize>() {
|
|
|
|
let range = 1..=context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points()
|
|
|
|
.len();
|
|
|
|
if range.contains(&n) {
|
2022-01-25 11:21:05 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-01-25 11:21:05 +01:00
|
|
|
.break_points_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.remove(n - 1);
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point #{n} deleted.")
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
|
2022-02-08 12:02:40 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
2022-01-28 11:59:18 +01:00
|
|
|
}
|
2022-02-11 15:43:09 +01:00
|
|
|
["delete" | "d"] => {
|
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.clear();
|
|
|
|
println!("All break-points deleted.");
|
|
|
|
}
|
|
|
|
["break" | "b", fn_name, args] => {
|
|
|
|
if let Ok(args) = args.parse::<usize>() {
|
|
|
|
let bp = rhai::debugger::BreakPoint::AtFunctionCall {
|
|
|
|
name: fn_name.trim().into(),
|
|
|
|
args,
|
|
|
|
enabled: true,
|
|
|
|
};
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point added for {bp}");
|
2022-02-11 15:43:09 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.push(bp);
|
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid number of arguments: '{args}'\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Property name
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
["break" | "b", param] if param.starts_with('.') && param.len() > 1 => {
|
|
|
|
let bp = rhai::debugger::BreakPoint::AtProperty {
|
|
|
|
name: param[1..].into(),
|
|
|
|
enabled: true,
|
|
|
|
};
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point added for {bp}");
|
2022-02-11 15:43:09 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.push(bp);
|
|
|
|
}
|
|
|
|
// Numeric parameter
|
|
|
|
#[cfg(not(feature = "no_position"))]
|
|
|
|
["break" | "b", param] if param.parse::<usize>().is_ok() => {
|
|
|
|
let n = param.parse::<usize>().unwrap();
|
|
|
|
let range = if source.is_none() {
|
|
|
|
1..=lines.len()
|
|
|
|
} else {
|
|
|
|
1..=(u16::MAX as usize)
|
|
|
|
};
|
|
|
|
|
|
|
|
if range.contains(&n) {
|
|
|
|
let bp = rhai::debugger::BreakPoint::AtPosition {
|
2022-10-29 08:59:20 +02:00
|
|
|
source: source.map(|s| s.into()),
|
2022-02-11 15:43:09 +01:00
|
|
|
pos: Position::new(n as u16, 0),
|
|
|
|
enabled: true,
|
|
|
|
};
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point added {bp}");
|
2022-02-11 15:43:09 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.push(bp);
|
|
|
|
} else {
|
2022-10-27 07:38:21 +02:00
|
|
|
eprintln!("\x1b[31mInvalid line number: '{n}'\x1b[39m");
|
2022-02-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Function name parameter
|
|
|
|
["break" | "b", param] => {
|
|
|
|
let bp = rhai::debugger::BreakPoint::AtFunctionName {
|
|
|
|
name: param.trim().into(),
|
|
|
|
enabled: true,
|
|
|
|
};
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point added for {bp}");
|
2022-02-11 15:43:09 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.push(bp);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_position"))]
|
|
|
|
["break" | "b"] => {
|
|
|
|
let bp = rhai::debugger::BreakPoint::AtPosition {
|
2022-10-29 08:59:20 +02:00
|
|
|
source: source.map(|s| s.into()),
|
2022-02-11 15:43:09 +01:00
|
|
|
pos,
|
|
|
|
enabled: true,
|
|
|
|
};
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Break-point added {bp}");
|
2022-02-11 15:43:09 +01:00
|
|
|
context
|
|
|
|
.global_runtime_state_mut()
|
2022-11-24 13:35:56 +01:00
|
|
|
.debugger_mut()
|
2022-02-11 15:43:09 +01:00
|
|
|
.break_points_mut()
|
|
|
|
.push(bp);
|
|
|
|
}
|
|
|
|
["throw"] => break Err(EvalAltResult::ErrorRuntime(Dynamic::UNIT, pos).into()),
|
|
|
|
["throw", num] if num.trim().parse::<INT>().is_ok() => {
|
|
|
|
let value = num.trim().parse::<INT>().unwrap().into();
|
|
|
|
break Err(EvalAltResult::ErrorRuntime(value, pos).into());
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
["throw", num] if num.trim().parse::<rhai::FLOAT>().is_ok() => {
|
|
|
|
let value = num.trim().parse::<rhai::FLOAT>().unwrap().into();
|
|
|
|
break Err(EvalAltResult::ErrorRuntime(value, pos).into());
|
|
|
|
}
|
|
|
|
["throw", ..] => {
|
2022-07-27 10:04:24 +02:00
|
|
|
let msg = input.trim().split_once(' ').map(|(_, x)| x).unwrap_or("");
|
2022-02-11 15:43:09 +01:00
|
|
|
break Err(EvalAltResult::ErrorRuntime(msg.trim().into(), pos).into());
|
|
|
|
}
|
|
|
|
["run" | "r"] => {
|
2022-11-27 11:00:02 +01:00
|
|
|
println!("Terminating current run...");
|
2022-02-11 15:43:09 +01:00
|
|
|
break Err(EvalAltResult::ErrorTerminated(Dynamic::UNIT, pos).into());
|
|
|
|
}
|
|
|
|
_ => eprintln!(
|
|
|
|
"\x1b[31mInvalid debugger command: '{}'\x1b[39m",
|
|
|
|
input.trim()
|
|
|
|
),
|
|
|
|
},
|
|
|
|
Err(err) => panic!("input error: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let title = format!("Rhai Debugger (version {})", env!("CARGO_PKG_VERSION"));
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("{title}");
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("{0:=<1$}", "", title.len());
|
|
|
|
|
|
|
|
// Initialize scripting engine
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
|
|
|
engine.set_optimization_level(rhai::OptimizationLevel::None);
|
|
|
|
|
|
|
|
let (ast, script) = load_script(&engine);
|
|
|
|
|
|
|
|
// Hook up debugger
|
|
|
|
let lines: Vec<_> = script.trim().split('\n').map(|s| s.to_string()).collect();
|
|
|
|
|
2022-02-15 12:56:42 +01:00
|
|
|
#[allow(deprecated)]
|
2022-02-11 15:43:09 +01:00
|
|
|
engine.register_debugger(
|
|
|
|
// Store the current source in the debugger state
|
2022-11-25 06:20:03 +01:00
|
|
|
|engine, mut debugger| {
|
|
|
|
debugger.set_state(engine.const_empty_string());
|
|
|
|
debugger
|
|
|
|
},
|
2022-02-11 15:43:09 +01:00
|
|
|
// Main debugging interface
|
|
|
|
move |context, event, node, source, pos| {
|
|
|
|
debug_callback(context, event, node, source, pos, &lines)
|
2022-01-28 11:59:18 +01:00
|
|
|
},
|
|
|
|
);
|
2022-01-24 10:04:40 +01:00
|
|
|
|
|
|
|
// Set a file module resolver without caching
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
|
|
|
{
|
|
|
|
let mut resolver = rhai::module_resolvers::FileModuleResolver::new();
|
|
|
|
resolver.enable_cache(false);
|
|
|
|
engine.set_module_resolver(resolver);
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:43:09 +01:00
|
|
|
println!("Type 'help' for commands list.");
|
|
|
|
println!();
|
2022-01-24 10:04:40 +01:00
|
|
|
|
|
|
|
// Evaluate
|
2022-02-11 15:43:09 +01:00
|
|
|
while let Err(err) = engine.run_ast_with_scope(&mut Scope::new(), &ast) {
|
2022-01-25 14:31:35 +01:00
|
|
|
match *err {
|
|
|
|
// Loop back to restart
|
2022-11-27 11:00:02 +01:00
|
|
|
EvalAltResult::ErrorTerminated(..) => {
|
|
|
|
println!("Restarting script...");
|
|
|
|
}
|
2022-01-25 14:31:35 +01:00
|
|
|
// Break evaluation
|
|
|
|
_ => {
|
|
|
|
print_error(&script, *err);
|
2022-11-27 11:00:02 +01:00
|
|
|
println!();
|
2022-01-25 14:31:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 10:04:40 +01:00
|
|
|
}
|
2022-04-26 10:36:24 +02:00
|
|
|
|
|
|
|
println!("Script terminated. Bye!");
|
2022-01-24 10:04:40 +01:00
|
|
|
}
|