Skip to content

Truly independent web browser

LibWeb: Return sequences of cells from Rust bindings in MarkedVecs

Shannon Boothfc0c7be220e6
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index a7e3b864bfe..cdacc1dd412 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -14,8 +14,8 @@ use std::borrow::Cow;
use ak::{Utf16String, Utf16StringUnits, convert_valid_utf16_to_utf8, utf8_length_from_valid_utf16};
use libjs_rust::api::{
Array, ArrayBuffer, ArrayBufferOrder, ArrayBufferShared, DataView, ErrorKind, ErrorType, FunctionObject, Gc,
- MarkedVec, Must, Object, PrimitiveString, PropertyKey, Root, ThrowCompletionOr, Trace, TypedArrayBase,
- TypedArrayKind, Value, Vm, create_typed_array_on_buffer, get_iterator_from_method_impl, get_view_byte_length,
+ MarkedVec, Must, Object, PrimitiveString, PropertyKey, ThrowCompletionOr, Trace, TypedArrayBase, TypedArrayKind,
+ Value, Vm, create_typed_array_on_buffer, get_iterator_from_method_impl, get_view_byte_length,
is_typed_array_out_of_bounds, is_view_out_of_bounds, iterator_step_value,
make_data_view_with_buffer_witness_record, make_typed_array_with_buffer_witness_record, to_well_formed,
typed_array_byte_length,
@@ -1171,17 +1171,15 @@ pub(crate) fn sequence_to_js<T>(
Ok(Value::from_object(array))
}
-/// sequence_to_js() for a sequence whose elements are cells, such as objects of interfaces. The conversion of an
-/// element can allocate, and a Vec hides the elements after it from the collector, so the sequence is kept alive.
+/// sequence_to_js() for a sequence of cells, which stay rooted while converting an element allocates.
pub(crate) fn rooted_sequence_to_js<T: Trace + Clone + 'static>(
cx: &CallContext,
- sequence: Vec<T>,
+ sequence: MarkedVec<'_, T>,
mut element_to_js: impl FnMut(T) -> ThrowCompletionOr<Value>,
) -> ThrowCompletionOr<Value> {
- let sequence = Root::new(cx.vm(), sequence);
- let array = Array::create(cx.vm(), cx.realm(), sequence.get().len() as u64, None)?;
- for (index, element) in sequence.get().iter().enumerate() {
- let element = element_to_js(element.clone())?;
+ let array = Array::create(cx.vm(), cx.realm(), sequence.len() as u64, None)?;
+ for index in 0..sequence.len() {
+ let element = element_to_js(sequence.get(index).expect("the index is in the sequence"))?;
let index = u32::try_from(index).expect("an array of the length of the sequence was created");
array.create_data_property(cx.vm(), &PropertyKey::from(index), element, None, None)?;
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/readable_stream.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/readable_stream.rs
index 72a9ffdcfe5..e6daa558ef7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/readable_stream.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/readable_stream.rs
@@ -11,8 +11,8 @@ use core::cell::Cell;
use core::ffi::c_void;
use libjs_rust::api::{
- Array, Gc, IteratorHint, IteratorRecord, Object, Realm, Trace, Value, Vm, call, get_iterator, iterator_complete,
- iterator_next, iterator_value,
+ Array, Gc, IteratorHint, IteratorRecord, MarkedVec, Object, Realm, Trace, Value, Vm, call, get_iterator,
+ iterator_complete, iterator_next, iterator_value,
};
use crate::bindings::async_iterable::{AsyncIterable, END_OF_ITERATION};
@@ -410,12 +410,16 @@ impl ReadableStream {
}
// https://streams.spec.whatwg.org/#rs-tee
- pub(crate) fn tee(this: &Ref<Self>, cx: &mut CallContext) -> WebIdlResult<Vec<Ref<Self>>> {
- let cx = &this.context(cx.vm());
+ pub(crate) fn tee<'vm>(this: &Ref<Self>, cx: &mut CallContext<'vm>) -> WebIdlResult<MarkedVec<'vm, Ref<Self>>> {
+ let vm = cx.vm();
+ let cx = &this.context(vm);
// 1. Return ? ReadableStreamTee(this, false).
let (branch1, branch2) = readable_stream_tee(cx, *this, false)?;
- Ok(vec![branch1, branch2])
+ let branches = MarkedVec::with_capacity(vm, 2);
+ branches.push(branch1);
+ branches.push(branch2);
+ Ok(branches)
}
// https://streams.spec.whatwg.org/#ref-for-asynchronous-iterator-initialization-steps
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_base.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_base.rs
index 4126ca43f65..1424101a97b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_base.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_base.rs
@@ -7,7 +7,7 @@
//! https://registry.khronos.org/webgl/specs/latest/1.0/#5.14
use ak::Utf16String;
-use libjs_rust::api::{Gc, Object, Trace};
+use libjs_rust::api::{Gc, MarkedVec, Object, Trace};
use super::buffer_kind::BufferKind;
use super::buffer_source::copy_of_first_numbers;
@@ -823,20 +823,26 @@ impl<V: Version> RenderingContext<V> {
)))
}
- pub(crate) fn get_attached_shaders(
+ pub(crate) fn get_attached_shaders<'vm>(
&self,
- _: &mut CallContext,
+ cx: &mut CallContext<'vm>,
program: Ref<WebGLProgram>,
- ) -> WebIdlResult<Option<Vec<Ref<WebGLShader>>>> {
+ ) -> WebIdlResult<Option<MarkedVec<'vm, Ref<WebGLShader>>>> {
if self.handle_of(program).is_none() {
return Ok(None);
}
- let attached_shaders = [
+ let attached_shaders = MarkedVec::with_capacity(cx.vm(), 2);
+ for shader in [
program.attached_vertex_shader.get(),
program.attached_fragment_shader.get(),
- ];
- Ok(Some(attached_shaders.into_iter().flatten().collect()))
+ ]
+ .into_iter()
+ .flatten()
+ {
+ attached_shaders.push(shader);
+ }
+ Ok(Some(attached_shaders))
}
pub(crate) fn get_attrib_location(
diff --git a/Meta/Generators/libweb_bindings/rust_to_js_value.py b/Meta/Generators/libweb_bindings/rust_to_js_value.py
index 1af4f22d849..a24821221e2 100644
--- a/Meta/Generators/libweb_bindings/rust_to_js_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_js_value.py
@@ -54,7 +54,7 @@ def rust_to_js_value(
if isinstance(idl_type, IDLParameterizedType) and idl_type.name == "sequence":
element_type = idl_type.parameters[0]
# NB: The conversion of an element can allocate, as that of an object of an interface does for its wrapper,
- # and the elements after it must live through that.
+ # and the elements after it must live through that, in the MarkedVec that holds them (see rust_type()).
function = "rooted_sequence_to_js" if holds_cells(element_type, context) else "sequence_to_js"
return f"{function}({call_context}, {expression}, |value| {inner(element_type)})"
if isinstance(idl_type, IDLParameterizedType) and idl_type.name == "record":
diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.py
index 6dcd656b32c..7e2c62f8f4b 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -376,7 +376,12 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str, is_resul
name = idl_type.name
if isinstance(idl_type, IDLParameterizedType):
if name == "sequence":
- return f"Vec<{rust_type(idl_type.parameters[0], context, what, is_result)}>"
+ element_type = idl_type.parameters[0]
+ element = rust_type(element_type, context, what, is_result)
+ # NB: A Vec would hide cells that the implementation returns from the collector until they are converted.
+ if is_result and holds_cells(element_type, context):
+ return f"MarkedVec<'_, {element}>"
+ return f"Vec<{element}>"
if name == "Promise" and is_result:
return "WebIdlPromise"
if name == "record":