From 119dd2c541305eafea678b0162959cfbb3dff6a4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 21 Jun 2020 15:26:09 +0200 Subject: [PATCH] LibWeb: Generate HTMLImageElement bindings from IDL :^) --- .../CanvasRenderingContext2DWrapper.cpp | 2 +- .../Bindings/HTMLImageElementWrapper.cpp | 58 ------------------- .../LibWeb/Bindings/HTMLImageElementWrapper.h | 47 --------------- Libraries/LibWeb/CMakeLists.txt | 2 +- .../CodeGenerators/WrapperGenerator.cpp | 24 ++++---- Libraries/LibWeb/DOM/HTMLImageElement.idl | 3 + 6 files changed, 18 insertions(+), 118 deletions(-) delete mode 100644 Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp delete mode 100644 Libraries/LibWeb/Bindings/HTMLImageElementWrapper.h create mode 100644 Libraries/LibWeb/DOM/HTMLImageElement.idl diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index c5b9f50d42..62259bfed0 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -156,7 +156,7 @@ JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::draw_image) auto y = interpreter.argument(2).to_double(interpreter); if (interpreter.exception()) return {}; - impl->draw_image(static_cast(*image_argument).node(), x, y); + impl->draw_image(static_cast(*image_argument).impl(), x, y); return JS::js_undefined(); } diff --git a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp deleted file mode 100644 index d4c0f274ea..0000000000 --- a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -namespace Web { -namespace Bindings { - -HTMLImageElementWrapper::HTMLImageElementWrapper(JS::GlobalObject& global_object, HTMLImageElement& element) - : HTMLElementWrapper(global_object, element) -{ -} - -HTMLImageElementWrapper::~HTMLImageElementWrapper() -{ -} - -HTMLImageElement& HTMLImageElementWrapper::node() -{ - return static_cast(NodeWrapper::impl()); -} - -const HTMLImageElement& HTMLImageElementWrapper::node() const -{ - return static_cast(NodeWrapper::impl()); -} - -} -} diff --git a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.h b/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.h deleted file mode 100644 index 8d6cc0b40b..0000000000 --- a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace Web { -namespace Bindings { - -class HTMLImageElementWrapper : public HTMLElementWrapper { -public: - HTMLImageElementWrapper(JS::GlobalObject&, HTMLImageElement&); - virtual ~HTMLImageElementWrapper() override; - - HTMLImageElement& node(); - const HTMLImageElement& node() const; - -private: - virtual const char* class_name() const override { return "HTMLImageElementWrapper"; } -}; - -} -} diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 101c1a05f0..0d406bde37 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -3,7 +3,6 @@ set(SOURCES Bindings/EventListenerWrapper.cpp Bindings/EventWrapper.cpp Bindings/HTMLCanvasElementWrapper.cpp - Bindings/HTMLImageElementWrapper.cpp Bindings/ImageDataWrapper.cpp Bindings/LocationObject.cpp Bindings/MouseEventWrapper.cpp @@ -158,6 +157,7 @@ libweb_js_wrapper(Node) libweb_js_wrapper(Document) libweb_js_wrapper(Element) libweb_js_wrapper(HTMLElement) +libweb_js_wrapper(HTMLImageElement) get_property(WRAPPER_SOURCES GLOBAL PROPERTY wrapper_sources) set(SOURCES ${SOURCES} ${WRAPPER_SOURCES}) diff --git a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 0f52953e03..65edacd1d1 100644 --- a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -407,17 +407,19 @@ void generate_implementation(const IDL::Interface& interface) out() << "}"; // Implementation: impl_from() - out() << "static " << interface.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::ErrorType::NotA, \"" << interface.name << "\");"; - out() << " return nullptr;"; - out() << " }"; - out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();"; - out() << "}"; + if (!interface.attributes.is_empty() || !interface.functions.is_empty()) { + out() << "static " << interface.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::ErrorType::NotA, \"" << interface.name << "\");"; + out() << " return nullptr;"; + out() << " }"; + out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();"; + out() << "}"; + } auto generate_to_cpp = [&](auto& parameter, auto& js_name, auto& js_suffix, auto cpp_name, bool return_void = false) { auto generate_return = [&] { diff --git a/Libraries/LibWeb/DOM/HTMLImageElement.idl b/Libraries/LibWeb/DOM/HTMLImageElement.idl new file mode 100644 index 0000000000..7d48693da3 --- /dev/null +++ b/Libraries/LibWeb/DOM/HTMLImageElement.idl @@ -0,0 +1,3 @@ +interface HTMLImageElement : HTMLElement { + +}