Skip to content

Truly independent web browser

LibWeb: Move CppCell from the Rust streams to the bindings

Shannon Booth99031a378369
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/cpp_cell.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/cpp_cell.rs
new file mode 100644
index 00000000000..a32c253d0e1
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/cpp_cell.rs
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use libjs_rust::api::{ForeignCellSlot, Trace};
+
+/// A cell of LibWeb's C++ code, which this keeps alive.
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CppCell(ForeignCellSlot);
+
+impl Clone for CppCell {
+ fn clone(&self) -> Self {
+ // SAFETY: The slot keeps the cell alive.
+ unsafe { Self::from_raw(self.0.as_ptr()) }
+ }
+}
+
+impl CppCell {
+ /// # Safety
+ ///
+ /// `cell` must be a live cell of LibWeb's C++ code.
+ pub(crate) unsafe fn from_raw(cell: *mut c_void) -> Self {
+ let slot = ForeignCellSlot::empty();
+ // SAFETY: The caller passes a live cell.
+ unsafe { slot.set(Some(NonNull::new(cell).expect("C++ passes a cell"))) };
+ Self(slot)
+ }
+
+ pub(crate) fn as_ptr(&self) -> *mut c_void {
+ self.0.as_ptr()
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
index 217e2e4b9f8..097b352b006 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
@@ -21,6 +21,7 @@
pub(crate) mod async_iterable;
pub(crate) mod callback;
pub(crate) mod conversions;
+pub(crate) mod cpp_cell;
#[cfg(web_idl_bindings)]
pub(crate) mod generated;
pub(crate) mod interface;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs
index ce1f486f668..41bb9320e29 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs
@@ -10,8 +10,9 @@
use core::ffi::c_void;
use core::ptr::NonNull;
-use libjs_rust::api::{ForeignCellSlot, Gc, Trace, Value};
+use libjs_rust::api::{Gc, Trace, Value};
+use crate::bindings::cpp_cell::CppCell;
use crate::bindings::promise::{WebIdlPromise, promise_from_abi};
unsafe extern "C" {
@@ -37,34 +38,6 @@ unsafe extern "C" {
fn web_streams_call_value_steps(function: *mut c_void, value: u64);
}
-/// A cell of LibWeb's C++ code, which this keeps alive.
-#[derive(Trace)]
-#[gc(crate = libjs_rust)]
-pub(crate) struct CppCell(ForeignCellSlot);
-
-impl Clone for CppCell {
- fn clone(&self) -> Self {
- // SAFETY: The slot keeps the cell alive.
- unsafe { Self::from_raw(self.0.as_ptr()) }
- }
-}
-
-impl CppCell {
- /// # Safety
- ///
- /// `cell` must be a live cell of LibWeb's C++ code.
- pub(crate) unsafe fn from_raw(cell: *mut c_void) -> Self {
- let slot = ForeignCellSlot::empty();
- // SAFETY: The caller passes a live cell.
- unsafe { slot.set(Some(NonNull::new(cell).expect("C++ passes a cell"))) };
- Self(slot)
- }
-
- pub(crate) fn as_ptr(&self) -> *mut c_void {
- self.0.as_ptr()
- }
-}
-
/// A promise-returning algorithm with no arguments that C++ set a stream up with, a pullAlgorithm or a flushAlgorithm.
#[derive(Clone, Trace)]
#[gc(crate = libjs_rust)]
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
index 01b5c57f6ac..dcc5ea44ee2 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
@@ -16,6 +16,7 @@ use libjs_rust::api::{
RawNativeFunctionResult, Realm, Trace, Value, Vm,
};
+use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Ref, allocate};
use crate::bindings::promise::{
NativeSteps, WebIdlPromise, create_promise, create_rejected_promise_from_exception, create_resolved_promise,
@@ -26,7 +27,6 @@ use crate::bindings::{CallContext, Exception, MustNotThrow, WebIdlResult};
use crate::streams::algorithms::{
AbortAlgorithm, CancelAlgorithm, CloseAlgorithm, PullAlgorithm, SizeAlgorithm, StartAlgorithm, WriteAlgorithm,
};
-use crate::streams::cpp::CppCell;
use crate::streams::readable_stream::{ReadableStream, initialize_readable_stream};
use crate::streams::readable_stream_default_controller::{
ReadableStreamDefaultController, readable_stream_default_controller_close,
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs
index 6fb37d67279..28f14287951 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs
@@ -13,12 +13,13 @@ use core::ptr::NonNull;
use libjs_rust::api::{RawNativeFunctionResult, Realm, ThrowCompletionOr, Value, Vm};
use crate::bindings::conversions::BufferSource;
+use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Ref, allocate, cell_from_abi, cell_into_abi, realm_arguments};
use crate::bindings::promise::WebIdlPromise;
use crate::bindings::{CallContext, MustNotThrow, WebIdlResult};
use crate::streams::algorithms::{CancelAlgorithm, FlushAlgorithm, TransformAlgorithm};
use crate::streams::cpp::{
- CppBytesSteps, CppCell, CppPromiseAlgorithm, CppPromiseAlgorithmWithArgument, CppReadRequest, CppValueSteps,
+ CppBytesSteps, CppPromiseAlgorithm, CppPromiseAlgorithmWithArgument, CppReadRequest, CppValueSteps,
};
use crate::streams::readable_byte_stream_controller::readable_byte_stream_controller_enqueue_native_bytes;
use crate::streams::readable_stream::ReadableStreamReader;