diff options
author | Andreas Kling <kling@serenityos.org> | 2022-12-09 18:48:25 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-09 18:51:03 +0000 |
commit | fbf9cb338728bdae160655a1e1527f5ce0e1871a (patch) | |
tree | 6cc659f9f431338e9a8977b341f98fd1f85a7c22 /Userland/Libraries | |
parent | 23b07b3408728f5ea184c6fa928729e11b0c4957 (diff) | |
download | serenity-fbf9cb338728bdae160655a1e1527f5ce0e1871a.zip |
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().
Diffstat (limited to 'Userland/Libraries')
4 files changed, 9 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp index 20bdc1bc86..2dc3be1e19 100644 --- a/Userland/Libraries/LibJS/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Interpreter.cpp @@ -35,7 +35,7 @@ Interpreter::Interpreter(VM& vm) } // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation -ThrowCompletionOr<Value> Interpreter::run(Script& script_record) +ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override) { auto& vm = this->vm(); @@ -62,6 +62,10 @@ ThrowCompletionOr<Value> Interpreter::run(Script& script_record) // 7. Set the LexicalEnvironment of scriptContext to globalEnv. script_context.lexical_environment = &global_environment; + // Non-standard: Override the lexical environment if requested. + if (lexical_environment_override) + script_context.lexical_environment = lexical_environment_override; + // 8. Set the PrivateEnvironment of scriptContext to null. // NOTE: This isn't in the spec, but we require it. diff --git a/Userland/Libraries/LibJS/Interpreter.h b/Userland/Libraries/LibJS/Interpreter.h index e5937c86bc..d84e93ff3b 100644 --- a/Userland/Libraries/LibJS/Interpreter.h +++ b/Userland/Libraries/LibJS/Interpreter.h @@ -66,7 +66,7 @@ public: ~Interpreter() = default; - ThrowCompletionOr<Value> run(Script&); + ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = {}); ThrowCompletionOr<Value> run(SourceTextModule&); Realm& realm(); diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index 2fdc908821..d4dedf47a5 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -71,7 +71,7 @@ JS::NonnullGCPtr<ClassicScript> ClassicScript::create(DeprecatedString filename, } // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script -JS::Completion ClassicScript::run(RethrowErrors rethrow_errors) +JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr<JS::Environment> lexical_environment_override) { auto& vm = settings_object().realm().vm(); @@ -97,7 +97,7 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors) // 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record). auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm()); - evaluation_status = interpreter->run(*m_script_record); + evaluation_status = interpreter->run(*m_script_record, lexical_environment_override); // FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null. diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h index 9f1ef9c2ab..3d602f1d66 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.h @@ -32,7 +32,7 @@ public: No, Yes, }; - JS::Completion run(RethrowErrors = RethrowErrors::No); + JS::Completion run(RethrowErrors = RethrowErrors::No, JS::GCPtr<JS::Environment> lexical_environment_override = {}); MutedErrors muted_errors() const { return m_muted_errors; } |