From 3a11f65451d751bfa729264a05ac4ca076cf9a12 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 15 Mar 2023 19:15:45 -0400 Subject: [PATCH] LibWeb: Add crude support for dereferencing JavaScript built-ins in IDL This works similarly to the is_platform_object() helper, in that we do not know that a type like ArrayBuffer needs to be qualified with JS::. --- .../BindingsGenerator/IDLGenerators.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 257e222603..206d0d7adc 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -60,6 +60,19 @@ static bool is_platform_object(Type const& type) return false; } +// FIXME: Generate this automatically somehow. +static bool is_javascript_builtin(Type const& type) +{ + // NOTE: This is a hand-curated subset of JavaScript built-in types that are actually relevant + // in places where this function is used. If you add IDL code and get compile errors, you + // might simply need to add another type here. + static constexpr Array types = { + "ArrayBuffer"sv, + }; + + return types.span().contains_slow(type.name()); +} + static StringView sequence_storage_type_to_cpp_storage_type_name(SequenceStorageType sequence_storage_type) { switch (sequence_storage_type) { @@ -1522,7 +1535,10 @@ static void generate_wrap_statement(SourceGenerator& generator, DeprecatedString auto scoped_generator = generator.fork(); scoped_generator.set("value", value); if (!libweb_interface_namespaces.span().contains_slow(type.name())) { - scoped_generator.set("type", type.name()); + if (is_javascript_builtin(type)) + scoped_generator.set("type", DeprecatedString::formatted("JS::{}", type.name())); + else + scoped_generator.set("type", type.name()); } else { // e.g. Document.getSelection which returns Selection, which is in the Selection namespace. StringBuilder builder; @@ -3109,6 +3125,7 @@ void generate_prototype_implementation(IDL::Interface const& interface, StringBu #include #include #include +#include #include #include #include