LibWeb: Populate filename in WindowOrWorkerGlobalScope.reportError()

Previously, when `WindowOrWorkerGlobalScope.reportError()` was called
the `filename` property of the dispatched error event was blank. It is
now populated with the full path of the active script.
This commit is contained in:
Tim Ledbetter 2024-07-07 15:57:34 +01:00 committed by Andreas Kling
parent 572324d47b
commit 34b9873664
Notes: sideshowbarker 2024-07-16 20:31:50 +09:00
3 changed files with 18 additions and 7 deletions

View File

@ -1,5 +1,6 @@
message = Reporting an Error!
filename =
lineno = 0
colno = 0
error = Error: Reporting an Error!
filename URL scheme = file:
filename URL final path segment = WindowOrWorkerGlobalScope-reportError.html

View File

@ -3,11 +3,13 @@
test(() => {
window.onerror = (message, filename, lineno, colno, error) => {
println(`message = ${message}`);
println(`filename = ${filename}`);
println(`lineno = ${lineno}`);
println(`colno = ${colno}`);
println(`error = ${error}`);
// We can't simply print the filename because it is the full path to the active script, which varies between machines.
const filenameURL = new URL(filename);
println(`filename URL scheme = ${filenameURL.protocol}`);
println(`filename URL final path segment = ${filenameURL.pathname.split('/').pop()}`);
return true;
};

View File

@ -783,22 +783,30 @@ void WindowOrWorkerGlobalScopeMixin::report_error(JS::Value e)
auto error_value = e;
// 5. Let urlString be the result of applying the URL serializer to the URL record that corresponds to the resource from which script was obtained.
// FIXME: Use the URL of the current running script.
auto url_string = String {};
// NOTE: urlString is set below once we have determined whether we are dealing with a script or a module.
String url_string;
auto script_or_module_filename = [](auto const& script_or_module) {
return MUST(String::from_utf8(script_or_module->filename()));
};
// 6. If script is a classic script and script's muted errors is true, then set message to "Script error.",
// urlString to the empty string, line and col to 0, and errorValue to null.
script_or_module.visit(
[&](const JS::NonnullGCPtr<JS::Script>& js_script) {
[&](JS::NonnullGCPtr<JS::Script> const& js_script) {
if (verify_cast<ClassicScript>(js_script->host_defined())->muted_errors() == ClassicScript::MutedErrors::Yes) {
message = "Script error."_string;
url_string = String {};
line = 0;
col = 0;
error_value = JS::js_null();
} else {
url_string = script_or_module_filename(js_script);
}
},
[](auto const&) {});
[&](JS::NonnullGCPtr<JS::Module> const& js_module) {
url_string = script_or_module_filename(js_module);
},
[](Empty) {});
// 7. Let notHandled be true.
auto not_handled = true;