1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:38:11 +00:00

LibWeb: Move DOM classes into the Web::DOM namespace

LibWeb keeps growing and the Web namespace is filling up fast.
Let's put DOM stuff into Web::DOM, just like we already started doing
with SVG stuff in Web::SVG.
This commit is contained in:
Andreas Kling 2020-07-26 19:37:56 +02:00
parent 96d13f75cf
commit 11ff9d0f17
178 changed files with 516 additions and 523 deletions

View file

@ -26,6 +26,7 @@
#include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
@ -108,6 +109,7 @@ struct Interface {
// Added for convenience after parsing
String wrapper_class;
String wrapper_base_class;
String fully_qualified_name;
};
OwnPtr<Interface> parse_interface(const StringView& input)
@ -312,6 +314,9 @@ int main(int argc, char** argv)
return 1;
}
LexicalPath lexical_path(path);
auto namespace_ = lexical_path.parts().at(lexical_path.parts().size() - 2);
auto data = file_or_error.value()->read_all();
auto interface = IDL::parse_interface(data);
@ -320,6 +325,17 @@ int main(int argc, char** argv)
return 1;
}
if (namespace_ == "DOM") {
StringBuilder builder;
builder.append(namespace_);
builder.append("::");
builder.append(interface->name);
interface->fully_qualified_name = builder.to_string();
} else {
interface->fully_qualified_name = interface->name;
}
#if 0
dbg() << "Attributes:";
for (auto& attribute : interface->attributes) {
@ -402,22 +418,21 @@ static void generate_header(const IDL::Interface& interface)
if (wrapper_base_class != "Wrapper")
out() << "#include <LibWeb/Bindings/" << wrapper_base_class << ".h>";
out() << "namespace Web {";
out() << "namespace Bindings {";
out() << "namespace Web::Bindings {";
out() << "class " << wrapper_class << " : public " << wrapper_base_class << " {";
out() << " JS_OBJECT(" << wrapper_class << ", " << wrapper_base_class << ");";
out() << "public:";
out() << " " << wrapper_class << "(JS::GlobalObject&, " << interface.name << "&);";
out() << " " << wrapper_class << "(JS::GlobalObject&, " << interface.fully_qualified_name << "&);";
out() << " virtual void initialize(JS::GlobalObject&) override;";
out() << " virtual ~" << wrapper_class << "() override;";
if (wrapper_base_class == "Wrapper") {
out() << " " << interface.name << "& impl() { return *m_impl; }";
out() << " const " << interface.name << "& impl() const { return *m_impl; }";
out() << " " << interface.fully_qualified_name << "& impl() { return *m_impl; }";
out() << " const " << interface.fully_qualified_name << "& impl() const { return *m_impl; }";
} else {
out() << " " << interface.name << "& impl() { return static_cast<" << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
out() << " const " << interface.name << "& impl() const { return static_cast<const " << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
out() << " " << interface.fully_qualified_name << "& impl() { return static_cast<" << interface.fully_qualified_name << "&>(" << wrapper_base_class << "::impl()); }";
out() << " const " << interface.fully_qualified_name << "& impl() const { return static_cast<const " << interface.fully_qualified_name << "&>(" << wrapper_base_class << "::impl()); }";
}
auto is_foo_wrapper_name = snake_name(String::format("Is%s", wrapper_class.characters()));
@ -436,17 +451,16 @@ static void generate_header(const IDL::Interface& interface)
}
if (wrapper_base_class == "Wrapper") {
out() << " NonnullRefPtr<" << interface.name << "> m_impl;";
out() << " NonnullRefPtr<" << interface.fully_qualified_name << "> m_impl;";
}
out() << "};";
if (should_emit_wrapper_factory(interface)) {
out() << wrapper_class << "* wrap(JS::GlobalObject&, " << interface.name << "&);";
out() << wrapper_class << "* wrap(JS::GlobalObject&, " << interface.fully_qualified_name << "&);";
}
out() << "}";
out() << "}";
}
void generate_implementation(const IDL::Interface& interface)
@ -473,11 +487,17 @@ void generate_implementation(const IDL::Interface& interface)
out() << "#include <LibWeb/Bindings/ImageDataWrapper.h>";
out() << "#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>";
out() << "namespace Web {";
out() << "namespace Bindings {";
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
out() << "using Web::DOM::Node;";
out() << "using Web::DOM::Document;";
out() << "using Web::DOM::DocumentType;";
out() << "using Web::DOM::Element;";
out() << "using Web::DOM::EventListener;";
out() << "namespace Web::Bindings {";
// Implementation: Wrapper constructor
out() << wrapper_class << "::" << wrapper_class << "(JS::GlobalObject& global_object, " << interface.name << "& impl)";
out() << wrapper_class << "::" << wrapper_class << "(JS::GlobalObject& global_object, " << interface.fully_qualified_name << "& impl)";
if (wrapper_base_class == "Wrapper") {
out() << " : Wrapper(*global_object.object_prototype())";
out() << " , m_impl(impl)";
@ -510,13 +530,13 @@ void generate_implementation(const IDL::Interface& interface)
// Implementation: impl_from()
if (!interface.attributes.is_empty() || !interface.functions.is_empty()) {
out() << "static " << interface.name << "* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
out() << "static " << interface.fully_qualified_name << "* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
out() << "{";
out() << " auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);";
out() << " if (!this_object)";
out() << " return {};";
out() << " if (!this_object->inherits(\"" << wrapper_class << "\")) {";
out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << interface.name << "\");";
out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << interface.fully_qualified_name << "\");";
out() << " return nullptr;";
out() << " }";
out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();";
@ -676,12 +696,11 @@ void generate_implementation(const IDL::Interface& interface)
// Implementation: Wrapper factory
if (should_emit_wrapper_factory(interface)) {
out() << wrapper_class << "* wrap(JS::GlobalObject& global_object, " << interface.name << "& impl)";
out() << wrapper_class << "* wrap(JS::GlobalObject& global_object, " << interface.fully_qualified_name << "& impl)";
out() << "{";
out() << " return static_cast<" << wrapper_class << "*>(wrap_impl(global_object, impl));";
out() << "}";
}
out() << "}";
out() << "}";
}