diff options
author | Andreas Kling <kling@serenityos.org> | 2020-09-20 19:22:44 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-20 19:22:44 +0200 |
commit | c6ae0c41d908ae59a858a28a737b4382cceaeb6c (patch) | |
tree | 1de5f866114b541956eabb772c4b9f14aa128160 | |
parent | 976e55e942a186143612b5503358469d2f012bf3 (diff) | |
download | serenity-c6ae0c41d908ae59a858a28a737b4382cceaeb6c.zip |
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.)
-rw-r--r-- | Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp | 35 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/ScriptExecutionContext.h | 43 | ||||
-rw-r--r-- | Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.h | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/EventDispatcher.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/EventTarget.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/EventTarget.h | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Node.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Window.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/XMLHttpRequest.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/Forward.h | 1 |
11 files changed, 100 insertions, 8 deletions
diff --git a/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp b/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp new file mode 100644 index 0000000000..ea7e58864e --- /dev/null +++ b/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp @@ -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() +{ +} + +} diff --git a/Libraries/LibWeb/Bindings/ScriptExecutionContext.h b/Libraries/LibWeb/Bindings/ScriptExecutionContext.h new file mode 100644 index 0000000000..1c21910741 --- /dev/null +++ b/Libraries/LibWeb/Bindings/ScriptExecutionContext.h @@ -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; +}; + +} diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index e8b928504b..549ea0baa8 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -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 diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index f438ff3c4f..2164b2b1b7 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -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&); diff --git a/Libraries/LibWeb/DOM/EventDispatcher.cpp b/Libraries/LibWeb/DOM/EventDispatcher.cpp index 45b30f008f..bbdbbbc065 100644 --- a/Libraries/LibWeb/DOM/EventDispatcher.cpp +++ b/Libraries/LibWeb/DOM/EventDispatcher.cpp @@ -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(); diff --git a/Libraries/LibWeb/DOM/EventTarget.cpp b/Libraries/LibWeb/DOM/EventTarget.cpp index c923c436ed..60f7138b25 100644 --- a/Libraries/LibWeb/DOM/EventTarget.cpp +++ b/Libraries/LibWeb/DOM/EventTarget.cpp @@ -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) { } diff --git a/Libraries/LibWeb/DOM/EventTarget.h b/Libraries/LibWeb/DOM/EventTarget.h index 7822064c75..69c4efcb74 100644 --- a/Libraries/LibWeb/DOM/EventTarget.h +++ b/Libraries/LibWeb/DOM/EventTarget.h @@ -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; }; diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index 42eae09d39..3e4c696b74 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -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) { } diff --git a/Libraries/LibWeb/DOM/Window.cpp b/Libraries/LibWeb/DOM/Window.cpp index 7799b00837..8b218a68ca 100644 --- a/Libraries/LibWeb/DOM/Window.cpp +++ b/Libraries/LibWeb/DOM/Window.cpp @@ -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(); diff --git a/Libraries/LibWeb/DOM/XMLHttpRequest.cpp b/Libraries/LibWeb/DOM/XMLHttpRequest.cpp index dfa9347328..12d28c6089 100644 --- a/Libraries/LibWeb/DOM/XMLHttpRequest.cpp +++ b/Libraries/LibWeb/DOM/XMLHttpRequest.cpp @@ -40,7 +40,8 @@ namespace Web { XMLHttpRequest::XMLHttpRequest(DOM::Window& window) - : m_window(window) + : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(window.document())) + , m_window(window) { } diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 8df979d35f..5da2efc583 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -241,6 +241,7 @@ class ImageDataWrapper; class LocationObject; class MouseEventWrapper; class NodeWrapper; +class ScriptExecutionContext; class TextWrapper; class UIEventWrapper; class WindowObject; |