Add AsHandleValue trait to Heap<Value> and make Heap values rooted (#38024)

Encapsulates the unsafe conversion from Heap<Value> to HandleValue<'a>,
and reducing repetitive unsafe code at call.

fix #37258
This commit is contained in:
Taym Haddadi 2025-08-04 18:42:53 +02:00 committed by GitHub
parent 9416251cab
commit 04ec710e60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 204 additions and 146 deletions

View file

@ -24,7 +24,7 @@ use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::root::{AsHandleValue, Dom, DomRoot};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::structuredclone;
use crate::dom::event::Event;
@ -53,18 +53,18 @@ pub(crate) struct History {
impl History {
pub(crate) fn new_inherited(window: &Window) -> History {
let state = Heap::default();
state.set(NullValue());
History {
reflector_: Reflector::new(),
window: Dom::from_ref(window),
state,
state: Heap::default(),
state_id: Cell::new(None),
}
}
pub(crate) fn new(window: &Window, can_gc: CanGc) -> DomRoot<History> {
reflect_dom_object(Box::new(History::new_inherited(window)), window, can_gc)
let dom_root = reflect_dom_object(Box::new(History::new_inherited(window)), window, can_gc);
dom_root.state.set(NullValue());
dom_root
}
}
@ -84,7 +84,6 @@ impl History {
/// <https://html.spec.whatwg.org/multipage/#history-traversal>
/// Steps 5-16
#[allow(unsafe_code)]
pub(crate) fn activate_state(
&self,
state_id: Option<HistoryStateId>,
@ -145,7 +144,7 @@ impl History {
PopStateEvent::dispatch_jsval(
self.window.upcast::<EventTarget>(),
&self.window,
unsafe { HandleValue::from_raw(self.state.handle()) },
self.state.as_handle_value(),
can_gc,
);
}