LibWeb: Add Bindings::ScriptExecutionContext

This will be inherited by documents and workers, to provide a common
abstraction for script execution. (We don't have workers yet, but we
might as well make this little space for them now to simplify things
down the road.)
This commit is contained in:
Andreas Kling 2020-09-20 19:22:44 +02:00
parent 976e55e942
commit c6ae0c41d9
Notes: sideshowbarker 2024-07-19 02:19:26 +09:00
11 changed files with 100 additions and 8 deletions

View file

@ -0,0 +1,35 @@
/*
* 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 <LibWeb/Bindings/ScriptExecutionContext.h>
namespace Web::Bindings {
ScriptExecutionContext::~ScriptExecutionContext()
{
}
}

View file

@ -0,0 +1,43 @@
/*
* 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 <AK/Weakable.h>
#include <LibJS/Forward.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
class ScriptExecutionContext {
public:
virtual ~ScriptExecutionContext();
// FIXME: This should not work this way long-term, interpreters should be on the stack.
virtual JS::Interpreter& interpreter() = 0;
};
}

View file

@ -5,6 +5,7 @@ set(SOURCES
Bindings/LocationObject.cpp
Bindings/NavigatorObject.cpp
Bindings/NodeWrapperFactory.cpp
Bindings/ScriptExecutionContext.cpp
Bindings/WindowObject.cpp
Bindings/Wrappable.cpp
Bindings/XMLHttpRequestConstructor.cpp

View file

@ -35,6 +35,7 @@
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/ScriptExecutionContext.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/CSS/StyleSheetList.h>
@ -51,7 +52,8 @@ enum class QuirksMode {
class Document
: public ParentNode
, public NonElementParentNode<Document> {
, public NonElementParentNode<Document>
, public Bindings::ScriptExecutionContext {
public:
using WrapperType = Bindings::DocumentWrapper;
@ -131,7 +133,7 @@ public:
const String& source() const { return m_source; }
void set_source(const String& source) { m_source = source; }
JS::Interpreter& interpreter();
virtual JS::Interpreter& interpreter() override;
JS::Value run_javascript(const StringView&);

View file

@ -27,6 +27,7 @@
#include <LibJS/Runtime/Function.h>
#include <LibWeb/Bindings/EventTargetWrapper.h>
#include <LibWeb/Bindings/EventTargetWrapperFactory.h>
#include <LibWeb/Bindings/ScriptExecutionContext.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
@ -43,7 +44,8 @@ void EventDispatcher::dispatch(EventTarget& target, NonnullRefPtr<Event> event)
auto& function = listener.listener->function();
auto& global_object = function.global_object();
auto* this_value = Bindings::wrap(global_object, target);
auto& interpreter = function.interpreter();
auto& interpreter = target.script_execution_context()->interpreter();
(void)interpreter.call(function, this_value, Bindings::wrap(global_object, target));
if (interpreter.exception())
interpreter.clear_exception();

View file

@ -24,12 +24,14 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibWeb/Bindings/ScriptExecutionContext.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/EventTarget.h>
namespace Web::DOM {
EventTarget::EventTarget()
EventTarget::EventTarget(Bindings::ScriptExecutionContext& script_execution_context)
: m_script_execution_context(&script_execution_context)
{
}

View file

@ -49,6 +49,7 @@ public:
virtual void dispatch_event(NonnullRefPtr<Event>) = 0;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) = 0;
Bindings::ScriptExecutionContext* script_execution_context() { return m_script_execution_context; }
struct EventListenerRegistration {
FlyString event_name;
@ -58,12 +59,15 @@ public:
const Vector<EventListenerRegistration>& listeners() const { return m_listeners; }
protected:
EventTarget();
explicit EventTarget(Bindings::ScriptExecutionContext&);
virtual void ref_event_target() = 0;
virtual void unref_event_target() = 0;
private:
// FIXME: This should not be a raw pointer.
Bindings::ScriptExecutionContext* m_script_execution_context { nullptr };
Vector<EventListenerRegistration> m_listeners;
};

View file

@ -51,7 +51,8 @@
namespace Web::DOM {
Node::Node(Document& document, NodeType type)
: m_document(&document)
: EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
, m_document(&document)
, m_type(type)
{
}

View file

@ -94,7 +94,7 @@ void Window::timer_did_fire(Badge<Timer>, Timer& timer)
m_timers.remove(timer.id());
}
auto& interpreter = wrapper()->interpreter();
auto& interpreter = document().interpreter();
(void)interpreter.call(timer.callback(), wrapper());
if (interpreter.exception())
interpreter.clear_exception();

View file

@ -40,7 +40,8 @@
namespace Web {
XMLHttpRequest::XMLHttpRequest(DOM::Window& window)
: m_window(window)
: EventTarget(static_cast<Bindings::ScriptExecutionContext&>(window.document()))
, m_window(window)
{
}

View file

@ -241,6 +241,7 @@ class ImageDataWrapper;
class LocationObject;
class MouseEventWrapper;
class NodeWrapper;
class ScriptExecutionContext;
class TextWrapper;
class UIEventWrapper;
class WindowObject;