diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-04 11:03:16 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | 018d2364391b36709438c8c53c82449664e4b7df (patch) | |
tree | b510df6a30385f4425b31c324da75a1418b25804 /Userland/Libraries/LibWeb | |
parent | 7e508456a0c52400bcd46713e6538b4c9b47c36f (diff) | |
download | serenity-018d2364391b36709438c8c53c82449664e4b7df.zip |
LibWeb: Make TextDecoder GC-allocated
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextDecoder.h | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Forward.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/idl_files.cmake | 2 |
4 files changed, 33 insertions, 29 deletions
diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp index 3e0e3b24ef..4895ce1b62 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp @@ -9,9 +9,31 @@ #include <LibWeb/Bindings/IDLAbstractOperations.h> #include <LibWeb/Bindings/Wrapper.h> #include <LibWeb/Encoding/TextDecoder.h> +#include <LibWeb/HTML/Window.h> namespace Web::Encoding { +DOM::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::create_with_global_object(HTML::Window& window, FlyString encoding) +{ + auto decoder = TextCodec::decoder_for(encoding); + if (!decoder) + return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, String::formatted("Invalid encoding {}", encoding) }; + + return JS::NonnullGCPtr(*window.heap().allocate<TextDecoder>(window.realm(), window, *decoder, move(encoding), false, false)); +} + +// https://encoding.spec.whatwg.org/#dom-textdecoder +TextDecoder::TextDecoder(HTML::Window& window, TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom) + : PlatformObject(window.realm()) + , m_decoder(decoder) + , m_encoding(move(encoding)) + , m_fatal(fatal) + , m_ignore_bom(ignore_bom) +{ +} + +TextDecoder::~TextDecoder() = default; + // https://encoding.spec.whatwg.org/#dom-textdecoder-decode DOM::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input) const { diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h index 729c30fae1..328d39027b 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h @@ -8,35 +8,22 @@ #include <AK/Forward.h> #include <AK/NonnullRefPtr.h> -#include <AK/RefCounted.h> #include <LibJS/Forward.h> #include <LibTextCodec/Decoder.h> -#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/Forward.h> namespace Web::Encoding { // https://encoding.spec.whatwg.org/#textdecoder -class TextDecoder - : public RefCounted<TextDecoder> - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::TextDecoderWrapper; - - static DOM::ExceptionOr<NonnullRefPtr<TextDecoder>> create(FlyString encoding) - { - auto decoder = TextCodec::decoder_for(encoding); - if (!decoder) - return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, String::formatted("Invalid encoding {}", encoding) }; +class TextDecoder : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject); - return adopt_ref(*new TextDecoder(*decoder, move(encoding), false, false)); - } +public: + static DOM::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> create_with_global_object(HTML::Window&, FlyString encoding); - static DOM::ExceptionOr<NonnullRefPtr<TextDecoder>> create_with_global_object(HTML::Window&, FlyString label) - { - return TextDecoder::create(move(label)); - } + virtual ~TextDecoder() override; DOM::ExceptionOr<String> decode(JS::Handle<JS::Object> const&) const; @@ -44,15 +31,9 @@ public: bool fatal() const { return m_fatal; } bool ignore_bom() const { return m_ignore_bom; }; -protected: +private: // https://encoding.spec.whatwg.org/#dom-textdecoder - TextDecoder(TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom) - : m_decoder(decoder) - , m_encoding(move(encoding)) - , m_fatal(fatal) - , m_ignore_bom(ignore_bom) - { - } + TextDecoder(HTML::Window&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom); TextCodec::Decoder& m_decoder; FlyString m_encoding; @@ -61,3 +42,5 @@ protected: }; } + +WRAPPER_HACK(TextDecoder, Web::Encoding) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 7ca3802e98..3f125a72d8 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -461,7 +461,6 @@ class OptionConstructor; class RangePrototype; class ResizeObserverWrapper; class SelectionWrapper; -class TextDecoderWrapper; class URLSearchParamsIteratorWrapper; class URLSearchParamsWrapper; class URLWrapper; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 1345cba942..47d5f3476a 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -51,7 +51,7 @@ libweb_js_wrapper(DOM/StaticRange NO_INSTANCE) libweb_js_wrapper(DOM/Text NO_INSTANCE) libweb_js_wrapper(DOM/TreeWalker NO_INSTANCE) libweb_js_wrapper(DOMParsing/XMLSerializer NO_INSTANCE) -libweb_js_wrapper(Encoding/TextDecoder) +libweb_js_wrapper(Encoding/TextDecoder NO_INSTANCE) libweb_js_wrapper(Encoding/TextEncoder NO_INSTANCE) libweb_js_wrapper(Fetch/Headers ITERABLE) libweb_js_wrapper(FileAPI/Blob) |