1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +00:00

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
11 changed files with 100 additions and 8 deletions

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