Truly independent web browser
LibWeb: Report the location of script parse errors
Andreas Klingd2f9c2789e3b
diff --git a/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Libraries/LibWeb/HTML/Scripting/ClassicScript.cppindex c71e3ccace5..0fba71ad865 100644--- a/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp+++ b/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp@@ -69,7 +69,7 @@ GC::Ref<ClassicScript> ClassicScript::create(ByteString filename, Utf16View sourdbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to parse: {}", parse_error.to_utf16_string());// 1. Set script's parse error and its error to rethrow to result[0].- script->set_parse_error(JS::SyntaxError::create(settings.realm(), parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(settings.realm(), parse_error.to_utf16_string()), parse_error.position);script->set_error_to_rethrow(script->parse_error());// 2. Return script.@@ -105,7 +105,7 @@ GC::Ref<ClassicScript> ClassicScript::create_from_pre_parsed(ByteString filenameauto& parse_error = result.error().first();dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to compile: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);script->set_error_to_rethrow(script->parse_error());return script;@@ -138,7 +138,7 @@ GC::Ref<ClassicScript> ClassicScript::create_from_pre_compiled(ByteString filenaauto& parse_error = result.error().first();dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to materialize: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);script->set_error_to_rethrow(script->parse_error());return script;@@ -171,7 +171,7 @@ GC::Ref<ClassicScript> ClassicScript::create_from_bytecode_cache(ByteString fileauto& parse_error = result.error().first();dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to materialize bytecode cache: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);script->set_error_to_rethrow(script->parse_error());return script;@@ -241,7 +241,7 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, GC::Ptr<JS::EnviVERIFY(rethrow_errors == RethrowErrors::No);// 1. Report an exception given by evaluationStatus.[[Value]] for script's settings object's global object.- report_an_exception(settings.global_object(), evaluation_status.value());+ report_an_exception(settings.global_object(), evaluation_status.value(), OmitError::No, this);// 2. Clean up after running script with settings.clean_up_after_running_script(settings);diff --git a/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp b/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cppindex 221aa8fce2f..8f57044dc58 100644--- a/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp+++ b/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp@@ -67,7 +67,7 @@ void report_exception_to_console(JS::Value value, JS::Realm& realm, ErrorInPromi}// https://html.spec.whatwg.org/multipage/webappapis.html#report-an-exception-void report_an_exception(JS::Object& global, JS::Value exception, OmitError omit_error)+void report_an_exception(JS::Object& global, JS::Value exception, OmitError omit_error, GC::Ptr<Script> script){auto& realm = relevant_realm(global);auto& vm = realm.vm();@@ -80,22 +80,38 @@ void report_an_exception(JS::Object& global, JS::Value exception, OmitError omit// 3. Let script be a script found in an implementation-defined way, or null. This should usually be the// running script (most notably during run a classic script).- auto script_or_module = vm.get_active_script_or_module();+ // NB: The running script's execution context is gone by the time its error gets reported, so callers that know+ // the script pass it in.+ if (!script) {+ vm.get_active_script_or_module().visit(+ [&](GC::Ref<JS::Script> const& js_script) {+ script = script_from_host_defined(js_script->host_defined());+ },+ [&](GC::Ref<JS::Module> const& module) {+ script = script_from_host_defined(module->host_defined());+ },+ [](auto const&) { });+ }++ // NB: A parse error is created before its script runs, so take its location from the script.+ if (script && !script->parse_error().is_null() && script->parse_error() == exception) {+ error_info.filename = Utf16String::from_utf8(script->filename());+ if (auto const& position = script->parse_error_position(); position.has_value()) {+ error_info.lineno = position->line;+ error_info.colno = position->column;+ }+ }// 4. If script is a classic script and script's muted errors is true, then set errorInfo[error] to null,// errorInfo[message] to "Script error.", errorInfo[filename] to the empty string, errorInfo[lineno] to// 0, and errorInfo[colno] to 0.- script_or_module.visit(- [&](GC::Ref<JS::Script> const& js_script) {- if (as<ClassicScript>(script_from_host_defined(js_script->host_defined()))->muted_errors() == ClassicScript::MutedErrors::Yes) {- error_info.error = JS::js_null();- error_info.message = "Script error."_utf16;- error_info.filename = Utf16String {};- error_info.lineno = 0;- error_info.colno = 0;- }- },- [](auto const&) { });+ if (auto* classic_script = as_if<ClassicScript>(script.ptr()); classic_script && classic_script->muted_errors() == ClassicScript::MutedErrors::Yes) {+ error_info.error = JS::js_null();+ error_info.message = "Script error."_utf16;+ error_info.filename = Utf16String {};+ error_info.lineno = 0;+ error_info.colno = 0;+ }// 5. If omitError is true, then set errorInfo[error] to null.if (omit_error == OmitError::Yes)diff --git a/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h b/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.hindex 78608526ed4..4c5e477b95b 100644--- a/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h+++ b/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h@@ -6,11 +6,14 @@#pragma once+#include <LibGC/Ptr.h>#include <LibJS/Forward.h>#include <LibWeb/Export.h>namespace Web::HTML {+class Script;+enum class ErrorInPromise {No,Yes,@@ -23,7 +26,7 @@ enum class OmitError {void report_exception_to_console(JS::Value, JS::Realm&, ErrorInPromise);WEB_API void report_exception(JS::Completion const&, JS::Realm&);-void report_an_exception(JS::Object& global, JS::Value exception, OmitError = OmitError::No);+void report_an_exception(JS::Object& global, JS::Value exception, OmitError = OmitError::No, GC::Ptr<Script> = {});template<typename T>inline void report_exception(JS::ThrowCompletionOr<T> const& result, JS::Realm& realm)diff --git a/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp b/Libraries/LibWeb/HTML/Scripting/ModuleScript.cppindex 631b9c88d83..2e71b31c958 100644--- a/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp+++ b/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp@@ -78,7 +78,7 @@ WebIDL::ExceptionOr<GC::Ptr<ModuleScript>> ModuleScript::create_a_javascript_moddbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_utf16_string());// 1. Set script's parse error to result[0].- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);// 2. Return script.return script;@@ -105,7 +105,7 @@ WebIDL::ExceptionOr<GC::Ptr<ModuleScript>> ModuleScript::create_from_pre_parsed(if (result.is_error()) {auto& parse_error = result.error().first();dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);return script;}@@ -127,7 +127,7 @@ WebIDL::ExceptionOr<GC::Ptr<ModuleScript>> ModuleScript::create_from_pre_compileif (result.is_error()) {auto& parse_error = result.error().first();dbgln("JavaScriptModuleScript: Failed to materialize: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);return script;}@@ -149,7 +149,7 @@ WebIDL::ExceptionOr<GC::Ptr<ModuleScript>> ModuleScript::create_from_bytecode_caif (result.is_error()) {auto& parse_error = result.error().first();dbgln("JavaScriptModuleScript: Failed to materialize bytecode cache: {}", parse_error.to_utf16_string());- script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()));+ script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);return script;}@@ -350,8 +350,8 @@ GC::Ptr<WebIDL::Promise> ModuleScript::run(PreventErrorReporting prevent_error_r// 8. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.if (prevent_error_reporting == PreventErrorReporting::No) {HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };- WebIDL::upon_rejection(*evaluation_promise, GC::create_function(GC::Heap::the(), [&realm](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {- report_an_exception(realm.global_object(), reason);+ WebIDL::upon_rejection(*evaluation_promise, GC::create_function(GC::Heap::the(), [&realm, script = GC::Ref { *this }](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {+ report_an_exception(realm.global_object(), reason, OmitError::No, script);return JS::js_undefined();}));}diff --git a/Libraries/LibWeb/HTML/Scripting/Script.h b/Libraries/LibWeb/HTML/Scripting/Script.hindex 57a71be5eeb..9432c410dc3 100644--- a/Libraries/LibWeb/HTML/Scripting/Script.h+++ b/Libraries/LibWeb/HTML/Scripting/Script.h@@ -8,6 +8,7 @@#include <AK/Utf16String.h>#include <LibJS/Heap/Cell.h>+#include <LibJS/Position.h>#include <LibJS/Script.h>#include <LibJS/SourceCode.h>#include <LibURL/URL.h>@@ -38,7 +39,14 @@ public:void set_error_to_rethrow(JS::Value value) { m_error_to_rethrow = value; }[[nodiscard]] JS::Value parse_error() const { return m_parse_error; }- void set_parse_error(JS::Value value) { m_parse_error = value; }+ void set_parse_error(JS::Value value, Optional<JS::Position> position = {})+ {+ m_parse_error = value;+ m_parse_error_position = position;+ }++ // NB: A parse error is created before the script runs, so it has no source location of its own.+ [[nodiscard]] Optional<JS::Position> const& parse_error_position() const { return m_parse_error_position; }protected:Script(Optional<URL::URL> base_url, ByteString filename, EnvironmentSettingsObject&);@@ -60,6 +68,7 @@ private:// https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-parse-errorJS::Value m_parse_error;+ Optional<JS::Position> m_parse_error_position;// https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-error-to-rethrowJS::Value m_error_to_rethrow;diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cppindex adbb98fa180..1a8425a5580 100644--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp@@ -899,8 +899,8 @@ WebIDL::ExceptionOr<i32> WindowOrWorkerGlobalScopeMixin::run_timer_initializatio// 6. Let script be the result of creating a classic script given handler, settings object, base URL, and fetch options.// FIXME: Pass fetch options.- auto basename = base_url.basename();- auto script = ClassicScript::create(basename, source, settings_object, move(base_url));+ auto filename = base_url.to_byte_string();+ auto script = ClassicScript::create(move(filename), source, settings_object, move(base_url));// 7. Run the classic script script.(void)script->run();diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error-data-url.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error-data-url.txtindex ab24c0fb7f4..eea8b3326fa 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error-data-url.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error-data-url.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail <body onerror> - compile error in <script src=data:...>+2 Pass+Pass <body onerror> - compile error in <script src=data:...>Pass <body onerror> - compile error in <script src=data:...> (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error.txtindex 469c186fef2..202a8830e71 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/body-onerror-compile-error.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail <body onerror> - compile error in <script>+2 Pass+Pass <body onerror> - compile error in <script>Pass <body onerror> - compile error in <script> (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-data-url.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-data-url.txtindex 9ff6b63f945..eefd1926359 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-data-url.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-data-url.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail window.onerror - compile error in <script src=data:...>+2 Pass+Pass window.onerror - compile error in <script src=data:...>Pass window.onerror - compile error in <script src=data:...> (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setInterval.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setInterval.txtindex 7058c0b38c5..0e6e78c4fc1 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setInterval.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setInterval.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail window.onerror - compile error in setInterval+2 Pass+Pass window.onerror - compile error in setIntervalPass window.onerror - compile error in setInterval (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setTimeout.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setTimeout.txtindex b8095cd0266..c2132bf1600 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setTimeout.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-in-setTimeout.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail window.onerror - compile error in setTimeout+2 Pass+Pass window.onerror - compile error in setTimeoutPass window.onerror - compile error in setTimeout (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-same-origin.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-same-origin.txtindex ec2aebb68dd..22cdc6a1c0a 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-same-origin.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-same-origin.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail window.onerror - compile error in <script src=...>+2 Pass+Pass window.onerror - compile error in <script src=...>Pass window.onerror - compile error in <script src=...> (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error.txtindex 5ad2bbac6dc..4542fdebe33 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/compile-error.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail window.onerror - compile error in <script>+2 Pass+Pass window.onerror - compile error in <script>Pass window.onerror - compile error in <script> (column)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/window-onerror-parse-error.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/window-onerror-parse-error.txtindex fd30cf00c80..7e535a18041 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/window-onerror-parse-error.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/processing-model-2/window-onerror-parse-error.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-1 Pass-2 Fail-Fail correct url passed to window.onerror-Fail correct line number passed to window.onerror+3 Pass+Pass correct url passed to window.onerror+Pass correct line number passed to window.onerrorPass correct number of calls to window.onerror\ No newline at end of file