Truly independent web browser
Utilities/js: Add exception pause controls
Sam Atkins3853514eac30
diff --git a/Libraries/LibJS/Rust/src/utilities/js.rs b/Libraries/LibJS/Rust/src/utilities/js.rsindex fc36a7749a6..d9738d89ab1 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, PauseReason, ResumeMode};+use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode};use crate::gc::class::{GcCell, cast_ref, define_cell};use crate::gc::root::Root;use crate::hash_table::HashTable;@@ -74,6 +74,7 @@ struct Options {disable_syntax_highlight: bool,disable_debug_printing: bool,debug: bool,+ pause_on_exceptions: Option<String>,evaluate_script: String,use_test262_global: bool,expose_jit_testing: bool,@@ -96,6 +97,7 @@ enum OptionTarget {DisableSyntaxHighlight,DisableDebugPrinting,Debug,+ PauseOnExceptions,EvaluateScript,UseTest262Global,ExposeJitTesting,@@ -189,6 +191,15 @@ const OPTIONS: &[OptionDescription] = &[OptionTarget::DisableDebugPrinting,),option("Run with the JavaScript debugger", "debug", None, OptionTarget::Debug),+ OptionDescription {+ value_name: Some("none|unhandled|all"),+ ..option(+ "Pause on JavaScript exceptions while debugging",+ "pause-on-exceptions",+ None,+ OptionTarget::PauseOnExceptions,+ )+ },OptionDescription {value_name: Some("script"),..option(@@ -237,6 +248,7 @@ impl OptionDescription {OptionTarget::DisableSyntaxHighlight => options.disable_syntax_highlight = true,OptionTarget::DisableDebugPrinting => options.disable_debug_printing = true,OptionTarget::Debug => options.debug = true,+ OptionTarget::PauseOnExceptions => options.pause_on_exceptions = value.map(str::to_owned),OptionTarget::EvaluateScript => options.evaluate_script = value.unwrap_or_default().to_string(),OptionTarget::UseTest262Global => options.use_test262_global = true,OptionTarget::ExposeJitTesting => options.expose_jit_testing = true,@@ -379,6 +391,12 @@ fn parse_arguments(arguments: &[String], output: &mut dyn Write) -> Result<Optiolet _ = print_usage(output, argv0);return Err(0);}+ if let Some(value) = &options.pause_on_exceptions+ && parse_pause_on_exceptions(value.as_bytes()).is_none()+ {+ eprintln!("Invalid --pause-on-exceptions value '{value}'. Expected none, unhandled, or all.");+ return Err(1);+ }Ok(options)}@@ -724,6 +742,23 @@ fn print_breakpoint(debugger: &Debugger, breakpoint: &Breakpoint) {standard_output::outln(&line);}+fn parse_pause_on_exceptions(value: &[u8]) -> Option<PauseOnExceptions> {+ match value {+ b"none" => Some(PauseOnExceptions::None),+ b"unhandled" => Some(PauseOnExceptions::Uncaught),+ b"all" => Some(PauseOnExceptions::All),+ _ => None,+ }+}++fn pause_on_exceptions_name(mode: PauseOnExceptions) -> &'static [u8] {+ match mode {+ PauseOnExceptions::None => b"none",+ PauseOnExceptions::Uncaught => b"unhandled",+ PauseOnExceptions::All => b"all",+ }+}+fn print_debugger_help() {standard_output::outln(b"Debugger commands:");standard_output::outln(b" break (b) [file:]<line>[:column]");@@ -731,6 +766,8 @@ 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" set pause-on-exceptions <none|unhandled|all>");+ standard_output::outln(b" show pause-on-exceptions");}/// warnln() of bytes that need not be UTF-8.@@ -780,7 +817,12 @@ impl<'a> DebuggerCommandLexer<'a> {}}-fn run_debugger_prompt(vm: &Vm, pause_info: &PauseInfo, line_editor: Option<&JSLineEditor>) {+fn run_debugger_prompt(+ vm: &Vm,+ pause_info: &PauseInfo,+ line_editor: Option<&JSLineEditor>,+ pause_on_exceptions: &Cell<PauseOnExceptions>,+) {let debugger = vm.debugger().expect("execution pauses in the attached debugger");let reason = debugger_pause_reason(pause_info.reason);if let Some(range) = &pause_info.source_range {@@ -839,6 +881,31 @@ fn run_debugger_prompt(vm: &Vm, pause_info: &PauseInfo, line_editor: Option<&JSLcontinue;}+ if command_name == b"set" {+ let setting = command.consume_command_name();+ command.skip_whitespace();+ let mode = parse_pause_on_exceptions(trim_whitespace(command.consume_remaining()));+ if setting != b"pause-on-exceptions" || mode.is_none() {+ eprintln!("Usage: set pause-on-exceptions <none|unhandled|all>");+ continue;+ }+ let mode = mode.expect("mode was checked");+ pause_on_exceptions.set(mode);+ debugger.set_pause_on_exceptions(mode);+ continue;+ }++ if command_name == b"show" {+ if trim_whitespace(command.consume_remaining()) != b"pause-on-exceptions" {+ eprintln!("Usage: show pause-on-exceptions");+ continue;+ }+ let mut line = b"pause-on-exceptions: ".to_vec();+ line.extend_from_slice(pause_on_exceptions_name(pause_on_exceptions.get()));+ standard_output::outln(&line);+ continue;+ }+if command_name == b"break" || command_name == b"b" {let current_filename = pause_info.source_range@@ -1873,10 +1940,18 @@ fn ladybird_main(arguments: &[String], line_editor: Option<JSLineEditor>) -> c_ivm.set_dynamic_imports_allowed(true);if options.debug {+ let pause_on_exceptions = Cell::new(+ options+ .pause_on_exceptions+ .as_deref()+ .and_then(|value| parse_pause_on_exceptions(value.as_bytes()))+ .unwrap_or(PauseOnExceptions::None),+ );vm.enable_debugging();let debugger = vm.debugger().expect("debugging was just enabled");+ debugger.set_pause_on_exceptions(pause_on_exceptions.get());debugger.set_pause_callback(move |vm, pause_info| {- run_debugger_prompt(vm, pause_info, line_editor.as_ref());+ run_debugger_prompt(vm, pause_info, line_editor.as_ref(), &pause_on_exceptions);});debugger.request_pause_on_next_bytecode_execution();}diff --git a/Tests/LibJS/Debugger/expected/basic.txt b/Tests/LibJS/Debugger/expected/basic.txtindex f8bb4114793..5e97b539706 100644--- a/Tests/LibJS/Debugger/expected/basic.txt+++ b/Tests/LibJS/Debugger/expected/basic.txt@@ -5,6 +5,11 @@ Debugger commands:continue (c)delete <id>help+ set pause-on-exceptions <none|unhandled|all>+ show pause-on-exceptions+pause-on-exceptions: none+pause-on-exceptions: unhandled+pause-on-exceptions: allNo breakpoints.1: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)1: <ladybird>/Tests/LibJS/Debugger/input/basic.js:4 (resolved)@@ -12,3 +17,5 @@ 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)Deleted breakpoint 2.+Paused at <ladybird>/Tests/LibJS/Debugger/input/basic.js:10:5 (exception)+pause-on-exceptions: nonediff --git a/Tests/LibJS/Debugger/expected/pause-on-exceptions.txt b/Tests/LibJS/Debugger/expected/pause-on-exceptions.txtnew file mode 100644index 00000000000..a6bb0792cd4--- /dev/null+++ b/Tests/LibJS/Debugger/expected/pause-on-exceptions.txt@@ -0,0 +1,2 @@+Paused at <ladybird>/Tests/LibJS/Debugger/input/pause-on-exceptions.js:7:1 (entry)+Paused at <ladybird>/Tests/LibJS/Debugger/input/pause-on-exceptions.js:10:5 (exception)diff --git a/Tests/LibJS/Debugger/input/basic.commands b/Tests/LibJS/Debugger/input/basic.commandsindex 04c6e18f251..e187fb92d77 100644--- a/Tests/LibJS/Debugger/input/basic.commands+++ b/Tests/LibJS/Debugger/input/basic.commands@@ -1,4 +1,9 @@help+show pause-on-exceptions+set pause-on-exceptions unhandled+show pause-on-exceptions+set pause-on-exceptions all+show pause-on-exceptionsbreakpointsbreak 4breakpoints@@ -7,3 +12,6 @@ b 4cdelete 2continue+set pause-on-exceptions none+show pause-on-exceptions+continuediff --git a/Tests/LibJS/Debugger/input/basic.js b/Tests/LibJS/Debugger/input/basic.jsindex 5f5f90411c4..06d5aa44f86 100644--- a/Tests/LibJS/Debugger/input/basic.js+++ b/Tests/LibJS/Debugger/input/basic.js@@ -5,3 +5,7 @@ function calculate() {}calculate();++try {+ throw new Error("handled");+} catch {}diff --git a/Tests/LibJS/Debugger/input/pause-on-exceptions.args b/Tests/LibJS/Debugger/input/pause-on-exceptions.argsnew file mode 100644index 00000000000..c93e1d703fc--- /dev/null+++ b/Tests/LibJS/Debugger/input/pause-on-exceptions.args@@ -0,0 +1 @@+--pause-on-exceptions alldiff --git a/Tests/LibJS/Debugger/input/pause-on-exceptions.commands b/Tests/LibJS/Debugger/input/pause-on-exceptions.commandsnew file mode 100644index 00000000000..153508010ce--- /dev/null+++ b/Tests/LibJS/Debugger/input/pause-on-exceptions.commands@@ -0,0 +1,2 @@+continue+continuediff --git a/Tests/LibJS/Debugger/input/pause-on-exceptions.js b/Tests/LibJS/Debugger/input/pause-on-exceptions.jsnew file mode 100644index 00000000000..06d5aa44f86--- /dev/null+++ b/Tests/LibJS/Debugger/input/pause-on-exceptions.js@@ -0,0 +1,11 @@+function calculate() {+ let first = 20;+ let second = 22;+ return first + second;+}++calculate();++try {+ throw new Error("handled");+} catch {}