Skip to content

Commit 684f906

Browse files
committed
LibWeb: Change HTML::Script to hold a realm instead of settings object
This is part of a refactor needed for introduction of the shadow realm proposal in the web platform.
1 parent a47976c commit 684f906

18 files changed

+168
-158
lines changed

Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp

+60-54
Large diffs are not rendered by default.

Userland/Libraries/LibWeb/Bindings/MainThreadVM.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ struct WebEngineCustomData final : public JS::VM::CustomData {
6666
};
6767

6868
struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData {
69-
WebEngineCustomJobCallbackData(HTML::EnvironmentSettingsObject& incumbent_settings, OwnPtr<JS::ExecutionContext> active_script_context)
70-
: incumbent_settings(incumbent_settings)
69+
WebEngineCustomJobCallbackData(JS::Realm& incumbent_realm, OwnPtr<JS::ExecutionContext> active_script_context)
70+
: incumbent_realm(incumbent_realm)
7171
, active_script_context(move(active_script_context))
7272
{
7373
}
7474

7575
virtual ~WebEngineCustomJobCallbackData() override = default;
7676

77-
JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> incumbent_settings;
77+
JS::NonnullGCPtr<JS::Realm> incumbent_realm;
7878
OwnPtr<JS::ExecutionContext> active_script_context;
7979
};
8080

Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ void HTMLScriptElement::execute_script()
159159
}
160160

161161
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
162+
// https://whatpr.org/html/9893/scripting.html#prepare-a-script
162163
void HTMLScriptElement::prepare_script()
163164
{
164165
// 1. If el's already started is true, then return.
@@ -433,9 +434,9 @@ void HTMLScriptElement::prepare_script()
433434
// 2. Switch on el's type:
434435
// -> "classic"
435436
if (m_script_type == ScriptType::Classic) {
436-
// 1. Let script be the result of creating a classic script using source text, settings object, base URL, and options.
437+
// 1. Let script be the result of creating a classic script using source text, settings object's realm, base URL, and options.
437438
// FIXME: Pass options.
438-
auto script = ClassicScript::create(m_document->url().to_byte_string(), source_text, settings_object, base_url, m_source_line_number);
439+
auto script = ClassicScript::create(m_document->url().to_byte_string(), source_text, settings_object.realm(), base_url, m_source_line_number);
439440

440441
// 2. Mark as ready el given script.
441442
mark_as_ready(Result(move(script)));

Userland/Libraries/LibWeb/HTML/Navigable.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(URL::URL const& url,
15741574
}
15751575

15761576
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#evaluate-a-javascript:-url
1577+
// https://whatpr.org/html/9893/browsing-the-web.html#evaluate-a-javascript:-url
15771578
WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> Navigable::evaluate_javascript_url(URL::URL const& url, URL::Origin const& new_document_origin, String navigation_id)
15781579
{
15791580
auto& vm = this->vm();
@@ -1595,8 +1596,8 @@ WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> Navigable::evaluate_javascript_url
15951596
// 5. Let baseURL be settings's API base URL.
15961597
auto base_url = settings.api_base_url();
15971598

1598-
// 6. Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
1599-
auto script = HTML::ClassicScript::create("(javascript url)", script_source, settings, base_url);
1599+
// 6. Let script be the result of creating a classic script given scriptSource, settings's realm, baseURL, and the default classic script fetch options.
1600+
auto script = HTML::ClassicScript::create("(javascript url)", script_source, settings.realm(), base_url);
16001601

16011602
// 7. Let evaluationStatus be the result of running the classic script script.
16021603
auto evaluation_status = script->run();

Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ JS_DEFINE_ALLOCATOR(ClassicScript);
2020

2121
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
2222
// https://whatpr.org/html/9893/webappapis.html#creating-a-classic-script
23-
JS::NonnullGCPtr<ClassicScript> ClassicScript::create(ByteString filename, StringView source, EnvironmentSettingsObject& environment_settings_object, URL::URL base_url, size_t source_line_number, MutedErrors muted_errors)
23+
JS::NonnullGCPtr<ClassicScript> ClassicScript::create(ByteString filename, StringView source, JS::Realm& realm, URL::URL base_url, size_t source_line_number, MutedErrors muted_errors)
2424
{
25-
auto& realm = environment_settings_object.realm();
2625
auto& vm = realm.vm();
2726

2827
// 1. If muted errors is true, then set baseURL to about:blank.
@@ -34,11 +33,9 @@ JS::NonnullGCPtr<ClassicScript> ClassicScript::create(ByteString filename, Strin
3433
source = ""sv;
3534

3635
// 3. Let script be a new classic script that this algorithm will subsequently initialize.
37-
auto script = vm.heap().allocate_without_realm<ClassicScript>(move(base_url), move(filename), environment_settings_object);
38-
39-
// FIXME: 4. Set script's realm to realm. (NOTE: This was already done when constructing.)
40-
41-
// 5. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
36+
// 4. Set script's realm to realm.
37+
// 5. Set script's base URL to baseURL.
38+
auto script = vm.heap().allocate_without_realm<ClassicScript>(move(base_url), move(filename), realm);
4239

4340
// FIXME: 6. Set script's fetch options to options.
4441

@@ -80,11 +77,10 @@ JS::NonnullGCPtr<ClassicScript> ClassicScript::create(ByteString filename, Strin
8077
// https://whatpr.org/html/9893/webappapis.html#run-a-classic-script
8178
JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr<JS::Environment> lexical_environment_override)
8279
{
83-
// 1. Let settings be the settings object of script.
84-
auto& settings = settings_object();
85-
auto& realm = settings.realm();
80+
// 1. Let realm be the realm of script.
81+
auto& realm = this->realm();
8682

87-
// 2. Check if we can run script with settings. If this returns "do not run" then return NormalCompletion(empty).
83+
// 2. Check if we can run script with realm. If this returns "do not run" then return NormalCompletion(empty).
8884
if (can_run_script(realm) == RunScriptDecision::DoNotRun)
8985
return JS::normal_completion({});
9086

@@ -131,8 +127,8 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr<JS::En
131127
// 3. Otherwise, rethrow errors is false. Perform the following steps:
132128
VERIFY(rethrow_errors == RethrowErrors::No);
133129

134-
// 1. Report an exception given by evaluationStatus.[[Value]] for script's settings object's global object.
135-
auto* window_or_worker = dynamic_cast<WindowOrWorkerGlobalScopeMixin*>(&settings.global_object());
130+
// 1. Report an exception given by evaluationStatus.[[Value]] for realms's global object.
131+
auto* window_or_worker = dynamic_cast<WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
136132
VERIFY(window_or_worker);
137133
window_or_worker->report_an_exception(*evaluation_status.value());
138134

@@ -154,8 +150,8 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr<JS::En
154150
// Return Completion { [[Type]]: throw, [[Value]]: a new "QuotaExceededError" DOMException, [[Target]]: empty }.
155151
}
156152

157-
ClassicScript::ClassicScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object)
158-
: Script(move(base_url), move(filename), environment_settings_object)
153+
ClassicScript::ClassicScript(URL::URL base_url, ByteString filename, JS::Realm& realm)
154+
: Script(move(base_url), move(filename), realm)
159155
{
160156
}
161157

Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ClassicScript final : public Script {
2424
No,
2525
Yes,
2626
};
27-
static JS::NonnullGCPtr<ClassicScript> create(ByteString filename, StringView source, EnvironmentSettingsObject&, URL::URL base_url, size_t source_line_number = 1, MutedErrors = MutedErrors::No);
27+
static JS::NonnullGCPtr<ClassicScript> create(ByteString filename, StringView source, JS::Realm&, URL::URL base_url, size_t source_line_number = 1, MutedErrors = MutedErrors::No);
2828

2929
JS::Script* script_record() { return m_script_record; }
3030
JS::Script const* script_record() const { return m_script_record; }
@@ -38,7 +38,7 @@ class ClassicScript final : public Script {
3838
MutedErrors muted_errors() const { return m_muted_errors; }
3939

4040
private:
41-
ClassicScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object);
41+
ClassicScript(URL::URL base_url, ByteString filename, JS::Realm&);
4242

4343
virtual void visit_edges(Cell::Visitor&) override;
4444

Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,25 @@ bool is_scripting_disabled(JS::Realm const& realm)
260260
}
261261

262262
// https://html.spec.whatwg.org/multipage/webappapis.html#module-type-allowed
263-
bool EnvironmentSettingsObject::module_type_allowed(StringView module_type) const
263+
// https://whatpr.org/html/9893/webappapis.html#module-type-allowed
264+
bool module_type_allowed(JS::Realm const&, StringView module_type)
264265
{
265266
// 1. If moduleType is not "javascript", "css", or "json", then return false.
266267
if (module_type != "javascript"sv && module_type != "css"sv && module_type != "json"sv)
267268
return false;
268269

269-
// FIXME: 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in settings's Realm, then return false.
270+
// FIXME: 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in realm, then return false.
270271

271272
// 3. Return true.
272273
return true;
273274
}
274275

275276
// https://html.spec.whatwg.org/multipage/webappapis.html#disallow-further-import-maps
276-
void EnvironmentSettingsObject::disallow_further_import_maps()
277+
// https://whatpr.org/html/9893/webappapis.html#disallow-further-import-maps
278+
void disallow_further_import_maps(JS::Realm& realm)
277279
{
278-
// 1. Let global be settingsObject's global object.
279-
auto& global = global_object();
280+
// 1. Let global be realm's global object.
281+
auto& global = realm.global_object();
280282

281283
// 2. If global does not implement Window, then return.
282284
if (!is<Window>(global))

Userland/Libraries/LibWeb/HTML/Scripting/Environments.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ struct EnvironmentSettingsObject : public Environment {
9696
// https://fetch.spec.whatwg.org/#concept-fetch-group
9797
Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>>& fetch_group() { return m_fetch_group; }
9898

99-
bool module_type_allowed(StringView module_type) const;
100-
101-
void disallow_further_import_maps();
102-
10399
SerializedEnvironmentSettingsObject serialize();
104100

105101
JS::NonnullGCPtr<StorageAPI::StorageManager> storage_manager();
@@ -142,6 +138,8 @@ void clean_up_after_running_script(JS::Realm const&);
142138
void prepare_to_run_callback(JS::Realm&);
143139
void clean_up_after_running_callback(JS::Realm const&);
144140
ModuleMap& module_map_of_realm(JS::Realm&);
141+
bool module_type_allowed(JS::Realm const&, StringView module_type);
142+
void disallow_further_import_maps(JS::Realm&);
145143

146144
EnvironmentSettingsObject& incumbent_settings_object();
147145
JS::Realm& incumbent_realm();

0 commit comments

Comments
 (0)