mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibWeb/HTML: Extract ErrorInformation struct into its own files
This is used from a few different places in the spec.
This commit is contained in:
parent
c1c24e8fd6
commit
218e646e72
Notes:
github-actions[bot]
2025-11-30 10:48:18 +00:00
Author: https://github.com/AtkinsSJ
Commit: 218e646e72
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6955
4 changed files with 97 additions and 66 deletions
|
|
@ -474,6 +474,7 @@ set(SOURCES
|
|||
HTML/ElementInternals.cpp
|
||||
HTML/EmbedderPolicy.cpp
|
||||
HTML/ErrorEvent.cpp
|
||||
HTML/ErrorInformation.cpp
|
||||
HTML/EventHandler.cpp
|
||||
HTML/EventLoop/EventLoop.cpp
|
||||
HTML/EventLoop/Task.cpp
|
||||
|
|
|
|||
70
Libraries/LibWeb/HTML/ErrorInformation.cpp
Normal file
70
Libraries/LibWeb/HTML/ErrorInformation.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
|
||||
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/VM.h>
|
||||
#include <LibWeb/HTML/ErrorInformation.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#extract-error
|
||||
ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)
|
||||
{
|
||||
// 1. Let attributes be an empty map keyed by IDL attributes.
|
||||
ErrorInformation attributes;
|
||||
|
||||
// 2. Set attributes[error] to exception.
|
||||
attributes.error = exception;
|
||||
|
||||
// 3. Set attributes[message], attributes[filename], attributes[lineno], and attributes[colno] to
|
||||
// implementation-defined values derived from exception.
|
||||
attributes.message = [&] {
|
||||
if (exception.is_object()) {
|
||||
auto& object = exception.as_object();
|
||||
if (MUST(object.has_own_property(vm.names.message))) {
|
||||
auto message = object.get_without_side_effects(vm.names.message);
|
||||
return message.to_string_without_side_effects();
|
||||
}
|
||||
}
|
||||
|
||||
return MUST(String::formatted("Uncaught exception: {}", exception.to_string_without_side_effects()));
|
||||
}();
|
||||
|
||||
// FIXME: This offset is relative to the javascript source. Other browsers appear to do it relative
|
||||
// to the entire source document! Calculate that somehow.
|
||||
|
||||
// NB: If we got an Error object, then try and extract the information from the location the object was made.
|
||||
if (exception.is_object() && is<JS::Error>(exception.as_object())) {
|
||||
auto const& error = static_cast<JS::Error&>(exception.as_object());
|
||||
for (auto const& frame : error.traceback()) {
|
||||
auto source_range = frame.source_range();
|
||||
if (source_range.start.line != 0 || source_range.start.column != 0) {
|
||||
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
||||
attributes.lineno = source_range.start.line;
|
||||
attributes.colno = source_range.start.column;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// NB: Otherwise, we fall back to try and find the location of the invocation of the function itself.
|
||||
else {
|
||||
for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; --i) {
|
||||
auto& frame = vm.execution_context_stack()[i];
|
||||
if (frame->executable) {
|
||||
auto source_range = frame->executable->source_range_at(frame->program_counter).realize();
|
||||
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
||||
attributes.lineno = source_range.start.line;
|
||||
attributes.colno = source_range.start.column;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Return attributes.
|
||||
return attributes;
|
||||
}
|
||||
|
||||
}
|
||||
25
Libraries/LibWeb/HTML/ErrorInformation.h
Normal file
25
Libraries/LibWeb/HTML/ErrorInformation.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
|
||||
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#extract-error
|
||||
struct ErrorInformation {
|
||||
String message;
|
||||
String filename;
|
||||
JS::Value error;
|
||||
size_t lineno { 0 };
|
||||
size_t colno { 0 };
|
||||
};
|
||||
|
||||
ErrorInformation extract_error_information(JS::VM&, JS::Value exception);
|
||||
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#include <LibWeb/Fetch/FetchMethod.h>
|
||||
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
|
||||
#include <LibWeb/HTML/ErrorEvent.h>
|
||||
#include <LibWeb/HTML/ErrorInformation.h>
|
||||
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
||||
#include <LibWeb/HTML/EventSource.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
|
|
@ -1151,72 +1152,6 @@ void WindowOrWorkerGlobalScopeMixin::report_error(JS::Value e)
|
|||
report_an_exception(e);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#extract-error
|
||||
struct ErrorInformation {
|
||||
String message;
|
||||
String filename;
|
||||
JS::Value error;
|
||||
size_t lineno { 0 };
|
||||
size_t colno { 0 };
|
||||
};
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#extract-error
|
||||
static ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)
|
||||
{
|
||||
// 1. Let attributes be an empty map keyed by IDL attributes.
|
||||
ErrorInformation attributes;
|
||||
|
||||
// 2. Set attributes[error] to exception.
|
||||
attributes.error = exception;
|
||||
|
||||
// 3. Set attributes[message], attributes[filename], attributes[lineno], and attributes[colno] to
|
||||
// implementation-defined values derived from exception.
|
||||
attributes.message = [&] {
|
||||
if (exception.is_object()) {
|
||||
auto& object = exception.as_object();
|
||||
if (MUST(object.has_own_property(vm.names.message))) {
|
||||
auto message = object.get_without_side_effects(vm.names.message);
|
||||
return message.to_string_without_side_effects();
|
||||
}
|
||||
}
|
||||
|
||||
return MUST(String::formatted("Uncaught exception: {}", exception.to_string_without_side_effects()));
|
||||
}();
|
||||
|
||||
// FIXME: This offset is relative to the javascript source. Other browsers appear to do it relative
|
||||
// to the entire source document! Calculate that somehow.
|
||||
|
||||
// If we got an Error object, then try and extract the information from the location the object was made.
|
||||
if (exception.is_object() && is<JS::Error>(exception.as_object())) {
|
||||
auto const& error = static_cast<JS::Error&>(exception.as_object());
|
||||
for (auto const& frame : error.traceback()) {
|
||||
auto source_range = frame.source_range();
|
||||
if (source_range.start.line != 0 || source_range.start.column != 0) {
|
||||
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
||||
attributes.lineno = source_range.start.line;
|
||||
attributes.colno = source_range.start.column;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Otherwise, we fall back to try and find the location of the invocation of the function itself.
|
||||
else {
|
||||
for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; --i) {
|
||||
auto& frame = vm.execution_context_stack()[i];
|
||||
if (frame->executable) {
|
||||
auto source_range = frame->executable->source_range_at(frame->program_counter).realize();
|
||||
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
||||
attributes.lineno = source_range.start.line;
|
||||
attributes.colno = source_range.start.column;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Return attributes.
|
||||
return attributes;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#report-an-exception
|
||||
void WindowOrWorkerGlobalScopeMixin::report_an_exception(JS::Value exception, OmitError omit_error)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue