Files
ladybird/Libraries/LibWeb/Encoding/TextEncoder.h
Shannon Booth 7beac55210 LibWeb/Bindings: Pass exact typed array types through WebIDL bindings
Generate exact JS buffer types for exact IDL buffer arguments instead
of widening them to BufferSource or ArrayBufferView.

This fixes cases like TextEncoder.encodeInto(), whose IDL requires
a Uint8Array destination. Previously the generated binding accepted
any BufferSource, so DataView, other typed arrays, and
ArrayBuffer-backed values were let through. With exact conversion,
those are rejected at the binding layer as expected.
2026-04-26 17:50:58 +02:00

50 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Encoding/TextEncoderCommon.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebIDL/Buffers.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::Encoding {
// https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
struct TextEncoderEncodeIntoResult {
WebIDL::UnsignedLongLong read;
WebIDL::UnsignedLongLong written;
};
// https://encoding.spec.whatwg.org/#textencoder
class TextEncoder final
: public Bindings::PlatformObject
, public TextEncoderCommonMixin {
WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(TextEncoder);
public:
static WebIDL::ExceptionOr<GC::Ref<TextEncoder>> construct_impl(JS::Realm&);
virtual ~TextEncoder() override;
GC::Ref<JS::Uint8Array> encode(String const& input) const;
TextEncoderEncodeIntoResult encode_into(String const& source, GC::Root<JS::Uint8Array> const& destination) const;
protected:
// https://encoding.spec.whatwg.org/#dom-textencoder
explicit TextEncoder(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
}