aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/script/dom/bindings/codegen/CodegenRust.py38
-rw-r--r--components/script/dom/bindings/finalize.rs52
-rw-r--r--components/script/dom/bindings/mod.rs1
3 files changed, 60 insertions, 31 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py
index 9a8dee594c8..1457eed4318 100644
--- a/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/components/script/dom/bindings/codegen/CodegenRust.py
@@ -6101,38 +6101,12 @@ let this = native_from_object_static::<%s>(obj).unwrap();
def finalizeHook(descriptor, hookName, context):
- release = ""
if descriptor.isGlobal():
- release += """\
-finalize_global(obj);
-"""
+ release = "finalize_global(obj, this);"
elif descriptor.weakReferenceable:
- release += """\
-let mut slot = UndefinedValue();
-JS_GetReservedSlot(obj, DOM_WEAK_SLOT, &mut slot);
-let weak_box_ptr = slot.to_private() as *mut WeakBox<%s>;
-if !weak_box_ptr.is_null() {
- let count = {
- let weak_box = &*weak_box_ptr;
- assert!(weak_box.value.get().is_some());
- assert!(weak_box.count.get() > 0);
- weak_box.value.set(None);
- let count = weak_box.count.get() - 1;
- weak_box.count.set(count);
- count
- };
- if count == 0 {
- mem::drop(Box::from_raw(weak_box_ptr));
- }
-}
-""" % descriptor.concreteType
- release += """\
-if !this.is_null() {
- // The pointer can be null if the object is the unforgeable holder of that interface.
- let _ = Box::from_raw(this as *mut %s);
-}
-debug!("%s finalize: {:p}", this);\
-""" % (descriptor.concreteType, descriptor.concreteType)
+ release = "finalize_weak_referenceable(obj, this);"
+ else:
+ release = "finalize_common(this);"
return release
@@ -6569,7 +6543,9 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'crate::dom::bindings::utils::ProtoOrIfaceArray',
'crate::dom::bindings::utils::callargs_is_constructing',
'crate::dom::bindings::utils::enumerate_global',
- 'crate::dom::bindings::utils::finalize_global',
+ 'crate::dom::bindings::finalize::finalize_common',
+ 'crate::dom::bindings::finalize::finalize_global',
+ 'crate::dom::bindings::finalize::finalize_weak_referenceable',
'crate::dom::bindings::utils::generic_getter',
'crate::dom::bindings::utils::generic_lenient_getter',
'crate::dom::bindings::utils::generic_lenient_setter',
diff --git a/components/script/dom/bindings/finalize.rs b/components/script/dom/bindings/finalize.rs
new file mode 100644
index 00000000000..9ef4665118a
--- /dev/null
+++ b/components/script/dom/bindings/finalize.rs
@@ -0,0 +1,52 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use std::any::type_name;
+use std::mem;
+
+use js::glue::JS_GetReservedSlot;
+use js::jsapi::JSObject;
+use js::jsval::UndefinedValue;
+
+use crate::dom::bindings::utils::finalize_global as do_finalize_global;
+use crate::dom::bindings::weakref::{WeakBox, WeakReferenceable, DOM_WEAK_SLOT};
+
+/// Generic finalizer implementations for DOM binding implementations.
+
+pub unsafe fn finalize_common<T>(this: *const T) {
+ if !this.is_null() {
+ // The pointer can be null if the object is the unforgeable holder of that interface.
+ let _ = Box::from_raw(this as *mut T);
+ }
+ debug!("{} finalize: {:p}", type_name::<T>(), this);
+}
+
+pub unsafe fn finalize_global<T>(obj: *mut JSObject, this: *const T) {
+ do_finalize_global(obj);
+ finalize_common::<T>(this);
+}
+
+pub unsafe fn finalize_weak_referenceable<T: WeakReferenceable>(
+ obj: *mut JSObject,
+ this: *const T,
+) {
+ let mut slot = UndefinedValue();
+ JS_GetReservedSlot(obj, DOM_WEAK_SLOT, &mut slot);
+ let weak_box_ptr = slot.to_private() as *mut WeakBox<T>;
+ if !weak_box_ptr.is_null() {
+ let count = {
+ let weak_box = &*weak_box_ptr;
+ assert!(weak_box.value.get().is_some());
+ assert!(weak_box.count.get() > 0);
+ weak_box.value.set(None);
+ let count = weak_box.count.get() - 1;
+ weak_box.count.set(count);
+ count
+ };
+ if count == 0 {
+ mem::drop(Box::from_raw(weak_box_ptr));
+ }
+ }
+ finalize_common::<T>(this);
+}
diff --git a/components/script/dom/bindings/mod.rs b/components/script/dom/bindings/mod.rs
index 9733cabc73e..82608367aef 100644
--- a/components/script/dom/bindings/mod.rs
+++ b/components/script/dom/bindings/mod.rs
@@ -140,6 +140,7 @@ pub mod cell;
pub mod constant;
pub mod conversions;
pub mod error;
+pub mod finalize;
pub mod guard;
pub mod htmlconstructor;
pub mod inheritance;