Truly independent web browser
LibJS+Meta+LibWeb: Count the external memory of Rust interface objects
Andreas Kling36b540a7c5d2
diff --git a/Libraries/LibJS/Rust/src/api.rs b/Libraries/LibJS/Rust/src/api.rsindex 3804eff5fdc..875805d2376 100644--- a/Libraries/LibJS/Rust/src/api.rs+++ b/Libraries/LibJS/Rust/src/api.rs@@ -10,6 +10,7 @@//! runtime's own business.pub use crate::embedding::host::host_object::{HostObject, host_class_of, host_data_of, is_host_instance_of};+pub use crate::gc::class::ExternalMemorySize;pub use crate::gc::embedder_cell::{EmbedderCell, EmbedderClass, EmbedderValue};pub use crate::gc::foreign::ForeignCellSlot;pub use crate::gc::root::{MarkedVec, Root};diff --git a/Libraries/LibJS/Rust/src/gc/embedder_cell.rs b/Libraries/LibJS/Rust/src/gc/embedder_cell.rsindex 2c577a88ab7..2d75d3bb72a 100644--- a/Libraries/LibJS/Rust/src/gc/embedder_cell.rs+++ b/Libraries/LibJS/Rust/src/gc/embedder_cell.rs@@ -17,7 +17,7 @@ use core::mem::{ManuallyDrop, MaybeUninit};use core::ops::Deref;use core::ptr::NonNull;-use super::class::{Class, GcCell, MIN_CELL_SIZE, class_of};+use super::class::{Class, ExternalMemorySize, GcCell, MIN_CELL_SIZE, class_of};use super::class_id::ClassId;use super::visitor::{Trace, Visitor};use crate::interpreter::vm::Vm;@@ -75,9 +75,8 @@ unsafe impl<T: Trace> Trace for EmbedderCell<T> {/// }/// ```///-/// LibGC traces the cells of the class, and destroys those whose value has a destructor. It does not finalize them-/// or count memory they own outside the heap: an embedder type that needs either has to opt in through its class,-/// as the runtime's own cells do through theirs, which no embedder type does yet.+/// LibGC traces the cells of the class, and destroys those whose value has a destructor. It does not finalize them,+/// and counts memory they own outside the heap only for a class that new_with_external_memory_size() creates.pub struct EmbedderClass<T: 'static> {class: Class,_value: PhantomData<fn() -> T>,@@ -102,6 +101,27 @@ impl<T: Trace + 'static> EmbedderClass<T> {}}+impl<T: Trace + ExternalMemorySize + 'static> EmbedderClass<T> {+ /// A class whose cells LibGC asks how much memory their values own outside the heap, which it counts towards the+ /// bytes that make it collect, as it asks a C++ cell that overrides Cell::external_memory_size().+ pub const fn new_with_external_memory_size(name: &'static str) -> Self {+ let mut class = Self::new(name);+ class.class.type_info.external_memory_size = Some(external_memory_size_of_value::<T>);+ class+ }+}++/// The external_memory_size entry of a class that new_with_external_memory_size() creates.+///+/// # Safety+///+/// Only LibGC calls it, with a cell of that class.+unsafe extern "C" fn external_memory_size_of_value<T: ExternalMemorySize>(cell: *const c_void) -> usize {+ // SAFETY: LibGC passes a cell of the class, whose value stays intact for the call.+ let cell = unsafe { &*cell.cast::<EmbedderCell<T>>() };+ (**cell).external_memory_size()+}+/// A type of the embedder whose values live in cells of their own.pub trait EmbedderValue: Trace + Sized + 'static {const CLASS: &'static EmbedderClass<Self>;diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rsindex 37cca5e6047..62a48155334 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs@@ -22,9 +22,10 @@ use core::ptr::NonNull;use ak::Utf16FlyString;use libjs_rust::api::{- Attribute, EmbedderCell, EmbedderClass, EmbedderValue, ErrorKind, ErrorType, FunctionObject, Gc, HostObject,- JSHostClass, Object, PrimitiveString, PropertyAttributes, PropertyKey, RawNativeFunction, RawNativeFunctionPointer,- Realm, Throw, ThrowCompletionOr, Trace, Value, Vm, get_function_realm, host_data_of, is_host_instance_of,+ Attribute, EmbedderCell, EmbedderClass, EmbedderValue, ErrorKind, ErrorType, ExternalMemorySize, FunctionObject,+ Gc, HostObject, JSHostClass, Object, PrimitiveString, PropertyAttributes, PropertyKey, RawNativeFunction,+ RawNativeFunctionPointer, Realm, Throw, ThrowCompletionOr, Trace, Value, Vm, get_function_realm, host_data_of,+ is_host_instance_of,};use crate::bindings::CallContext;@@ -43,6 +44,16 @@ pub(crate) trait Wrappable {fn relevant_realm(&self, _vm: &Vm) -> Option<Gc<Realm>> {None}++ /// Whether the object owns memory outside the heap, which LibGC then asks external_memory_size() for while it+ /// sweeps and counts towards the bytes that make it collect, as Cell::external_memory_size() of a C+++ /// implementation object.+ const OWNS_EXTERNAL_MEMORY: bool = false;++ /// How much memory outside the heap the object owns, which LibGC asks for if OWNS_EXTERNAL_MEMORY is true.+ fn external_memory_size(&self) -> usize {+ 0+ }}/// The value of the cell of an object of an interface: its implementation, and its wrapper in the main world.@@ -93,6 +104,21 @@ impl<T: Interface> EmbedderValue for Wrapped<T> {const CLASS: &'static EmbedderClass<Self> = T::CELL_CLASS;}+impl<T: Wrappable + Trace> ExternalMemorySize for Wrapped<T> {+ fn external_memory_size(&self) -> usize {+ self.implementation.external_memory_size()+ }+}++/// The class of the cells of the objects of an interface, which the generated bindings of the interface define once.+pub(crate) const fn cell_class<T: Wrappable + Trace + 'static>(name: &'static str) -> EmbedderClass<Wrapped<T>> {+ if T::OWNS_EXTERNAL_MEMORY {+ EmbedderClass::new_with_external_memory_size(name)+ } else {+ EmbedderClass::new(name)+ }+}+/// Without the generated bindings, which a build of the crate on its own leaves out, nothing creates or wraps an/// object, but the implementations still name each other's objects. This stands in for the bindings of every/// interface so that they compile.diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 6ced0006169..09660511abf 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -403,7 +403,7 @@ def write_rust_interface(to_unsigned_long_long, to_usv_string,}};use crate::bindings::interface::{{- Inherits, Interface, Ref, Wrapped, construct_arguments, construct_wrapper, define_attribute,+ Inherits, Interface, Ref, Wrapped, cell_class, construct_arguments, construct_wrapper, define_attribute,define_cached_attribute, define_constants, define_interface_object_properties, define_operation,define_to_string_tag, define_unimplemented_property, entry_point_arguments, implementation_if,implementation_of, prototype_from_new_target, rejected_promise, set_interface_prototype_object_prototype,@@ -418,7 +418,7 @@ def write_rust_interface(pub(crate) const INTERFACE_NAME: u16 = {interface_name_value};/// The class of the cells of the objects of the interface.- static CELL_CLASS: EmbedderClass<Wrapped<Implementation>> = EmbedderClass::new("{interface.name}");+ static CELL_CLASS: EmbedderClass<Wrapped<Implementation>> = cell_class("{interface.name}");unsafe extern "C" {{static {wrapper_host_class_symbol(interface)}: JSHostClass;