Skip to content

Truly independent web browser

LibJS+LibWeb: Lend a MarkedVec's values instead of copying them out

Shannon Booth672f4a8a2cb9
diff --git a/Libraries/LibJS/Rust/src/embedding/compile.rs b/Libraries/LibJS/Rust/src/embedding/compile.rs
index bfc8a7cd230..3879b4a07e7 100644
--- a/Libraries/LibJS/Rust/src/embedding/compile.rs
+++ b/Libraries/LibJS/Rust/src/embedding/compile.rs
@@ -636,9 +636,9 @@ unsafe extern "C" fn install_compiled_lazy_functions(compiled: *mut c_void) {
// SAFETY: The embedder runs each task once, with the data that came with it, which is this boxed result.
let compiled = *unsafe { Box::from_raw(compiled.cast::<CompiledLazyFunctions>()) };
let awaiting_compilation = compiled.awaiting_compilation.0;
- let shared_function_data = awaiting_compilation.shared_function_data.get().clone();
+ let shared_function_data = awaiting_compilation.shared_function_data.get();
assert_eq!(shared_function_data.len(), compiled.compiled_functions.len());
- for (shared_data, compiled_function) in shared_function_data.into_iter().zip(compiled.compiled_functions) {
+ for (&shared_data, compiled_function) in shared_function_data.iter().zip(compiled.compiled_functions) {
// A function that started running in the meantime compiled on this thread, so the worker compiles the
// functions it creates instead.
if let Some(executable) = shared_data.executable() {
diff --git a/Libraries/LibJS/Rust/src/embedding/debugger.rs b/Libraries/LibJS/Rust/src/embedding/debugger.rs
index b8ac4de00d8..f3be1187e9b 100644
--- a/Libraries/LibJS/Rust/src/embedding/debugger.rs
+++ b/Libraries/LibJS/Rust/src/embedding/debugger.rs
@@ -460,8 +460,7 @@ pub unsafe extern "C" fn js_debugger_bindings_for_frame(
let sink = unsafe { sink.as_ref() }.expect("a sink is not null");
let append = sink.append.expect("a sink has an append function");
let bindings = debugger.bindings_for_frame(vm, execution_context);
- // The copy leaves the marked list unborrowed while the sink runs, and the list keeps the values alive.
- for binding in bindings.to_vec() {
+ for binding in bindings.values().iter() {
let binding_for_c = JSDebuggerFrameBinding {
name: JSUtf16View::of(Utf16View::of_fly_string(&binding.name)),
value: binding.value.0,
diff --git a/Libraries/LibJS/Rust/src/embedding/host/console_client.rs b/Libraries/LibJS/Rust/src/embedding/host/console_client.rs
index 54d34971602..1d821e81922 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/console_client.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/console_client.rs
@@ -154,8 +154,6 @@ impl HostConsoleClient {
return Ok(Value::UNDEFINED);
};
- // The views point into `arguments` and these copies, which all live until the printer returns. The values
- // stay alive in the MarkedVec of `arguments`.
let trace_frames;
let values;
let printer_arguments = match &arguments {
@@ -179,7 +177,7 @@ impl HostConsoleClient {
}
}
PrinterArguments::Values(marked_values) => {
- values = marked_values.to_vec();
+ values = marked_values.values();
JSConsolePrinterArguments {
kind: JS_CONSOLE_PRINTER_ARGUMENTS_VALUES,
label: JSUtf16View::of(Utf16View::EMPTY),
diff --git a/Libraries/LibJS/Rust/src/gc/root.rs b/Libraries/LibJS/Rust/src/gc/root.rs
index a7b0af367d4..547855eee40 100644
--- a/Libraries/LibJS/Rust/src/gc/root.rs
+++ b/Libraries/LibJS/Rust/src/gc/root.rs
@@ -6,7 +6,8 @@
//! Precise roots for values that live outside cells and outside the stack, where the collector would not find them.
-use core::cell::{OnceCell, RefCell};
+use core::cell::{OnceCell, Ref, RefCell, RefMut};
+use core::ops::{Deref, DerefMut};
use super::gc_ref_cell::GcRefCell;
use super::visitor::{Trace, Visitor};
@@ -77,8 +78,8 @@ impl<T: Trace + 'static> Drop for Root<'_, T> {
}
/// A growable list of values that are kept alive, for the temporaries a Vec would hide from the collector. Elements
-/// are copied in and out, so no reference into the list outlives a call. The list registers its root with the first
-/// element, so an empty one costs nothing.
+/// are copied in and out, or lent by values() and values_mut(), while which the list cannot change. The list registers
+/// its root with the first element, so an empty one costs nothing.
pub struct MarkedVec<'vm, T: Trace + Clone + 'static> {
vm: &'vm Vm,
values: OnceCell<Root<'vm, GcRefCell<Vec<T>>>>,
@@ -138,12 +139,15 @@ impl<'vm, T: Trace + Clone + 'static> MarkedVec<'vm, T> {
self.len() == 0
}
- /// Lends the values to `callback`, which must not change the list.
- pub fn with_values<R>(&self, callback: impl FnOnce(&[T]) -> R) -> R {
- match self.values.get() {
- Some(values) => callback(&values.get().borrow()),
- None => callback(&[]),
- }
+ /// Lends the values until the result drops, which may outlive collections. Changing the list meanwhile panics.
+ pub fn values(&self) -> MarkedValues<'_, T> {
+ MarkedValues(self.values.get().map(|values| values.get().borrow()))
+ }
+
+ /// Lends the values to change in place until the result drops. Nothing may collect meanwhile, which stops the
+ /// process, and using the list meanwhile panics.
+ pub fn values_mut(&self) -> MarkedValuesMut<'_, T> {
+ MarkedValuesMut(self.values.get().map(|values| values.get().borrow_mut()))
}
/// Copies the values out, for handing them to code that does not collect.
@@ -153,3 +157,31 @@ impl<'vm, T: Trace + Clone + 'static> MarkedVec<'vm, T> {
.map_or_else(Vec::new, |values| values.get().borrow().clone())
}
}
+
+/// The values of a MarkedVec, which stay alive and unchanged while this lends them.
+pub struct MarkedValues<'a, T>(Option<Ref<'a, Vec<T>>>);
+
+impl<T> Deref for MarkedValues<'_, T> {
+ type Target = [T];
+
+ fn deref(&self) -> &[T] {
+ self.0.as_deref().map_or(&[], Vec::as_slice)
+ }
+}
+
+/// The values of a MarkedVec, which stay alive while this lends them to change in place.
+pub struct MarkedValuesMut<'a, T>(Option<RefMut<'a, Vec<T>>>);
+
+impl<T> Deref for MarkedValuesMut<'_, T> {
+ type Target = [T];
+
+ fn deref(&self) -> &[T] {
+ self.0.as_deref().map_or(&[], Vec::as_slice)
+ }
+}
+
+impl<T> DerefMut for MarkedValuesMut<'_, T> {
+ fn deref_mut(&mut self) -> &mut [T] {
+ self.0.as_deref_mut().map_or(&mut [], Vec::as_mut_slice)
+ }
+}
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index 1ffa2d32f4b..4ab16b7e178 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -418,9 +418,14 @@ fn call_other_function_from_jit(
Err(throw) => return Some(handle_asm_exception(vm, site.pc, throw.value())),
}
}
- return Some(argument_list.with_values(|arguments| {
- call_from_jit_or_generic(vm, site, this_value, target_this, arguments, unwrap_depth + 1)
- }));
+ return Some(call_from_jit_or_generic(
+ vm,
+ site,
+ this_value,
+ target_this,
+ &argument_list.values(),
+ unwrap_depth + 1,
+ ));
}
return None;
@@ -745,7 +750,7 @@ fn call_spreading_arguments_from_jit(
return handle_asm_exception(vm, pc, throw.value());
}
let this_value = slots[call.this_value.0 as usize].get();
- spread_arguments.with_values(|arguments| call_from_jit_or_generic(vm, &site, callee, this_value, arguments, 0))
+ call_from_jit_or_generic(vm, &site, callee, this_value, &spread_arguments.values(), 0)
}
/// `i64 libjs_jit_call_forwarding_arguments(VM*, ExecutionContext* frame, u32 pc)`: runs the Call instruction at `pc`
diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rs
index 228f4a2ef27..d3d8144e0bf 100644
--- a/Libraries/LibJS/Rust/src/runtime/array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array.rs
@@ -156,7 +156,7 @@ impl Array {
/// CreateArrayFromList for a list that is kept alive while the array is allocated.
pub fn create_from_list(vm: &Vm, realm: Gc<Realm>, elements: &MarkedVec<'_, Value>) -> Gc<Array> {
let array = Self::create(vm, realm, 0, None).must();
- elements.with_values(|elements| array.set_indexed_property_elements(elements));
+ array.set_indexed_property_elements(&elements.values());
array
}
diff --git a/Libraries/LibJS/Rust/src/runtime/async_function_constructor.rs b/Libraries/LibJS/Rust/src/runtime/async_function_constructor.rs
index 062ab9662e8..e2e644f7f8f 100644
--- a/Libraries/LibJS/Rust/src/runtime/async_function_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/async_function_constructor.rs
@@ -80,7 +80,7 @@ impl AsyncFunctionConstructor {
for index in 0..vm.argument_count() {
marked_arguments.push(vm.argument(index));
}
- let arguments = marked_arguments.to_vec();
+ let arguments = marked_arguments.values();
let parameter_args = &arguments[..arguments.len().saturating_sub(1)];
diff --git a/Libraries/LibJS/Rust/src/runtime/async_generator_function_constructor.rs b/Libraries/LibJS/Rust/src/runtime/async_generator_function_constructor.rs
index ae3db21bece..63cb5184fab 100644
--- a/Libraries/LibJS/Rust/src/runtime/async_generator_function_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/async_generator_function_constructor.rs
@@ -83,7 +83,7 @@ impl AsyncGeneratorFunctionConstructor {
for index in 0..vm.argument_count() {
marked_arguments.push(vm.argument(index));
}
- let arguments = marked_arguments.to_vec();
+ let arguments = marked_arguments.values();
let parameter_args = &arguments[..arguments.len().saturating_sub(1)];
diff --git a/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs b/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs
index 29f10a6aeba..461ff217004 100644
--- a/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs
+++ b/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs
@@ -819,18 +819,12 @@ impl CyclicModule {
// 11. Let _sortedExecList_ be a List whose elements are the elements of _execList_, sorted by their
// [[AsyncEvaluationOrder]] field in ascending order.
- // NB: The sorted copy holds the same modules as the list, which keeps them alive, and nothing collects while
- // it is sorted.
- let mut sorted_exec_list = exec_list.to_vec();
- sorted_exec_list.sort_by_key(|module| {
+ exec_list.values_mut().sort_by_key(|module| {
module
.async_evaluation_order
.get()
.expect("the module has an async evaluation order")
});
- for (index, module) in sorted_exec_list.into_iter().enumerate() {
- exec_list.set(index, module);
- }
// 12. For each Cyclic Module Record m of sortedExecList, do
for index in 0..exec_list.len() {
diff --git a/Libraries/LibJS/Rust/src/runtime/function_constructor.rs b/Libraries/LibJS/Rust/src/runtime/function_constructor.rs
index 873195d33c6..9ff353b18bd 100644
--- a/Libraries/LibJS/Rust/src/runtime/function_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/function_constructor.rs
@@ -301,7 +301,7 @@ impl FunctionConstructor {
for index in 0..vm.argument_count() {
marked_arguments.push(vm.argument(index));
}
- let arguments = marked_arguments.to_vec();
+ let arguments = marked_arguments.values();
let parameter_args = &arguments[..arguments.len().saturating_sub(1)];
diff --git a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs b/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
index 266b56136dd..85343ec2887 100644
--- a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
@@ -224,8 +224,7 @@ impl FunctionPrototype {
return call_function_object(vm, function, this_arg, arguments);
}
let elements = arg_array_object.indexed_packed_elements(vm);
- return elements
- .with_values(|elements| call_function_object(vm, function, this_arg, &elements[..length]));
+ return call_function_object(vm, function, this_arg, &elements.values()[..length]);
}
}
@@ -248,7 +247,7 @@ impl FunctionPrototype {
// FIXME: 5. Perform PrepareForTailCall().
// 6. Return ? Call(func, thisArg, argList).
- arguments.with_values(|arguments| call_function_object(vm, function, this_arg, arguments))
+ call_function_object(vm, function, this_arg, &arguments.values())
}
// 20.2.3.2 Function.prototype.bind ( thisArg, ...args ), https://tc39.es/ecma262/#sec-function.prototype.bind
@@ -404,7 +403,7 @@ impl FunctionPrototype {
return call_function_object(vm, function, this_arg, args);
}
let args = arguments_after_the_first(vm);
- args.with_values(|args| call_function_object(vm, function, this_arg, args))
+ call_function_object(vm, function, this_arg, &args.values())
}
// 20.2.3.5 Function.prototype.toString ( ), https://tc39.es/ecma262/#sec-function.prototype.tostring
diff --git a/Libraries/LibJS/Rust/src/runtime/generator_function_constructor.rs b/Libraries/LibJS/Rust/src/runtime/generator_function_constructor.rs
index 31f58984520..f6b417ad243 100644
--- a/Libraries/LibJS/Rust/src/runtime/generator_function_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/generator_function_constructor.rs
@@ -80,7 +80,7 @@ impl GeneratorFunctionConstructor {
for index in 0..vm.argument_count() {
marked_arguments.push(vm.argument(index));
}
- let arguments = marked_arguments.to_vec();
+ let arguments = marked_arguments.values();
let parameter_args = &arguments[..arguments.len().saturating_sub(1)];
diff --git a/Libraries/LibJS/Rust/src/runtime/intrinsics.rs b/Libraries/LibJS/Rust/src/runtime/intrinsics.rs
index 9fb9d8414a0..540f2ef0a49 100644
--- a/Libraries/LibJS/Rust/src/runtime/intrinsics.rs
+++ b/Libraries/LibJS/Rust/src/runtime/intrinsics.rs
@@ -880,7 +880,7 @@ macro_rules! native_javascript_backed_function_accessors {
impl Intrinsics {
fn $create(&self, vm: &Vm) {
let shared_data_list = parse_builtin_file(vm, $source());
- for shared_data in shared_data_list.to_vec() {
+ for &shared_data in shared_data_list.values().iter() {
let function_name = shared_data.name();
$(
if function_name == Utf16FlyString::from_utf8($function_name) {
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index b537cd6c541..f931ccb8809 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -4422,10 +4422,10 @@ impl Object {
// For FunctionPrototype.apply fast path
pub fn indexed_packed_elements<'vm>(&self, vm: &'vm Vm) -> MarkedVec<'vm, Value> {
let count = self.indexed_packed_elements_span_size();
- // NB: Reading the elements does not collect, so they need no root until they are copied out.
- let values: Vec<Value> = (0..count).map(|index| self.indexed_element(index)).collect();
- let elements = MarkedVec::new(vm);
- elements.extend_from_slice(&values);
+ let elements = MarkedVec::with_capacity(vm, count as usize);
+ for index in 0..count {
+ elements.push(self.indexed_element(index));
+ }
elements
}
diff --git a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
index 588811395e6..744e50d12a7 100644
--- a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
@@ -1148,7 +1148,7 @@ impl ProxyObject {
}
return callback(&arguments[..passed_argument_count]);
}
- Self::arguments_of(vm, callee_context).with_values(callback)
+ callback(&Self::arguments_of(vm, callee_context).values())
}
// 10.5.12 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
diff --git a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs b/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
index 7ba8766d0c5..2cd7f452229 100644
--- a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
@@ -105,7 +105,7 @@ impl ReflectObject {
// 3. Perform PrepareForTailCall().
// 4. Return ? Call(target, thisArgument, args).
- args.with_values(|args| call_function_object(vm, target.as_function(), this_argument, args))
+ call_function_object(vm, target.as_function(), this_argument, &args.values())
}
// 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
index fcba198d39a..99d74427e24 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
@@ -1885,7 +1885,7 @@ impl RegExpPrototype {
// NB: Everything in the list is kept alive by the captures, the variables above, and the caller.
replacer_args.clear();
replacer_args.push(Value::from_string(matched));
- captures.with_values(|captures| replacer_args.extend_from_slice(captures));
+ replacer_args.extend_from_slice(&captures.values());
replacer_args.push(Value::from_f64(position));
replacer_args.push(Value::from_string(string));
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index e70722cde24..a4931d54041 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -811,7 +811,7 @@ impl Shape {
}
let own_string_keys = OwnStringKeys {
generation,
- keys: keys.with_values(|keys| keys.into()),
+ keys: (*keys.values()).into(),
enumerable: property_keys.iter().map(|(_, enumerable)| *enumerable).collect(),
};
self.with_rare_data(|rare_data| rare_data.own_string_keys = Some(own_string_keys));
diff --git a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs b/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
index 9f6a4e7540b..45ff09d395e 100644
--- a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
+++ b/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
@@ -613,7 +613,7 @@ impl SourceTextModule {
}
_ => self.executable.set(Some(executable)),
}
- for function in existing_functions.to_vec() {
+ for function in existing_functions.values().iter() {
function.clear_non_bytecode_cache_compile_inputs();
}
self.executable_backing.set(ExecutableBacking::MappedBytecodeCache);
diff --git a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs b/Libraries/LibJS/Rust/src/runtime/string_prototype.rs
index 2bc08fe6260..c0807beed5a 100644
--- a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/string_prototype.rs
@@ -2203,8 +2203,7 @@ impl<'vm> SubstringList<'vm> {
// finds them there if the allocation collects.
let array = Array::create(vm, realm, 0, None).must();
let mut elements = self.first_substrings.to_vec();
- self.other_substrings
- .with_values(|others| elements.extend_from_slice(others));
+ elements.extend_from_slice(&self.other_substrings.values());
array.set_indexed_property_elements(&elements);
Value::from_object(array.upcast::<Object>())
}
diff --git a/Libraries/LibJS/Rust/src/runtime/typed_array.rs b/Libraries/LibJS/Rust/src/runtime/typed_array.rs
index bd608e1b658..c737c1b08cd 100644
--- a/Libraries/LibJS/Rust/src/runtime/typed_array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/typed_array.rs
@@ -1470,7 +1470,7 @@ fn initialize_typed_array_from_list(
// 2. Perform ? AllocateTypedArrayBuffer(O, len).
allocate_typed_array_buffer(vm, typed_array, length)?;
- if list.with_values(|values| set_elements_to_numbers_if_unobservable(typed_array, values)) {
+ if set_elements_to_numbers_if_unobservable(typed_array, &list.values()) {
return Ok(());
}
diff --git a/Libraries/LibJS/Rust/src/runtime/typed_array_constructor.rs b/Libraries/LibJS/Rust/src/runtime/typed_array_constructor.rs
index f6d65445691..8c29a891239 100644
--- a/Libraries/LibJS/Rust/src/runtime/typed_array_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/typed_array_constructor.rs
@@ -160,9 +160,7 @@ impl TypedArrayConstructor {
// c. Let targetObj be ? TypedArrayCreate(C, « 𝔽(len) »).
let target_object = typed_array_create(vm, constructor.as_function(), &[Value::from_f64(length as f64)])?;
- if map_fn.is_none()
- && values.with_values(|values| set_elements_to_numbers_if_unobservable(&target_object, values))
- {
+ if map_fn.is_none() && set_elements_to_numbers_if_unobservable(&target_object, &values.values()) {
return Ok(Value::from_object(target_object));
}
diff --git a/Libraries/LibJS/Rust/src/script.rs b/Libraries/LibJS/Rust/src/script.rs
index 43524fe82ce..4f0b3ed633e 100644
--- a/Libraries/LibJS/Rust/src/script.rs
+++ b/Libraries/LibJS/Rust/src/script.rs
@@ -489,7 +489,7 @@ impl Script {
}
self.executable.set(executable);
- for function in existing_functions.to_vec() {
+ for function in existing_functions.values().iter() {
function.clear_non_bytecode_cache_compile_inputs();
}
self.executable_backing.set(ExecutableBacking::MappedBytecodeCache);
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
index db2530650aa..e358d546305 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
@@ -243,32 +243,30 @@ pub(crate) fn process_blob_parts(blob_parts: &MarkedVec<'_, BlobPart>, options:
let mut bytes = Vec::new();
// 2. For each element in parts:
- blob_parts.with_values(|blob_parts| {
- for element in blob_parts {
- match element {
- // 1. If element is a USVString, run the following sub-steps:
- BlobPart::USVString(string) => {
- // 1. Let s be element.
- // 2. If the endings member of options is "native", set s to the result of converting line endings
- // to native of element.
- // 3. Append the result of UTF-8 encoding s to bytes.
- if options.endings == EndingType::Native {
- bytes.extend_from_slice(convert_line_endings_to_native(&string.to_utf8()).as_bytes());
- } else {
- append_utf8(&mut bytes, string);
- }
+ for element in blob_parts.values().iter() {
+ match element {
+ // 1. If element is a USVString, run the following sub-steps:
+ BlobPart::USVString(string) => {
+ // 1. Let s be element.
+ // 2. If the endings member of options is "native", set s to the result of converting line endings
+ // to native of element.
+ // 3. Append the result of UTF-8 encoding s to bytes.
+ if options.endings == EndingType::Native {
+ bytes.extend_from_slice(convert_line_endings_to_native(&string.to_utf8()).as_bytes());
+ } else {
+ append_utf8(&mut bytes, string);
}
- // 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those
- // bytes to bytes.
- BlobPart::BufferSource(buffer_source) => {
- // SAFETY: Appending the bytes neither runs script nor allocates a cell.
- unsafe { buffer_source.with_bytes(|held_bytes| bytes.extend_from_slice(held_bytes)) };
- }
- // 3. If element is a Blob, append the bytes it represents to bytes.
- BlobPart::Blob(blob) => bytes.extend_from_slice(&blob.raw_bytes()),
}
+ // 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those
+ // bytes to bytes.
+ BlobPart::BufferSource(buffer_source) => {
+ // SAFETY: Appending the bytes neither runs script nor allocates a cell.
+ unsafe { buffer_source.with_bytes(|held_bytes| bytes.extend_from_slice(held_bytes)) };
+ }
+ // 3. If element is a Blob, append the bytes it represents to bytes.
+ BlobPart::Blob(blob) => bytes.extend_from_slice(&blob.raw_bytes()),
}
- });
+ }
// 3. Return bytes.
bytes