LibWeb+WebContent: Store Realm instead of Interpreter in ConsoleClient

This commit is contained in:
Andreas Kling 2022-09-01 20:08:38 +02:00
parent 905eb8cb4d
commit 2d72abc3d4
Notes: sideshowbarker 2024-07-17 07:27:31 +09:00
5 changed files with 20 additions and 19 deletions

View File

@ -1058,14 +1058,14 @@ JS::Interpreter& Document::interpreter()
JS::Value Document::run_javascript(StringView source, StringView filename)
{
// FIXME: The only user of this function now is javascript: URLs. Refactor them to follow the spec: https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol
auto& interpreter = document().interpreter();
auto script_or_error = JS::Script::parse(source, interpreter.realm(), filename);
auto interpreter = JS::Interpreter::create_with_existing_realm(realm());
auto script_or_error = JS::Script::parse(source, realm(), filename);
if (script_or_error.is_error()) {
// FIXME: Add error logging back.
return JS::js_undefined();
}
auto result = interpreter.run(script_or_error.value());
auto result = interpreter->run(script_or_error.value());
if (result.is_error()) {
// FIXME: I'm sure the spec could tell us something about error propagation here!

View File

@ -13,7 +13,6 @@
#include <LibGfx/SystemTheme.h>
#include <LibJS/Console.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
@ -386,13 +385,13 @@ Messages::WebContentServer::GetHoveredNodeIdResponse ConnectionFromClient::get_h
void ConnectionFromClient::initialize_js_console(Badge<PageHost>)
{
auto* document = page().top_level_browsing_context().active_document();
auto interpreter = document->interpreter().make_weak_ptr();
if (m_interpreter.ptr() == interpreter.ptr())
auto realm = document->realm().make_weak_ptr();
if (m_realm.ptr() == realm.ptr())
return;
auto& console_object = *interpreter->realm().intrinsics().console_object();
m_interpreter = interpreter;
m_console_client = make<WebContentConsoleClient>(console_object.console(), interpreter, *this);
auto& console_object = *realm->intrinsics().console_object();
m_realm = realm;
m_console_client = make<WebContentConsoleClient>(console_object.console(), *m_realm, *this);
console_object.console().set_client(*m_console_client.ptr());
}

View File

@ -92,7 +92,7 @@ private:
HashMap<i32, NonnullRefPtr<Gfx::Bitmap>> m_backing_stores;
WeakPtr<JS::Interpreter> m_interpreter;
WeakPtr<JS::Realm> m_realm;
OwnPtr<WebContentConsoleClient> m_console_client;
JS::Handle<JS::GlobalObject> m_console_global_object;

View File

@ -16,18 +16,17 @@
namespace WebContent {
WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<JS::Interpreter> interpreter, ConnectionFromClient& client)
WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, JS::Realm& realm, ConnectionFromClient& client)
: ConsoleClient(console)
, m_client(client)
, m_interpreter(interpreter)
, m_realm(realm)
{
JS::DeferGC defer_gc(m_interpreter->heap());
JS::DeferGC defer_gc(realm.heap());
auto& vm = m_interpreter->vm();
auto& realm = m_interpreter->realm();
auto& vm = realm.vm();
auto& window = static_cast<Web::HTML::Window&>(realm.global_object());
auto console_global_object = m_interpreter->heap().allocate_without_realm<ConsoleGlobalObject>(realm, window);
auto console_global_object = realm.heap().allocate_without_realm<ConsoleGlobalObject>(realm, window);
// NOTE: We need to push an execution context here for NativeFunction::create() to succeed during global object initialization.
// It gets removed immediately after creating the interpreter in Document::interpreter().
@ -41,7 +40,10 @@ WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<J
void WebContentConsoleClient::handle_input(String const& js_source)
{
auto& settings = verify_cast<Web::HTML::EnvironmentSettingsObject>(*m_interpreter->realm().host_defined());
if (!m_realm)
return;
auto& settings = verify_cast<Web::HTML::EnvironmentSettingsObject>(*m_realm->host_defined());
auto script = Web::HTML::ClassicScript::create("(console)", js_source, settings, settings.api_base_url());
// FIXME: Add parse error printouts back once ClassicScript can report parse errors.

View File

@ -18,7 +18,7 @@ namespace WebContent {
class WebContentConsoleClient final : public JS::ConsoleClient {
public:
WebContentConsoleClient(JS::Console&, WeakPtr<JS::Interpreter>, ConnectionFromClient&);
WebContentConsoleClient(JS::Console&, JS::Realm&, ConnectionFromClient&);
void handle_input(String const& js_source);
void send_messages(i32 start_index);
@ -28,7 +28,7 @@ private:
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, PrinterArguments) override;
ConnectionFromClient& m_client;
WeakPtr<JS::Interpreter> m_interpreter;
WeakPtr<JS::Realm> m_realm;
JS::Handle<ConsoleGlobalObject> m_console_global_object;
void clear_output();