From 0e3fb39a0ab9a54d708e18bfc638fe3ec182e6d0 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 30 May 2023 09:48:23 +0330 Subject: [PATCH] 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. --- .../LibWeb/BindingsGenerator/IDLGenerators.cpp | 17 ++++++++++++++++- .../expected/TextDecoder/TextDecoder_decode.txt | 2 ++ .../input/TextDecoder/TextDecoder_decode.html | 12 ++++++++++++ .../Libraries/LibWeb/Encoding/TextDecoder.cpp | 7 +++++-- .../Libraries/LibWeb/Encoding/TextDecoder.h | 2 +- 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt create mode 100644 Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 2b3db2a60546dc8150080caa9185110765d0db6c..b1e9c8b343363e1dbd21f88ed27278965d4a18a5 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/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> @cpp_name@; + if (!@js_name@@js_suffix@.is_undefined()) { +)~~~"); + } else { + scoped_generator.append(R"~~~( + JS::Handle @cpp_name@; +)~~~"); + } scoped_generator.append(R"~~~( if (!@js_name@@js_suffix@.is_object() || !(is(@js_name@@js_suffix@.as_object()) || is(@js_name@@js_suffix@.as_object()) || is(@js_name@@js_suffix@.as_object()))) return vm.throw_completion(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"~~~( diff --git a/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt b/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt new file mode 100644 index 0000000000000000000000000000000000000000..8fc16ea51f23691f604e975dd89ed54e4e982c6b --- /dev/null +++ b/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt @@ -0,0 +1,2 @@ +[ABC] +[] diff --git a/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html b/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html new file mode 100644 index 0000000000000000000000000000000000000000..4a2273770ce8005d915c8461724b28094add347e --- /dev/null +++ b/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html @@ -0,0 +1,12 @@ + + diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp index d975b7f8b583ccdbcf1d13aaf07758040b88b244..eb02aa78221436dfd60c3f1afce3ffdeb96de3cd 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp @@ -44,11 +44,14 @@ JS::ThrowCompletionOr TextDecoder::initialize(JS::Realm& realm) } // https://encoding.spec.whatwg.org/#dom-textdecoder-decode -WebIDL::ExceptionOr TextDecoder::decode(JS::Handle const& input) const +WebIDL::ExceptionOr TextDecoder::decode(Optional> 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(); diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h index 02210830ae79fc86077a61229671ce905a8b4a8b..e5e7c72f9ba1bea4db671f32f094efaf6515b5c0 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h @@ -25,7 +25,7 @@ public: virtual ~TextDecoder() override; - WebIDL::ExceptionOr decode(JS::Handle const&) const; + WebIDL::ExceptionOr decode(Optional> const&) const; DeprecatedFlyString const& encoding() const { return m_encoding; } bool fatal() const { return m_fatal; }