From 6c5e79ce3aad05d03c403bb2826c3d555e27b2c9 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 14 Mar 2023 07:26:42 -0400 Subject: [PATCH] LibWeb: Begin adding support for Function IDL types in unions For example, setTimeout/setInterval use a TimerHandler, which is a union of (DOMString or Function). --- .../BindingsGenerator/IDLGenerators.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 30150ea04d..f527f1a14f 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -145,6 +145,9 @@ CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface) if (type.name() == "File") return { .name = "JS::Handle", .sequence_storage_type = SequenceStorageType::MarkedVector }; + if (type.name() == "Function") + return { .name = "JS::Handle", .sequence_storage_type = SequenceStorageType::MarkedVector }; + if (type.name() == "sequence") { auto& parameterized_type = verify_cast(type); auto& sequence_type = parameterized_type.parameters().first(); @@ -1092,9 +1095,23 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter // 1. If types includes a typed array type whose name is the value of V’s [[TypedArrayName]] internal slot, then return the result of converting V to that type. // 2. If types includes object, then return the IDL value that is a reference to the object V. - // FIXME: 9. If IsCallable(V) is true, then: - // 1. If types includes a callback function type, then return the result of converting V to that callback function type. - // 2. If types includes object, then return the IDL value that is a reference to the object V. + // 9. If IsCallable(V) is true, then: + // 1. If types includes a callback function type, then return the result of converting V to that callback function type. + // 2. If types includes object, then return the IDL value that is a reference to the object V. + bool includes_callable = false; + for (auto const& type : types) { + if (type->name() == "Function"sv) { + includes_callable = true; + break; + } + } + + if (includes_callable) { + union_generator.append(R"~~~( + if (@js_name@@js_suffix@_object.is_function()) + return JS::make_handle(vm.heap().allocate_without_realm(@js_name@@js_suffix@.as_function(), HTML::incumbent_settings_object())); +)~~~"); + } // 10. If Type(V) is Object, then: // 1. If types includes a sequence type, then: