diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-05 14:32:33 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | 8f9ed415a02dd62c46fce4f5d352ad51bc779a27 (patch) | |
tree | d0425cc30ced0333f50e667c55fee52c28b5adb1 /Userland/Libraries/LibWeb/HTML/Scripting | |
parent | 00c8f071923a6d6fbbd4fe989dd6980d3a6b18af (diff) | |
download | serenity-8f9ed415a02dd62c46fce4f5d352ad51bc779a27.zip |
LibJS+LibWeb: Make HTML::Script GC-allocated
This allows the garbage collector to keep HTML::Script objects alive and
fixes a bug where a HTMLScriptElement could get GC'd while its code was
executing.
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/Scripting')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/Scripting/Script.h | 8 |
3 files changed, 31 insertions, 11 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index d3d1aa7022..22ce90d442 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -16,8 +16,10 @@ namespace Web::HTML { // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script -NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, EnvironmentSettingsObject& environment_settings_object, AK::URL base_url, size_t source_line_number, MutedErrors muted_errors) +JS::NonnullGCPtr<ClassicScript> ClassicScript::create(String filename, StringView source, EnvironmentSettingsObject& environment_settings_object, AK::URL base_url, size_t source_line_number, MutedErrors muted_errors) { + auto& vm = environment_settings_object.realm().vm(); + // 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.) // 2. If muted errors is true, then set baseURL to about:blank. @@ -29,7 +31,7 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s source = ""sv; // 4. Let script be a new classic script that this algorithm will subsequently initialize. - auto script = adopt_ref(*new ClassicScript(move(base_url), move(filename), environment_settings_object)); + auto script = vm.heap().allocate_without_realm<ClassicScript>(move(base_url), move(filename), environment_settings_object); // 5. Set script's settings object to settings. (NOTE: This was already done when constructing.) @@ -45,7 +47,7 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s // 10. Let result be ParseScript(source, settings's Realm, script). auto parse_timer = Core::ElapsedTimer::start_new(); - auto result = JS::Script::parse(source, environment_settings_object.realm(), script->filename(), script.ptr(), source_line_number); + auto result = JS::Script::parse(source, environment_settings_object.realm(), script->filename(), script, source_line_number); dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed()); // 11. If result is a list of errors, then: @@ -58,14 +60,14 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s script->m_error_to_rethrow = parse_error; // 2. Return script. - return script; + return JS::NonnullGCPtr(*script); } // 12. Set script's record to result. - script->m_script_record = result.release_value(); + script->m_script_record = *result.release_value(); // 13. Return script. - return script; + return JS::NonnullGCPtr(*script); } // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script @@ -158,4 +160,15 @@ ClassicScript::ClassicScript(AK::URL base_url, String filename, EnvironmentSetti ClassicScript::~ClassicScript() = default; +void ClassicScript::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_script_record); +} + +void ClassicScript::visit_host_defined_self(Cell::Visitor& visitor) +{ + visitor.visit(this); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h index 7d092c33b4..41004659c3 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h @@ -16,6 +16,8 @@ namespace Web::HTML { class ClassicScript final : public Script , public JS::Script::HostDefined { + JS_CELL(ClassicScript, Script); + public: virtual ~ClassicScript() override; @@ -23,7 +25,7 @@ public: No, Yes, }; - static NonnullRefPtr<ClassicScript> create(String filename, StringView source, EnvironmentSettingsObject&, AK::URL base_url, size_t source_line_number = 1, MutedErrors = MutedErrors::No); + static JS::NonnullGCPtr<ClassicScript> create(String filename, StringView source, EnvironmentSettingsObject&, AK::URL base_url, size_t source_line_number = 1, MutedErrors = MutedErrors::No); JS::Script* script_record() { return m_script_record; } JS::Script const* script_record() const { return m_script_record; } @@ -39,7 +41,10 @@ public: private: ClassicScript(AK::URL base_url, String filename, EnvironmentSettingsObject& environment_settings_object); - RefPtr<JS::Script> m_script_record; + virtual void visit_edges(Cell::Visitor&) override; + virtual void visit_host_defined_self(Cell::Visitor&) override; + + JS::GCPtr<JS::Script> m_script_record; MutedErrors m_muted_errors { MutedErrors::No }; Optional<JS::Parser::Error> m_error_to_rethrow; }; diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Script.h b/Userland/Libraries/LibWeb/HTML/Scripting/Script.h index f80c052cb1..862cee3763 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/Script.h +++ b/Userland/Libraries/LibWeb/HTML/Scripting/Script.h @@ -6,16 +6,18 @@ #pragma once -#include <AK/RefCounted.h> #include <AK/URL.h> +#include <LibJS/Heap/Cell.h> #include <LibWeb/Forward.h> namespace Web::HTML { // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script -class Script : public RefCounted<Script> { +class Script : public JS::Cell { + JS_CELL(Script, JS::Cell); + public: - virtual ~Script(); + virtual ~Script() override; AK::URL const& base_url() const { return m_base_url; } String const& filename() const { return m_filename; } |