1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

LibWeb: Generate JS bindings for XMLHttpRequest from IDL :^)

Remove the hand-written XHR bindings in favor of generated ones.
This commit is contained in:
Andreas Kling 2021-01-23 13:23:17 +01:00
parent 25056830f0
commit 8363b3ae99
13 changed files with 38 additions and 431 deletions

View file

@ -93,9 +93,6 @@ void WindowObject::initialize()
ADD_WINDOW_OBJECT_INTERFACES;
m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this);
add_constructor("XMLHttpRequest", m_xhr_constructor, m_xhr_prototype);
m_range_prototype = heap().allocate<RangePrototype>(*this, *this);
add_constructor("Range", m_range_constructor, m_range_prototype);
}
@ -107,8 +104,6 @@ WindowObject::~WindowObject()
void WindowObject::visit_edges(Visitor& visitor)
{
GlobalObject::visit_edges(visitor);
visitor.visit(m_xhr_constructor);
visitor.visit(m_xhr_prototype);
visitor.visit(m_range_constructor);
visitor.visit(m_range_prototype);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -47,9 +47,6 @@ public:
Origin origin() const;
XMLHttpRequestPrototype* xhr_prototype() { return m_xhr_prototype; }
XMLHttpRequestConstructor* xhr_constructor() { return m_xhr_constructor; }
RangePrototype* range_prototype() { return m_range_prototype; }
RangeConstructor* range_constructor() { return m_range_constructor; }
@ -103,9 +100,6 @@ private:
NonnullRefPtr<DOM::Window> m_impl;
XMLHttpRequestConstructor* m_xhr_constructor { nullptr };
XMLHttpRequestPrototype* m_xhr_prototype { nullptr };
RangePrototype* m_range_prototype { nullptr };
RangeConstructor* m_range_constructor { nullptr };

View file

@ -218,6 +218,8 @@
#include <LibWeb/Bindings/TextPrototype.h>
#include <LibWeb/Bindings/UIEventConstructor.h>
#include <LibWeb/Bindings/UIEventPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
#define ADD_WINDOW_OBJECT_INTERFACE(name) \
{ \
@ -319,4 +321,5 @@
ADD_WINDOW_OBJECT_INTERFACE(SVGPathElement) \
ADD_WINDOW_OBJECT_INTERFACE(SVGSVGElement) \
ADD_WINDOW_OBJECT_INTERFACE(Text) \
ADD_WINDOW_OBJECT_INTERFACE(UIEvent)
ADD_WINDOW_OBJECT_INTERFACE(UIEvent) \
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest)

View file

@ -1,73 +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 <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/XHR/XMLHttpRequest.h>
namespace Web::Bindings {
XMLHttpRequestConstructor::XMLHttpRequestConstructor(JS::GlobalObject& global_object)
: NativeFunction(*global_object.function_prototype())
{
}
void XMLHttpRequestConstructor::initialize(JS::GlobalObject& global_object)
{
auto& vm = this->vm();
NativeFunction::initialize(global_object);
auto& window = static_cast<WindowObject&>(global_object);
define_property(vm.names.prototype, window.xhr_prototype(), 0);
define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
define_property("UNSENT", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable);
define_property("OPENED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable);
define_property("HEADERS_RECEIVED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable);
define_property("LOADING", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable);
define_property("DONE", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable);
}
XMLHttpRequestConstructor::~XMLHttpRequestConstructor()
{
}
JS::Value XMLHttpRequestConstructor::call()
{
vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "XMLHttpRequest");
return {};
}
JS::Value XMLHttpRequestConstructor::construct(Function&)
{
auto& window = static_cast<WindowObject&>(global_object());
return heap().allocate<XMLHttpRequestWrapper>(window, window, XHR::XMLHttpRequest::create(window.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 <LibJS/Runtime/NativeFunction.h>
namespace Web::Bindings {
class XMLHttpRequestConstructor final : public JS::NativeFunction {
public:
explicit XMLHttpRequestConstructor(JS::GlobalObject&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~XMLHttpRequestConstructor() override;
virtual JS::Value call() override;
virtual JS::Value construct(JS::Function& new_target) override;
private:
virtual bool has_constructor() const override { return true; }
virtual const char* class_name() const override { return "XMLHttpRequestConstructor"; }
};
}

View file

@ -1,128 +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/Function.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/XHR/XMLHttpRequest.h>
namespace Web::Bindings {
XMLHttpRequestPrototype::XMLHttpRequestPrototype(JS::GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
}
void XMLHttpRequestPrototype::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
define_native_function("open", open, 2);
define_native_function("send", send, 0);
define_native_function("setRequestHeader", set_request_header, 2);
define_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_property("UNSENT", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable);
define_property("OPENED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable);
define_property("HEADERS_RECEIVED", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable);
define_property("LOADING", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable);
define_property("DONE", JS::Value((i32)XHR::XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable);
}
XMLHttpRequestPrototype::~XMLHttpRequestPrototype()
{
}
static XHR::XMLHttpRequest* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return nullptr;
if (!is<XMLHttpRequestWrapper>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "XMLHttpRequest");
return nullptr;
}
return &static_cast<XMLHttpRequestWrapper*>(this_object)->impl();
}
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::open)
{
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
auto arg0 = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto arg1 = vm.argument(1).to_string(global_object);
if (vm.exception())
return {};
impl->open(arg0, arg1);
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send)
{
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
impl->send();
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::set_request_header)
{
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
auto arg0 = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto arg1 = vm.argument(1).to_string(global_object);
if (vm.exception())
return {};
impl->set_request_header(arg0, arg1);
return JS::js_undefined();
}
JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter)
{
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
return JS::Value((i32)impl->ready_state());
}
JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::response_text_getter)
{
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
return JS::js_string(vm, impl->response_text());
}
}

View file

@ -1,50 +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 <LibJS/Runtime/Object.h>
namespace Web::Bindings {
class XMLHttpRequestPrototype final : public JS::Object {
JS_OBJECT(XMLHttpRequestPrototype, JS::Object);
public:
explicit XMLHttpRequestPrototype(JS::GlobalObject&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~XMLHttpRequestPrototype() override;
private:
JS_DECLARE_NATIVE_FUNCTION(open);
JS_DECLARE_NATIVE_FUNCTION(send);
JS_DECLARE_NATIVE_FUNCTION(set_request_header);
JS_DECLARE_NATIVE_GETTER(ready_state_getter);
JS_DECLARE_NATIVE_GETTER(response_text_getter);
};
}

View file

@ -1,62 +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/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/XHR/XMLHttpRequest.h>
namespace Web::Bindings {
XMLHttpRequestWrapper* wrap(JS::GlobalObject& global_object, XHR::XMLHttpRequest& impl)
{
return static_cast<XMLHttpRequestWrapper*>(wrap_impl(global_object, impl));
}
XMLHttpRequestWrapper::XMLHttpRequestWrapper(JS::GlobalObject& global_object, XHR::XMLHttpRequest& impl)
: EventTargetWrapper(global_object, impl)
{
set_prototype(static_cast<WindowObject&>(global_object).xhr_prototype());
}
XMLHttpRequestWrapper::~XMLHttpRequestWrapper()
{
}
XHR::XMLHttpRequest& XMLHttpRequestWrapper::impl()
{
return static_cast<XHR::XMLHttpRequest&>(EventTargetWrapper::impl());
}
const XHR::XMLHttpRequest& XMLHttpRequestWrapper::impl() const
{
return static_cast<const XHR::XMLHttpRequest&>(EventTargetWrapper::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/EventTargetWrapper.h>
namespace Web::Bindings {
class XMLHttpRequestWrapper final : public EventTargetWrapper {
public:
XMLHttpRequestWrapper(JS::GlobalObject&, XHR::XMLHttpRequest&);
virtual ~XMLHttpRequestWrapper() override;
XHR::XMLHttpRequest& impl();
const XHR::XMLHttpRequest& impl() const;
private:
virtual const char* class_name() const override { return "XMLHttpRequestWrapper"; }
};
XMLHttpRequestWrapper* wrap(JS::GlobalObject&, XHR::XMLHttpRequest&);
}