LibWeb: Generate HTMLImageElement bindings from IDL :^)

This commit is contained in:
Andreas Kling 2020-06-21 15:26:09 +02:00
parent af51dc105a
commit 119dd2c541
Notes: sideshowbarker 2024-07-19 05:29:29 +09:00
6 changed files with 18 additions and 118 deletions

View file

@ -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<const HTMLImageElementWrapper&>(*image_argument).node(), x, y);
impl->draw_image(static_cast<const HTMLImageElementWrapper&>(*image_argument).impl(), x, y);
return JS::js_undefined();
}

View file

@ -1,58 +0,0 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* 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 <AK/FlyString.h>
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
#include <LibWeb/DOM/HTMLImageElement.h>
namespace Web {
namespace Bindings {
HTMLImageElementWrapper::HTMLImageElementWrapper(JS::GlobalObject& global_object, HTMLImageElement& element)
: HTMLElementWrapper(global_object, element)
{
}
HTMLImageElementWrapper::~HTMLImageElementWrapper()
{
}
HTMLImageElement& HTMLImageElementWrapper::node()
{
return static_cast<HTMLImageElement&>(NodeWrapper::impl());
}
const HTMLImageElement& HTMLImageElementWrapper::node() const
{
return static_cast<const HTMLImageElement&>(NodeWrapper::impl());
}
}
}

View file

@ -1,47 +0,0 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* 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 <LibWeb/Bindings/HTMLElementWrapper.h>
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"; }
};
}
}

View file

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

View file

@ -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::TypeError>(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::TypeError>(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 = [&] {

View file

@ -0,0 +1,3 @@
interface HTMLImageElement : HTMLElement {
}