Skip to content

Truly independent web browser

Utilities/js: Print paused expressions

Sam Atkinscac38d82557d
diff --git a/Libraries/LibJS/Rust/src/utilities/js.rs b/Libraries/LibJS/Rust/src/utilities/js.rs
index 93bd673eaf6..60d8293e2ef 100644
--- a/Libraries/LibJS/Rust/src/utilities/js.rs
+++ b/Libraries/LibJS/Rust/src/utilities/js.rs
@@ -25,7 +25,7 @@ use crate::breakpoint::Breakpoint;
use crate::compile::parse;
use crate::console::{Console, ConsoleClient, ConsoleClientMethods, LogLevel, PrinterArguments};
use crate::contrib::test262::global_object::Test262GlobalObject;
-use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode};
+use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode, UpdateOriginalBindings};
use crate::gc::class::{GcCell, cast_ref, define_cell};
use crate::gc::root::Root;
use crate::hash_table::HashTable;
@@ -766,9 +766,7 @@ fn print_backtrace(pause_info: &PauseInfo) {
.executable
.get()
.map(crate::bytecode::executable::Executable::from_head);
- let function_name = executable
- .as_ref()
- .map(|executable| executable.name());
+ let function_name = executable.as_ref().map(|executable| executable.name());
let mut line = format!("#{index} ").into_bytes();
match function_name {
None => line.extend_from_slice(b"<native>"),
@@ -778,9 +776,7 @@ fn print_backtrace(pause_info: &PauseInfo) {
if let Some(source_range) = &frame.source_range {
line.extend_from_slice(b" at ");
line.extend(Utf16View::of_string(source_range.filename()).to_wtf8());
- line.extend_from_slice(
- format!(":{}:{}", source_range.start.line, source_range.start.column).as_bytes(),
- );
+ line.extend_from_slice(format!(":{}:{}", source_range.start.line, source_range.start.column).as_bytes());
}
standard_output::outln(&line);
}
@@ -794,6 +790,7 @@ fn print_debugger_help() {
standard_output::outln(b" continue (c)");
standard_output::outln(b" delete <id>");
standard_output::outln(b" help");
+ standard_output::outln(b" print (p) <expression>");
standard_output::outln(b" set pause-on-exceptions <none|unhandled|all>");
standard_output::outln(b" show pause-on-exceptions");
}
@@ -914,6 +911,29 @@ fn run_debugger_prompt(
continue;
}
+ if command_name == b"print" || command_name == b"p" {
+ let expression = trim_whitespace(command.consume_remaining());
+ if expression.is_empty() {
+ eprintln!("Usage: print <expression>");
+ continue;
+ }
+ let Some(expression) = utf16_from_wtf8(expression) else {
+ eprintln!("Expression is not valid UTF-8.");
+ continue;
+ };
+ let context = unsafe { pause_info.stack_trace[0].execution_context.as_ref() };
+ match debugger.evaluate_in_frame(vm, context, Utf16View::Utf16(&expression), UpdateOriginalBindings::No) {
+ Ok(value) => {
+ let _ = print_value(vm, value, PrintTarget::StandardOutput, PrintEnd::Newline);
+ }
+ Err(throw) => {
+ eprint!("Evaluation threw: ");
+ let _ = print_value(vm, throw.value(), PrintTarget::StandardError, PrintEnd::Newline);
+ }
+ }
+ continue;
+ }
+
if command_name == b"set" {
let setting = command.consume_command_name();
command.skip_whitespace();
diff --git a/Tests/LibJS/Debugger/expected/basic.txt b/Tests/LibJS/Debugger/expected/basic.txt
index e3e8648eb93..69832b4564f 100644
--- a/Tests/LibJS/Debugger/expected/basic.txt
+++ b/Tests/LibJS/Debugger/expected/basic.txt
@@ -6,6 +6,7 @@ Debugger commands:
continue (c)
delete <id>
help
+ print (p) <expression>
set pause-on-exceptions <none|unhandled|all>
show pause-on-exceptions
pause-on-exceptions: none
@@ -17,6 +18,9 @@ No breakpoints.
Deleted breakpoint 1.
2: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)
Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:4:5 (breakpoint)
+42
+100
+20
#0 calculate at <ladybird>/Tests/LibJS/Debugger/input/basic.js:4:5
#1 <global> at <ladybird>/Tests/LibJS/Debugger/input/basic.js:7:10
#2 <native>
diff --git a/Tests/LibJS/Debugger/input/basic.commands b/Tests/LibJS/Debugger/input/basic.commands
index 876f6752b55..92c7d3940d8 100644
--- a/Tests/LibJS/Debugger/input/basic.commands
+++ b/Tests/LibJS/Debugger/input/basic.commands
@@ -10,6 +10,9 @@ breakpoints
delete 1
b 4
c
+p first + second
+print first = 100
+p first
bt
delete 2
continue