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.
This commit is contained in:
Ali Mohammad Pur 2023-05-30 09:48:23 +03:30 committed by Andreas Kling
parent 540ea9f1c4
commit 0e3fb39a0a
Notes: sideshowbarker 2024-07-17 01:46:43 +09:00
5 changed files with 36 additions and 4 deletions

View file

@ -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"~~~(

View file

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

View file

@ -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>

View file

@ -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();

View file

@ -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; }