mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-06-28 12:10:28 +00:00
Represent BufferSource and ArrayBufferView as ordinary IDL typedefs over their underlying union types, instead of special casing in the IDL generator. This allows the union conversion/return machinery handle these types consistently with other typedefs, which removes buffer specific paths from the IDL generator. This necessitates changing the WebIDL::BufferSource and WebIDL::ArrayBufferView classes as views over these variants. This replaces the old GC backed BufferableObject wrapper structure and provide convenience helpers to determine things such as the byte length, byte offset, backing buffer, and typed-array APIs.
82 lines
3.4 KiB
C++
82 lines
3.4 KiB
C++
/*
|
||
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
||
* Copyright (c) 2024, Simon Wanner <simon@skyrising.xyz>
|
||
*
|
||
* SPDX-License-Identifier: BSD-2-Clause
|
||
*/
|
||
|
||
#include <AK/FlyString.h>
|
||
#include <LibJS/Runtime/TypedArray.h>
|
||
#include <LibWeb/Bindings/Intrinsics.h>
|
||
#include <LibWeb/Bindings/TextDecoder.h>
|
||
#include <LibWeb/Encoding/TextDecoder.h>
|
||
#include <LibWeb/WebIDL/AbstractOperations.h>
|
||
#include <LibWeb/WebIDL/Buffers.h>
|
||
|
||
namespace Web::Encoding {
|
||
|
||
GC_DEFINE_ALLOCATOR(TextDecoder);
|
||
|
||
// https://encoding.spec.whatwg.org/#dom-textdecoder
|
||
WebIDL::ExceptionOr<GC::Ref<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString label, Optional<Bindings::TextDecoderOptions> const& options)
|
||
{
|
||
auto& vm = realm.vm();
|
||
|
||
// 1. Let encoding be the result of getting an encoding from label.
|
||
auto encoding = TextCodec::get_standardized_encoding(label);
|
||
|
||
// 2. If encoding is failure or replacement, then throw a RangeError.
|
||
if (!encoding.has_value() || encoding->equals_ignoring_ascii_case("replacement"sv))
|
||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", label)) };
|
||
|
||
// 3. Set this’s encoding to encoding.
|
||
// https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
|
||
// The encoding getter steps are to return this’s encoding’s name, ASCII lowercased.
|
||
auto lowercase_encoding_name = encoding.value().to_ascii_lowercase_string();
|
||
|
||
// 4. If options["fatal"] is true, then set this’s error mode to "fatal".
|
||
auto error_mode = options.value_or({}).fatal ? ErrorMode::Fatal : ErrorMode::Replacement;
|
||
|
||
// 5. Set this’s ignore BOM to options["ignoreBOM"].
|
||
auto ignore_bom = options.value_or({}).ignore_bom;
|
||
|
||
// NOTE: This should happen in decode(), but we don't support streaming yet and share decoders across calls.
|
||
auto decoder = TextCodec::decoder_for_exact_name(encoding.value());
|
||
VERIFY(decoder.has_value());
|
||
|
||
return realm.create<TextDecoder>(realm, *decoder, lowercase_encoding_name, error_mode, ignore_bom);
|
||
}
|
||
|
||
// https://encoding.spec.whatwg.org/#dom-textdecoder
|
||
TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, FlyString encoding, ErrorMode error_mode, bool ignore_bom)
|
||
: PlatformObject(realm)
|
||
, TextDecoderCommonMixin(decoder, move(encoding), error_mode, ignore_bom)
|
||
{
|
||
}
|
||
|
||
TextDecoder::~TextDecoder() = default;
|
||
|
||
void TextDecoder::initialize(JS::Realm& realm)
|
||
{
|
||
WEB_SET_PROTOTYPE_FOR_INTERFACE(TextDecoder);
|
||
Base::initialize(realm);
|
||
}
|
||
|
||
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
||
WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<WebIDL::BufferSourceVariant> input, Optional<Bindings::TextDecodeOptions> const&) const
|
||
{
|
||
if (!input.has_value())
|
||
return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({}));
|
||
|
||
// FIXME: Implement the streaming stuff.
|
||
auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input);
|
||
if (data_buffer_or_error.is_error())
|
||
return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"_utf16);
|
||
auto& data_buffer = data_buffer_or_error.value();
|
||
auto result = TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() }));
|
||
if (this->fatal() && result.contains(0xfffd))
|
||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Decoding failed"sv };
|
||
return result;
|
||
}
|
||
|
||
}
|