Selaa lähdekoodia

LibWeb: Make 'optional BufferSource' IDL arguments actually optional

Previously this was compiled to require an object despite the IDL file
specifying 'optional'.
This commit makes IDLGenerator respect this modifier, and fixes the only
affected instance.
Ali Mohammad Pur 2 vuotta sitten
vanhempi
commit
0e3fb39a0a

+ 16 - 1
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -644,13 +644,28 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
 )~~~");
         }
     } else if (parameter.type->name() == "BufferSource") {
+        if (optional) {
+            scoped_generator.append(R"~~~(
+    Optional<JS::Handle<JS::Object>> @cpp_name@;
+    if (!@js_name@@js_suffix@.is_undefined()) {
+)~~~");
+        } else {
+            scoped_generator.append(R"~~~(
+    JS::Handle<JS::Object> @cpp_name@;
+)~~~");
+        }
         scoped_generator.append(R"~~~(
     if (!@js_name@@js_suffix@.is_object() || !(is<JS::TypedArrayBase>(@js_name@@js_suffix@.as_object()) || is<JS::ArrayBuffer>(@js_name@@js_suffix@.as_object()) || is<JS::DataView>(@js_name@@js_suffix@.as_object())))
         return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
 
     // TODO: Should we make this a Variant?
-    auto @cpp_name@ = JS::make_handle(&@js_name@@js_suffix@.as_object());
+    @cpp_name@ = JS::make_handle(&@js_name@@js_suffix@.as_object());
 )~~~");
+        if (optional) {
+            scoped_generator.append(R"~~~(
+        }
+)~~~");
+        }
     } else if (parameter.type->name() == "any") {
         if (variadic) {
             scoped_generator.append(R"~~~(

+ 2 - 0
Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt

@@ -0,0 +1,2 @@
+[ABC]
+[]

+ 12 - 0
Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html

@@ -0,0 +1,12 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        try {
+            let decoder = new TextDecoder("utf-8");
+            println(`[${decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))}]`); // "ABC"
+            println(`[${decoder.decode()}]`);
+        } catch(e) {
+            println("ERROR: " + e.name + ": " + e.message);
+        }
+    });
+</script>

+ 5 - 2
Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp

@@ -44,11 +44,14 @@ JS::ThrowCompletionOr<void> TextDecoder::initialize(JS::Realm& realm)
 }
 
 // https://encoding.spec.whatwg.org/#dom-textdecoder-decode
-WebIDL::ExceptionOr<DeprecatedString> TextDecoder::decode(JS::Handle<JS::Object> const& input) const
+WebIDL::ExceptionOr<DeprecatedString> TextDecoder::decode(Optional<JS::Handle<JS::Object>> const& input) 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.cell());
+    auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input->cell());
     if (data_buffer_or_error.is_error())
         return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer");
     auto& data_buffer = data_buffer_or_error.value();

+ 1 - 1
Userland/Libraries/LibWeb/Encoding/TextDecoder.h

@@ -25,7 +25,7 @@ public:
 
     virtual ~TextDecoder() override;
 
-    WebIDL::ExceptionOr<DeprecatedString> decode(JS::Handle<JS::Object> const&) const;
+    WebIDL::ExceptionOr<DeprecatedString> decode(Optional<JS::Handle<JS::Object>> const&) const;
 
     DeprecatedFlyString const& encoding() const { return m_encoding; }
     bool fatal() const { return m_fatal; }