Forráskód Böngészése

LibWeb: Make TextEncoder GC-allocated

Andreas Kling 2 éve
szülő
commit
7e508456a0

+ 16 - 5
Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp

@@ -8,15 +8,26 @@
 #include <LibJS/Runtime/TypedArray.h>
 #include <LibJS/Runtime/TypedArray.h>
 #include <LibWeb/Bindings/Wrapper.h>
 #include <LibWeb/Bindings/Wrapper.h>
 #include <LibWeb/Encoding/TextEncoder.h>
 #include <LibWeb/Encoding/TextEncoder.h>
+#include <LibWeb/HTML/Window.h>
 
 
 namespace Web::Encoding {
 namespace Web::Encoding {
 
 
+JS::NonnullGCPtr<TextEncoder> TextEncoder::create_with_global_object(HTML::Window& window)
+{
+    return *window.heap().allocate<TextEncoder>(window.realm(), window);
+}
+
+TextEncoder::TextEncoder(HTML::Window& window)
+    : PlatformObject(window.realm())
+{
+    set_prototype(&window.cached_web_prototype("TextEncoder"));
+}
+
+TextEncoder::~TextEncoder() = default;
+
 // https://encoding.spec.whatwg.org/#dom-textencoder-encode
 // https://encoding.spec.whatwg.org/#dom-textencoder-encode
 JS::Uint8Array* TextEncoder::encode(String const& input) const
 JS::Uint8Array* TextEncoder::encode(String const& input) const
 {
 {
-    auto& vm = wrapper()->vm();
-    auto& realm = *vm.current_realm();
-
     // NOTE: The AK::String returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.
     // NOTE: The AK::String returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.
 
 
     // 1. Convert input to an I/O queue of scalar values.
     // 1. Convert input to an I/O queue of scalar values.
@@ -29,8 +40,8 @@ JS::Uint8Array* TextEncoder::encode(String const& input) const
 
 
     auto byte_buffer = input.to_byte_buffer();
     auto byte_buffer = input.to_byte_buffer();
     auto array_length = byte_buffer.size();
     auto array_length = byte_buffer.size();
-    auto* array_buffer = JS::ArrayBuffer::create(realm, move(byte_buffer));
-    return JS::Uint8Array::create(realm, array_length, *array_buffer);
+    auto* array_buffer = JS::ArrayBuffer::create(realm(), move(byte_buffer));
+    return JS::Uint8Array::create(realm(), array_length, *array_buffer);
 }
 }
 
 
 // https://encoding.spec.whatwg.org/#dom-textencoder-encoding
 // https://encoding.spec.whatwg.org/#dom-textencoder-encoding

+ 8 - 14
Userland/Libraries/LibWeb/Encoding/TextEncoder.h

@@ -16,21 +16,13 @@
 namespace Web::Encoding {
 namespace Web::Encoding {
 
 
 // https://encoding.spec.whatwg.org/#textencoder
 // https://encoding.spec.whatwg.org/#textencoder
-class TextEncoder
-    : public RefCounted<TextEncoder>
-    , public Bindings::Wrappable {
-public:
-    using WrapperType = Bindings::TextEncoderWrapper;
+class TextEncoder final : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
 
 
-    static NonnullRefPtr<TextEncoder> create()
-    {
-        return adopt_ref(*new TextEncoder());
-    }
+public:
+    static JS::NonnullGCPtr<TextEncoder> create_with_global_object(HTML::Window&);
 
 
-    static NonnullRefPtr<TextEncoder> create_with_global_object(HTML::Window&)
-    {
-        return TextEncoder::create();
-    }
+    virtual ~TextEncoder() override;
 
 
     JS::Uint8Array* encode(String const& input) const;
     JS::Uint8Array* encode(String const& input) const;
 
 
@@ -38,7 +30,9 @@ public:
 
 
 protected:
 protected:
     // https://encoding.spec.whatwg.org/#dom-textencoder
     // https://encoding.spec.whatwg.org/#dom-textencoder
-    TextEncoder() = default;
+    explicit TextEncoder(HTML::Window&);
 };
 };
 
 
 }
 }
+
+WRAPPER_HACK(TextEncoder, Web::Encoding)

+ 0 - 1
Userland/Libraries/LibWeb/Forward.h

@@ -462,7 +462,6 @@ class RangePrototype;
 class ResizeObserverWrapper;
 class ResizeObserverWrapper;
 class SelectionWrapper;
 class SelectionWrapper;
 class TextDecoderWrapper;
 class TextDecoderWrapper;
-class TextEncoderWrapper;
 class URLSearchParamsIteratorWrapper;
 class URLSearchParamsIteratorWrapper;
 class URLSearchParamsWrapper;
 class URLSearchParamsWrapper;
 class URLWrapper;
 class URLWrapper;

+ 1 - 1
Userland/Libraries/LibWeb/idl_files.cmake

@@ -52,7 +52,7 @@ libweb_js_wrapper(DOM/Text NO_INSTANCE)
 libweb_js_wrapper(DOM/TreeWalker NO_INSTANCE)
 libweb_js_wrapper(DOM/TreeWalker NO_INSTANCE)
 libweb_js_wrapper(DOMParsing/XMLSerializer NO_INSTANCE)
 libweb_js_wrapper(DOMParsing/XMLSerializer NO_INSTANCE)
 libweb_js_wrapper(Encoding/TextDecoder)
 libweb_js_wrapper(Encoding/TextDecoder)
-libweb_js_wrapper(Encoding/TextEncoder)
+libweb_js_wrapper(Encoding/TextEncoder NO_INSTANCE)
 libweb_js_wrapper(Fetch/Headers ITERABLE)
 libweb_js_wrapper(Fetch/Headers ITERABLE)
 libweb_js_wrapper(FileAPI/Blob)
 libweb_js_wrapper(FileAPI/Blob)
 libweb_js_wrapper(FileAPI/File)
 libweb_js_wrapper(FileAPI/File)