Skip to content

Truly independent web browser

LibJS: Lend the attached debugger instead of sharing it

Shannon Boothd3590c5a0394
diff --git a/Libraries/LibJS/Rust/src/embedding/debugger.rs b/Libraries/LibJS/Rust/src/embedding/debugger.rs
index f3be1187e9b..b43a148e6eb 100644
--- a/Libraries/LibJS/Rust/src/embedding/debugger.rs
+++ b/Libraries/LibJS/Rust/src/embedding/debugger.rs
@@ -10,9 +10,9 @@
//! js_debugger_is_enabled() needs debugging to be enabled, as C++ reaches the debugger through VM::debugger(), which is
//! null otherwise.
+use core::cell::Ref;
use core::ffi::c_void;
use core::ptr::NonNull;
-use std::rc::Rc;
use crate::breakpoint::BreakpointID;
use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode, UpdateOriginalBindings};
@@ -163,7 +163,7 @@ fn source_range_into_abi(source_range: Option<&SourceRange>) -> JSDebuggerSource
/// # Safety
///
/// `vm` must point to a live VM.
-unsafe fn debugger_of<'a>(vm: *mut JSVM) -> (&'a Vm, Rc<Debugger>) {
+unsafe fn debugger_of<'a>(vm: *mut JSVM) -> (&'a Vm, Ref<'a, Debugger>) {
// SAFETY: The caller passes a live VM.
let vm = unsafe { vm_from_abi(vm) };
(vm, vm.debugger().expect("debugging is enabled"))
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 695d618dc44..1be74e30f94 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -4,14 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use core::cell::{Cell, OnceCell, RefCell};
+use core::cell::{Cell, OnceCell, Ref, RefCell};
use core::ffi::c_void;
use core::ops::ControlFlow;
use core::ptr::NonNull;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
-use std::rc::Rc;
use ak::{Utf16FlyString, Utf16String};
use libjs_runtime_macros::Trace;
@@ -584,7 +583,7 @@ pub struct Vm {
/// their object. The objects are weak: the sweep callback forgets the ones that die.
intrinsic_accessors: RefCell<IntrinsicAccessorMap>,
/// The debugger, which the head points to while it is attached.
- debugger: RefCell<Option<Rc<Debugger>>>,
+ debugger: RefCell<Option<Box<Debugger>>>,
/// The properties defined with Object::define_unimplemented_property. The objects are weak: the sweep callback
/// forgets the ones that die.
unimplemented_properties: RefCell<UnimplementedPropertyMap>,
@@ -1092,9 +1091,8 @@ impl Vm {
if self.debugger.borrow().is_some() {
return;
}
- let debugger = Rc::new(Debugger::new());
- self.head.debugger.set(Rc::as_ptr(&debugger).cast_mut().cast());
- *self.debugger.borrow_mut() = Some(debugger);
+ let debugger: *const Debugger = self.debugger.borrow_mut().insert(Box::new(Debugger::new())).as_ref();
+ self.head.debugger.set(debugger.cast_mut().cast());
self.update_dispatch_tables();
// NB: Debuggers step through bytecode, so nothing enters JIT code while one is attached, not even JIT code
// that runs already, which calls other JIT code only through the entries of executables.
@@ -1106,12 +1104,12 @@ impl Vm {
}
}
+ /// Detaches the debugger, which must not be paused. Nothing may hold the debugger meanwhile.
pub fn disable_debugging(&self) {
- let debugger = self.debugger.borrow_mut().take();
- if let Some(debugger) = &debugger {
- assert!(!debugger.is_paused());
- }
+ let mut debugger = self.debugger.borrow_mut();
+ assert!(!debugger.as_ref().is_some_and(|debugger| debugger.is_paused()));
self.head.debugger.set(core::ptr::null_mut());
+ *debugger = None;
drop(debugger);
self.update_dispatch_tables();
}
@@ -1121,12 +1119,13 @@ impl Vm {
!self.head.debugger.get().is_null()
}
+ /// Lends the attached debugger, which cannot be detached while it is lent.
#[inline]
- pub fn debugger(&self) -> Option<Rc<Debugger>> {
+ pub fn debugger(&self) -> Option<Ref<'_, Debugger>> {
if !self.debugging_enabled() {
return None;
}
- self.debugger.borrow().clone()
+ Ref::filter_map(self.debugger.borrow(), |debugger| debugger.as_deref()).ok()
}
pub fn register_weak_container(&self, weak_container: WeakContainer) {