1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:27:34 +00:00

LibWeb: Start generating JS wrappers from (simplified) WebIDL :^)

This patch introduces a hackish but functional IDL parser and uses it
to generate the JS bindings for Node and Document.

We'll see how far this simple parser takes us. The important thing
right now is generating code, not being a perfect IDL parser. :^)
This commit is contained in:
Andreas Kling 2020-06-20 22:09:38 +02:00
parent 8d6910b78e
commit 1ffffa0053
15 changed files with 561 additions and 234 deletions

View file

@ -34,6 +34,7 @@
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
#include <LibWeb/Bindings/ImageDataWrapper.h>
#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
#include <LibWeb/DOM/HTMLCanvasElement.h>
#include <LibWeb/DOM/HTMLImageElement.h>

View file

@ -1,134 +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 <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
namespace Web {
namespace Bindings {
DocumentWrapper::DocumentWrapper(JS::GlobalObject& global_object, Document& document)
: NodeWrapper(global_object, document)
{
}
void DocumentWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
{
NodeWrapper::initialize(interpreter, global_object);
define_native_function("getElementById", get_element_by_id, 1);
define_native_function("querySelector", query_selector, 1);
define_native_function("querySelectorAll", query_selector_all, 1);
}
DocumentWrapper::~DocumentWrapper()
{
}
Document& DocumentWrapper::node()
{
return static_cast<Document&>(NodeWrapper::node());
}
const Document& DocumentWrapper::node() const
{
return static_cast<const Document&>(NodeWrapper::node());
}
static Document* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
{
auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
if (!this_object)
return {};
if (StringView("DocumentWrapper") != this_object->class_name()) {
interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "DocumentWrapper");
return {};
}
return &static_cast<DocumentWrapper*>(this_object)->node();
}
JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::get_element_by_id)
{
auto* document = impl_from(interpreter, global_object);
if (!document)
return {};
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getElementById");
auto id = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception())
return {};
auto* element = document->get_element_by_id(id);
if (!element)
return JS::js_null();
return wrap(interpreter.heap(), const_cast<Element&>(*element));
}
JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::query_selector)
{
auto* document = impl_from(interpreter, global_object);
if (!document)
return {};
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelector");
auto selector = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception())
return {};
// FIXME: Throw if selector is invalid
auto element = document->query_selector(selector);
if (!element)
return JS::js_null();
return wrap(interpreter.heap(), *element);
}
JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::query_selector_all)
{
auto* document = impl_from(interpreter, global_object);
if (!document)
return {};
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelectorAll");
auto selector = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception())
return {};
// FIXME: Throw if selector is invalid
auto elements = document->query_selector_all(selector);
// FIXME: This should be a static NodeList, not a plain JS::Array.
auto* node_list = JS::Array::create(global_object);
for (auto& element : elements) {
node_list->indexed_properties().append(wrap(interpreter.heap(), element));
}
return node_list;
}
}
}

View file

@ -1,52 +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/NodeWrapper.h>
namespace Web {
namespace Bindings {
class DocumentWrapper : public NodeWrapper {
public:
DocumentWrapper(JS::GlobalObject&, Document&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~DocumentWrapper() override;
Document& node();
const Document& node() const;
private:
virtual const char* class_name() const override { return "DocumentWrapper"; }
JS_DECLARE_NATIVE_FUNCTION(get_element_by_id);
JS_DECLARE_NATIVE_FUNCTION(query_selector);
JS_DECLARE_NATIVE_FUNCTION(query_selector_all);
};
}
}

View file

@ -61,12 +61,12 @@ ElementWrapper::~ElementWrapper()
Element& ElementWrapper::node()
{
return static_cast<Element&>(NodeWrapper::node());
return static_cast<Element&>(NodeWrapper::impl());
}
const Element& ElementWrapper::node() const
{
return static_cast<const Element&>(NodeWrapper::node());
return static_cast<const Element&>(NodeWrapper::impl());
}
static Element* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)

View file

@ -57,12 +57,12 @@ HTMLCanvasElementWrapper::~HTMLCanvasElementWrapper()
HTMLCanvasElement& HTMLCanvasElementWrapper::node()
{
return static_cast<HTMLCanvasElement&>(NodeWrapper::node());
return static_cast<HTMLCanvasElement&>(NodeWrapper::impl());
}
const HTMLCanvasElement& HTMLCanvasElementWrapper::node() const
{
return static_cast<const HTMLCanvasElement&>(NodeWrapper::node());
return static_cast<const HTMLCanvasElement&>(NodeWrapper::impl());
}
static HTMLCanvasElement* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)

View file

@ -46,12 +46,12 @@ HTMLImageElementWrapper::~HTMLImageElementWrapper()
HTMLImageElement& HTMLImageElementWrapper::node()
{
return static_cast<HTMLImageElement&>(NodeWrapper::node());
return static_cast<HTMLImageElement&>(NodeWrapper::impl());
}
const HTMLImageElement& HTMLImageElementWrapper::node() const
{
return static_cast<const HTMLImageElement&>(NodeWrapper::node());
return static_cast<const HTMLImageElement&>(NodeWrapper::impl());
}
}

View file

@ -24,9 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FlyString.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
@ -52,30 +50,5 @@ NodeWrapper* wrap(JS::Heap& heap, Node& node)
return static_cast<NodeWrapper*>(wrap_impl(heap, node));
}
NodeWrapper::NodeWrapper(JS::GlobalObject& global_object, Node& node)
: EventTargetWrapper(global_object, node)
{
}
void NodeWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
{
EventTargetWrapper::initialize(interpreter, global_object);
put("nodeName", JS::js_string(interpreter.heap(), node().node_name()));
}
NodeWrapper::~NodeWrapper()
{
}
Node& NodeWrapper::node()
{
return static_cast<Node&>(EventTargetWrapper::impl());
}
const Node& NodeWrapper::node() const
{
return static_cast<const Node&>(EventTargetWrapper::impl());
}
}
}

View file

@ -26,24 +26,12 @@
#pragma once
#include <LibWeb/Bindings/EventTargetWrapper.h>
#include <LibWeb/Forward.h>
#include <LibJS/Forward.h>
namespace Web {
namespace Bindings {
class NodeWrapper : public EventTargetWrapper {
public:
NodeWrapper(JS::GlobalObject&, Node&);
virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
virtual ~NodeWrapper() override;
Node& node();
const Node& node() const;
private:
virtual const char* class_name() const override { return "NodeWrapper"; }
};
NodeWrapper* wrap(JS::Heap&, Node&);
}

View file

@ -36,6 +36,7 @@
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/NavigatorObject.h>
#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>