1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00
serenity/Userland/Services/WebContent/ConsoleGlobalEnvironmentExtensions.h
Andreas Kling fbf9cb3387 WebContent+LibWeb+LibJS: Simplify injection of JS console globals
Instead of creating a new global object and proxying everything through
it, we now evaluate console inputs inside a `with` environment.

This seems to match the behavior of WebKit and Gecko in my basic
testing, and removes the ConsoleGlobalObject which has been a source of
confusion and invalid downcasts.

The globals now live in a class called ConsoleGlobalObjectExtensions
(renamed from ConsoleGlobalObject since it's no longer a global object).

To make this possible, I had to add a way to override the initial
lexical environment when calling JS::Interpreter::run(). This is plumbed
via Web::HTML::ClassicScript::run().
2022-12-09 18:51:03 +00:00

42 lines
1.4 KiB
C++

/*
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/HTML/Window.h>
namespace WebContent {
class ConsoleGlobalEnvironmentExtensions final : public JS::Object {
JS_OBJECT(ConsoleGlobalEnvironmentExtensions, JS::Object);
public:
ConsoleGlobalEnvironmentExtensions(JS::Realm&, Web::HTML::Window&);
virtual void initialize(JS::Realm&) override;
virtual ~ConsoleGlobalEnvironmentExtensions() override = default;
void set_most_recent_result(JS::Value result) { m_most_recent_result = move(result); }
private:
virtual void visit_edges(Visitor&) override;
// $0, the DOM node currently selected in the inspector
JS_DECLARE_NATIVE_FUNCTION($0_getter);
// $_, the value of the most recent expression entered into the console
JS_DECLARE_NATIVE_FUNCTION($__getter);
// $(selector, element), equivalent to `(element || document).querySelector(selector)`
JS_DECLARE_NATIVE_FUNCTION($_function);
// $$(selector, element), equivalent to `(element || document).querySelectorAll(selector)`
JS_DECLARE_NATIVE_FUNCTION($$_function);
JS::NonnullGCPtr<Web::HTML::Window> m_window_object;
JS::Value m_most_recent_result { JS::js_undefined() };
};
}