Skip to content

Truly independent web browser

LibJS: Lend the execution contexts of a paused stack trace

Shannon Booth32946751e7de
diff --git a/Libraries/LibJS/Rust/src/debugger.rs b/Libraries/LibJS/Rust/src/debugger.rs
index a5443c4bada..9bb6a1a0fa2 100644
--- a/Libraries/LibJS/Rust/src/debugger.rs
+++ b/Libraries/LibJS/Rust/src/debugger.rs
@@ -8,6 +8,7 @@
//! the paused frames and evaluate code in them.
use core::cell::{Cell, RefCell};
+use core::marker::PhantomData;
use core::ops::ControlFlow;
use std::collections::HashMap;
use std::rc::Rc;
@@ -78,6 +79,16 @@ pub struct PauseInfo<'a> {
pub exception: Option<Value>,
pub exception_will_be_caught: bool,
pub reason: PauseReason,
+ /// Only the debugger makes these, while execution is paused and the frames are on the stack.
+ _frames: PhantomData<&'a ExecutionContext>,
+}
+
+impl<'a> PauseInfo<'a> {
+ pub fn execution_context(&self, index: usize) -> &'a ExecutionContext {
+ // SAFETY: The frames of the stack trace stay on the stack while execution is paused, which it is while the
+ // debugger lends this.
+ unsafe { self.stack_trace[index].execution_context.as_ref() }
+ }
}
#[derive(Clone, Trace)]
@@ -223,6 +234,7 @@ impl Debugger {
exception,
exception_will_be_caught,
reason,
+ _frames: PhantomData,
},
);
assert!(!self.is_paused.get(), "the pause callback continues execution");
diff --git a/Libraries/LibJS/Rust/src/utilities/js.rs b/Libraries/LibJS/Rust/src/utilities/js.rs
index 8e5d559347e..c1c97a31e52 100644
--- a/Libraries/LibJS/Rust/src/utilities/js.rs
+++ b/Libraries/LibJS/Rust/src/utilities/js.rs
@@ -772,7 +772,7 @@ fn pause_on_exceptions_name(mode: PauseOnExceptions) -> &'static [u8] {
fn print_backtrace(pause_info: &PauseInfo) {
for (index, frame) in pause_info.stack_trace.iter().enumerate() {
- let context = unsafe { frame.execution_context.as_ref() };
+ let context = pause_info.execution_context(index);
let executable = context
.executable
.get()
@@ -865,7 +865,7 @@ fn run_debugger_prompt(
) {
let debugger = vm.debugger().expect("execution pauses in the attached debugger");
if pause_info.reason == PauseReason::Breakpoint {
- let context = unsafe { pause_info.stack_trace[0].execution_context.as_ref() };
+ let context = pause_info.execution_context(0);
let conditions = breakpoint_conditions.borrow();
let mut should_pause = false;
for breakpoint_id in &pause_info.breakpoint_ids {
@@ -978,7 +978,7 @@ fn run_debugger_prompt(
eprintln!("Expression is not valid UTF-8.");
continue;
};
- let context = unsafe { pause_info.stack_trace[0].execution_context.as_ref() };
+ let context = pause_info.execution_context(0);
match debugger.evaluate_in_frame(vm, context, Utf16View::Utf16(&expression), UpdateOriginalBindings::No) {
Ok(value) => {
let _ = print_value(vm, value, PrintTarget::StandardOutput, PrintEnd::Newline);