LibWeb: Use as_if instead of dynamic_cast in a few places

This commit is contained in:
Tim Ledbetter 2025-01-31 09:49:16 +00:00 committed by Alexander Kalenik
commit 8dfd382e12
Notes: github-actions[bot] 2025-01-31 13:30:46 +00:00
2 changed files with 11 additions and 16 deletions

View file

@ -791,10 +791,7 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(HTMLToken const& token, Opt
// element is either not listed or doesn't have a form attribute, and the intended parent is in the same tree as the element pointed to by the form element pointer,
// then associate element with the form element pointed to by the form element pointer and set element's parser inserted flag.
// FIXME: Check if the element is not a form-associated custom element.
if (is<FormAssociatedElement>(*element)) {
auto* form_associated_element = dynamic_cast<FormAssociatedElement*>(element.ptr());
VERIFY(form_associated_element);
if (auto* form_associated_element = as_if<FormAssociatedElement>(*element)) {
auto& html_element = form_associated_element->form_associated_element_to_html_element();
if (m_form_element.ptr()

View file

@ -1211,8 +1211,8 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
// 5. For each transferable of transferList:
for (auto& transferable : transfer_list) {
auto is_array_buffer = is<JS::ArrayBuffer>(*transferable);
auto is_detached = is_array_buffer && dynamic_cast<JS::ArrayBuffer&>(*transferable).is_detached();
auto array_buffer = as_if<JS::ArrayBuffer>(*transferable);
auto is_detached = array_buffer && array_buffer->is_detached();
// 1. If transferable has an [[ArrayBufferData]] internal slot and IsDetachedBuffer(transferable) is true, then throw a "DataCloneError" DOMException.
if (is_detached) {
@ -1220,9 +1220,8 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
}
// 2. If transferable has a [[Detached]] internal slot and transferable.[[Detached]] is true, then throw a "DataCloneError" DOMException.
if (is<Bindings::Transferable>(*transferable)) {
auto& transferable_object = dynamic_cast<Bindings::Transferable&>(*transferable);
if (transferable_object.is_detached()) {
if (auto* transferable_object = as_if<Bindings::Transferable>(*transferable)) {
if (transferable_object->is_detached()) {
return WebIDL::DataCloneError::create(*vm.current_realm(), "Value already transferred"_string);
}
}
@ -1232,17 +1231,16 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
TransferDataHolder data_holder;
// 4. If transferable has an [[ArrayBufferData]] internal slot, then:
if (is_array_buffer) {
if (array_buffer) {
// 1. If transferable has an [[ArrayBufferMaxByteLength]] internal slot, then:
auto& array_buffer = dynamic_cast<JS::ArrayBuffer&>(*transferable);
if (!array_buffer.is_fixed_length()) {
if (!array_buffer->is_fixed_length()) {
// 1. Set dataHolder.[[Type]] to "ResizableArrayBuffer".
// 2. Set dataHolder.[[ArrayBufferData]] to transferable.[[ArrayBufferData]].
// 3. Set dataHolder.[[ArrayBufferByteLength]] to transferable.[[ArrayBufferByteLength]].
// 4. Set dataHolder.[[ArrayBufferMaxByteLength]] to transferable.[[ArrayBufferMaxByteLength]].
serialize_enum<TransferType>(data_holder.data, TransferType::ResizableArrayBuffer);
MUST(serialize_bytes(vm, data_holder.data, array_buffer.buffer().bytes())); // serializes both byte length and bytes
serialize_primitive_type<size_t>(data_holder.data, array_buffer.max_byte_length());
MUST(serialize_bytes(vm, data_holder.data, array_buffer->buffer().bytes())); // serializes both byte length and bytes
serialize_primitive_type<size_t>(data_holder.data, array_buffer->max_byte_length());
}
// 2. Otherwise:
@ -1251,12 +1249,12 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
// 2. Set dataHolder.[[ArrayBufferData]] to transferable.[[ArrayBufferData]].
// 3. Set dataHolder.[[ArrayBufferByteLength]] to transferable.[[ArrayBufferByteLength]].
serialize_enum<TransferType>(data_holder.data, TransferType::ArrayBuffer);
MUST(serialize_bytes(vm, data_holder.data, array_buffer.buffer().bytes())); // serializes both byte length and bytes
MUST(serialize_bytes(vm, data_holder.data, array_buffer->buffer().bytes())); // serializes both byte length and bytes
}
// 3. Perform ? DetachArrayBuffer(transferable).
// NOTE: Specifications can use the [[ArrayBufferDetachKey]] internal slot to prevent ArrayBuffers from being detached. This is used in WebAssembly JavaScript Interface, for example. See: https://html.spec.whatwg.org/multipage/references.html#refsWASMJS
TRY(JS::detach_array_buffer(vm, array_buffer));
TRY(JS::detach_array_buffer(vm, *array_buffer));
}
// 5. Otherwise: