From a85e659b29ec105fe25a90d70c0ba0427e5185a9 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 10 May 2016 12:15:59 -0400 Subject: [PATCH] Ensure that the fields of BrowsingContext are traced and the Rust object is freed when the reflector is finalized. --- components/script/dom/browsingcontext.rs | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/components/script/dom/browsingcontext.rs b/components/script/dom/browsingcontext.rs index b314bce603d..f4f15cdf7c7 100644 --- a/components/script/dom/browsingcontext.rs +++ b/components/script/dom/browsingcontext.rs @@ -7,6 +7,7 @@ use dom::bindings::conversions::{ToJSValConvertible, root_from_handleobject}; use dom::bindings::js::{JS, Root, RootedReference}; use dom::bindings::proxyhandler::{fill_property_descriptor, get_property_descriptor}; use dom::bindings::reflector::{Reflectable, Reflector}; +use dom::bindings::trace::JSTraceable; use dom::bindings::utils::WindowProxyHandler; use dom::bindings::utils::get_array_index_from_id; use dom::document::Document; @@ -14,10 +15,10 @@ use dom::element::Element; use dom::window::Window; use js::JSCLASS_IS_GLOBAL; use js::glue::{CreateWrapperProxyHandler, ProxyTraps, NewWindowProxy}; -use js::glue::{GetProxyPrivate, SetProxyExtra}; +use js::glue::{GetProxyPrivate, SetProxyExtra, GetProxyExtra}; use js::jsapi::{Handle, HandleId, HandleObject, HandleValue, JSAutoCompartment, JSAutoRequest}; use js::jsapi::{JSContext, JSPROP_READONLY, JSErrNum, JSObject, PropertyDescriptor, JS_DefinePropertyById}; -use js::jsapi::{JS_ForwardGetPropertyTo, JS_ForwardSetPropertyTo, JS_GetClass}; +use js::jsapi::{JS_ForwardGetPropertyTo, JS_ForwardSetPropertyTo, JS_GetClass, JSTracer, FreeOp}; use js::jsapi::{JS_GetOwnPropertyDescriptorById, JS_HasPropertyById, MutableHandle}; use js::jsapi::{MutableHandleValue, ObjectOpResult, RootedObject, RootedValue}; use js::jsval::{UndefinedValue, PrivateValue}; @@ -261,13 +262,31 @@ static PROXY_HANDLER: ProxyTraps = ProxyTraps { fun_toString: None, boxedValue_unbox: None, defaultValue: None, - trace: None, - finalize: None, + trace: Some(trace), + finalize: Some(finalize), objectMoved: None, isCallable: None, isConstructor: None, }; +#[allow(unsafe_code)] +unsafe extern fn finalize(_fop: *mut FreeOp, obj: *mut JSObject) { + let this = GetProxyExtra(obj, 0).to_private() as *mut BrowsingContext; + assert!(!this.is_null()); + let _ = Box::from_raw(this); + debug!("BrowsingContext finalize: {:p}", this); +} + +#[allow(unsafe_code)] +unsafe extern fn trace(trc: *mut JSTracer, obj: *mut JSObject) { + let this = GetProxyExtra(obj, 0).to_private() as *const BrowsingContext; + if this.is_null() { + // GC during obj creation + return; + } + (*this).trace(trc); +} + #[allow(unsafe_code)] pub fn new_window_proxy_handler() -> WindowProxyHandler { unsafe {